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:

bind pose v shoulder elbow inverseBind elbow's space (70, 20) elbow at the origin world now posed v′ 60° J = world(elbow now) · inverseBind(elbow) v′ = J · v
The inverse bind matrix is stored once per joint. In the bind pose 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 mesh's own node does not place it

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:

crack

One joint per vertex

Rigid parts: the skin tears open outside the joint and folds into itself inside.

smooth

Blended near the joint

The vertices in a band around the elbow mix both joints, so the skin curves round the bend.

collapses

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.

Bend the elbow, then shrink the blend band to 0 to see the rigid crack. Widen it again and bend past 90°: the elbow gets thin, the loss of volume of linear blend skinning.

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:

skin skin skin skin skin cascade 01 23camera 5 × the skinning work no extra memory

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.

skin once posed vertices c 0 c 1 c 2 c 3 camera memory: a posed copy per frame slot

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:

64 Ba posed vertex in one frame slot, the base vertex's layout
128 Bper vertex per animated character, two slots64 B · 2
409 KiBone character of 3 273 vertices3 273 · 128 B
400 MiBa crowd of 1 000 of them1 000 · 409 KiB

In a frame, the compute pass sits between the pose and the views:

One frame time posejoint matrices skinonce cascade 0 cascade 1 cascade 2 cascade 3 camera view posed vertices of this frame slot each view: a vertex fetch from the posed buffer instead of the base one
Green is compute work, blue is a view's culling and draws. Adding a shadow map adds draws, not skinning.

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:

  1. At import Per joint: a sphere around the bind-pose vertices that joint moves (weight above 0).
  2. Each frame Per joint: move its sphere by its joint matrix. The center is J · c; the radius is r times the matrix's largest stretch.
  3. Then The sphere around all moved spheres: the center of their box, the radius to the farthest edge.
A skinned vertex is a mix of points inside the joints' spheres J₀·v: inside the upper arm's sphere J₁·v: inside the forearm's sphere v′ = w₀·J₀·v + w₁·J₁·v: on the line between them the big sphere holds both points and, being round, every point between
The vertex is not always inside one of the small spheres, but a sphere is convex: it holds every mix of points it holds. So the big sphere holds every skinned vertex.
Bend the arm and change the band. The dots turn red if a skinned vertex leaves the big circle; they never do. The big circle grows with the bend, but needs no pass over the vertices.

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.

The same vertex in both slots: index 17 this frame 17 frame before 17 vertex shader motion = now − before a new character, or a jump in its clip: the other slot holds nothing that fits, so use before = now: no motion that frame
The vertex's index is the same in both slots, so the shader needs no search: it reads the same index from the other buffer.

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.

Import, once per mesh JOINTS_n · WEIGHTS_n 4 largest, scaled to 1 skin stream, 16 B per vertex, read by Skin 4 × u16 joints · 4 × unorm16 animation pool joints · inverse binds · spheres Animate: a group per entity row 1 nodes: local → world (node animation) 2 per joint: world · inverseBind → joints 3 per posed sub-mesh: InstanceTransform matrix, bounds, skinnedVertex Joint Matrix Buffer (slot) 48 B per joint per entity Morph Weight Buffer (slot) + morph stream: morph targets Animated Transform Buffer bounds read by every Cull Skin: x = entity row, y = 64 vertices of one sub-mesh first morph: base + Σ wₜ · Δₜ over the targets with wₜ ≠ 0 then skin, with joints: M = Σ wᵢ · joints[first + jointᵢ]; position by M, normal by its cofactors, tangent by M's 3 × 3 Skinned Vertex Buffer 64 B per vertex, one per slot every view: camera, cascades, spot maps, cube faces getVertex · getPreviousObjectPosition · getWorldSphere
A row whose 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

PartThis pageRASM
Joints and weightsup to 4 per vertex, adding up to 1readSkinVertices 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.
Skinsjoints and inverse bind matricesimportSkinJoints 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 nodeignoredA skinned primitive's SubGLTFRaw::transform is the identity. The entity's matrix places the posed skin.
Where it skinsonce per frameThe 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.
Memorya copy per slot128 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.
Boundsa sphere per joint, the sphere around themgetJointSpheres 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 vectorsthe same vertex in the other slotgetPreviousObjectPosition 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 playingthe skeleton's rest poseAn 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 screenskip the workEvery 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

WhatWhere
The public typesSkinVertex, SubGLTFRaw in include/rasm/mesh.h; AnimationJoint, AnimationRaw in include/rasm/animation.h; EngineConfig::geometryPoolSkins in include/rasm/types.h
Import: weights, spheres, jointsreadSkinVertices, getJointSpheres, importSkinJoints in src/assets/skinImport.cpp, called from src/assets/meshImport.cpp and importAnimations in src/assets/animationImport.cpp
The skin streamGeometryPool::upload in src/renderer/geometryPool.cpp (GeometryData::skinOffset); weldVertices in src/assets/meshLod.cpp keeps it with its vertices
Morph targets in the same passsee morph targets: getMorphGrowth in shaders/passes/animate.slang, the morph loop of skinMain
The pool, the buffers, the passesbuildAnimationPoolData, Renderer::uploadMeshAnimation, Renderer::growAnimationBuffers, Renderer::addAnimationPass, Renderer::recordSkinning in src/renderer/animation.cpp
The shadersanimateMain in shaders/passes/animate.slang; skinMain in shaders/passes/skin.slang
The GPU structsshaders/lib/animationTypes.slang, shaders/lib/geometryTypes.slang and src/renderer/uberMaterialTypes.h
Reading posed verticesgetVertex, getPreviousObjectPosition in shaders/lib/core.slang; getWorldSphere in shaders/lib/frustumCulling.slang
Teststests/skinningTests.cpp
Keep these true
  • Every shader that reads a draw's vertices goes through getVertex, never SceneData::vertsPtr directly, 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 SkinVertex add 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