Drawing

Keyframe animation: clips, channels and the node tree

A door opens, a wheel turns, a crane lifts its arm. A model file stores such motion as a few keys: a value at a time. The engine finds the value between the keys for the current time, builds each part's matrix, and multiplies the matrices down the part tree. This page explains the keys, the three ways to fill the gaps, why rotations need care, how the tree turns local matrices into world matrices, and why the clock must stay small. The last part shows how RASM does it on the GPU.

In short

  • A clip (an animation) has channels. A channel moves one property of one node: its translation, rotation or scale. Its sampler holds the key times, the values and the interpolation.
  • To sample at time t: find the keys around t (binary search), then STEP holds the left value, LINEAR mixes the two, CUBICSPLINE follows a curve with a tangent at each key.
  • Rotations are unit quaternions. Mix them with slerp, the short way round, or normalize the mix.
  • A node's local matrix is T · R · S. Its world matrix is parent world · local, all the way up the tree.
  • The time comes from the application, never from a wall clock inside the engine, so a frame is the same on every run.
  • A 32-bit float of 86 400 (one day in seconds) has steps of 7.8 ms. Keep the numbers the GPU adds small: send the seconds since a recent base.

1Keys instead of frames

A game does not store the pose of every frame. It stores a few keys, and the frames between them are computed. Here one node moves up and down with three keys:

3 keys, 120 frames: every frame reads the line between two keys time height 0 s 1 s 2 s key: 1.0 frame at 0.5 s: 0.5
The amber dots are stored. The green line is computed, at whatever time the frame asks for: 60 or 144 frames per second play the same motion.
360 000floats to store every frame of a 10 s clip of 60 nodes600 frames · 60 nodes · 10 floats (T, R, S)
13 860floats for the same clip with a key every 0.5 s: 26 times less21 keys · 60 nodes · (1 time + 10 values)
anyframe rate plays it: the keys do not know the frame

2Clips, channels and samplers

The glTF format, which most tools export, stores motion in three layers. A clip is a named motion such as "Walk". It has channels: each one points at a node and one of its paths (translation, rotation or scale). A channel reads a sampler: two arrays and a rule. A fourth path, weights, drives a mesh's morph targets: its values are one number per target per key.

clip "Walk" channel 0: node 3, rotation channel 1: node 3, translation channel 2: node 5, rotation sampler 0 times 0.0 0.5 1.0 values q0 q1 q2 LINEAR sampler 1 times 0.0 1.0 values p0 p1 STEP
Two channels can share one sampler: here two nodes turn with the same keys. A rotation value is a quaternion (4 floats), a translation or a scale 3 floats.
A node that a clip moves has no matrix A glTF node gives its rest pose either as a 4 × 4 matrix or as translation, rotation and scale. A node that a channel targets must use the three parts: a channel replaces one part and keeps the other two.

3Sampling a channel

To read a channel at time t, find the key k with tk ≤ t < tk+1. The times only grow, so a binary search finds it in log2 of the key count steps. Before the first key the value is the first value; after the last key, the last. Between the keys, the position in the gap is:

s = (t − tk) / (tk+1 − tk)    0 ≤ s < 1

s
how far between key k and key k + 1
tk
the time of key k

The interpolation rule turns s into a value:

STEP

The value of key k until the next key. For on/off motion, and for sprites.

LINEAR

mix(v[k], v[k+1], s). Corners at the keys. Slerp for rotations.

CUBICSPLINE

A Hermite curve. Each key stores three values: an in-tangent, the value, an out-tangent.

The cubic rule weighs the two values and the two tangents that face the gap. The tangents are slopes per second, so they are scaled by the gap's length d = tk+1 − tk:

p(s) = (2s³ − 3s² + 1) · v[k]  +  (s³ − 2s² + s) · d · out[k]
     + (−2s³ + 3s²)   · v[k+1] +  (s³ − s²)      · d · in[k+1]
Drag the time along the line and change the rule. Watch where STEP jumps, where LINEAR has corners, and how the cubic curve follows the orange tangents and can pass above the keys.

4Rotations: slerp, the short way round

A rotation key is a unit quaternion: a point on a 4D sphere. A straight mix of two of them cuts through the sphere, so the result is shorter than 1 and turns at an uneven speed. Two fixes work:

length 0.71 ab

Plain mix

Between two rotations 90° apart the middle has length 0.71: it is not a rotation until it is normalized, and its speed is uneven.

length 1 ab

Slerp

Moves along the arc at a constant speed. Close keys (an angle near 0) use the normalized mix, which is the same there and has no division by a tiny sine.

slerp(a, b, s) = (sin((1 − s)·θ) · a + sin(s·θ) · b) / sin θ    cos θ = a · b

a · b < 0
use −b: q and −q are the same rotation, and −b is the short way round

5The node tree: local to world

