Animation clips
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// playing clips (see ANIMATION.md): what one does to the bones it moves at a time into it, sampled the way
|
||||
// blockbench's timeline does a bedrock animation. no drawing here, so tests can use it
|
||||
import type { ClipJson, ClipKeyframe } from "@/common/mod_data.ts";
|
||||
import { lerp } from "@/common/worldgen/noise.ts";
|
||||
import type { BoneOffsets, Vec3 } from "./model_math.ts";
|
||||
|
||||
// a keyframe this close to the time is the one it's on, in seconds
|
||||
const ON_KEYFRAME = 1 / 1200;
|
||||
|
||||
// adds what clip does seconds into it to each bone's offsets, scaled by weight: rotation and position times weight,
|
||||
// scale weight of the way from 1. a clip that loops wraps around its length, one that doesn't stays on its last frame
|
||||
export function sample_clip(clip: ClipJson, seconds: number, offsets: Map<string, BoneOffsets>, weight = 1) {
|
||||
const time = clip.loop && clip.length > 0 ? ((seconds % clip.length) + clip.length) % clip.length : seconds;
|
||||
for (const [bone, channels] of Object.entries(clip.bones)) {
|
||||
let offset = offsets.get(bone);
|
||||
if (!offset) {
|
||||
offset = { rotation: [0, 0, 0], position: [0, 0, 0], scale: [1, 1, 1] };
|
||||
offsets.set(bone, offset);
|
||||
}
|
||||
for (const channel of ["rotation", "position"] as const) {
|
||||
const keyframes = channels[channel];
|
||||
if (!keyframes) continue;
|
||||
const value = sample_channel(keyframes, time, clip.loop);
|
||||
for (let i = 0; i < 3; i++) offset[channel][i] += value[i] * weight;
|
||||
}
|
||||
if (channels.scale) {
|
||||
const value = sample_channel(channels.scale, time, clip.loop);
|
||||
for (let i = 0; i < 3; i++) offset.scale[i] *= 1 + (value[i] - 1) * weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// a channel's value at time, from the keyframes around it. on a keyframe it's what it comes in at, and before the
|
||||
// first or after the last it's that keyframe's
|
||||
export function sample_channel(keyframes: ClipKeyframe[], time: number, loop: boolean): Vec3 {
|
||||
let after = keyframes.findIndex((keyframe) => keyframe.time >= time);
|
||||
if (after === -1) after = keyframes.length;
|
||||
const from = keyframes[after - 1];
|
||||
const to = keyframes[after];
|
||||
if (from && time - from.time < ON_KEYFRAME) return from.pre;
|
||||
if (to && to.time - time < ON_KEYFRAME) return to.pre;
|
||||
if (!to) return from.post;
|
||||
if (!from) return to.pre;
|
||||
|
||||
const t = (time - from.time) / (to.time - from.time);
|
||||
if (!from.smooth && !to.smooth) return [0, 1, 2].map((i) => lerp(t, from.post[i], to.pre[i])) as Vec3;
|
||||
// through the keyframes either side too, wrapping around a loop of three or more, whose first and last are the same
|
||||
// moment. where there's none, or the one next to it jumps, the curve starts or ends at the next one's value again
|
||||
let before: ClipKeyframe | undefined = keyframes[after - 2];
|
||||
let beyond: ClipKeyframe | undefined = keyframes[after + 1];
|
||||
if (loop && keyframes.length >= 3) {
|
||||
before ??= keyframes.at(-2);
|
||||
beyond ??= keyframes[1];
|
||||
}
|
||||
const p0 = before && same(from.pre, from.post) ? before.post : from.post;
|
||||
const p3 = beyond && same(to.pre, to.post) ? beyond.pre : to.pre;
|
||||
return [0, 1, 2].map((i) => catmullrom(t, p0[i], from.post[i], to.pre[i], p3[i])) as Vec3;
|
||||
}
|
||||
|
||||
function same(a: Vec3, b: Vec3) {
|
||||
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
|
||||
}
|
||||
|
||||
// a uniform catmull-rom spline from p1 at t 0 to p2 at t 1
|
||||
function catmullrom(t: number, p0: number, p1: number, p2: number, p3: number) {
|
||||
const v0 = (p2 - p0) / 2;
|
||||
const v1 = (p3 - p1) / 2;
|
||||
return (2 * p1 - 2 * p2 + v0 + v1) * t ** 3 + (-3 * p1 + 3 * p2 - 2 * v0 - v1) * t ** 2 + v0 * t + p1;
|
||||
}
|
||||
@@ -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