I'm currently facing an issue with my three.js code.
My goal is to align two objects by selecting two faces and rotating the second face (and object) to match the normal vector of the first object's selected face.
Here is what I have so far:
var normalMatrix = new THREE.Matrix3().getNormalMatrix( object.matrixWorld );
var worldNormal = face.normal.clone().applyMatrix3( normalMatrix ).normalize();
var normalMatrixRef = new THREE.Matrix3().getNormalMatrix( objectRef.matrixWorld );
var worldNormalRef = faceRef.normal.clone().applyMatrix3( normalMatrixRef).normalize();
Where: object represents the object that will be rotated, face is the selected face of that object, objectRef is the stationary object reference, and faceRef is the normal of the object I want to match.
Prior to calling
object.lookAt(worldNormalRef);
, I've attempted to set the 'up' direction using any of the following options:
object.up = new THREE.Vector3(0,0,1);
object.up = worldNormal;
object.up = face.normal;
Unfortunately, neither of these approaches seem to work as intended. When I perform the action, the target object to be rotated indeed rotates, but fails to align the faces correctly. The rotation appears arbitrary, sometimes along (0,1,0) or similar directions depending on the object's current rotation state.
In theory, using lookAt with a world vector like faceRef should work as long as the 'up' direction is set correctly, but it simply isn't working in practice.
Any suggestions or ideas on how to tackle this issue?