【问题标题】:Three.js - issue with rendering - animation is shakingThree.js - 渲染问题 - 动画抖动
【发布时间】:2016-03-26 03:09:51
【问题描述】:

我在渲染旋转的球体时遇到了一个奇怪的问题:动画似乎在晃动,我不知道这个问题来自哪里。

这是this link上的示例

和渲染功能:

 function render() {

  controls.update();
  requestAnimationFrame(render);

  // For camera rotation : parametric parameter 
  timer = Date.now()*0.0001;

  // Coordinates of camera
  coordCamera.set(radiusCamera*Math.cos(timer), radiusCamera*Math.sin(timer), 0);

  // Rotate camera function
  rotateCamera();

  // Rendering
  renderer.render(scene, camera);

  }

带有rotateCameracomputeRotation 函数:

function computeRotation (objectRotation, coordObject) {

  // Apply rotation matrix 
  var rotationAll = new THREE.Matrix4();
  var rotationX = new THREE.Matrix4().makeRotationX(objectRotation.rotation.x);
  var rotationY = new THREE.Matrix4().makeRotationY(objectRotation.rotation.y);
  var rotationZ = new THREE.Matrix4().makeRotationZ(objectRotation.rotation.z);
  rotationAll.multiplyMatrices(rotationX, rotationY);
  rotationAll.multiply(rotationZ);

  // Compute world coordinates 
  coordObject.applyMatrix4(rotationAll);

  }

  function rotateCamera() {

  // Compute coordinates of camera
  computeRotation(torus, coordCamera);

  // Set camera position for motion
  camera.position.set(coordCamera.x, coordCamera.y, coordCamera.z)

  }

如果有人能看出问题所在,那就太好了,

感谢您的帮助

更新:

我试图在下面插入解决方案;我没有制作动画,没有显示任何内容:这是我对 jsfiddle 的尝试:

https://jsfiddle.net/ysis81/uau3nw2q/5/

我也尝试了 performance.now() 但动画仍然在晃动;你可以在https://jsfiddle.net/ysis81/2Lok5agy/3/查看这个

我已经开始悬赏来解决这个问题。

【问题讨论】:

  • 摇晃是什么意思?您是否尝试过使用具有恒定更新时间和动画插值的游戏循环?
  • @Michał Miszczyszyn 在旋转过程中,球体正在变白,就像动画的刷新不连续或生涩......你的意思是你不能在你的电脑上重现这个问题我的 jsfiddle 链接?
  • 试试这个:jsfiddle.net/3urtkpkv。您不应该要求TrackballControls控制相机,然后尝试自己控制相机。把事情简单化。实际上,我会切换到OrbitControls。它有一个autoRotate 属性。

标签: javascript three.js rt


【解决方案1】:

requestAnimationFrame 将使用时间戳参数执行您的回调渲染函数。使用该参数计算自上一帧以来经过了多少毫秒。然后将该差异用作旋转的某个百分比。

var mySpecificLogic = function (millesecondsSinceLastFrame) {
  // rotate your camera here
  // if you want it to rotate 20 degrees every second
  var rotPerSecond = 20;
  var rotationPerMS = rotPerSecond / 1000;
  var rotationInDegrees = millesecondsSinceLastFrame * rotationPerMS;
  // call your rotation function
  // note: if you are also trying to move the ball with the mouse
  //    you'll want a hook to disable this basic rotation

  // set the camera to whatever rotation you want

  renderer.render(scene, camera);
};


var startTheWholeThing = function() {
    var lastFrameTime;
    var deltaT;

    function recurse( thisFrameTime ) {
        window.requestAnimationFrame(recurse);
        lastFrameTime = lastFrameTime || thisFrameTime;
        deltaT = thisFrameTime - lastFrameTime;

        mySpecificLogic(deltaT);

        lastFrameTime = thisFrameTime;
    }
  recurse();
};

startTheWholeThing();

【讨论】:

  • 谢谢,但我没有在我的脚本中包含上面的这段代码 sn-p。这是我尝试将其插入到此链接上的代码中:jsfiddle.net/ysis81/uau3nw2q/4。如果你可以看看我的脚本,问候
  • 固定旋转对我来说看起来是正确的。你是在说你尝试用鼠标操纵球体的时候吗?
  • 以下版本不显示球体:jsfiddle.net/ysis81/uau3nw2q/4,您能在 jsfiddle 上建议您的固定版本吗?
  • 我也尝试过 performance.now() 但动画仍然在晃动,结果如下:jsfiddle.net/ysis81/2Lok5agy/3
  • 谢谢,工作解决方案差不多了,我已将您的代码 sn-p 包含在我的中,不幸的是,没有显示球体,您可以在 jsfiddle.net/ysis81/uzqwtsqm/7 上看到结果。你能看出有什么问题吗?抱歉,如果您注意到一个基本错误,但我不是 Javascript 专家,问候
猜你喜欢
  • 1970-01-01
  • 2021-06-24
  • 1970-01-01
  • 1970-01-01
  • 2012-08-07
  • 1970-01-01
  • 2015-12-17
  • 2012-09-10
  • 2014-09-26
相关资源
最近更新 更多