// 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][]) { const mask = new Uint8Array(TEXTURE_SIZE * TEXTURE_SIZE); for (const [x, y] of pixels) mask[y * TEXTURE_SIZE + x] = 1; return 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(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); } });