Skinned Meshes & Blending
Characters bend: elbows, spines, tails, tentacles. Moving thousands of vertices by hand every frame would be hopeless, so animated characters use a skeleton: a small hierarchy of bones, each vertex attached to one or more bones with weights. Animate the few bones, and the GPU moves every vertex. In Three.js that is a SkinnedMesh with a Skeleton of Bone objects, and it is what GLTFLoader creates for rigged models.
Linear blend skinning
Each vertex stores up to four bone indices (skinIndex) and four weights (skinWeight, summing to 1). Its final position is the weighted average of where each of its bones would carry it:
v' = Σ wᵢ · (boneWorldᵢ × boneInverseᵢ) · v
boneInverseᵢ (the inverse bind matrix) undoes the bone's rest pose, so only the bone's movement since binding is applied. This 2D model of a two-bone arm shows the effect of weights on the vertices around the elbow:
// 2D two-bone arm: shoulder at (0,0), elbow at (2,0). The forearm bone bends 90° at the elbow.
const rot = ([x, y], a, [cx, cy]) => { // rotate (x,y) around centre c by angle a
const dx = x - cx, dy = y - cy;
return [cx + dx * Math.cos(a) - dy * Math.sin(a), cy + dx * Math.sin(a) + dy * Math.cos(a)];
};
const elbow = [2, 0], bend = Math.PI / 2;
function skin(v, wForearm) {
const byUpper = v; // the upper-arm bone didn't move
const byForearm = rot(v, bend, elbow); // the forearm bone rotated around the elbow
const w = wForearm;
return [(1 - w) * byUpper[0] + w * byForearm[0], (1 - w) * byUpper[1] + w * byForearm[1]];
}
const verts = [[1.5, 0, 0], [2, 0, 0.5], [2.5, 0, 0.75], [3, 0, 1], [4, 0, 1]]; // x, y, weight on the forearm
for (const [x, y, w] of verts) {
const [sx, sy] = skin([x, y], w);
console.log(`vertex (${x.toFixed(1)}, ${y.toFixed(1)}) forearm weight ${w.toFixed(2)} -> (${sx.toFixed(2)}, ${sy.toFixed(2)})`);
}vertex (1.5, 0.0) forearm weight 0.00 -> (1.50, 0.00)
vertex (2.0, 0.0) forearm weight 0.50 -> (2.00, 0.00)
vertex (2.5, 0.0) forearm weight 0.75 -> (2.13, 0.38)
vertex (3.0, 0.0) forearm weight 1.00 -> (2.00, 1.00)
vertex (4.0, 0.0) forearm weight 1.00 -> (2.00, 2.00)
Vertices fully on the forearm swing up with it; vertices with mixed weights land in between, which rounds off the elbow. Mixing positions linearly is also why extreme bends make joints thin ("candy-wrapper" twists): the averaged point lies inside the arc. Artists counter that with extra helper bones or corrective shapes; for most game characters plain linear blend skinning is fine.
A skinned mesh from scratch
Rigging is normally done in Blender, but building one in code shows every piece. The tentacle below is a CylinderGeometry with many height segments, a chain of five bones, and skin weights computed from each vertex's height. Two clips, Sway and Curl, animate the bone rotations; the buttons cross-fade between them, and the bar shows each action's effective weight.
<style>html, body { margin: 0; height: 100%; overflow: hidden } canvas { display: block }
#ui { position: fixed; left: 8px; top: 8px; display: flex; gap: 4px; align-items: center; color: #fff; font: 12px system-ui, sans-serif }
#ui button { font: 12px system-ui, sans-serif } #w { margin-left: 6px; text-shadow: 0 1px 2px #000 }</style>
<div id="ui"><button id="sway">Sway</button><button id="curl">Curl</button><button id="mix">50 / 50</button><label><input id="bones" type="checkbox" checked> bones</label><span id="w"></span></div>
<script type="module">
import * as THREE from 'three';
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color('#152033');
const camera = new THREE.PerspectiveCamera(45, innerWidth / innerHeight, 0.1, 50);
camera.position.set(4, 2.4, 5);
camera.lookAt(0, 1.8, 0);
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
scene.add(new THREE.HemisphereLight('#cfe3ff', '#223', 1.4));
const sun = new THREE.DirectionalLight('#fff', 2.2);
sun.position.set(2, 5, 4);
scene.add(sun);
// 1. Geometry with many rings along its height (needed for smooth bending).
const HEIGHT = 4, BONES = 5, SEG = BONES - 1, SEG_H = HEIGHT / SEG;
const geo = new THREE.CylinderGeometry(0.12, 0.4, HEIGHT, 16, 32);
geo.translate(0, HEIGHT / 2, 0); // base at y = 0
// 2. Skin indices and weights: each vertex is shared by the two bones around its height.
const pos = geo.attributes.position, skinIndex = [], skinWeight = [];
for (let i = 0; i < pos.count; i++) {
const y = pos.getY(i);
const b = Math.min(Math.floor(y / SEG_H), SEG - 1); // the bone below this vertex
const w = (y - b * SEG_H) / SEG_H; // 0 at that bone, 1 at the next
skinIndex.push(b, b + 1, 0, 0);
skinWeight.push(1 - w, w, 0, 0);
}
geo.setAttribute('skinIndex', new THREE.Uint16BufferAttribute(skinIndex, 4));
geo.setAttribute('skinWeight', new THREE.Float32BufferAttribute(skinWeight, 4));
// 3. Bones: a chain, each one SEG_H above its parent. Names are used by the animation tracks.
const bones = [];
for (let i = 0; i < BONES; i++) {
const bone = new THREE.Bone();
bone.name = `bone${i}`;
bone.position.y = i === 0 ? 0 : SEG_H;
if (i) bones[i - 1].add(bone);
bones.push(bone);
}
// 4. The SkinnedMesh: add the root bone, then bind (this computes the inverse bind matrices).
const tentacle = new THREE.SkinnedMesh(geo, new THREE.MeshStandardMaterial({ color: '#c06cf0', roughness: 0.5 }));
tentacle.add(bones[0]);
tentacle.bind(new THREE.Skeleton(bones));
scene.add(tentacle);
const helper = new THREE.SkeletonHelper(tentacle);
scene.add(helper);
console.log(`${pos.count} vertices driven by ${bones.length} bones`);
// 5. Two clips that rotate the bones (quaternion tracks), looping.
function clip(name, duration, anglesAt) { // anglesAt(t, boneIndex) -> [x, z] radians
const times = [0, duration / 4, duration / 2, (3 * duration) / 4, duration];
const tracks = bones.slice(1).map((b, k) => {
const values = [];
for (const t of times) {
const [x, z] = anglesAt(t / duration, k + 1);
values.push(...new THREE.Quaternion().setFromEuler(new THREE.Euler(x, 0, z)).toArray());
}
return new THREE.QuaternionKeyframeTrack(`${b.name}.quaternion`, times, values);
});
return new THREE.AnimationClip(name, duration, tracks);
}
const sway = clip('Sway', 2, (u, i) => [0, Math.sin(u * Math.PI * 2) * 0.4]);
const curl = clip('Curl', 1.6, (u, i) => [-(0.5 - 0.5 * Math.cos(u * Math.PI * 2)) * 0.7, 0]);
const mixer = new THREE.AnimationMixer(tentacle);
const swayAction = mixer.clipAction(sway).play();
const curlAction = mixer.clipAction(curl).play();
curlAction.setEffectiveWeight(0); // both run; weights decide what you see
let current = swayAction;
function fadeTo(next) {
if (next === current) return;
next.enabled = true;
next.setEffectiveTimeScale(1);
next.setEffectiveWeight(1);
current.crossFadeTo(next, 0.5, false); // fade current out, next in over 0.5 s
current = next;
}
document.getElementById('sway').onclick = () => fadeTo(swayAction);
document.getElementById('curl').onclick = () => fadeTo(curlAction);
document.getElementById('mix').onclick = () => {
for (const a of [swayAction, curlAction]) { a.stopFading(); a.enabled = true; a.setEffectiveWeight(0.5); }
current = null;
};
document.getElementById('bones').onchange = (e) => { helper.visible = e.target.checked; };
const wLabel = document.getElementById('w');
const timer = new THREE.Timer();
renderer.setAnimationLoop((t) => {
timer.update(t);
mixer.update(Math.min(Math.max(timer.getDelta(), 0), 0.1));
wLabel.textContent = `sway ${swayAction.getEffectiveWeight().toFixed(2)} curl ${curlAction.getEffectiveWeight().toFixed(2)}`;
renderer.render(scene, camera);
});
</script>Press 50 / 50 and both clips apply at half weight: the tentacle sways and curls. That is animation blending. The mixer combines all active actions on the same property by weight (quaternions with spherical interpolation), so you get new motion for free.
Blending in games
| Technique | How in Three.js | Use |
|---|---|---|
| Cross-fade | current.crossFadeTo(next, 0.25) (or next.crossFadeFrom(current, 0.25)) |
idle → walk → run transitions, attacks |
| Blend by a parameter | keep idle/walk/run all playing; set weights from the speed every frame | smooth locomotion (a "1D blend tree") |
| Sync loops | walk.syncWith(run) before fading, or warp time scales (crossFadeTo(next, d, true)) |
footsteps that don't stutter between walk and run |
| Additive layers | THREE.AnimationUtils.makeClipAdditive(clip) (marks the clip AdditiveAnimationBlendMode), then play it alongside the base clips |
breathing, aiming offsets, flinches on top of locomotion |
| Partial body | build a clip with only the upper-body bone tracks (filter clip.tracks) |
wave or reload while running |
A sensible locomotion setup for a character controller:
const actions = { idle, walk, run }; // all three playing, looping
function updateLocomotion(speed) { // speed in m/s, from the character controller
const walkW = THREE.MathUtils.clamp(1 - Math.abs(speed - 2) / 2, 0, 1); // peak at 2 m/s
const runW = THREE.MathUtils.clamp((speed - 2) / 3, 0, 1); // from 2 to 5 m/s
idle.setEffectiveWeight(Math.max(0, 1 - walkW - runW));
walk.setEffectiveWeight(walkW);
run.setEffectiveWeight(runW);
run.timeScale = THREE.MathUtils.clamp(speed / 5, 0.8, 1.3); // feet match the ground speed
}Working with rigged glTF characters
- The rig and clips come from the file:
gltf.scenecontains theSkinnedMeshand its bones;gltf.animationsthe clips. - Use
SkeletonUtils.clonefor multiple copies (a plainclone()shares the skeleton). - Frustum culling of a
SkinnedMeshuses a bounding sphere computed once, from the pose at that moment. Animations can move vertices outside it, so a character may vanish at the screen edge. Callmesh.computeBoundingSphere()again in a pose that covers the motion, enlargemesh.boundingSphere.radius, or setmesh.frustumCulled = false. - Each bone costs CPU every frame (matrix updates). 50–70 bones is typical for a hero; crowds need simpler rigs, fewer updates, or baked animation textures.
- Morph targets (blend shapes) are the other deformation system: whole alternative vertex positions blended by
mesh.morphTargetInfluences[i]. They're used for faces and corrective shapes and are animated withNumberKeyframeTrack('Face.morphTargetInfluences[smile]', …).
Test yourself
How many bones can influence a single vertex in a Three.js SkinnedMesh?
- 1
- 2
- Up to 4 (skinIndex/skinWeight are vec4)
- Unlimited
What does current.crossFadeTo(next, 0.5) do?
- Stops
currentimmediately and startsnext - Fades
current's weight to 0 andnext's to 1 over 0.5 seconds, blending the poses - Plays the two clips one after the other
- Copies keyframes from one clip to the other
Why do vertices near the elbow in the skinning model end up between the two bones' results?
- Rounding errors
- They have mixed weights, so their position is a weighted average of where each bone carries them
- The bind matrix is wrong
- Quaternions interpolate them