Marrow Open Marrow

Guides

How to load and play GLB animations in three.js

By bxzex · · Updated

three.js plays skinned .glb animations with two pieces: GLTFLoader to load the file and AnimationMixer to play its clips. Marrow itself is built on three.js, so its downloads play exactly as they look in the app.

How to load and play GLB animations in three.js: a character rigged and animated in Marrow

Load the character and play a clip

import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 0.1, 100);
camera.position.set(0, 1.4, 4);
scene.add(new THREE.HemisphereLight(0xffffff, 0x444444, 2));

const gltf = await new GLTFLoader().loadAsync('/character.glb');
scene.add(gltf.scene);

const mixer = new THREE.AnimationMixer(gltf.scene);
const clip = THREE.AnimationClip.findByName(gltf.animations, 'Walk');
mixer.clipAction(clip).play();

const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
  mixer.update(clock.getDelta());
  renderer.render(scene, camera);
});

Every animation in your Marrow download is in gltf.animations, named after it. Log gltf.animations.map((c) => c.name) to see them.

Cross-fade between animations

const actions = Object.fromEntries(gltf.animations.map((c) => [c.name, mixer.clipAction(c)]));
let current = actions.Idle;
current.play();

function playAnim(name, fade = 0.25) {
  const next = actions[name];
  if (next === current) return;
  next.reset().play();
  current.crossFadeTo(next, fade, false);
  current = next;
}

One-shot animations

For a jump or an attack, set the action to play once and hold its last frame, then listen for the mixer's finished event to go back to idle:

const jump = actions.Jump;
jump.setLoop(THREE.LoopOnce, 1);
jump.clampWhenFinished = true;
mixer.addEventListener('finished', (e) => { if (e.action === jump) playAnim('Idle'); });

Common problems

Show many copies of one character

A skinned character cannot be copied with .clone(), because the copies would share one skeleton. Use the helper that ships with three.js:

import { clone } from 'three/addons/utils/SkeletonUtils.js';

const copy = clone(gltf.scene);
const copyMixer = new THREE.AnimationMixer(copy);
copyMixer.clipAction(clip).play();
scene.add(copy);

Each copy needs its own mixer. Start each one at a different time, action.time = Math.random() * clip.duration, so a crowd does not move in step.

Attach something to a bone

const hand = gltf.scene.getObjectByName('RightHand');
hand.add(sword);

Characters rigged from scratch in Marrow use plain names such as Hips, Chest, Head, LeftHand and RightFoot. A model that kept its own skeleton keeps its own bone names.

Smaller, faster files

Questions

Does this work with React Three Fiber?

Yes. The useGLTF and useAnimations hooks from drei wrap the same loader and mixer.

Why does the character look flat?

It needs light to look right. Add an environment map to the scene, set renderer.toneMapping = THREE.ACESFilmicToneMapping and turn on shadows.

Open Marrow