Animate a loaded model
Most broken model animation comes from four mistakes: clip names guessed instead of read, no mixer.update(delta)
in the loop, rigged copies made with object.clone() (which keeps pointing at the original skeleton), and clips
from another file whose bone names or units do not match. Read the file, then wire the pattern below that fits.
Process
- Run
threejs-model-inspecton the character and on every animation file. Note clip names and durations, whether clips areskeletal, the bone names (Mixamo bones aremixamorigHipsin three.js) and the scale. - Pick the stack's recipe file and read only the sections you need:
- Vanilla three.js:
references/vanilla.md - React Three Fiber with drei:
references/r3f.md - Clips from Mixamo or another file, root motion, rig mismatches:
references/mixamo.md
- Vanilla three.js:
- Create exactly one
AnimationMixerper animated model instance, and advance it every frame with the frame delta fromTHREE.Timer(Clockis deprecated since r183). drei'suseAnimationsdoes this for you. - Build a name-to-action map from the clips once, then drive state changes through one
fadeTo(name)helper so two clips never fight at full weight. - For several copies of a rigged model, clone with
SkeletonUtils.cloneand give each copy its own mixer. Clips can be shared across mixers. - Watch the result unpaused: transitions, foot sliding, drifting off its spot (root motion), and stretched limbs
(a scale mismatch between clip and rig). Without a running app, pose the file on a clip with
threejs-model-inspect'srender.mjs model.glb --clip Run --time 0.3at a few times and look at the sheets; a clip that leaves the model in its bind pose, or stretches limbs, shows there.
Rules
- Names come from the inspect report or
gltf.animations.map((clip) => clip.name); never hard-code a guess. - A clip applies to the hierarchy it was made for. Before playing a clip from another file, compare its track names
(
clip.tracks[0].nameisboneName.property) with the rig's bone names. - Stop and uncache on teardown (
mixer.stopAllAction(); mixer.uncacheRoot(root)) in apps that load and unload models.
Anti-patterns
const copy = gltf.scene.clone()for a skinned model: the copy deforms with the original or not at all.- Calling
action.play()on the next clip without fading the previous one out: both run at full weight. new THREE.Clock()in new code, or a delta computed fromperformance.now()that jumps after a tab switch.- Fixing a character that slides across the floor by moving it back every frame instead of handling root motion.
Done when
- Every clip the feature needs plays by its real name, with fades between states.
- Copies animate independently, and one-shot clips return to a loop state when finished.
- Nothing drifts, slides or stretches unintentionally when watched for a few cycles.
Next: threejs-model-fix if the model is in a T-pose, invisible or distorted.
