Files
bworld/tests/item_model_test.ts
T
2026-10-12 15:09:22 -03:00

71 lines
3.2 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// dropped items are drawn 3d like minecraft's: the sprite front and back, and a side face on every exposed pixel edge
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][], 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
function faces_outward(quad: ItemQuad, center: number[]) {
const [a, b, c] = quad.corners;
const e1 = [b[0] - a[0], b[1] - a[1], b[2] - a[2]];
const e2 = [c[0] - a[0], c[1] - a[1], c[2] - a[2]];
const normal = [e1[1] * e2[2] - e1[2] * e2[1], e1[2] * e2[0] - e1[0] * e2[2], e1[0] * e2[1] - e1[1] * e2[0]];
const middle = [0, 1, 2].map((axis) => quad.corners.reduce((sum, p) => sum + p[axis], 0) / 4);
return normal.reduce((sum, n, axis) => sum + n * (middle[axis] - center[axis]), 0) > 0;
}
Deno.test("one pixel is a little box: front, back and four sides", () => {
const quads = extrude_item(mask([[3, 5]]));
assertEquals(quads.length, 6);
// the box is around that pixel's middle
const center = [3.5 / TEXTURE_SIZE - 0.5, 1 - 5.5 / TEXTURE_SIZE, 0];
for (const quad of quads.slice(2)) assert(faces_outward(quad, center));
assert(faces_outward(quads[0], [0, 0.5, 0]) && faces_outward(quads[1], [0, 0.5, 0]));
});
Deno.test("only edges next to nothing get a side, and see-through pixels get none", () => {
// 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({
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);
// before the atlas is built there's no mask, it's flat
assertEquals(extrude_item(undefined).length, 2);
});
Deno.test("side faces show their pixel's color, and the model is one pixel thick", () => {
const [, , ...sides] = extrude_item(mask([[7, 2]]));
for (const side of sides) {
for (const [u, v] of side.uvs) assert(u > 7 && u < 8 && v > 2 && v < 3, `${u}, ${v} is outside the pixel`);
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);
}
}
});