The documentation for three.js mentions that there is a function called Matrix3 which has a method called .getInverse
.
However, it requires a Matrix4
as the first parameter.
I attempted to use this function in a simple way in version r71:
var m = new THREE.Matrix3();
console.log('Initial:', m.elements);
m.set(10,8,3,15,7,2,10,6,1);
console.log('Set :', m.elements);
console.log('Inverse:', m.getInverse(m).elements);
You can test it out here: http://jsfiddle.net/as8g61nb/5/ (valid only in Chrome)
Unfortunately, the result I got was unexpected:
Initial: [1, 0, 0, 0, 1, 0, 0, 0, 1] // Correct
Set : [10, 15, 10, 8, 7, 6, 3, 2, 1] // Random values
Inverse: [NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN] // Not the desired output
Interestingly, Wolfram Alpha produces a valid 3x3 Matrix when given the same input.
Input:
Inverse[{{10, 15, 10}, {8, 7, 6}, {3, 2, 1}}]
Output:
{{-1/10, 1/10, 2/5}, {1/5, -2/5, 2/5}, {-1/10, 1/2, -1}}
I delved into the source code of three.js to understand how .getInverse
works, but this only added to my confusion.
It appears that the function expects a Matrix4
as its first argument, leading to the accessing of elements[10]
and elements[11]
, which cannot be done with a Matrix3. This could explain why the output contains NaN
elements.
So, should I convert my Matrix3 to a Matrix4, invert it, and then revert it back to obtain the inverse Matrix3? Or am I overlooking something obvious?