Allow for textures that aren't 16x16

This commit is contained in:
2026-09-28 16:20:37 -03:00
parent 52ef4e5ca6
commit fe149bb9ad
18 changed files with 268 additions and 136 deletions
+23 -5
View File
@@ -3,10 +3,10 @@ import { assert, assertEquals } from "@std/assert";
import { extrude_item, type ItemQuad } from "$/client/rendering/item_model.ts";
import { TEXTURE_SIZE } from "$/common/constants.ts";
function mask(pixels: [number, number][]) {
const mask = new Uint8Array(TEXTURE_SIZE * TEXTURE_SIZE);
for (const [x, y] of pixels) mask[y * TEXTURE_SIZE + x] = 1;
return mask;
function mask(pixels: [number, number][], width = TEXTURE_SIZE, height = TEXTURE_SIZE) {
const mask = new Uint8Array(width * height);
for (const [x, y] of pixels) mask[y * width + x] = 1;
return { width, height, mask };
}
// each quad's corners go counter clockwise seen from outside, or it's culled from the side it should show
@@ -32,7 +32,11 @@ Deno.test("only edges next to nothing get a side, and see-through pixels get non
// two pixels side by side share an edge, so 6 sides instead of 8
assertEquals(extrude_item(mask([[0, 0], [1, 0]])).length, 2 + 6);
// a full sprite only has its border
const full = extrude_item(new Uint8Array(TEXTURE_SIZE * TEXTURE_SIZE).fill(1));
const full = extrude_item({
width: TEXTURE_SIZE,
height: TEXTURE_SIZE,
mask: new Uint8Array(TEXTURE_SIZE * TEXTURE_SIZE).fill(1),
});
assertEquals(full.length, 2 + 4 * TEXTURE_SIZE);
// nothing to see, only the (see-through) front and back
assertEquals(extrude_item(mask([])).length, 2);
@@ -47,3 +51,17 @@ Deno.test("side faces show their pixel's color, and the model is one pixel thick
for (const [, , z] of side.corners) assertEquals(Math.abs(z), 0.5 / TEXTURE_SIZE);
}
});
Deno.test("a bigger sprite has smaller pixels, and is as thick as a 16 pixel one", () => {
const [front, , ...sides] = extrude_item(mask([[31, 0]], 32, 64));
// the front covers the whole sprite
assert(front.uvs.some(([u, v]) => u > 31 && v > 63));
// the top right pixel: 1/32 across and 1/64 down
for (const side of sides) {
for (const [x, y, z] of side.corners) {
assert(x >= 0.5 - 1 / 32 - 1e-9 && x <= 0.5 + 1e-9, `x ${x}`);
assert(y >= 1 - 1 / 64 - 1e-9 && y <= 1 + 1e-9, `y ${y}`);
assertEquals(Math.abs(z), 0.5 / TEXTURE_SIZE);
}
}
});