Rotation for cubes on entities

This commit is contained in:
2026-11-13 23:06:37 -03:00
parent c74ddedd6e
commit 9bf6b98953
5 changed files with 58 additions and 15 deletions
+8 -7
View File
@@ -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<string, Matrix> {
const bones = new Map<string, Matrix>();
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);
}
+3 -3
View File
@@ -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;
+12 -2
View File
@@ -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<typeof entity_cube>;
@@ -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 }),
};
}),
};
}),
+1 -1
View File
@@ -193,7 +193,7 @@ get the id `<mod id>:<name>`. 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.
+34 -2
View File
@@ -2,8 +2,11 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// mobs mods define in entities/<name>.json, drawn with a bedrock model from models/<name>.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 }), [