简体   繁体   English

Three.js animation 混频器不更新,除非登录 animation 循环

[英]Three.js animation mixer doesn't update unless logged in animation loop

Very strange behaviour in JavaScript, a simple AnimationMixer.update(delta) doesn't work unless I console.log it: JavaScript 中非常奇怪的行为,一个简单的AnimationMixer.update(delta)不起作用,除非我对它进行控制台记录:

// This works
function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
  // Update mixers...
  Object.values(sandbox.models).forEach(x => (x.mixer && console.log(x.mixer.update(clock.getDelta()))));
}

// These don't work
function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
  // Update mixers...
  Object.values(sandbox.models).forEach(x => (x.mixer && (x.mixer.update(clock.getDelta()))));
}

function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
  // Update mixers...
  Object.values(sandbox.models).forEach(x => (x.mixer && void(x.mixer.update(clock.getDelta()))));
}

Then, when playing the animation by AnimationMixer.clipAction(animation).play() does not play unless I do the first one.然后,当AnimationMixer.clipAction(animation).play()播放 animation 时,除非我做第一个,否则不会播放。

This is very strange, why does the mixer only update when logged the return value?这很奇怪,为什么混音器只在记录返回值时才更新?

I can't show all the code because it's too long and won't work because I have linked files too.我无法显示所有代码,因为它太长而且无法工作,因为我也有链接文件。

And as you may know you can't log thousands of times per second, it's stupid in general and will slow down the game.正如您可能知道的那样,您不能每秒登录数千次,这通常很愚蠢并且会减慢游戏速度。

You should never call Clock.getDelta() more than once in your animation loop.您永远不应在 animation 循环中多次调用Clock.getDelta() Otherwise the delta values will be near 0 (since you get the elapsed time between the last getDelta() call).否则增量值将接近0 (因为您得到的是上次getDelta()调用之间经过的时间)。 So try it like so:所以像这样尝试:

function animate() {
  requestAnimationFrame(animate);
  const delta = clock.getDelta();
  renderer.render(scene, camera);
  // Update mixers...
  Object.values(sandbox.models).forEach( x => {
    if (x.mixer) x.mixer.update(delta);
  } );
}

Also avoid the usage of Object.values() per frame since it will return a new array on each invocation which is bad for performance (this just leads to GC overhead).还要避免每帧使用Object.values() ,因为它会在每次调用时返回一个新数组,这对性能不利(这只会导致 GC 开销)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM