播放一次动画,停止,稍后反向播放

时间:2020-12-30 20:49:42

标签: three.js

我想播放一次动画,在最后一帧停止,然后返回一个函数,稍后我可以调用该函数来播放动画一次以返回到起始位置。我有什么:

 function animate() {
    ...
    gltf.animations.forEach((animation) => {
      const action = object.animation.clipAction(animation).setLoop(THREE.LoopRepeat, 1);
      action.clampWhenFinished = true;
      action.play();
    });

    return () => {
      gltf.animations.forEach((animation) => {
        const action = object.animation.existingAction(animation);
        action.timeScale = -1;
        action.play();
      });
    }
 }

object.animation 是对象的 AnimationMixer。第一部分正在工作,但是当我调用返回的函数时什么也没有发生。我做错了什么?

1 个答案:

答案 0 :(得分:1)

文档指出 clampWhenFinished 正在设置 .paused=true (clampWhenFinished)。因此,当您想要恢复动画而不是再次触发 false 时,您需要将其设置为 .play()。 此外,您应该重置动画以使其能够重复播放,并且您应该将循环设置为 LoopOnce 以仅播放一次 AnimationAction。如果您像这样更改代码,它应该可以工作:

function animate() {
    ...
    gltf.animations.forEach((animation) => {
      const action = object.animation.clipAction(animation);
      action.reset();
      action.clampWhenFinished = true;
      action.timeScale = 1;
      action.setLoop(THREE.LoopOnce, 1);
      action.play();
    });

    return () => {
      gltf.animations.forEach((animation) => {
        const action = object.animation.existingAction(animation);
        action.timeScale = -1;
        action.paused = false;
      });
    }
 }
相关问题