// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later import { assert, assertEquals } from "@std/assert"; 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"]; Deno.test("cubes resolve the old texture shapes", () => { assertEquals(resolve_texture("#top", "a:all", cube), "a:all"); const pillar = { top: "a:top", bottom: "a:bottom", side: "a:side" }; assertEquals(["#top", "#bottom", "#front", "#side"].map((t) => resolve_texture(t, pillar, cube)), [ "a:top", "a:bottom", "a:side", "a:side", ]); const furnace = { front: "a:front", side: "a:side" }; assertEquals(["#top", "#bottom", "#front"].map((t) => resolve_texture(t, furnace, cube)), [ "a:side", "a:side", "a:front", ]); assertEquals(resolve_texture("#nothing", { crop: "a:crop" }), "engine:missing"); }); Deno.test("a cube bakes into six full faces that cull against their neighbors", () => { const quads = bake_model(cube, "a:all"); assertEquals(quads.length, 6); assert(quads.every((q) => q.flush && q.cull === q.face && q.texture === "a:all")); // the top face, corners and uvs like the mesher always had assertEquals(quads[0].positions, [0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0]); assertEquals(quads[0].uvs, [0, 16, 16, 16, 16, 0, 0, 0]); }); Deno.test("crop and cross models are planes inside the block, seen from both sides", () => { const crop = bake_model(BUILTIN_MODELS["engine:crop"], { crop: "a:wheat" }); assertEquals(crop.length, 8); assert(crop.every((q) => !q.flush && q.cull === -1 && !q.shade && q.texture === "a:wheat")); assertEquals(crop[0].positions.filter((_, i) => i % 3 === 0), [0.25, 0.25, 0.25, 0.25]); const cross = bake_model(BUILTIN_MODELS["engine:cross"], { cross: "a:flower" }); assertEquals(cross.length, 4); // rescaled so the diagonal planes stay as wide as the block, like minecraft's (0.8 to 15.2 pixels) for (const quad of cross) { for (const v of quad.positions) assert(v > -1e-6 && v < 1 + 1e-6, `${v} is outside the block`); } const xs = cross[0].positions.filter((_, i) => i % 3 === 0).map((v) => Math.round(v * 1000) / 1000); assertEquals(new Set(xs), new Set([0.05, 0.95])); }); Deno.test("turning a model moves its faces and cullfaces", () => { const model: ModelJson = { id: "a:half", elements: [{ from: [0, 0, 0], to: [16, 16, 8], faces: { north: { texture: "a:x", cullface: "north" } }, }], }; const [north] = bake_model(model, undefined, 0); const [east] = bake_model(model, undefined, 90); assertEquals([north.face, north.cull], [3, 3]); assertEquals([east.face, east.cull], [5, 5]); assert(east.flush); }); Deno.test("an x turn tips a model's top to the north, before its y turn", () => { // a slab's top face, culled by what's above it const model: ModelJson = { id: "a:slab", elements: [{ from: [0, 0, 0], to: [16, 8, 16], faces: { top: { texture: "a:x" }, bottom: { texture: "a:x", cullface: "bottom" } }, }], }; const [top, bottom] = bake_model(model, undefined, 0, 90); assertEquals(top.face, 3); // the bottom tips to the south, and is still against the block's edge there assertEquals([bottom.face, bottom.cull], [2, 2]); assert(bottom.flush); const [east] = bake_model(model, undefined, 90, 90); assertEquals(east.face, 5); const [down] = bake_model(model, undefined, 0, 180); assertEquals(down.face, 1); }); Deno.test("variants pick textures by state", () => { const { block } = block_from_json(JSON.parse(Deno.readTextFileSync("mods/bworld/blocks/wheat.json")).block); assertEquals(block_variant(block, { age: 5 }).textures, { crop: "bworld:wheat_stage_5" }); assertEquals(block_variant(block, { age: 5 }).model, "engine:crop"); assertEquals(block_variant(block).textures, { crop: "bworld:wheat_stage_0" }); // 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", ]); });