Start maybe working on a native app

This commit is contained in:
2026-11-22 17:38:28 -03:00
parent 681d3afb4d
commit a552883795
31 changed files with 1881 additions and 678 deletions
+46 -41
View File
@@ -7,6 +7,8 @@ import { TEXTURE_SIZE } from "@/common/constants.ts";
import type { SpriteRegion } from "@/common/constants.ts";
import { type Animation, texture_animation, type TextureJson } from "@/common/texture_animation.ts";
import { AnimatedTexture } from "./texture_animations.ts";
import { decode_png, type Pixels } from "./png.ts";
import { platform } from "./platform.ts";
export interface AtlasLayout {
// in pixels, a power of two
@@ -67,17 +69,16 @@ function pack_shelves(entries: [string, TextureSize][], size: number) {
// textures are png bytes by id, texture_json the .json of the ones that have one. ones that don't decode show as
// missing. an animated texture takes one frame's room, its first frame, and plays from animated (see
// texture_animations.ts)
export async function build_atlas(textures: Map<string, Uint8Array>, texture_json = new Map<string, TextureJson>()) {
const images = new Map<string, ImageBitmap>();
await Promise.all([...textures].map(async ([id, png]) => {
export function build_atlas(textures: Map<string, Uint8Array>, texture_json = new Map<string, TextureJson>()) {
const images = new Map<string, Pixels>();
for (const [id, png] of textures) {
if (id === "cubi:missing") continue;
try {
images.set(id, await createImageBitmap(new Blob([png as Uint8Array<ArrayBuffer>], { type: "image/png" })));
images.set(id, decode_png(png));
} catch {
console.warn(`Texture ${id} isn't a png that can be shown, it will show as missing`);
}
}));
images.get("cubi:missing")?.close();
images.delete("cubi:missing");
}
const animations = new Map<string, Animation>();
for (const [id, image] of images) {
@@ -91,43 +92,36 @@ export async function build_atlas(textures: Map<string, Uint8Array>, texture_jso
}));
const { size, regions } = atlas_layout(sizes);
const canvas = new OffscreenCanvas(size, size);
const ctx = canvas.getContext("2d")!;
const pixels = new Uint8ClampedArray(size * size * 4);
const atlas = { width: size, height: size, data: pixels };
// magenta and black checker
const missing = regions["cubi:missing"];
const half = TEXTURE_SIZE / 2;
ctx.fillStyle = "magenta";
ctx.fillRect(missing.x, missing.y, half, half);
ctx.fillRect(missing.x + half, missing.y + half, half, half);
ctx.fillStyle = "black";
ctx.fillRect(missing.x + half, missing.y, half, half);
ctx.fillRect(missing.x, missing.y + half, half, half);
for (let py = 0; py < TEXTURE_SIZE; py++) {
for (let px = 0; px < TEXTURE_SIZE; px++) {
const i = ((missing.y + py) * size + missing.x + px) * 4;
const magenta = (px < half) === (py < half);
pixels.set(magenta ? [255, 0, 255, 255] : [0, 0, 0, 255], i);
}
}
const animated: AnimatedTexture[] = [];
for (const [id, image] of images) {
const animation = animations.get(id);
const { x, y, width, height } = regions[id];
if (animation) {
const first = animation.frames[0];
ctx.drawImage(image, first.x, first.y, width, height, x, y, width, height);
animated.push(new AnimatedTexture(regions[id], animation, image_pixels(image), image.width));
} else {
ctx.drawImage(image, x, y);
}
image.close();
const first = animation?.frames[0] ?? { x: 0, y: 0 };
copy_pixels(image, first.x, first.y, width, height, atlas, x, y);
if (animation) animated.push(new AnimatedTexture(regions[id], animation, image.data, image.width));
}
for (const id of textures.keys()) {
regions[id] ??= missing;
}
const image_data = ctx.getImageData(0, 0, size, size);
const pixels = image_data.data;
extrude_borders(pixels, size, regions);
ctx.putImageData(image_data, 0, 0);
// which pixels of each texture can be seen, for the 3d models of dropped items (see item_model.ts)
built_atlas = image_data;
built_atlas = atlas;
opaque.clear();
for (const [id, region] of Object.entries(regions)) {
const mask = new Uint8Array(region.width * region.height);
@@ -140,7 +134,24 @@ export async function build_atlas(textures: Map<string, Uint8Array>, texture_jso
opaque.set(id, { width: region.width, height: region.height, mask });
}
return { image: canvas.transferToImageBitmap(), regions, animated };
return { image: atlas, regions, animated };
}
// a width × height part of from, starting at from_x from_y, into to at to_x to_y
function copy_pixels(
from: Pixels,
from_x: number,
from_y: number,
width: number,
height: number,
to: Pixels,
to_x: number,
to_y: number,
) {
for (let row = 0; row < height; row++) {
const start = ((from_y + row) * from.width + from_x) * 4;
to.data.set(from.data.subarray(start, start + width * 4), ((to_y + row) * to.width + to_x) * 4);
}
}
// fills each region's border with copies of its edge pixels, the corners with its corners. pixels is a size × size
@@ -169,13 +180,6 @@ export function extrude_borders(pixels: Uint8ClampedArray, size: number, regions
}
}
// every frame of an animated texture stays in memory, to copy into the atlas when it's that frame's turn
function image_pixels(image: ImageBitmap) {
const ctx = new OffscreenCanvas(image.width, image.height).getContext("2d")!;
ctx.drawImage(image, 0, 0);
return ctx.getImageData(0, 0, image.width, image.height).data;
}
// a texture's pixels, 1 (can be seen) or 0, row by row from the top
export interface OpaqueMask {
width: number;
@@ -192,22 +196,23 @@ export function opaque_pixels(texture_id: string): OpaqueMask | undefined {
}
// the last atlas built, before any animated texture moved on from its first frame
let built_atlas: ImageData | undefined;
let built_atlas: Pixels | undefined;
// the atlas as a png, undefined before one is built. for the f3 screen's copy button
// the atlas as a png, undefined before one is built or where there's no canvas to encode it (the desktop app). for
// the f3 screen's copy button
export function atlas_png(): Promise<Blob> | undefined {
if (!built_atlas) return undefined;
if (!built_atlas || typeof document === "undefined") return undefined;
const canvas = new OffscreenCanvas(built_atlas.width, built_atlas.height);
canvas.getContext("2d")!.putImageData(built_atlas, 0, 0);
canvas.getContext("2d")!.putImageData(new ImageData(built_atlas.data, built_atlas.width), 0, 0);
return canvas.convertToBlob({ type: "image/png" });
}
// the engine's own textures, which come with the client instead of a mod. see build_engine_textures in build.ts
export async function engine_textures(): Promise<Map<string, Uint8Array>> {
const names: string[] = await (await fetch("/assets/textures/index.json")).json();
const names: string[] = await (await fetch(new URL("/assets/textures/index.json", platform.page))).json();
const textures = new Map<string, Uint8Array>();
await Promise.all(names.map(async (name) => {
const response = await fetch(`/assets/textures/${name}.png`);
const response = await fetch(new URL(`/assets/textures/${name}.png`, platform.page));
textures.set(`cubi:${name}`, new Uint8Array(await response.arrayBuffer()));
}));
return textures;