I have an array containing a 4x4 transformation matrix encoding rotation and position data (I've already confirmed that this matrix represents a valid transformation in Octave). I'd like to set the matrixWorld property of a three.js object (an Object3D) using the contents of this array, i.e., set the position and rotation of the object based on the transformation matrix.
According to this page from the three.js documentation and various questions asked here (e.g., this one and this one), it seems the key is to set nameOfObject.matrixAutoUpdate
to false. However, with this accounted for, I've tried a variety of approaches to set nameOfObject.matrixWorld
and none of them change the location where the object is rendered: it remains at the origin with no rotation, as is the case when matrixWorld
is the 4x4 identity matrix.
Here's what I've tried (this code is inside an update method before render()
is called):
// Setting up a three.js matrix
var tempMatrix = new THREE.Matrix4();
tempMatrix.fromArray(arrayContainingTransformationMatrix);
// Values are set as expected
console.log(tempMatrix.elements);
// Note that the approaches below were tried one at a time
// First approach (doesn't work)
nameOfObject.matrixAutoUpdate = false;
nameOfObject.matrixWorld.copy(tempMatrix);
// Second approach (also doesn't work)
// Based on the second SO question linked above
nameOfObject.matrixAutoUpdate = false;
nameOfObject.matrix.copy(tempMatrix);
nameOfObject.updateMatrixWorld(true);
// Third approach (this doesn't work either, unfortunately)
nameOfObject.matrixAutoUpdate = false;
nameOfObject.matrixWorld.fromArray(arrayContainingTransformationMatrix);
// Appears to be set correctly regardless of which approach is used
console.log(sphere1.matrixWorld.elements);
A few notes:
- I'd expect that it's not actually necessary to set
matrixAutoUpdate
to false inside every iteration, but for now I've done so just to play it safe. - If
nameOfObject.position
is modified based on the values in the fourth column of the transformation matrix instead of changingmatrixWorld
, the object's position changes as expected, so this doesn't appear to be a rendering issue. - I've read in multiple locations that
updateMatrix()
shouldn't be called when manually setting the matrix as it overwrites it based on theposition
androtation
properties. I'd assumeupdateMatrixWorld()
is subject to similar considerations, but I haven't found as much discussion of what it does.
Any suggestions would be appreciated! Worst case I'll look through the source, but three.js has been quite easy to use thus far and I suspect I'm simply missing something.