我在Three.JS
中有一个由一些网格对象组成的Object3d
组。
我想要绕Y轴旋转该组,以它的中心为中心,该中心远离世界中心(0,0,0)。
我只知道Group.rotate.y += deg
代码,但对于每个轴方向,它总是围绕(0,0,0)旋转物体而非我的组中心!
该如何解决?
更新:
请查看评论
看一下 Object3D 的 rotateOnAxis(axis, angle)
函数。它应该是这样的:
//declared once at the top of your code
var axis = new THREE.Vector3(0.5,0.5,0);//tilted a bit on x and y - feel free to plug your different axis here
//in your update/draw function
rad += radIncrement;
object.rotateOnAxis(axis,rad);
HTH
这个答案对我有所帮助,但代码还不完全正确。轴必须是一个单位向量,而且object.rotateOnAxis()
是增量的。
下面是一个正常工作的例子:
var axis = new THREE.Vector3(4, 0, 7).normalize();
var speed = 0.01;
// set up scene, etc.
function animate () {
// other code
if (object) {
object.rotateOnAxis(axis, speed);
}
}
我是这样解决这个问题的:
我为ThreeJS创建了“ObjectControls”模块,它允许您旋转单个对象(或组),而不是场景。
包含库:
用法:
var controls = new THREE.ObjectControls(camera, renderer.domElement, yourMesh);
您可以在此处找到实时演示:https://albertopiras.github.io/threeJS-object-controls/
这是存储库:https://github.com/albertopiras/threeJS-object-controls。
var group = new THREE.Object3D();group.add(object1);group.add(object2);object2.x = 100
在设置代码顶部,然后简单地group.rotation.y += radians
- George Profenzagroup.rotation.y += deg
,但它并没有围绕组中心旋转!它是围绕 (0,0,0) 旋转的。 - MeTe-30