Fix for THREE.Matrix4: .getInverse() can't invert

This patches the condition where first 4 Matrix4 elements are 0.

Exact console warning for reference:
THREE.Matrix4: .getInverse() can't invert matrix, determinant is 0

This sometimes occurs with measurements. These elements will always be
factored with the other elements when getInverse() is called,
meaning the result will be 0 (an invalid state with no inverse).
In these cases, ThreeJS will return an identity Matrix4. We can sidestep
a bunch of math, saving CPU cycles, by checking these 4 values.
If all are 0 or don't exist, the determinant will always be 0.

TODO: Eventually, we should figure out how to prevent invalid Matrix4s.
For now, this is better than wasting CPU and forcing Three to help us.
This commit is contained in:
Ben Arnett 2020-04-27 22:56:37 -05:00
parent 8a354118c0
commit 16b05186fe

View File

@ -1760,8 +1760,12 @@ export class Viewer extends EventDispatcher{
let clipBoxes = boxes.map( box => {
box.updateMatrixWorld();
let boxInverse = new THREE.Matrix4().getInverse(box.matrixWorld);
let boxPosition = box.getWorldPosition(new THREE.Vector3());
// Fix for getInverse "determinant is 0" - saves resources
const matrixMultipliersZero = !box.matrixWorld.elements[0] && !box.matrixWorld.elements[1] && !box.matrixWorld.elements[2] && !box.matrixWorld.elements[3];
const boxInverse = matrixMultipliersZero ? new THREE.Matrix4() : new THREE.Matrix4().getInverse(box.matrixWorld);
const boxPosition = box.getWorldPosition(new THREE.Vector3());
return {box: box, inverse: boxInverse, position: boxPosition};
});