Drawing
Skinning: a mesh that bends with its skeleton
A character is one mesh, its skin, over a tree of joints, its skeleton. Node animation moves joints; skinning moves each vertex of the skin with the joints near it, so knees and elbows bend smoothly. This page explains the bind pose, the joint matrices, the weights and the blend, where in the frame to do the work, how to cull a mesh whose shape changes every frame, and its motion vectors. The last part shows how RASM skins once per frame on the GPU.
In short
- The mesh is modelled once, in the bind pose. Each joint stores its inverse bind matrix: the inverse of its world matrix in that pose.
- A joint's joint matrix is
world(joint now) · inverseBind. It moves a bind-pose vertex the way the joint moved since the bind pose. - A vertex follows up to 4 joints, with weights that add up to 1:
v' = Σ wᵢ · Jᵢ · v. This is linear blend skinning. - Skin once per frame in a compute pass, into a buffer of posed vertices that every view reads: the camera and each shadow map. Skinning in the vertex shader repeats the work in every view.
- The bounds: a sphere per joint around the vertices it moves, moved by its joint matrix, and the sphere around all of them.
- Motion vectors need the vertex's place in the frame before: keep last frame's posed vertices too.
1The bind pose and the joint matrix
An artist models the skin once, with the skeleton in one pose: the bind pose, often arms out in a T. The vertices are stored in that pose. To pose the skin, each vertex is taken into its joint's own space as it was in the bind pose, then out again with the joint's matrix of today:
world · inverseBind is the identity, so the skin stays as modelled.The joints are nodes of the same tree that node animation moves (see the node tree). So the joint matrices cost one multiply per joint after the world matrices are known, and the skeleton can have any depth.
The joints' world matrices already put the skin in the scene. glTF says a skinned mesh ignores the matrix of the node that holds it; only the object's own placement (where the character stands) is applied on top.
2Weights: a vertex follows several joints
A vertex near the elbow should move partly with the upper arm and partly with the forearm. Each vertex stores up to 4 joints and 4 weights. Its posed place is the weighted mix of the places its joints put it:
v′ = w₀·J₀·v + w₁·J₁·v + w₂·J₂·v + w₃·J₃·v
w₀ + w₁ + w₂ + w₃ = 1
- Jᵢ
- the joint matrix of the vertex's joint i
- wᵢ
- its weight; weights that do not add up to 1 shrink or grow the vertex, so scale them to 1
The sum of matrices Σ wᵢ·Jᵢ can be built first and applied once, to the position and, as a normal matrix, to the normal. This is linear blend skinning. It is cheap and it is what glTF asks for. Its known faults:
One joint per vertex
Rigid parts: the skin tears open outside the joint and folds into itself inside.
Blended near the joint
The vertices in a band around the elbow mix both joints, so the skin curves round the bend.
Twist: the candy wrapper
Mixing two matrices turned far apart shortens the vertices: a twisted wrist or a bent elbow loses volume.
A vertex may name more than 4 joints (glTF allows more sets of 4). Keep the 4 largest weights and scale them to add up to 1 again: the lost weights are small, and 4 fit one vector.
3Where to skin: once per frame, for every view
A frame draws a skinned character many times: in the camera's view and in every shadow map that sees it. A sun with 4 cascades and the camera make 5 views; a point light's cube adds 6 more. There are two places for the skinning math:
In each view's vertex shader
Each draw reads the base vertex and the joint matrices and skins again. Fine for one view; the cost grows with every shadow map.
Once, in a compute pass
A pass before the views writes each posed vertex, with the same layout as the base vertex. The views read those instead.
The posed copy has a cost in memory. It holds the whole vertex (position, normal, tangent and UVs), and with two frames in flight there is one copy per frame slot, so the frame that draws never reads a buffer the next frame writes:
In a frame, the compute pass sits between the pose and the views:
4Bounds of a mesh that changes shape
Culling needs a sphere that holds the mesh. A rigid mesh has one sphere, computed once, moved by its matrix. A skinned mesh changes shape every frame: an arm reaches out past the bind-pose sphere. Computing a new sphere from all the posed vertices would cost a pass over the vertices before culling. A cheaper bound that is still safe:
- At import Per joint: a sphere around the bind-pose vertices that joint moves (weight above 0).
- Each frame Per joint: move its sphere by its joint matrix. The center is
J · c; the radius isrtimes the matrix's largest stretch. - Then The sphere around all moved spheres: the center of their box, the radius to the farthest edge.
5Motion vectors of skinned vertices
TAA and motion blur need, per pixel, where the surface was in the frame before (see TAA). For a rigid mesh, the frame before's matrix is enough. A skinned vertex also changes its place inside the mesh, so the matrix does not tell where it was. The posed buffers already hold the answer: there is one per frame slot, and the other slot holds the frame before's posed vertices.
6Pitfalls
- The character lies on its back, or stands far from its place
- The mesh node's matrix was applied on top of the joints'. A skinned mesh ignores its own node's matrix.
- The skin explodes into spikes
- Joint indices read with the wrong type (glTF stores them as 8 or 16-bit integers), or matrices in the wrong order:
world · inverseBind, not the other way round. - Parts of the skin grow or shrink as they move
- The weights do not add up to 1. Scale them to 1 at import, after keeping the 4 largest.
- Elbows and wrists get thin when they bend or twist
- Linear blend skinning loses volume. Add helper joints, or use dual quaternion skinning.
- An arm disappears at the edge of the screen
- The mesh was culled with its bind-pose sphere. Use bounds that follow the joints.
- Shadow maps cost much more with a character in them
- Each shadow view skins the mesh again in its vertex shader. Skin once per frame and let every view read the result.
- A walking character smears under TAA
- Its motion vectors use this frame's posed vertex for both frames. Keep the frame before's posed vertices.
7In RASM
RASM skins on the GPU, once per frame. The import puts each skinned vertex's joints and weights into a stream of the geometry pool, beside the 64-byte vertex. The Animate pass (node animation) also writes the joint matrices and each posed sub-mesh's bounds. The Skin pass then writes the posed vertices, which the culling and draws of every view read. The same pass first adds a mesh's morph targets, so a morphed mesh with no skin goes through it too.
InstanceTransform has skinnedVertex = −1 reads the geometry pool, so static and rigid meshes run the same code as before. Each posed sub-mesh starts at a multiple of 64 among its entity's skinned vertices, so a group never holds two sub-meshes.Nothing new in the API: an entity with a skinned mesh plays a clip like any animated entity.
const rasm::MeshHandle fox = engine.loadGLTF("models/gltf/Fox/Fox.glb");
engine.waitForLoads();
const int32_t run = engine.findAnimation(fox, "Run");
if (run >= 0)
engine.playAnimation(entity, {.clip = static_cast<uint32_t>(run)});
Where RASM follows the textbook, and where it differs
| Part | This page | RASM |
|---|---|---|
| Joints and weights | up to 4 per vertex, adding up to 1 | readSkinVertices reads every pair of JOINTS_n / WEIGHTS_n, adds up the weights of a joint named in two sets, drops a weight whose joint is past the skin's joints, keeps the 4 largest (one warning per primitive that has more) and scales them to 1. A SkinVertex stores 4 16-bit joints and 4 UNORM16 weights that add up to 65535 exactly: the rounding's rest goes to the largest. A vertex with no weight follows joint 0. |
| Skins | joints and inverse bind matrices | importSkinJoints puts each skin a sub-mesh uses once in AnimationRaw::joints: its kept node and its inverse bind matrix (the identity without the accessor). A skin's joints are always kept nodes, moved by a clip or not. A skin with a joint outside the scene leaves its sub-meshes unskinned. |
| The mesh's node | ignored | A skinned primitive's SubGLTFRaw::transform is the identity. The entity's matrix places the posed skin. |
| Where it skins | once per frame | The Skin pass (skinMain), an async compute pass right after Animate, writes 64-byte vertices to GpuAnimation::skinnedVertices of the slot. A group is a row of the animated entity table (x) and 64 of its skinned vertices (y), all of one sub-mesh: each posed sub-mesh starts at a multiple of 64 (AnimationSubMeshData::firstSkinnedVertex). An entity may have up to 65 535 · 64 of them. A thread first adds its vertex's morph target deltas, then mixes the joints when the sub-mesh has some, so a morphed mesh with no skin is posed by the same pass. A stopped row returns at once. |
| Memory | a copy per slot | 128 B per skinned vertex of each playing entity. The buffers grow on demand by doubling, with no config, and log their size; a growth restarts every entity's motion for one frame. The joint matrices take 48 B per joint per entity, per slot. |
| Bounds | a sphere per joint, the sphere around them | getJointSpheres at import; getSkinnedBounds in animate.slang each frame, with getStretch, the same stretch bound as culling. A morphed sub-mesh first grows each joint sphere's radius by Σ |wₜ| · rₜ (getMorphGrowth), before the stretch. The result goes to InstanceTransform::boundsCenter and boundsRadius, and getWorldSphere uses it in every view's culling, LOD choice and blend sort. |
| Motion vectors | the same vertex in the other slot | getPreviousObjectPosition reads SceneData::previousSkinnedVertsPtr when InstanceTransform::skinnedMotion is 1. It is 0 in the frame an entity starts, a play jumps its clip, the first setMorphWeights after none, a clearMorphWeights, or the buffers grow (the rule of AnimatedEntity::resetSync), and then before = now. |
| LODs | — | The LODs share the sub-mesh's vertices, so every LOD draws the posed ones. They are simplified in the bind pose without looking at the weights, which can collapse an edge across a bending joint (D29). |
| Not playing | the skeleton's rest pose | An entity of a skinned mesh takes its animation row when it gets its instance rows (poseAtRest), and with no clip its row names the channel row after ALL_ANIMATION_CLIPS's, which has no channel: the Animate pass computes every node at its rest TRS, so the joint matrices are restWorld · inverseBind and the mesh is skinned at rest, with its rest morph weights. A file without clips is posed the same way. stopAnimation goes back to it, a jump with no motion from the frame before. Such an entity is posed and skinned every frame (P18). |
| Paused or off screen | skip the work | Every playing entity is skinned every frame, still or not, seen or not: P18. |
| Mirroring | — | A skinned sub-mesh is drawn without face culling when a clip can take one of its joints' scale to 0 or below (animationCanMirror). |
See also the API reference, vertex pulling, frustum culling and the GPU scene tables. examples/13_skinning plays CesiumMan, three foxes and RiggedFigure with shadows; --crowd 1000 adds a crowd.
Where it lives in code
| What | Where |
|---|---|
| The public types | SkinVertex, SubGLTFRaw in include/rasm/mesh.h; AnimationJoint, AnimationRaw in include/rasm/animation.h; EngineConfig::geometryPoolSkins in include/rasm/types.h |
| Import: weights, spheres, joints | readSkinVertices, getJointSpheres, importSkinJoints in src/assets/skinImport.cpp, called from src/assets/meshImport.cpp and importAnimations in src/assets/animationImport.cpp |
| The skin stream | GeometryPool::upload in src/renderer/geometryPool.cpp (GeometryData::skinOffset); weldVertices in src/assets/meshLod.cpp keeps it with its vertices |
| Morph targets in the same pass | see morph targets: getMorphGrowth in shaders/passes/animate.slang, the morph loop of skinMain |
| The pool, the buffers, the passes | buildAnimationPoolData, Renderer::uploadMeshAnimation, Renderer::growAnimationBuffers, Renderer::addAnimationPass, Renderer::recordSkinning in src/renderer/animation.cpp |
| The shaders | animateMain in shaders/passes/animate.slang; skinMain in shaders/passes/skin.slang |
| The GPU structs | shaders/lib/animationTypes.slang, shaders/lib/geometryTypes.slang and src/renderer/uberMaterialTypes.h |
| Reading posed vertices | getVertex, getPreviousObjectPosition in shaders/lib/core.slang; getWorldSphere in shaders/lib/frustumCulling.slang |
| Tests | tests/skinningTests.cpp |
- Every shader that reads a draw's vertices goes through
getVertex, neverSceneData::vertsPtrdirectly, or it draws a skinned mesh in its bind pose. - A skinned vertex has the base vertex's layout, so every view draws it with the same code.
- Both slots' skinned vertex buffers grow together, after the GPU is done with them: a frame reads the other slot's.
- The weights of a
SkinVertexadd up to 65535 exactly. - The skin stream is welded and reordered with its vertices, like the color stream.
- Morph before skin: the deltas are in the bind pose.
Further reading
- The Khronos Group, glTF 2.0 Specification, section 3.7.3 "Skins" and 3.7.2.1 "Joints and weights".
- Ladislav Kavan, Steven Collins, Jiří Žára and Carol O'Sullivan, Skinning with Dual Quaternions, I3D 2007: a fix for the candy wrapper.
- Jason Gregory, Game Engine Architecture, chapter "Animation Systems", section on skinning.
- Keyframe animation, Morph targets, Temporal anti-aliasing, LOD selection.