Geo renderer improvements

This commit is contained in:
2026-11-21 19:24:09 -03:00
parent 717d2cb766
commit 4bdf5203d4
6 changed files with 141 additions and 18 deletions
+51 -4
View File
@@ -8,6 +8,7 @@ import {
entity_model_from_geo,
type EntityBoneJson,
type EntityJson,
type EntityModelJson,
validate_entity,
} from "@/common/mod_data.ts";
import type { Texture } from "@/client/renderer/mod.ts";
@@ -33,8 +34,8 @@ Deno.test("a bedrock geo.json becomes an entity model with only what the engine
],
});
// per face uvs and parents that come later aren't supported
const per_face = { origin: [0, 0, 0], size: [1, 1, 1], uv: { north: { uv: [0, 0], uv_size: [1, 1] } } };
// parents that come later and uvs that are neither box nor per face aren't supported
const bad_uv = { origin: [0, 0, 0], size: [1, 1, 1], uv: { north: { uv: [0, 0] } } };
const geo_problems = (bones: unknown[]) => {
const geometry = { description: { texture_width: 16, texture_height: 16 }, bones };
const { model, problems } = entity_model_from_geo({ "minecraft:geometry": [geometry] }, "a:b");
@@ -43,11 +44,57 @@ Deno.test("a bedrock geo.json becomes an entity model with only what the engine
};
const late_parent = geo_problems([{ name: "leg", parent: "body" }, { name: "body" }]);
assert(late_parent.some((p) => p.includes("parent body must be a bone before it")), String(late_parent));
const per_face_problems = geo_problems([{ name: "leg", cubes: [per_face] }]);
assert(per_face_problems.some((p) => p.includes("per face uvs aren't supported")), String(per_face_problems));
const uv_problems = geo_problems([{ name: "leg", cubes: [bad_uv] }]);
assert(uv_problems.some((p) => p.includes("uv.north.uv_size is required")), String(uv_problems));
assertEquals(entity_model_from_geo({ format_version: "1.12.0" }, "a:b").model, undefined);
});
// every vertex of the model's bones as [x, y, z, u, v], in texture pixels
function model_vertices(model: EntityModelJson, only?: (bone: EntityBoneJson) => boolean) {
const out: number[][] = [];
const atlas = { width: model.texture_width, height: model.texture_height } as Texture;
const region = { x: 0, y: 0, width: model.texture_width, height: model.texture_height };
push_model(model, () => IDENTITY, atlas, region, (x, y, z) => [x, y, z], undefined, only, (x, y, z, u, v) => {
out.push([x, y, z, u * model.texture_width, v * model.texture_height]);
});
return out;
}
Deno.test("per face uvs texture a cube like box uv does when they're where box uv puts each face", () => {
const [u, v] = [4, 2];
const [w, h, d] = [3, 5, 2];
const face = (fu: number, fv: number, fw: number, fh: number) => ({ uv: [u + fu, v + fv], uv_size: [fw, fh] });
// the way blockbench writes a box uv cube out per face: the bottom upside down
const per_face = {
north: face(d, d, w, h),
east: face(0, d, d, h),
south: face(2 * d + w, d, w, h),
west: face(d + w, d, d, h),
up: face(d, 0, w, d),
down: face(d + w, d, w, -d),
};
const geometry = {
description: { texture_width: 32, texture_height: 32 },
bones: [
{ name: "box", pivot: [0, 0, 0], cubes: [{ origin: [1, 2, 3], size: [w, h, d], uv: [u, v] }] },
{ name: "per_face", pivot: [0, 0, 0], cubes: [{ origin: [1, 2, 3], size: [w, h, d], uv: per_face }] },
{
name: "top_only",
pivot: [0, 0, 0],
cubes: [{ origin: [1, 2, 3], size: [w, h, d], uv: { up: per_face.up } }],
},
],
};
const { model, problems } = entity_model_from_geo({ "minecraft:geometry": [geometry] }, "a:b");
assertEquals(problems, []);
const box = model_vertices(model!, (bone) => bone.name === "box");
const per_face_vertices = model_vertices(model!, (bone) => bone.name === "per_face");
assertEquals(per_face_vertices.length, box.length);
per_face_vertices.forEach((vertex, i) => vertex.forEach((n, j) => assertAlmostEquals(n, box[i][j], 1e-9)));
// faces it leaves out aren't drawn
assertEquals(model_vertices(model!, (bone) => bone.name === "top_only"), box.slice(0, 6));
});
Deno.test("a cube's rotation turns it around its pivot like the same rotation on its bone", () => {
const pivot = [1, 2, 3];
const rotation = [30, -45, 60];