Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 39 ยท Animation and Interaction

Keyframe Animation and AnimationMixer in Three.js

Animate with keyframes in Three.js: KeyframeTrack, AnimationClip, AnimationMixer, AnimationAction, looping, crossfading, events and a skinned bone chain, with live examples.

Writing mesh.position.x += dt works for simple motion, but characters, doors and product reveals are authored by artists as keyframes: "at 0 seconds the arm is here, at one second it is there, and the computer fills in between". Three.js has a complete system for playing such animations. The same system plays the clips inside glTF and FBX files, so learning to build one by hand teaches you how to control loaded ones.

This lesson covers the four classes involved, shows how to make clips in code, how to blend between them, and how skeletons (bones) fit in.

The animation system at a glance

Four classes, four jobs

KeyframeTrack Times and values for ONE property
AnimationClip A named set of tracks
AnimationMixer The player bound to one object
AnimationAction Play, pause, fade one clip

Clips are reusable data; the mixer applies them to an object.

1 / 2

Tracks and clips

A track names a property and gives two parallel arrays: the times (in seconds) and the values at those times. The property name is a path relative to the object the mixer controls (a bone or mesh name can start the path).

JavaScript
const times = [0, 1, 2];
const values = [0, 0, 0,   0, 2, 0,   0, 0, 0];      // x, y, z per time
const track = new THREE.VectorKeyframeTrack('.position', times, values);
const clip = new THREE.AnimationClip('hop', 2, [track]);   // name, duration, tracks

Values are flat arrays: three numbers per key for a vector, four for a quaternion, three for a color. Between keys the track interpolates; the default is smooth (InterpolateSmooth), and you can pass THREE.InterpolateLinear or THREE.InterpolateDiscrete (step) as the fourth constructor argument. Use quaternions for rotation because Euler tracks suffer from gimbal problems; make one with new THREE.Quaternion().setFromEuler(...).

Playing a clip

JavaScript
const mixer = new THREE.AnimationMixer(mesh);
const action = mixer.clipAction(clip);
action.play();

// in the loop:
mixer.update(dt);       // advance time by dt seconds

mixer.clipAction returns the same action for the same clip, so you can call it repeatedly. The mixer does nothing unless you call update(dt) every frame; that is the most common beginner mistake.

Position, rotation and color tracks

Passing -1 as the duration makes AnimationClip compute it from the last key. The default loop mode repeats the clip forever.

Controlling actions

An AnimationAction is a playback handle with many knobs:

Member Effect
play(), stop(), reset() Start, stop and rewind
paused Freeze at the current time
timeScale Speed (2 = double, -1 = backwards)
setLoop(mode, repetitions) LoopOnce, LoopRepeat, LoopPingPong
clampWhenFinished Stay on the last pose instead of snapping back
weight / setEffectiveWeight(w) How strongly this action affects the object (0 to 1)
time Current time inside the clip
fadeIn(s), fadeOut(s), crossFadeTo(other, s) Smooth transitions

The mixer fires 'finished' when a LoopOnce action ends and 'loop' when a repeating one wraps:

JavaScript
mixer.addEventListener('finished', (e) => console.log('done:', e.action.getClip().name));

Blending: crossfades and layers

A character walks, then runs, then jumps. Cutting between clips pops; instead blend their weights. crossFadeTo fades one action out and another in over a number of seconds. Because the mixer sums weights per property, several actions can also play at once, as layers: a walk cycle plus a wave on the arms, each with weight 1 on different bones.

Crossfading two clips

Here the jump is a one-shot, and its finished event starts the fade back to idle. A real game uses the same pattern with Idle, Walk and Run clips from a glTF file: gltf.animations.find(c => c.name === 'Walk').

Skeletons and skinning

Characters are not moved vertex by vertex. A skeleton is a hierarchy of Bone objects, and a SkinnedMesh stores, for each vertex, which bones influence it (skinIndex) and by how much (skinWeight). Animating the bones then deforms the mesh. glTF and FBX files contain this already; to see how it works, build the smallest possible example: a three-bone tube that bends.

A skinned tube animated through its bones

SkeletonHelper draws the bones as lines, which is invaluable when you debug a rigged model. Note how the clip's tracks name the bones (Mid.rotation[z]): the mixer looks them up by name in the object's descendants. When you load a glTF character, new THREE.AnimationMixer(gltf.scene) does exactly this with the names from the file.

Tips

  • Clips in a loaded model are plain data; you can list them with gltf.animations.map(c => c.name).
  • Call mixer.stopAllAction() and mixer.uncacheRoot(root) when removing an animated object, otherwise the mixer keeps references and memory.
  • Use action.setEffectiveWeight() and setEffectiveTimeScale() to set a value directly; they also cancel any fade or warp in progress on that action.
  • When two clips have different speeds (walk vs run), use crossFadeTo(other, duration, true) (warp) to match their time scales during the fade.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must you call every frame for an AnimationMixer to advance?

    The mixer only advances when you call update(dt) with the elapsed seconds. Forgetting this leaves the object frozen.
  2. 2. What does a VectorKeyframeTrack('.position', times, values) need in values for 3 keys?

    Values are one flat array with three numbers per key for a vector: three keys give nine numbers.
  3. 3. How do you stop a jump action from snapping back to its first pose when it finishes?

    With LoopOnce plus clampWhenFinished the action stays on its last pose.
  4. 4. In a SkinnedMesh, what do skinIndex and skinWeight store for each vertex?

    Up to four bone indices and four weights per vertex let the GPU blend bone transforms to deform the mesh.

Summary

  • A KeyframeTrack animates one property, an AnimationClip groups tracks, an AnimationMixer plays clips on an object and an AnimationAction controls one clip.
  • Call mixer.update(dt) each frame; use LoopOnce, clampWhenFinished, timeScale and events to control playback.
  • crossFadeTo and weights blend clips smoothly or in layers.
  • Skeletons, SkinnedMesh and bone-name tracks are how rigged characters from glTF work; SkeletonHelper shows them.

Next: Raycasting and Picking Objects