Currently, I am utilizing Three.js and my goal is to swap out an object in the scene with a new one while maintaining the same rotation as the one I removed.
To achieve this, I take note of the rotation of the object I'm removing and then utilize the following function to apply the rotation to the new object:
rotateToWorldAxis = function(object, xradians, yradians, zradians) {
axisX = new THREE.Vector3(0, 1, 0);
axisY = new THREE.Vector3(1, 0, 0);
axisZ = new THREE.Vector3(0, 0, 1);
rotationMatrix = new THREE.Matrix4();
rotationMatrix.setRotationAxis(axisX.normalize(), xradians)
object.matrix = rotationMatrix;
object.rotation.setRotationFromMatrix(object.matrix);
rotationMatrix = new THREE.Matrix4();
rotationMatrix.setRotationAxis(axisY.normalize(), yradians);
rotationMatrix.multiplySelf(object.matrix);
object.matrix = rotationMatrix;
object.rotation.setRotationFromMatrix(object.matrix);
rotationMatrix = new THREE.Matrix4();
rotationMatrix.setRotationAxis(axisZ.normalize(), zradians);
rotationMatrix.multiplySelf(object.matrix);
object.matrix = rotationMatrix;
object.rotation.setRotationFromMatrix(object.matrix);
}
While the x and y rotations work as intended, the z rotation does not produce the desired outcome.
I would greatly appreciate it if someone could identify where I may be going wrong or suggest an alternative approach?
Thank you!