Files
bworld/tests/worn_render_test.ts
2026-11-04 21:51:26 -03:00

117 lines
5.6 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// what players wear is drawn on their parts, moving with them, see client/rendering/worn_renderer.ts
import { assertAlmostEquals, assertEquals, assertNotEquals } from "@std/assert";
import { entity_model_from_geo } from "@/common/mod_data.ts";
import { apply, bone_matrices, type Vec3 } from "@/client/rendering/model_math.ts";
import {
on_part,
part_point,
PARTS,
pose_at,
SLIM_PARTS,
worn_bone_pose,
worn_look,
} from "@/client/rendering/player_model.ts";
// a glove made over blockbench's player template: its right arm's box, and a claw on a bone under it
const glove = entity_model_from_geo({
format_version: "1.12.0",
"minecraft:geometry": [{
description: { identifier: "geometry.glove", texture_width: 32, texture_height: 32 },
bones: [
{ name: "right_arm", pivot: [-5, 22, 0], cubes: [{ origin: [-8, 12, -2], size: [4, 12, 4], uv: [0, 0] }] },
{ name: "claw", parent: "right_arm", pivot: [-6, 12, 0], cubes: [] },
],
}],
}, "test:glove").model!;
const walking = (position: number, slim = false) =>
pose_at({ x: 0, y: 0, z: 0, yaw: 0, pitch: 0, slim, walk: { position, speed: 1 } });
const near = (actual: Vec3, expected: Vec3) => actual.forEach((n, i) => assertAlmostEquals(n, expected[i], 1e-9));
Deno.test("a worn bone named like a player part moves with that part as they walk", () => {
const arm = PARTS.find((part) => part.name === "right_arm")!;
const corners: Vec3[][] = [];
for (const position of [0, 1.5, 3]) {
const pose = walking(position);
const bones = bone_matrices(glove, worn_bone_pose(pose));
// the cube's corners, mirrored on x into the engine's axes like the renderer does, are the arm's corners
const low = apply(bones.get("right_arm")!, 4, 12, -2);
const high = apply(bones.get("right_arm")!, 8, 24, 2);
near(low, part_point(pose, arm, -1, -10, -2));
near(high, part_point(pose, arm, 3, 2, 2));
// and the bone under it goes along
near(apply(bones.get("claw")!, 6, 12, 0), apply(bones.get("right_arm")!, 6, 12, 0));
corners.push([low, high]);
}
assertNotEquals(corners[0], corners[1], "it swung");
});
Deno.test("slim players wear the slim model, on their slim arms", () => {
const worn = { model: "a:wide", texture: "a:wide_texture", slim_model: "a:slim", slim_texture: "a:slim_texture" };
assertEquals(worn_look(worn, false), { model: "a:wide", texture: "a:wide_texture" });
assertEquals(worn_look(worn, true), { model: "a:slim", texture: "a:slim_texture" });
// without slim ones, the wide ones
assertEquals(worn_look({ model: "a:wide", texture: "a:t" }, true), { model: "a:wide", texture: "a:t" });
// the slim arm turns around its own pivot, half a pixel lower
const slim_arm = SLIM_PARTS.find((part) => part.name === "right_arm")!;
const pose = walking(1.5, true);
const bones = bone_matrices(glove, worn_bone_pose(pose));
near(apply(bones.get("right_arm")!, ...slim_arm.pivot), slim_arm.pivot);
});
Deno.test("first person draws the bones on the right arm, and the ones under them", () => {
const [arm, claw] = glove.bones;
assertEquals([on_part(glove, arm, "right_arm"), on_part(glove, claw, "right_arm")], [true, true]);
assertEquals(on_part(glove, arm, "left_arm"), false);
});
// artifacts' models, see mods/artifacts/models: a cube's box in the engine's axes, mirrored like the renderer does
const artifact = (name: string) => {
const json = JSON.parse(Deno.readTextFileSync(`mods/artifacts/models/${name}.geo.json`));
return entity_model_from_geo(json, `artifacts:${name}`).model!;
};
const box = (
{ origin: [x, y, z], size: [w, h, d] }: { origin: Vec3; size: Vec3 },
) => [[-(x + w), y, z], [-x, y + h, z + d]];
Deno.test("artifacts' power glove is a sleeve over the right arm, wide and slim", () => {
for (const [name, parts] of [["power_glove_wide", PARTS], ["power_glove_slim", SLIM_PARTS]] as const) {
const arm = parts.find((part) => part.name === "right_arm")!;
const from = arm.from.map((n, i) => n + arm.pivot[i]);
const arm_box = [from, from.map((n, i) => n + arm.size[i])];
for (const cube of artifact(name).bones[0].cubes) assertEquals(box(cube), arm_box, name);
}
});
Deno.test("artifacts' feral claws come out of the bottom of the right hand, wide and slim", () => {
for (const [name, parts] of [["feral_claws_wide", PARTS], ["feral_claws_slim", SLIM_PARTS]] as const) {
const arm = parts.find((part) => part.name === "right_arm")!;
const hand = arm.pivot[1] + arm.from[1];
const claws = artifact(name).bones[0].cubes.map(box);
// they hang from the hand, the long ones across the arm. on slim arms they're half a pixel off its middle, like
// in artifacts' ArmsMeshDefinitions
for (const [, high] of claws) assertEquals(high[1], hand, name);
const [left, right] = [arm.pivot[0] + arm.from[0], arm.pivot[0] + arm.from[0] + arm.size[0]];
for (const [low, high] of claws.slice(0, 2)) {
assertEquals(Math.abs((low[0] + high[0]) / 2 - (left + right) / 2) <= 0.5, true, name);
}
}
});
Deno.test("the giant chestplate covers the player's body and arms, a pixel out like minecraft's armor", () => {
const json = JSON.parse(Deno.readTextFileSync("mods/bworld/models/giant_chestplate.geo.json"));
const chestplate = entity_model_from_geo(json, "bworld:giant_chestplate").model!;
for (const bone of chestplate.bones) {
const part = PARTS.find((part) => part.name === bone.name)!;
const from = part.from.map((n, i) => n + part.pivot[i]);
assertEquals(box(bone.cubes[0]), [from, from.map((n, i) => n + part.size[i])], bone.name);
assertEquals(bone.cubes[0].inflate, 1);
}
assertEquals(chestplate.bones.map((bone) => bone.name), ["body", "right_arm", "left_arm"]);
});