Item rendering

This commit is contained in:
2026-10-13 22:29:38 -03:00
parent a18b099206
commit b34e97e487
269 changed files with 12566 additions and 3220 deletions
+79 -2
View File
@@ -2,8 +2,16 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import { assert, assertEquals } from "@std/assert";
import { bake_model, block_variant, BUILTIN_MODELS, type ModelJson, resolve_texture } from "$/common/block_models.ts";
import { block_from_json } from "$/common/mod_data.ts";
import {
bake_block,
bake_model,
type BakedQuad,
block_variant,
BUILTIN_MODELS,
type ModelJson,
resolve_texture,
} from "$/common/block_models.ts";
import { block_from_json, validate_block } from "$/common/mod_data.ts";
import { set_state_value } from "$/common/utils.ts";
const cube = BUILTIN_MODELS["engine:cube"];
@@ -96,3 +104,72 @@ Deno.test("variants pick textures by state", () => {
// states live above the numeric id
assertEquals(set_state_value(7, block, "age", 3), (3 << 16) | 7);
});
// where a texture's pixels are on an unturned block's face, at a point on it (in pixels): what the default uvs give
const UNTURNED_UV: ((x: number, y: number, z: number) => number[])[] = [
(x, _, z) => [x, z],
(x, _, z) => [x, 16 - z],
(x, y) => [x, 16 - y],
(x, y) => [16 - x, 16 - y],
(_, y, z) => [z, 16 - y],
(_, y, z) => [16 - z, 16 - y],
];
const where_unturned = (quad: BakedQuad) =>
[0, 1, 2, 3].map((i) => {
const [x, y, z] = quad.positions.slice(i * 3, i * 3 + 3).map((v) => Math.round(v * 16 * 1000) / 1000);
return UNTURNED_UV[quad.face](x, y, z);
});
const uvs_of = (quad: BakedQuad) => [0, 1, 2, 3].map((i) => quad.uvs.slice(i * 2, i * 2 + 2));
Deno.test("uvlock keeps every face's texture where it is on an unturned block, whichever way the model turns", () => {
// a stair's step: half a block, every face with its default uvs
const faces = Object.fromEntries(
["top", "bottom", "north", "south", "west", "east"].map((f) => [f, { texture: "a:x" }]),
);
const step: ModelJson = { id: "a:step", elements: [{ from: [8, 8, 0], to: [16, 16, 16], faces }] };
let differs = false;
for (const x of [0, 90, 180, 270]) {
for (const y of [0, 90, 180, 270]) {
for (const quad of bake_model(step, undefined, y, x, true)) {
assertEquals(uvs_of(quad), where_unturned(quad), `x ${x}, y ${y}, face ${quad.face}`);
}
differs ||= bake_model(step, undefined, y, x).some((q) => String(uvs_of(q)) !== String(where_unturned(q)));
}
}
assert(differs, "without uvlock the textures turn with the model");
});
Deno.test("a texture's part of a face turns with uvlock around the texture's middle", () => {
// the top's texture from its top left quarter, on a full face
const model: ModelJson = {
id: "a:corner",
elements: [{ from: [0, 0, 0], to: [16, 16, 16], faces: { top: { texture: "a:x", uv: [0, 0, 8, 8] } } }],
};
// turned a quarter clockwise, the quarter of the texture that was at the top left is at the top right
const [top] = bake_model(model, undefined, 90, 0, true);
const us = uvs_of(top).map(([u]) => u);
const vs = uvs_of(top).map(([, v]) => v);
assertEquals([Math.min(...us), Math.max(...us), Math.min(...vs), Math.max(...vs)], [8, 16, 0, 8]);
});
Deno.test("variants and parts turn with uvlock when they ask for it", () => {
const { block } = block_from_json({
id: "a:stair",
textures: "a:planks",
model: "a:step",
states: [{ name: "facing", bits: 2, default: 0 }],
variants: { "facing=1": { y: 90, uvlock: true }, "facing=2": { y: 180 } },
});
const step: ModelJson = {
id: "a:step",
elements: [{ from: [8, 8, 0], to: [16, 16, 16], faces: { top: { texture: "#all" } } }],
};
const [locked] = bake_block(block, (1 << 16) | 0, { "a:step": step });
assertEquals(uvs_of(locked), where_unturned(locked));
const [turned] = bake_block(block, (2 << 16) | 0, { "a:step": step });
assertEquals(block_variant(block, { facing: 2 }).uvlock, false);
assert(String(uvs_of(turned)) !== String(where_unturned(turned)));
assertEquals(validate_block({ id: "a:b", textures: "a:c", parts: [{ model: "a:d", uvlock: "yes" }] }), [
"parts[0].uvlock must be true or false",
]);
});
+46 -3
View File
@@ -105,6 +105,32 @@ Deno.test("a folder converts into the same folders, models only there to be pare
assertEquals(JSON.parse(Deno.readTextFileSync(`${output}/block/template.json`)).model.parent, undefined);
});
Deno.test("display transforms for the places bworld shows items are kept, and a root of only those is a parent", () => {
const display = {
gui: { rotation: [30, 45, 0], translation: [0, 0, 0], scale: [0.625, 0.625, 0.625] },
ground: { translation: [0, 3, 0], scale: [0.25, 0.25, 0.25] },
thirdperson_righthand: { rotation: [75, 45, 0] },
head: { scale: [1.5, 1.5, 1.5] },
};
const root = resources({
"block/block": { parent: "block/block", display },
"block/gear": { ...GEAR, parent: "gears:block/block" },
"item/third_person": {
parent: "block/block",
display: { thirdperson_righthand: display.thirdperson_righthand },
},
});
const output = Deno.makeTempDirSync();
const report = import_models(root, { output });
assertEquals(report.errors, []);
const block = JSON.parse(Deno.readTextFileSync(`${output}/block/block.json`)).model;
assertEquals(block, { id: "gears:block_block", display: { gui: display.gui, ground: display.ground } });
assertEquals(validate_model(block), []);
assertEquals(JSON.parse(Deno.readTextFileSync(`${output}/block/gear.json`)).model.parent, "gears:block_block");
// nothing in it is for a place bworld shows items
assertEquals(report.skipped.map(({ location }) => location), ["gears:item/third_person"]);
});
Deno.test("models that can't be converted are reported, not written", () => {
const root = resources({
"block/obj": { loader: "forge:obj", model: "gears:models/block/thing.obj" },
@@ -268,6 +294,21 @@ Deno.test("a blockstate's variants become numbered states, each drawn with its m
assertEquals(block.drops, "gears:shaft");
});
Deno.test("uvlock carries over, so turned textures stay where they are on an unturned block", () => {
const { block, report } = import_block({
"blockstates/steps": {
variants: {
"facing=east": { model: "gears:block/steps" },
"facing=south": { model: "gears:block/steps", y: 90, uvlock: true },
},
},
"models/block/steps": GEAR,
}, "steps");
assertEquals(block.variants["facing=1"], { model: "gears:block_steps", y: 90, uvlock: true });
assertEquals(block.variants["facing=0"], { model: "gears:block_steps" });
assertEquals(report.warnings, []);
});
Deno.test("multipart blockstates become parts, one for each way a part can be drawn", () => {
const { block } = import_block({
"blockstates/pipe": {
@@ -309,7 +350,7 @@ Deno.test("cubes stay solid, and models that can't be drawn are placeholders", (
assertStringIncludes(report.warnings[0].warning, "placeholder");
});
Deno.test("items that aren't a block's get their sprite as their texture", () => {
Deno.test("items that aren't a block's get their sprite as their texture, or their model when java draws them in 3D", () => {
const output = Deno.makeTempDirSync();
const report = import_items(
assets({
@@ -328,8 +369,10 @@ Deno.test("items that aren't a block's get their sprite as their texture", () =>
const sheet = JSON.parse(Deno.readTextFileSync(`${output}/sheet.json`)).item;
assertEquals(sheet, { id: "gears:sheet", texture: "gears:item_sheet" });
assertEquals(validate_item(sheet), []);
// java draws it in 3D, here it's flat until items can have models
assertEquals(JSON.parse(Deno.readTextFileSync(`${output}/wrench.json`)).item.texture, "gears:item_wrench");
// the model kind writes its model, and its texture is its model's
const wrench = JSON.parse(Deno.readTextFileSync(`${output}/wrench.json`)).item;
assertEquals(wrench, { id: "gears:wrench", texture: "gears:item_wrench", model: "gears:item_wrench" });
assertEquals(validate_item(wrench), []);
});
Deno.test("a multipart block is placed in a state that draws something, like a wall with its post", () => {
+73
View File
@@ -0,0 +1,73 @@
// 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 } 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<string, unknown>) {
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<string, unknown>) => {
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", () => {
const dir = mods_with({ textures: { "0": "bworld:oak_planks" }, elements: [HANDLE] });
Deno.removeSync(`${dir}/copper_tools/models/wrench.json`);
const errors = 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("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]);
});
+15
View File
@@ -4,6 +4,7 @@
import { assert, assertEquals } from "@std/assert";
import { BUILTIN_MODELS, type ModelJson, resolve_models, resolve_texture } from "$/common/block_models.ts";
import { EverythingRegistry } from "$/common/everything_registry.ts";
import { validate_model } from "$/common/mod_data.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";
@@ -24,6 +25,20 @@ Deno.test("a model gets its parent's elements and textures, with its own texture
assertEquals(tall.elements.length, 2);
});
Deno.test("a model gets its parent's display transform for each place it doesn't set its own", () => {
const { models } = resolve_models([
{ id: "a:base", display: { gui: { rotation: [30, 45, 0] }, ground: { scale: [0.25, 0.25, 0.25] } } },
{ id: "a:wrench", parent: "a:base", display: { gui: { scale: [2, 2, 2] } }, elements: [BOX] },
], () => undefined);
// the whole transform, not its fields one by one
assertEquals(models[1].display, { gui: { scale: [2, 2, 2] }, ground: { scale: [0.25, 0.25, 0.25] } });
assertEquals(validate_model({ id: "a:base", display: models[1].display }), []);
assertEquals(validate_model({ id: "a:base", display: { head: {}, gui: { scale: 2 } } }), [
'display: "head" isn\'t one of gui, firstperson_righthand, ground, fixed',
"display.gui.scale must be [x, y, z]",
]);
});
Deno.test("a parent can be another mod's model or a built-in one", () => {
const earlier: Record<string, ModelJson> = { "base:slab": { id: "base:slab", elements: [BOX] } };
const { models, problems } = resolve_models([