From 9bf6b989538421823b8e5b33d67ca0fef0851f70 Mon Sep 17 00:00:00 2001 From: Paula Date: Fri, 13 Nov 2026 23:06:37 -0300 Subject: [PATCH] Rotation for cubes on entities --- client/rendering/model_math.ts | 15 +++++++------ client/rendering/model_renderer.ts | 6 ++--- common/mod_data.ts | 14 ++++++++++-- docs/modding/entities/index.md | 2 +- tests/mod_entity_test.ts | 36 ++++++++++++++++++++++++++++-- 5 files changed, 58 insertions(+), 15 deletions(-) diff --git a/client/rendering/model_math.ts b/client/rendering/model_math.ts index 5383047..8854ec9 100644 --- a/client/rendering/model_math.ts +++ b/client/rendering/model_math.ts @@ -3,7 +3,7 @@ // turning and moving the parts of models: entity models' bones, the player's parts and what's worn on them. no // drawing here, so tests can use it -import type { EntityBoneJson, EntityModelJson } from "@/common/mod_data.ts"; +import type { EntityBoneJson, EntityCubeJson, EntityModelJson } from "@/common/mod_data.ts"; export type Vec3 = [number, number, number]; @@ -17,11 +17,12 @@ export function model_pivot(bone: EntityBoneJson): Vec3 { return [-bone.pivot[0], bone.pivot[1], bone.pivot[2]]; } -// its own rotation around its pivot. mirroring x turns x and y rotations the other way -export function bone_matrix(bone: EntityBoneJson): Matrix { - if (!bone.rotation) return IDENTITY; - const [rx, ry, rz] = bone.rotation.map((degrees) => degrees * Math.PI / 180); - return rotation_around(model_pivot(bone), -rx, -ry, rz); +// a bone's or a cube's own rotation around its pivot. mirroring x turns x and y rotations the other way +export function own_matrix(part: EntityBoneJson | EntityCubeJson): Matrix { + if (!part.rotation) return IDENTITY; + const [rx, ry, rz] = part.rotation.map((degrees) => degrees * Math.PI / 180); + const [px, py, pz] = part.pivot ?? [0, 0, 0]; + return rotation_around([-px, py, pz], -rx, -ry, rz); } // every bone's matrix: its own rotation, then pose's for it (like a head looking or a limb swinging), then its @@ -29,7 +30,7 @@ export function bone_matrix(bone: EntityBoneJson): Matrix { export function bone_matrices(model: EntityModelJson, pose: (bone: EntityBoneJson) => Matrix): Map { const bones = new Map(); for (const bone of model.bones) { - let matrix = multiply(pose(bone), bone_matrix(bone)); + let matrix = multiply(pose(bone), own_matrix(bone)); if (bone.parent) matrix = multiply(bones.get(bone.parent) ?? IDENTITY, matrix); bones.set(bone.name, matrix); } diff --git a/client/rendering/model_renderer.ts b/client/rendering/model_renderer.ts index df860c2..e62af6c 100644 --- a/client/rendering/model_renderer.ts +++ b/client/rendering/model_renderer.ts @@ -5,7 +5,7 @@ import type { Texture } from "@/client/renderer/mod.ts"; import type { SpriteRegion } from "@/common/constants.ts"; import type { EntityBoneJson, EntityModelJson } from "@/common/mod_data.ts"; -import { apply, bone_matrices, type Matrix, type Vec3 } from "./model_math.ts"; +import { apply, bone_matrices, type Matrix, multiply, own_matrix, type Vec3 } from "./model_math.ts"; import { push_box, type VertexSink } from "./player_renderer.ts"; // pose is each bone's turn before its parent's, like a head looking or a limb swinging. to_world takes the model's @@ -28,9 +28,9 @@ export function push_model( const matrices = bone_matrices(model, pose); for (const bone of model.bones) { if (only && !only(bone)) continue; - const matrix = matrices.get(bone.name)!; - const transform = (px: number, py: number, pz: number) => to_world(...apply(matrix, px, py, pz)); for (const cube of bone.cubes) { + const matrix = multiply(matrices.get(bone.name)!, own_matrix(cube)); + const transform = (px: number, py: number, pz: number) => to_world(...apply(matrix, px, py, pz)); const inflate = cube.inflate ?? 0; const [ox, oy, oz] = cube.origin; const [w, h, d] = cube.size; diff --git a/common/mod_data.ts b/common/mod_data.ts index 4627811..d1e879c 100644 --- a/common/mod_data.ts +++ b/common/mod_data.ts @@ -501,6 +501,10 @@ const entity_cube = z.object({ uv: z.tuple([z.number(), z.number()], "must be [u, v], per face uvs aren't supported"), // grows the box this many pixels each way without moving its texture inflate: z.number().optional(), + // what rotation turns the box around, inside its bone + pivot: vec3.optional(), + // degrees around x, y then z + rotation: vec3.optional(), }); export type EntityCubeJson = z.infer; @@ -932,8 +936,14 @@ export function entity_model_from_geo(json: unknown, id: string): { model?: Enti ...(rotation === undefined ? {} : { rotation }), cubes: cubes.map((cube) => { if (!is_object(cube)) return cube; - const { origin, size, uv, inflate } = cube; - return { origin, size, uv, ...(inflate === undefined ? {} : { inflate }) }; + const { origin, size, uv, inflate, pivot, rotation } = cube; + return { + origin, + size, + uv, + ...(inflate === undefined ? {} : { inflate }), + ...(rotation === undefined ? {} : { pivot: pivot ?? [0, 0, 0], rotation }), + }; }), }; }), diff --git a/docs/modding/entities/index.md b/docs/modding/entities/index.md index 5e477d7..a29e8d7 100644 --- a/docs/modding/entities/index.md +++ b/docs/modding/entities/index.md @@ -193,7 +193,7 @@ get the id `:`. From the file's first geometry, bworld uses: - the description's `texture_width` and `texture_height`, - each bone's `name`, `parent`, `pivot` and `rotation`, -- each cube's `origin`, `size`, `uv` and `inflate`. +- each cube's `origin`, `size`, `uv`, `inflate`, `pivot` and `rotation`. Cubes need box UV: per face UV isn't supported. A bone's parent has to come before it. The model faces -z, 16 pixels to a block with its feet at 0, and shows the way Blockbench shows it. diff --git a/tests/mod_entity_test.ts b/tests/mod_entity_test.ts index f87448a..c7586fa 100644 --- a/tests/mod_entity_test.ts +++ b/tests/mod_entity_test.ts @@ -2,8 +2,11 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // mobs mods define in entities/.json, drawn with a bedrock model from models/.geo.json -import { assert, assertEquals, assertStringIncludes } from "@std/assert"; -import { entity_model_from_geo, validate_entity } from "@/common/mod_data.ts"; +import { assert, assertAlmostEquals, assertEquals, assertNotEquals, assertStringIncludes } from "@std/assert"; +import { entity_model_from_geo, type EntityBoneJson, validate_entity } from "@/common/mod_data.ts"; +import type { Texture } from "@/client/renderer/mod.ts"; +import { IDENTITY } from "@/client/rendering/model_math.ts"; +import { push_model } from "@/client/rendering/model_renderer.ts"; import { Mob } from "@/server/game/entity/mob.ts"; import { create_entity } from "@/server/game/entity/entity_types.ts"; import { container_items, test_game } from "./helpers.ts"; @@ -39,6 +42,35 @@ Deno.test("a bedrock geo.json becomes an entity model with only what the engine assertEquals(entity_model_from_geo({ format_version: "1.12.0" }, "a:b").model, undefined); }); +Deno.test("a cube's rotation turns it around its pivot like the same rotation on its bone", () => { + const pivot = [1, 2, 3]; + const rotation = [30, -45, 60]; + const cube = { origin: [0, 0, 0], size: [2, 3, 4], uv: [0, 0] }; + const geometry = { + description: { texture_width: 16, texture_height: 16 }, + bones: [ + { name: "turned_bone", pivot, rotation, cubes: [cube] }, + { name: "turned_cube", pivot: [0, 0, 0], cubes: [{ ...cube, pivot, rotation }] }, + { name: "still", pivot: [0, 0, 0], cubes: [cube] }, + ], + }; + const { model, problems } = entity_model_from_geo({ "minecraft:geometry": [geometry] }, "a:b"); + assertEquals(problems, []); + const points = (name: string) => { + const out: number[] = []; + const atlas = { width: 16, height: 16 } as Texture; + const region = { x: 0, y: 0, width: 16, height: 16 }; + const only = (bone: EntityBoneJson) => bone.name === name; + push_model(model!, () => IDENTITY, atlas, region, (x, y, z) => [x, y, z], undefined, only, (x, y, z) => { + out.push(x, y, z); + }); + return out; + }; + const turned = points("turned_cube"); + points("turned_bone").forEach((n, i) => assertAlmostEquals(turned[i], n, 1e-9)); + assertNotEquals(turned, points("still")); +}); + Deno.test("an entity needs a texture and a size", () => { assertEquals(validate_entity({ id: "a:b", model: "a:b", texture: "a:b", width: 0.5, height: 1 }), []); assertEquals(validate_entity({ id: "a:b", model: "a:b", texture: "a:b", width: 0, height: 1, max_health: -1 }), [