73 lines
3.5 KiB
TypeScript
73 lines
3.5 KiB
TypeScript
// 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;
|
|
}
|