three.js

How to manually control animation frame by frame?


Regarding this Three.js example https://threejs.org/examples/#webgl_animation_keyframes I was able to load my own model and play animation in a loop. However I want to make a "slider" that will allow user to control animation frame by frame. How I can achieve this with AnimationMixer? Let's say that animation clip has duration 4s. I would like to control in realtime current animation time from 0s to 4s.


Solution

  • The answer is right there in the code of the example you linked:

    mixer = new THREE.AnimationMixer( model );
    mixer.clipAction( gltf.animations[ 0 ] ).play();
    
    // ...
    
    function animate() {
        var delta = clock.getDelta();
        mixer.update( delta );
    
        renderer.render( scene, camera );
    
        requestAnimationFrame( animate );
    }
    

    mixer.update( delta ); is what updates the animation by 0.0166 seconds on every frame (at 60FPS). If you want to jump to a specific moment, simply assign the .time property to whatever second you need.

    See here for more documentation on AnimationMixer.