Nodes form a tree. A node's matrix is local: it places the node in its parent's space. Turning the shoulder turns the arm, the hand and everything below. To draw a mesh, the engine needs its node's world matrix, the product of all the local matrices from the root down:

An arm: each part turns in its parent's space base: L₀ upper arm: L₁ 30° forearm: L₂ +40° hand: L₃ world matrices W₀ = L₀ W₁ = W₀ · L₁ W₂ = W₁ · L₂ W₃ = W₂ · L₃ = L₀ · L₁ · L₂ · L₃ change L₁ and W₁, W₂, W₃ all move
The order matters: parent · local, root on the left. Each local matrix is T · R · S: scale first, then rotate, then move.

A CPU walks the tree once, root first, and reuses each parent's world matrix. A GPU runs every node at once, on its own thread, and a thread cannot wait for another thread's result without a barrier. Two ways work:

0 1 2 3

Each node walks up

Its thread multiplies its parents' local matrices itself. No waiting, depth steps per node. Fine for trees under ~30 deep.

barriers

Level by level

One level of the tree per step, with a barrier between: one multiply per node, one barrier per level.

6The clock: from the application, in small numbers

The clip time is clipTime + (clock − startTime) · speed, wrapped into the clip's length for a loop. Two rules make it right:

  1. The application owns the clock. It passes the time each frame: wall time in a game, a frame count in a test or a video export. The same frame then shows the same pose on every run.
  2. The GPU adds small numbers. A 32-bit float has 24 bits of precision. The larger the number, the larger the step between two floats.
0.12 µsstep of a float near 1 s2⁻²³ s
0.24 msstep near 1 hour (3 600 s)2¹¹ · 2⁻²³ s
7.8 msstep near 24 hours (86 400 s): half a frame at 60 Hz2¹⁶ · 2⁻²³ s

So after a day the clock in floats can only move in 7.8 ms jumps, and frames 16.7 ms apart get 15.6 or 23.4 ms of motion: the animation stutters. The fix keeps the clock in doubles on the CPU and gives the GPU only small differences:

The base moves forward; the GPU only adds the last few seconds 0 h 24 h base clock < 64 s CPU, double: phase = clip time at the base, wrapped into one loop GPU, float: t = phase + (clock − base) · speed both terms small: steps of 8 µs or less
When the clock leaves the window (64 s past the base, or before it), the base jumps to the clock and every playing clip gets its phase again. That costs a little CPU once a minute, not every frame.
Move the clock to many hours and watch the ball, sampled every frame at 60 Hz, start to stutter. Then subtract a base.

7Pitfalls

The animation runs faster on a faster computer
It adds a fixed step per frame. Pass real time (or a fixed step on purpose, for tests).
It stutters after the program has run for hours
The clock is a large float. Subtract a recent base before the float math.
Parts shrink or wobble while they turn
Rotations mixed without normalizing, or without slerp. Also check the quaternion order: glTF stores x, y, z, w.
A part spins the long way round
The mix did not flip the second key when their dot product was negative.
A part flies to the origin
The child was multiplied in the wrong order (local · parent), or a static parent's matrix was left out.
A moving part smears under TAA
Its motion vectors use the matrix of this frame for both frames. Keep last frame's matrix too.

8In RASM

RASM poses animated nodes on the GPU, so the CPU pays nothing per animated entity per frame. The import keeps only the part of a glTF node tree that a clip moves. The Animate pass, a compute pass that the first scene view of a graph adds, writes each animated sub-mesh's matrices, which every view of the frame reads: the shaded view and every shadow map. A skinned mesh's clips move its joints instead, and a second pass skins its vertices: see Skinning. A morphed mesh's clips also move its weights, which the same pass writes for the Skin pass: see Morph targets.

CPU setAnimationTime every frame: 1 double playAnimation on a change: 1 table row finishMeshLoad once per mesh: nodes, samplers, keys Animated Entity Table a row per playing entity, per slot Animation Pool nodes · samplers · channels · keys · weights GPU · Animate, a group per row 1 sample channels → local, and weights 2 walk up the parents → world, per node 3 entity · node world → an InstanceTransform per sub-mesh barrier between the steps Animated Transform Buffer (this slot) world, normal matrix, previousWorld the other slot: last frame's world → this frame's previousWorld Cull, Scene, Transparent, every shadow view: getWorldMatrix
An instance row with animationRow = −1 keeps its own matrices, so a static mesh runs the same code as before. A playing entity's rows point at its transforms.

The pass finds a node's samplers in the channel table: one row of ints per clip, with three per node and then one per morph weight. A weights sampler is split at import into one sampler per target, so each weight has a channel of its own.

