Render geo models in items

This commit is contained in:
2026-11-22 12:24:14 -03:00
parent dee07eaa0c
commit e751d22c6e
10 changed files with 171 additions and 29 deletions
+44 -1
View File
@@ -5,7 +5,7 @@
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
import { mat4 } from "gl-matrix";
import { display_transform, drawn_as_model, item_model_of } from "@/client/rendering/item_renderer.ts";
import type { DisplayTransform } from "@/common/block_models.ts";
import { type DisplayTransform, FACE_INDEX } from "@/common/block_models.ts";
import { ItemStack } from "@/common/inventory.ts";
import { load_and_check } from "@/tools/check_mods.ts";
import { create_mod } from "@/tools/new_mod.ts";
@@ -57,6 +57,49 @@ Deno.test("check-mods reports an item's model that doesn't exist", async () => {
Deno.removeSync(dir, { recursive: true });
});
Deno.test("an item can be drawn with an entity model, in its own texture, placed by the model's display transforms", async () => {
const dir = mods_with({ textures: { "0": "bworld:oak_planks" }, elements: [HANDLE] });
// the same handle as a bedrock model, as blockbench exports it
Deno.removeSync(`${dir}/copper_tools/models/wrench.json`);
Deno.writeTextFileSync(
`${dir}/copper_tools/models/wrench.geo.json`,
JSON.stringify({
format_version: "1.21.0",
"minecraft:geometry": [{
description: { identifier: "geometry.wrench", texture_width: 16, texture_height: 16 },
item_display_transforms: {
gui: { ...GUI, translation: [0, 0, 0], rotation_pivot: [0, 0, 0], fit_to_frame: true },
thirdperson_righthand: { rotation: [70, 45, 0] },
},
bones: [{
name: "handle",
pivot: [0, 0, 0],
cubes: [{ origin: [-1, 0, -1], size: [2, 14, 2], uv: [0, 0] }],
}],
}],
}),
);
const report = (await load_and_check(dir)).find((mod) => mod.id === "copper_tools")!.report;
assertEquals(report.errors, []);
await test_game(dir);
const wrench = new ItemStack("copper_tools:wrench", 1);
const model = item_model_of(wrench)!;
assert(drawn_as_model(wrench));
assertEquals(model.quads.length, 6);
assert(model.quads.every((quad) => quad.texture === "bworld:stick"));
// only what the engine places items with, in the places it shows them
assertEquals(model.display, { gui: { ...GUI, translation: [0, 0, 0] } });
// standing in the middle of the block, 16 pixels to it
const xs = model.quads.flatMap((quad) => quad.positions.filter((_, i) => i % 3 === 0));
const ys = model.quads.flatMap((quad) => quad.positions.filter((_, i) => i % 3 === 1));
assertEquals([Math.min(...xs), Math.max(...xs)], [7 / 16, 9 / 16]);
assertEquals([Math.min(...ys), Math.max(...ys)], [0, 14 / 16]);
// faces know which way they face, for their shading
const top = model.quads.find((quad) => quad.face === FACE_INDEX.top)!;
assert(top.positions.filter((_, i) => i % 3 === 1).every((y) => y === 14 / 16));
Deno.removeSync(dir, { recursive: true });
});
Deno.test("a display transform moves, turns around z then y then x, and scales, around the model's middle", () => {
// an item's points are centered on x and z, from 0 to 1 high
const near = (actual: number[], expected: number[]) =>