// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // items drawn with a model, placed in each place they're shown by the model's display transforms 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, 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"; import { copy_dir, test_game } from "./helpers.ts"; const HANDLE = { from: [7, 0, 7], to: [9, 14, 9], faces: { north: { texture: "#0" }, south: { texture: "#0" }, top: { texture: "#0" } }, }; const GUI: DisplayTransform = { rotation: [30, 45, 0], scale: [0.625, 0.625, 0.625] }; // mods/bworld and a mod made from the template, with a wrench item drawn with this model function mods_with(model: Record) { const dir = Deno.makeTempDirSync({ prefix: "bworld_item_display_" }); copy_dir("mods/bworld", `${dir}/bworld`); create_mod("copper_tools", "Copper Tools", dir); const write = (path: string, json: Record) => { Deno.mkdirSync(`${dir}/copper_tools/${path.slice(0, path.lastIndexOf("/"))}`, { recursive: true }); Deno.writeTextFileSync(`${dir}/copper_tools/${path}.json`, JSON.stringify({ format_version: 1, ...json })); }; write("models/wrench", { model: { id: "copper_tools:wrench", ...model } }); write("items/wrench", { item: { id: "copper_tools:wrench", texture: "bworld:stick", model: "copper_tools:wrench" }, }); return dir; } Deno.test("an item with a model is drawn with it, in the model's own textures, placed by its display transforms", async () => { const dir = mods_with({ textures: { "0": "bworld:oak_planks" }, elements: [HANDLE], display: { gui: GUI } }); await test_game(dir); const wrench = new ItemStack("copper_tools:wrench", 1); const model = item_model_of(wrench)!; assertEquals(model.quads.length, 3); assert(model.quads.every((quad) => quad.texture === "bworld:oak_planks")); assertEquals(model.display, { gui: GUI }); // held and dropped like a block where it has no transform of its own assert(drawn_as_model(wrench)); assertEquals(item_model_of(new ItemStack("bworld:stick", 1)), undefined); assert(!drawn_as_model(new ItemStack("bworld:stick", 1))); Deno.removeSync(dir, { recursive: true }); }); Deno.test("check-mods reports an item's model that doesn't exist", async () => { const dir = mods_with({ textures: { "0": "bworld:oak_planks" }, elements: [HANDLE] }); Deno.removeSync(`${dir}/copper_tools/models/wrench.json`); const errors = (await load_and_check(dir)).find((mod) => mod.id === "copper_tools")!.report.errors.join("\n"); assert(errors.includes("model copper_tools:wrench doesn't exist"), errors); 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[]) => actual.forEach((v, i) => assertAlmostEquals(v, expected[i], 1e-9)); const base = mat4.fromTranslation(mat4.create(), [10, 20, 30]); // no transform: the model's middle where base puts things near(display_transform(base, {})(0, 0.5, 0), [10, 20, 30]); // translation is in pixels near(display_transform(base, { translation: [16, 8, 0] })(0, 0.5, 0), [11, 20.5, 30]); // the middle of its east side ends up on top: north (-z) after y, then up after x near(display_transform(base, { rotation: [90, 90, 0] })(0.5, 0.5, 0), [10, 20.5, 30]); near(display_transform(base, { rotation: [0, 90, 0] })(0.5, 0.5, 0), [10, 20, 29.5]); near(display_transform(base, { scale: [0.5, 0.5, 0.5] })(0.5, 1, 0), [10.25, 20.25, 30]); });