我想围绕世界的 x 轴和 y 轴旋转一个球体。我用这段代码成功地实现了这一点:
// Update ball rotation.
var tempMat = new THREE.Matrix4();
tempMat.makeRotationAxis(new THREE.Vector3(0,1,0), stepX/ballRadius);
tempMat.multiplySelf(ballMesh.matrix);
ballMesh.matrix = tempMat;
tempMat = new THREE.Matrix4();
tempMat.makeRotationAxis(new THREE.Vector3(1,0,0), -stepY/ballRadius);
tempMat.multiplySelf(ballMesh.matrix);
ballMesh.matrix = tempMat;
ballMesh.rotation.getRotationFromMatrix(ballMesh.matrix);
然而,当我以任何方式远离 (1,1,1) 缩放 ballMesh
时,旋转会以一种难以描述的方式出错。我在这里举了一个 jsfiddle 示例:
http://jsfiddle.net/pxTTv/26/ (使用方向键旋转)
如果您将比例(在 jsfiddle 代码中指示)改回 (1,1,1),它会按我预期的那样工作。
是什么原因造成的,我该如何解决?
最佳答案
您在调用 vector.getRotationFromMatrix( matrix, scale )
时遗漏了 scale
参数。
此外,最好不要弄乱对象矩阵——除非您真的知道自己在做什么。相反,只需设置对象的旋转、位置和比例,然后让库更新矩阵。
在您的情况下,您正在覆盖对象的矩阵,但幸运的是,它在渲染调用中被重新计算。
这是一个更正的 fiddle :http://jsfiddle.net/pxTTv/27/
关于javascript - 当我更改 Three.js 网格的比例时,为什么我的旋转会出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11605330/