donmccurdy / three-gltf-viewer

Drag-and-drop preview for glTF 2.0 models in WebGL using three.js.
https://gltf-viewer.donmccurdy.com/
MIT License
2.06k stars 530 forks source link

Animation Playback Issue. #332

Closed SarahMCollier closed 1 year ago

SarahMCollier commented 1 year ago

I am having an animation playback issue.

Do you know what is going on?

Do you know of any limitations to the player's playback that I should be aware of?

donmccurdy commented 1 year ago

Hi @SarahMCollier! I'm not sure I fully understand the issue — are you able to share a glTF file that demonstrates the problem? You can attach .ZIP archives to GitHub comments.

In the meantime, here are my thoughts. While an animation might contain 60 keyframes per second, there is no guarantee that a viewer can display each keyframe in sequence, precisely 1/60th of a second apart. The viewer has its own framerate (often driven by the web browser at approximately 60fps), and the viewer samples from the keyframes (i.e. interpolates between two neighboring keyframes).

If your animation is set up such that interpolating between two keyframes will show unwanted results, it would be important to export with glTF's "STEP" interpolation, rather than "LINEAR" or "CUBICSPLINE". This will clamp interpolation to the most recent keyframe rather than interpolating.

If you're not sure what you have, or how to use STEP interpolation, try loading the model in https://gltf.report and running the script below:

let updated = 0;

for (const animation of document.getRoot().listAnimations()) {
    for (const sampler of animation.listSamplers()) {
        const interpolation = sampler.getInterpolation();
        if (interpolation === 'LINEAR') {
            sampler.setInterpolation('STEP');
            console.log(`Changed ${interpolation} to STEP.`);
            updated++;
        }
    }
}

console.log(`Updated ${updated} samplers.`);