One row of the channel table: nodeCount · 3 + weightCount ints T R S T R S w0 w1 w2 node 0 node 1 one per morph weight a sampler index, −1: the rest value rows: clip 0, clip 1, …, then every clip's channels (ALL_ANIMATION_CLIPS), then none (the rest pose) a weight's rest value is the node's, else the mesh's, else 0 (glTF)
An AnimationPath::WEIGHTS channel's AnimationChannel::node is the weight's index in AnimationRaw::morphWeights, so it lands in the weight part of the row (getChannelRow).

The application gives the time and starts a clip:

engine.setAnimationTime(seconds);
const int32_t walk = engine.findAnimation(mesh, "Walk");
if (walk >= 0)
    engine.playAnimation(entity, {.clip = static_cast<uint32_t>(walk), .startTime = seconds});

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Which nodesthe whole treeimportAnimations keeps a node when a clip moves it or a parent, and it holds a mesh or a kept child, and always keeps a skin's joint (a skinned mesh's own node is not kept: its matrix is ignored). The node of a morphed mesh with no skin is always kept: it places the morphed vertices. A kept node under static ones hangs below one extra node whose rest matrix is their world matrix. A model whose clips move no mesh has no animation. Kept nodes are stored parents first (AnimationRaw::nodes).
Clipsone at a time, or blendedOne per entity (AnimationPlay::clip), or ALL_ANIMATION_CLIPS: every clip at once, a node moved by the last clip that animates it. No blending or layers. Every glTF animation keeps its index, also one left without channels.
SamplingSTEP, LINEAR (slerp), CUBICSPLINEAll three, in sampleChannel in shaders/passes/animate.slang, with a binary search. Close quaternions use the normalized mix (cos θ > 0.9995). A weights sampler becomes one sampler per morph target, its value in x, and is sampled like a translation. A weight that setMorphWeights sets wins over its channel.
Local to worldwalk up, or level by levelWalk up: each thread multiplies its node's parents itself, so any depth and any node count works. The 64 threads of a group go over the nodes in strides (examples/12_animation has a 100-node chain).
Clockfrom the application, small floatsRenderer::setAnimationTime sets a double. The base moves when the clock is ANIMATION_REBASE_SECONDS (64) past it or behind it (getAnimationBase), and each row gets its phase at the base (getAnimationPhase). A unit test checks the error stays under 0.1 ms after 24 hours.
Motion vectorskeep last frame's matrixThe pass reads last frame's world from the other slot's buffer (PassBuilder::readPrevious of the per-slot buffers) into previousWorld. A play, a stop and a growth of the buffers start the motion again for one frame (AnimatedEntity::resetSync), so TAA does not smear a jump.
Bounds—Culling multiplies the geometry's sphere by getWorldMatrix, the animated matrix, so a rigid part is culled where it is. A skinned part's sphere comes from its joints (Skinning).
Mirroringa negative scale turns the windingThe cull mode of a draw is chosen on the CPU from the rest pose. A sub-mesh whose clips can take a scale to 0 or below is drawn without face culling (animationCanMirror), so it shows right whichever way it turns.
Cost on the CPU—Nothing per frame per entity. A play, a stop or a move writes one row; the base moves once a minute and writes the playing rows.

See also the API reference, the GPU scene tables, Skinning and TAA.

Where it lives in code

WhatWhere
The public typesAnimationClip, AnimationPlay, ALL_ANIMATION_CLIPS, AnimationPath, AnimationRaw in include/rasm/animation.h
Import: nodes, clips, samplersimportAnimations in src/assets/animationImport.cpp, called by importGltf in src/assets/meshImport.cpp with the walk of traverseNodes; the rest weights from importMorphWeights in src/assets/morphImport.cpp
The pool and the tableGpuAnimation, buildAnimationPoolData, Renderer::uploadMeshAnimation, Renderer::writeAnimationRow, getChannelRow in src/renderer/animation.cpp
Play, stop, the clockRenderer::playAnimation, Renderer::stopAnimation, Renderer::setAnimationTime in src/renderer/animation.cpp; getAnimationBase, getAnimationPhase in src/renderer/animationTime.cpp
The passRenderer::addAnimationPass, Renderer::recordAnimation; animateMain in shaders/passes/animate.slang
The GPU structsshaders/lib/animationTypes.slang and src/renderer/uberMaterialTypes.h
Reading the matricesgetWorldMatrix, getNormalMatrix, getPreviousWorldPosition in shaders/lib/core.slang
Teststests/animationTests.cpp
Keep these true
  • Every shader that reads an instance's matrices goes through getWorldMatrix, getNormalMatrix and getPreviousWorldPosition, never InstanceData::world directly: those give the matrices in the view's render space.
  • Kept nodes are stored parents first, and a node's parent is an earlier node.
  • The engine never reads a wall clock for animation: the time comes from setAnimationTime.
  • The GPU sees only the clock minus the base and phases at the base, never a time of hours.
  • Both slots' transform buffers grow together, after the GPU is done with them, because a frame reads the other slot's.

Further reading