Rotation for cubes on entities
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user