Animation clips
This commit is contained in:
@@ -17,20 +17,43 @@ export function model_pivot(bone: EntityBoneJson): Vec3 {
|
||||
return [-bone.pivot[0], bone.pivot[1], bone.pivot[2]];
|
||||
}
|
||||
|
||||
// what animation does to a bone, in the model's bedrock space like its own pivot and rotation: degrees added to its
|
||||
// rotation, pixels it moves and how much it's scaled, around its pivot
|
||||
export type BoneOffsets = { rotation: Vec3; position: Vec3; scale: Vec3 };
|
||||
|
||||
// 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 turn_around(part.pivot ?? [0, 0, 0], part.rotation);
|
||||
}
|
||||
|
||||
function turn_around([px, py, pz]: Vec3, degrees: Vec3): Matrix {
|
||||
const [rx, ry, rz] = degrees.map((n) => n * Math.PI / 180);
|
||||
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
|
||||
// parent's. parents come before their children in the model
|
||||
export function bone_matrices(model: EntityModelJson, pose: (bone: EntityBoneJson) => Matrix): Map<string, Matrix> {
|
||||
// a bone's own rotation with what animation does to it: moved, then turned and scaled around its pivot
|
||||
function animated_matrix(bone: EntityBoneJson, { rotation, position, scale }: BoneOffsets): Matrix {
|
||||
const [rx, ry, rz] = bone.rotation ?? [0, 0, 0];
|
||||
const turned = turn_around(bone.pivot, [rx + rotation[0], ry + rotation[1], rz + rotation[2]]);
|
||||
const [px, py, pz] = model_pivot(bone);
|
||||
const [sx, sy, sz] = scale;
|
||||
const scaled = [sx, 0, 0, px - px * sx, 0, sy, 0, py - py * sy, 0, 0, sz, pz - pz * sz];
|
||||
const moved = [1, 0, 0, -position[0], 0, 1, 0, position[1], 0, 0, 1, position[2]];
|
||||
return multiply(moved, multiply(turned, scaled));
|
||||
}
|
||||
|
||||
// every bone's matrix: its own rotation (with offsets' for it, from animation), then pose's for it (like a head
|
||||
// looking or a limb swinging), then its parent's. parents come before their children in the model
|
||||
export function bone_matrices(
|
||||
model: EntityModelJson,
|
||||
pose: (bone: EntityBoneJson) => Matrix,
|
||||
offsets?: Map<string, BoneOffsets>,
|
||||
): Map<string, Matrix> {
|
||||
const bones = new Map<string, Matrix>();
|
||||
for (const bone of model.bones) {
|
||||
let matrix = multiply(pose(bone), own_matrix(bone));
|
||||
const offset = offsets?.get(bone.name);
|
||||
let matrix = multiply(pose(bone), offset ? animated_matrix(bone, offset) : own_matrix(bone));
|
||||
if (bone.parent) matrix = multiply(bones.get(bone.parent) ?? IDENTITY, matrix);
|
||||
bones.set(bone.name, matrix);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user