Hi,
I’m working on a project using three.js and I want to get a better grip on the transformation matrices and how they work. What I want to be able to do is to store - outside of three.js - geometry/meshes once (e.g. a box) that can be located at multiple locations (each location has a transformation matrix). When I was playing with transformation matrices, I’ve had some strange results while sending intermediate results to the console:
// cube is a mesh (box geometry) that is not transformed yet
var m1 = cube.matrix;
console.log(m1); //already updated before doing the transformation => NOT EXPECTED
//[2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 4, 0, 0, 1]
// do transformations on a standard cube (not transformed) using a matrix
cube.scale.set(2,2,2);
cube.translateX(4);
// checking transformation matrix again
var m2 = cube.matrix;
console.log(m2); //is updated according to transformations as expected
//[2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 4, 0, 0, 1]
console.log(m2.elements); // array (= the actual matrix) is not changed => NOT EXPECTED
//[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
console.log(m2.elements[12]); // an individual element of the matrix is not changed => NOT EXPECTED
//0
// other methods to extract translation/rotation/scaling: matrix components are not updated => NOT EXPECTED
var v = new THREE.Vector3();
var r = new THREE.Quaternion();
var s = new THREE.Vector3();
m2.decompose(v,r,s);
console.log(v);
//{x: 0, y: 0, z: 0}
console.log(r);
//{_x: 0, _y: 0, _z: 0, _w: 1}
console.log(s);
//{x: 1, y: 1, z: 1}
// other methods to extract translation/rotation/scaling: matrix components are not updated => NOT EXPECTED
var vec = new THREE.Vector3();
var rot = new THREE.Quaternion();
var sca = new THREE.Vector3();
vec.setFromMatrixPosition( cube.matrix );
rot.setFromRotationMatrix( cube.matrix );
sca.setFromMatrixScale( cube.matrix );
console.log(vec);
//{x: 0, y: 0, z: 0}
console.log(rot);
//{_x: 0, _y: 0, _z: 0, _w: 1}
console.log(sca);
//{x: 1, y: 1, z: 1}
So my question: is this normal behavior or is it some kind of bug? How should transformation matrices be accessed?