我有一些物体,每个旋转轴都有一个单独的父级(X-rotation有一个,Y-rotation有一个,Z-rotation也有一个)。它们之间也都是相互关联的:X-rotation对象是Y-rotation对象的子对象,而Y-rotation对象又是Z-rotation对象的子对象。
我试图创建一个功能,使用户可以将场景中的所有对象一起旋转(它们都包含在单个Object3D中)。当该Object3D被旋转时,程序必须找到所有对象相对于世界的绝对位置和旋转,以便程序能够输出每个对象的新值。
为了做到这一点,我目前已经设置好了移动对象,使其在“场景旋转器”内的位置设置为相对于世界的绝对位置。现在,我正在尝试使对象的旋转成为相对于世界的绝对旋转,以便当“场景旋转器”的旋转发生变化时相应地进行更改。此外,在我尝试仅对子对象运行setFromRotationMatrix方法时,它没有正常工作,因此我必须针对每个父对象再次运行它,并相应地得到每个单独的旋转。
以下是我目前拥有的代码,打算获取对象相对于世界的绝对旋转:
然后,它必须将绝对旋转应用于对象的相对旋转。
这在第二个父元素的 Y 轴旋转角度为 -90 至 90 度之间时可以正常工作。
我试图创建一个功能,使用户可以将场景中的所有对象一起旋转(它们都包含在单个Object3D中)。当该Object3D被旋转时,程序必须找到所有对象相对于世界的绝对位置和旋转,以便程序能够输出每个对象的新值。
为了做到这一点,我目前已经设置好了移动对象,使其在“场景旋转器”内的位置设置为相对于世界的绝对位置。现在,我正在尝试使对象的旋转成为相对于世界的绝对旋转,以便当“场景旋转器”的旋转发生变化时相应地进行更改。此外,在我尝试仅对子对象运行setFromRotationMatrix方法时,它没有正常工作,因此我必须针对每个父对象再次运行它,并相应地得到每个单独的旋转。
以下是我目前拥有的代码,打算获取对象相对于世界的绝对旋转:
var beforeRotForX = new THREE.Euler();
beforeRotForX.setFromRotationMatrix(objects[i].parent.matrixWorld, "ZYX");
var beforeRotForY = new THREE.Euler(); // Had to be a separate one for some reason...
beforeRotForY.setFromRotationMatrix(objects[i].parent.parent.matrixWorld, "ZYX");
var beforeRotForZ = new THREE.Euler(); // And apparently this one has to be separate too
beforeRotForZ.setFromRotationMatrix(objects[i].parent.parent.parent.matrixWorld, "ZYX");
// Absolute before rotation
objects[i].userData.sceneBeforeRotAbs = {
x: beforeRotForX.x,
y: beforeRotForY.y,
z: beforeRotForZ.z
};
然后,它必须将绝对旋转应用于对象的相对旋转。
objects[i].parent.rotation.x = objects[i].userData.sceneBeforeRotAbs.x;
objects[i].parent.parent.rotation.y = objects[i].userData.sceneBeforeRotAbs.y;
objects[i].parent.parent.parent.rotation.z = objects[i].userData.sceneBeforeRotAbs.z;
这在第二个父元素的 Y 轴旋转角度为 -90 至 90 度之间时可以正常工作。
// Results of absolute world rotation when the Y-rotation of the
// second parent is set to 90 degrees (1.5707... as euler)
objects[i].userData.sceneBeforeRotAbs.x === 0
objects[i].userData.sceneBeforeRotAbs.y === 1.5707963267948966
objects[i].userData.sceneBeforeRotAbs.z === 0
但是当第二个父节点的Y轴旋转小于-90或大于90时,结果会显示错误的绝对世界X轴和Y轴旋转角度值。
// Results of absolute world rotation when the Y-rotation of the
// second parent is set to 91 degrees (1.5882... as euler)
objects[i].userData.sceneBeforeRotAbs.x === 3.141592653589793
objects[i].userData.sceneBeforeRotAbs.y === 1.5533438924131038
objects[i].userData.sceneBeforeRotAbs.z === 0