5.3 动画导入与采样
动画剪辑长什么样?轨道(track)× 关键帧(keyframe)。一个走路的动画:髋关节有「位移轨道」(0 秒在 A,1 秒在 B…),膝关节有「旋转轨道」。播放动画 = 在任意时刻对每条轨道插值出值——采样。本节实现 AnimationClip(轨道集合)与 sampleClip(按时间采样)。
sampleClip(clip, time):对整条剪辑采样,产出关节索引 → 值的映射。sampleTrack(track, time):单轨道采样——区间内线性插值,越界钳制。AnimationClip.duration:剪辑总时长。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 剪辑求值 | helio 3.1 剪辑、轨道与求值 | 那篇在运行期求值剪辑;本节在导入期定义剪辑结构 |
| 骨骼 | 5.2 骨骼与蒙皮 | 采样出的值驱动骨骼关节 |
| glTF 解析 | 5.1 glTF 解析与模型导入 | glTF 的 animations 用本节结构承载 |
采样:插值与钳制
动画里 90% 的时刻不在关键帧上。采样要做的:找到时间点落在哪两个关键帧之间,按比例线性插值。t = (time - a.time) / (b.time - a.time),value = a + (b - a) * t——这是所有插值(包括曲线编辑器里更高级的贝塞尔、样条)的最简骨架。
采样同时要处理越界:time 早于首个关键帧或晚于末个,怎么办?钳制——取边界关键帧的值。为什么钳制而不是循环?因为循环是「动画播放器」的职责(在 duration 上取模),不是「采样器」的职责。采样器只管「给我这个时刻的值」,循环/镜像/倒放都是播放器的事——职责分开,两边的逻辑都干净。
对标动画采样策略
glTF 动画采样器区分 STEP、LINEAR 和 CUBICSPLINE。旋转通道使用四元数,插值有专门的规则。helio 示例对数值轨道做线性插值,并在超出时间范围时返回端点值。完整导入器需按目标属性和采样器模式选择求值方式。 Khronos glTF 2.0 规范
可运行产物:6 条用例
下面 src/anim-clip.ts 层级 L1,完整可编译,命令 node --test test/05-3-animation-clip-sample.test.ts:
// file: src/anim-clip.ts
// tier: L1
// verify: node --test test/05-3-animation-clip-sample.test.ts
//
// 资产管线(第五章):动画导入与采样。
// 动画剪辑 = 若干轨道(track)× 每个轨道一串关键帧。播放时按时间在关键帧
// 间插值——采样。本节的 sampleClip 对每个轨道:找到当前时间所在区间,
// 线性插值出值;超出剪辑范围则钳制到首/末关键帧。单通道标量值把插值数学
// 讲透,真实引擎是向量/四元数插值,骨架相同。
export interface Keyframe {
time: number;
value: number;
}
export interface Track {
/** 该轨道作用的关节索引。 */
joint: number;
keyframes: Keyframe[];
}
export interface AnimationClip {
name: string;
duration: number;
tracks: Track[];
}
/** 对剪辑在时间 t 采样,产出 关节索引 → 值 的映射。 */
export function sampleClip(clip: AnimationClip, time: number): Map<number, number> {
const out = new Map<number, number>();
for (const track of clip.tracks) {
const value = sampleTrack(track, time);
if (value !== undefined) out.set(track.joint, value);
}
return out;
}
/** 单轨道采样:区间内线性插值,越界钳制。 */
export function sampleTrack(track: Track, time: number): number | undefined {
const kf = track.keyframes;
if (kf.length === 0) return undefined;
if (time <= kf[0].time) return kf[0].value;
const last = kf[kf.length - 1];
if (time >= last.time) return last.value;
for (let i = 1; i < kf.length; i++) {
if (time <= kf[i].time) {
const a = kf[i - 1];
const b = kf[i];
const span = b.time - a.time;
if (span <= 0) return b.value;
const t = (time - a.time) / span;
return a.value + (b.value - a.value) * t;
}
}
return last.value;
}
sampleTrack 的边界处理很细心:空轨道返回 undefined(无值可采);span <= 0 的两关键帧直接取后值(防除零);遍历从 i = 1 开始跳过首帧比较。采样器是每帧都要跑的热路径,边界必须便宜而正确。
可运行产物:6 条用例
下面是 test/05-3-animation-clip-sample.test.ts 层级 L1,命令 node --test test/05-3-animation-clip-sample.test.ts:
// file: test/05-3-animation-clip-sample.test.ts
// tier: L1
// verify: node --test test/05-3-animation-clip-sample.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { sampleClip, sampleTrack, type AnimationClip } from '../src/anim-clip.ts';
test('采样在关键帧上精确返回关键帧值', () => {
const track = { joint: 0, keyframes: [{ time: 0, value: 0 }, { time: 1, value: 10 }] };
assert.equal(sampleTrack(track, 0), 0);
assert.equal(sampleTrack(track, 1), 10);
});
test('关键帧之间线性插值', () => {
const track = { joint: 0, keyframes: [{ time: 0, value: 0 }, { time: 2, value: 10 }] };
assert.equal(sampleTrack(track, 1), 5); // 中点
assert.equal(sampleTrack(track, 0.5), 2.5);
});
test('越界钳制:早于首帧取首值,晚于末帧取末值', () => {
const track = { joint: 0, keyframes: [{ time: 1, value: 4 }, { time: 3, value: 8 }] };
assert.equal(sampleTrack(track, 0), 4);
assert.equal(sampleTrack(track, 99), 8);
});
test('sampleClip 多轨道各自采样,产出关节 → 值映射', () => {
const clip: AnimationClip = {
name: 'walk',
duration: 2,
tracks: [
{ joint: 0, keyframes: [{ time: 0, value: 0 }, { time: 2, value: 10 }] },
{ joint: 1, keyframes: [{ time: 0, value: 5 }, { time: 2, value: 5 }] },
],
};
const sample = sampleClip(clip, 1);
assert.equal(sample.get(0), 5); // 插值
assert.equal(sample.get(1), 5); // 常数轨道
assert.equal(sample.size, 2);
});
test('空轨道不产出值', () => {
const clip: AnimationClip = {
name: 'empty',
duration: 1,
tracks: [{ joint: 0, keyframes: [] }],
};
assert.equal(sampleClip(clip, 0.5).size, 0);
});
test('duration = 末关键帧时间', () => {
const clip: AnimationClip = {
name: 'jump',
duration: 1.5,
tracks: [
{ joint: 0, keyframes: [{ time: 0, value: 0 }, { time: 1.5, value: 9 }] },
],
};
assert.equal(clip.duration, 1.5);
assert.equal(sampleClip(clip, 1.5).get(0), 9);
});
本机实测(Node v26.5.0,node --test test/05-3-animation-clip-sample.test.ts):tests 6、pass 6、fail 0,耗时约 40 ms。
第一、二、三条把采样的三态钉死:精确命中、区间插值、越界钳制。第四条验证多轨道的映射产出(常数轨道给常数)。第五条空轨道不产出值——剪辑里可以有「这帧没驱动」的轨道。
小结
- 动画剪辑 = 轨道 × 关键帧;播放 = 按时间采样。
- 采样三态:精确命中关键帧、区间线性插值、越界钳制。
- 采样器不管循环,播放器才管——职责分开。
- 曲线是线性插值的增强,采样骨架不变。
- 下一节进入音频:WAV 解析与音频导入——RIFF 头、PCM 数据、声道与采样率。
阅读导航:上一节:5.2 骨骼与蒙皮 · 下一节:6.1 WAV 解析与音频导入 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。