8.2 Dope Sheet 时间轴
曲线编辑器看「一个数值怎么变」,Dope Sheet 看「一条轨道上什么时候有什么」:音频、动画、特效各自一行,剪辑块按时间摆放。本节做它的区间代数。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 曲线插值 | 8.1 曲线关键帧与插值 | 曲线管单值;本节管多轨时间 |
| 运行时动画 | 游戏引擎实现原理 | 剪辑最终由运行时回放 |
| 全卷脉络 | 本卷目录 | toc 列章节;Dope Sheet 是时间的目录 |
从零实现:Dope Sheet 时间轴
clipsAt 返回给定时刻命中的剪辑。moveClip 平移剪辑区间。trimClip 改剪辑首尾。overlap 判定两个区间重叠;overlapsInTrack 检查整条轨道有无重叠——剪辑块不能在同一轨道上打架。
对标关键帧视图与剪辑区间
Unity Animation 窗口的 Dopesheet 主要展示关键帧。helio 本节处理带起点和时长的剪辑块,采用半开区间。检测到重叠后是否拒绝放置、允许叠加或产生过渡,由具体轨道的编辑规则决定。 Unity Animation 窗口
可运行产物:6 条用例
下面是 src/dope-sheet.ts,层级 L1,完整可编译。运行命令 node --test test/08-2-dope-sheet-timeline.test.ts:
// file: src/dope-sheet.ts
// tier: L1
// verify: node --test test/08-2-dope-sheet-timeline.test.ts
//
// 视图(第八章):曲线与动画编辑器——Dope Sheet 与时间轴。
// Dope Sheet 按轨道显示片段(clip):每个片段有起点与时长,可以拖动整体
// 平移、拖两端裁剪、检测同轨重叠。时间轴查询用于「当前时刻哪些片段在
// 播放」,是预览与混合器的基础。
export interface Clip {
id: string;
start: number;
duration: number;
}
export interface Track {
name: string;
clips: Clip[];
}
/** 片段结束时刻。 */
export function clipEnd(c: Clip): number {
return c.start + c.duration;
}
/** 当前时刻活跃的片段(含起点、不含终点)。 */
export function clipsAt(tracks: Track[], time: number): Clip[] {
const out: Clip[] = [];
for (const track of tracks) {
for (const clip of track.clips) {
if (time >= clip.start && time < clipEnd(clip)) out.push(clip);
}
}
return out;
}
/** 平移片段(整体拖动)。返回新轨道数组。 */
export function moveClip(tracks: Track[], clipId: string, delta: number): Track[] {
return tracks.map((t) => ({
name: t.name,
clips: t.clips.map((c) =>
c.id === clipId ? { ...c, start: Math.max(0, c.start + delta) } : { ...c },
),
}));
}
/** 裁剪片段:设定新的起点与时长(时长不为负)。 */
export function trimClip(clip: Clip, newStart: number, newDuration: number): Clip {
return { ...clip, start: Math.max(0, newStart), duration: Math.max(0, newDuration) };
}
/** 两片段是否重叠(端点相接不算重叠)。 */
export function overlap(a: Clip, b: Clip): boolean {
return a.duration > 0 && b.duration > 0 &&
a.start < clipEnd(b) && b.start < clipEnd(a);
}
/** 轨道内重叠对;用于检测非法放置。 */
export function overlapsInTrack(track: Track): Array<[Clip, Clip]> {
const out: Array<[Clip, Clip]> = [];
const sorted = track.clips.slice().sort((x, y) => x.start - y.start);
for (let i = 0; i < sorted.length; i += 1) {
for (let j = i + 1; j < sorted.length; j += 1) {
if (overlap(sorted[i], sorted[j])) out.push([sorted[i], sorted[j]]);
}
}
return out;
}
剪辑就是区间 [start, end),一切操作都是区间变换:移动是整体平移、裁剪是改端。overlap 的判定看区间的交非空。检测器只报告重叠,是否拒绝操作由调用方决定。
下面是 test/08-2-dope-sheet-timeline.test.ts,层级 L1:
// file: test/08-2-dope-sheet-timeline.test.ts
// tier: L1
// verify: node --test test/08-2-dope-sheet-timeline.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
clipsAt,
moveClip,
trimClip,
overlap,
overlapsInTrack,
} from '../src/dope-sheet.ts';
import type { Track } from '../src/dope-sheet.ts';
const TRACKS: Track[] = [
{
name: 'walk',
clips: [
{ id: 'c1', start: 0, duration: 10 },
{ id: 'c2', start: 20, duration: 5 },
],
},
{ name: 'idle', clips: [{ id: 'c3', start: 5, duration: 10 }] },
];
test('clipsAt 返回当前时刻活跃片段(不含终点)', () => {
assert.deepEqual(clipsAt(TRACKS, 5).map((c) => c.id), ['c1', 'c3']);
assert.deepEqual(clipsAt(TRACKS, 10).map((c) => c.id), ['c3']);
assert.deepEqual(clipsAt(TRACKS, 12).map((c) => c.id), ['c3']);
});
test('平移片段不改原轨道', () => {
const out = moveClip(TRACKS, 'c1', 5);
assert.equal(out[0].clips[0].start, 5);
assert.equal(TRACKS[0].clips[0].start, 0);
});
test('平移到负时间被夹到 0', () => {
const out = moveClip(TRACKS, 'c1', -100);
assert.equal(out[0].clips[0].start, 0);
});
test('裁剪片段时长不为负', () => {
const c = trimClip({ id: 'x', start: 10, duration: 5 }, 8, -3);
assert.equal(c.start, 8);
assert.equal(c.duration, 0);
});
test('重叠判定:端点相接不算重叠', () => {
const a = { id: 'a', start: 0, duration: 10 };
const b = { id: 'b', start: 10, duration: 5 };
assert.equal(overlap(a, b), false);
assert.equal(overlap(a, { id: 'empty', start: 1, duration: 0 }), false);
assert.equal(overlap(a, { id: 'c', start: 9, duration: 5 }), true);
});
test('轨道内重叠检测', () => {
const track: Track = {
name: 't',
clips: [
{ id: 'a', start: 0, duration: 10 },
{ id: 'b', start: 5, duration: 10 },
{ id: 'c', start: 30, duration: 5 },
],
};
const pairs = overlapsInTrack(track);
assert.equal(pairs.length, 1);
assert.deepEqual(pairs[0].map((c) => c.id), ['a', 'b']);
});
本机实测(Node v26.5.0,node --test test/08-2-dope-sheet-timeline.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉时刻命中与移动。第三条钉裁剪。第四条钉两区间重叠判定。第五、六条钉轨道内重叠与不重叠。
小结
- 剪辑 = 区间,操作都是区间变换。
- 轨道内禁止重叠,冲突被编辑面拦截。
- clipsAt 是播放/高亮共用的时刻查询。
- Dope Sheet 把时间组织成可编辑的轨道。
阅读导航:上一节:曲线关键帧与插值 · 下一节:动画预览与状态机 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。