// the 3d model of a flat item, like minecraft's item/generated: its sprite on the front and back, one pixel apart, with // a side face along every edge between a pixel that can be seen and one that can't (or the sprite's border) import { TEXTURE_SIZE } from "$/common/constants.ts"; import { opaque_pixels, type OpaqueMask } from "$/client/atlas.ts"; export interface ItemQuad { // 4 corners counter clockwise seen from outside. x from -0.5 to 0.5 and y from 0 to 1 across the sprite, z its // thickness around 0 corners: [number, number, number][]; // u v per corner, in pixels of the sprite (0 to its width or height, v down) uvs: [number, number][]; shade: number; } // as thick as a pixel of a 16 pixel sprite, whatever the sprite's size const HALF_THICKNESS = 1 / TEXTURE_SIZE / 2; // keeps lookups off the neighboring sprite, and side faces in the middle of their pixel const FACE_PAD = 0.02; const SIDE_PAD = 0.25; // darker to the sides and bottom, so the edges show const SIDE_SHADE = 0.7; const TOP_SHADE = 0.9; const BOTTOM_SHADE = 0.5; const cache = new Map(); // cached per texture, the atlas is built once per page. without its pixels (not built yet) it's only the front and back export function item_model(texture_id: string): ItemQuad[] { let model = cache.get(texture_id); if (!model) { const mask = opaque_pixels(texture_id); model = extrude_item(mask); if (mask) cache.set(texture_id, model); } return model; } // the model for a sprite's opaque pixels (see opaque_pixels in atlas.ts) // without a mask it's a 16×16 sprite export function extrude_item(opaque: OpaqueMask | undefined): ItemQuad[] { const width = opaque?.width ?? TEXTURE_SIZE; const height = opaque?.height ?? TEXTURE_SIZE; const t = HALF_THICKNESS; const [u_lo, u_hi] = [FACE_PAD, width - FACE_PAD]; const [v_lo, v_hi] = [FACE_PAD, height - FACE_PAD]; const quads: ItemQuad[] = [ // the front, and the back mirrored so it reads the same way round as minecraft's quad( [[-0.5, 0, t], [0.5, 0, t], [0.5, 1, t], [-0.5, 1, t]], [[u_lo, v_hi], [u_hi, v_hi], [u_hi, v_lo], [ u_lo, v_lo, ]], [0, 0, 1], 1, ), quad( [[0.5, 0, -t], [-0.5, 0, -t], [-0.5, 1, -t], [0.5, 1, -t]], [[u_hi, v_hi], [u_lo, v_hi], [u_lo, v_lo], [ u_hi, v_lo, ]], [0, 0, -1], 1, ), ]; if (!opaque) return quads; const { mask } = opaque; const seen = (px: number, py: number) => px >= 0 && py >= 0 && px < width && py < height && mask[py * width + px] === 1; // a sprite that isn't square is squeezed into the square const pixel_w = 1 / width; const pixel_h = 1 / height; for (let py = 0; py < height; py++) { for (let px = 0; px < width; px++) { if (!seen(px, py)) continue; const x0 = px * pixel_w - 0.5; const x1 = x0 + pixel_w; const y1 = 1 - py * pixel_h; const y0 = y1 - pixel_h; // the whole side face is this pixel's color const u0 = px + SIDE_PAD; const u1 = px + 1 - SIDE_PAD; const v0 = py + SIDE_PAD; const v1 = py + 1 - SIDE_PAD; const uvs: [number, number][] = [[u0, v1], [u1, v1], [u1, v0], [u0, v0]]; if (!seen(px - 1, py)) { quads.push(quad([[x0, y0, -t], [x0, y0, t], [x0, y1, t], [x0, y1, -t]], uvs, [-1, 0, 0], SIDE_SHADE)); } if (!seen(px + 1, py)) { quads.push(quad([[x1, y0, t], [x1, y0, -t], [x1, y1, -t], [x1, y1, t]], uvs, [1, 0, 0], SIDE_SHADE)); } if (!seen(px, py - 1)) { quads.push(quad([[x0, y1, t], [x1, y1, t], [x1, y1, -t], [x0, y1, -t]], uvs, [0, 1, 0], TOP_SHADE)); } if (!seen(px, py + 1)) { quads.push(quad([[x0, y0, -t], [x1, y0, -t], [x1, y0, t], [x0, y0, t]], uvs, [0, -1, 0], BOTTOM_SHADE)); } } } return quads; } // a quad facing normal: the corners are put in the order that faces that way, since faces are only drawn from the front function quad( corners: [number, number, number][], uvs: [number, number][], normal: [number, number, number], shade: number, ): ItemQuad { const [a, b, c] = 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 facing = (e1[1] * e2[2] - e1[2] * e2[1]) * normal[0] + (e1[2] * e2[0] - e1[0] * e2[2]) * normal[1] + (e1[0] * e2[1] - e1[1] * e2[0]) * normal[2]; if (facing < 0) { return { corners: [corners[0], corners[3], corners[2], corners[1]], uvs: [uvs[0], uvs[3], uvs[2], uvs[1]], shade, }; } return { corners, uvs, shade }; }