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 isparent 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:
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.
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]
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:
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.
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:
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:
Each node walks up
Its thread multiplies its parents' local matrices itself. No waiting, depth steps per node. Fine for trees under ~30 deep.
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:
- 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.
- 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.
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:
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.
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.
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
| Part | This page | RASM |
|---|---|---|
| Which nodes | the whole tree | importAnimations 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). |
| Clips | one at a time, or blended | One 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. |
| Sampling | STEP, LINEAR (slerp), CUBICSPLINE | All 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 world | walk up, or level by level | Walk 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). |
| Clock | from the application, small floats | Renderer::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 vectors | keep last frame's matrix | The 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). |
| Mirroring | a negative scale turns the winding | The 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
| What | Where |
|---|---|
| The public types | AnimationClip, AnimationPlay, ALL_ANIMATION_CLIPS, AnimationPath, AnimationRaw in include/rasm/animation.h |
| Import: nodes, clips, samplers | importAnimations 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 table | GpuAnimation, buildAnimationPoolData, Renderer::uploadMeshAnimation, Renderer::writeAnimationRow, getChannelRow in src/renderer/animation.cpp |
| Play, stop, the clock | Renderer::playAnimation, Renderer::stopAnimation, Renderer::setAnimationTime in src/renderer/animation.cpp; getAnimationBase, getAnimationPhase in src/renderer/animationTime.cpp |
| The pass | Renderer::addAnimationPass, Renderer::recordAnimation; animateMain in shaders/passes/animate.slang |
| The GPU structs | shaders/lib/animationTypes.slang and src/renderer/uberMaterialTypes.h |
| Reading the matrices | getWorldMatrix, getNormalMatrix, getPreviousWorldPosition in shaders/lib/core.slang |
| Tests | tests/animationTests.cpp |
- Every shader that reads an instance's matrices goes through
getWorldMatrix,getNormalMatrixandgetPreviousWorldPosition, neverInstanceData::worlddirectly: 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
- The Khronos Group, glTF 2.0 Specification, section 3.11 "Animations" and appendix C "Interpolation".
- Ken Shoemake, Animating Rotation with Quaternion Curves, SIGGRAPH 1985: slerp.
- Jason Gregory, Game Engine Architecture, chapter "Animation Systems".
- Bruce Dawson, Don't Store That in a Float, Random ASCII blog, 2012: time in floats.
- Coordinate spaces and the view matrix, History buffers.