3d models for items

This commit is contained in:
2026-09-26 21:45:18 -03:00
parent 7e5bcaa239
commit aef8836f15
4 changed files with 209 additions and 14 deletions
+22
View File
@@ -49,9 +49,31 @@ export async function build_atlas(textures: Map<string, Uint8Array>) {
}
}));
// which pixels of each texture can be seen, for the 3d models of dropped items (see item_model.ts)
const pixels = ctx.getImageData(0, 0, size, size).data;
opaque.clear();
for (const [id, region] of Object.entries(regions)) {
const mask = new Uint8Array(TEXTURE_SIZE * TEXTURE_SIZE);
for (let py = 0; py < TEXTURE_SIZE; py++) {
for (let px = 0; px < TEXTURE_SIZE; px++) {
const i = ((region.y * TEXTURE_SIZE + py) * size + region.x * TEXTURE_SIZE + px) * 4;
mask[py * TEXTURE_SIZE + px] = pixels[i + 3] > 0 ? 1 : 0;
}
}
opaque.set(id, mask);
}
return { image: canvas.transferToImageBitmap(), regions };
}
// by texture id, TEXTURE_SIZE * TEXTURE_SIZE of 1 (can be seen) or 0, row by row from the top
const opaque = new Map<string, Uint8Array>();
// which pixels of a texture aren't fully see-through, undefined before the atlas is built or for unknown textures
export function opaque_pixels(texture_id: string): Uint8Array | undefined {
return opaque.get(texture_id);
}
// 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();
+114
View File
@@ -0,0 +1,114 @@
// 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 } 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 TEXTURE_SIZE, v down)
uvs: [number, number][];
shade: number;
}
// one pixel of a 16 pixel sprite
const PIXEL = 1 / TEXTURE_SIZE;
const HALF_THICKNESS = PIXEL / 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<string, ItemQuad[]>();
// 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)
export function extrude_item(mask: Uint8Array | undefined): ItemQuad[] {
const t = HALF_THICKNESS;
const lo = FACE_PAD;
const hi = TEXTURE_SIZE - 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]], [[lo, hi], [hi, hi], [hi, lo], [lo, lo]], [
0,
0,
1,
], 1),
quad([[0.5, 0, -t], [-0.5, 0, -t], [-0.5, 1, -t], [0.5, 1, -t]], [[hi, hi], [lo, hi], [lo, lo], [hi, lo]], [
0,
0,
-1,
], 1),
];
if (!mask) return quads;
const seen = (px: number, py: number) =>
px >= 0 && py >= 0 && px < TEXTURE_SIZE && py < TEXTURE_SIZE && mask[py * TEXTURE_SIZE + px] === 1;
for (let py = 0; py < TEXTURE_SIZE; py++) {
for (let px = 0; px < TEXTURE_SIZE; px++) {
if (!seen(px, py)) continue;
const x0 = px * PIXEL - 0.5;
const x1 = x0 + PIXEL;
const y1 = 1 - py * PIXEL;
const y0 = y1 - PIXEL;
// 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 };
}
+24 -14
View File
@@ -5,6 +5,7 @@ import { get_sprite_region } from "$/client/sprites.ts";
import type { ItemEntity } from "$/client/entity/item_entity.ts";
import { push_vertex, Texture } from "$/client/renderer/mod.ts";
import { block_item_texture, cube_face_texture } from "$/common/block_models.ts";
import { item_model } from "./item_model.ts";
// how big items on the ground are drawn, minecraft's ground transform
const BLOCK_SCALE = 0.25;
@@ -87,7 +88,7 @@ function push_block(
}
}
// a flat sprite standing up and turning, drawn from both sides
// a flat item as its 3d model (see item_model.ts), standing up and turning around its middle
function push_sprite(
atlas: Texture,
texture_id: string,
@@ -96,19 +97,28 @@ function push_sprite(
angle: number,
) {
const region = get_sprite_region(texture_id);
const dx = Math.cos(angle) * size / 2;
const dz = Math.sin(angle) * size / 2;
const bottom = base.y;
const top = base.y + size;
const front = [
[base.x - dx, bottom, base.z - dz],
[base.x + dx, bottom, base.z + dz],
[base.x + dx, top, base.z + dz],
[base.x - dx, top, base.z - dz],
];
push_textured_quad(atlas, region, front, 1);
// the back is the same quad the other way round, mirrored so it isn't drawn backwards
push_textured_quad(atlas, region, [front[1], front[0], front[3], front[2]], 1);
const sin = Math.sin(angle);
const cos = Math.cos(angle);
const u = (pixels: number) => (region.x * TEXTURE_SIZE + pixels) / atlas.width;
const v = (pixels: number) => (region.y * TEXTURE_SIZE + pixels) / atlas.height;
for (const quad of item_model(texture_id)) {
for (const i of [0, 1, 2, 0, 2, 3]) {
const [lx, ly, lz] = quad.corners[i];
const [pu, pv] = quad.uvs[i];
push_vertex(
base.x + (lx * cos - lz * sin) * size,
base.y + ly * size,
base.z + (lx * sin + lz * cos) * size,
u(pu),
v(pv),
quad.shade,
quad.shade,
quad.shade,
1,
);
}
}
}
function push_textured_quad(atlas: Texture, region: { x: number; y: number }, corners: number[][], shade: number) {
+49
View File
@@ -0,0 +1,49 @@
// 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);
}
});