Item rendering
This commit is contained in:
@@ -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",
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user