From 6f65eb50eac83fb2a57f4584c603238b4ea7c468 Mon Sep 17 00:00:00 2001 From: Gregg Tavares Date: Tue, 1 Mar 2022 09:13:45 -0800 Subject: [PATCH] fix m3 inverse --- webgl/resources/m3.js | 57 ++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/webgl/resources/m3.js b/webgl/resources/m3.js index 6a01a0ff0..b00534729 100644 --- a/webgl/resources/m3.js +++ b/webgl/resources/m3.js @@ -336,37 +336,32 @@ function inverse(m, dst) { dst = dst || new MatType(9); - const m00 = m[0 * 4 + 0]; - const m01 = m[0 * 4 + 1]; - const m02 = m[0 * 4 + 2]; - const m10 = m[1 * 4 + 0]; - const m11 = m[1 * 4 + 1]; - const m12 = m[1 * 4 + 2]; - const m20 = m[2 * 4 + 0]; - const m21 = m[2 * 4 + 1]; - const m22 = m[2 * 4 + 2]; - - const m11_x_m22 = m11 * m22; - const m21_x_m12 = m21 * m12; - const m01_x_m22 = m01 * m22; - const m21_x_m02 = m21 * m02; - const m01_x_m12 = m01 * m12; - const m11_x_m02 = m11 * m02; - - const invDet = 1 / ( - m00 * (m11_x_m22 - m21_x_m12) - - m10 * (m01_x_m22 - m21_x_m02) + - m20 * (m01_x_m12 - m11_x_m02)); - - dst[ 0] = +(m11_x_m22 - m21_x_m12) * invDet; - dst[ 1] = -(m10 * m22 - m20 * m12) * invDet; - dst[ 2] = +(m10 * m21 - m20 * m11) * invDet; - dst[ 3] = -(m01_x_m22 - m21_x_m02) * invDet; - dst[ 4] = +(m00 * m22 - m20 * m02) * invDet; - dst[ 5] = -(m00 * m21 - m20 * m01) * invDet; - dst[ 6] = +(m01_x_m12 - m11_x_m02) * invDet; - dst[ 7] = -(m00 * m12 - m10 * m02) * invDet; - dst[ 8] = +(m00 * m11 - m10 * m01) * invDet; + const m00 = m[0 * 3 + 0]; + const m01 = m[0 * 3 + 1]; + const m02 = m[0 * 3 + 2]; + const m10 = m[1 * 3 + 0]; + const m11 = m[1 * 3 + 1]; + const m12 = m[1 * 3 + 2]; + const m20 = m[2 * 3 + 0]; + const m21 = m[2 * 3 + 1]; + const m22 = m[2 * 3 + 2]; + + const b01 = m22 * m11 - m12 * m21; + const b11 = -m22 * m10 + m12 * m20; + const b21 = m21 * m10 - m11 * m20; + + const invDet = m00 * b01 + m01 * b11 + m02 * b21; + const det = 1.0 / invDet; + + dst[0] = b01 * det; + dst[1] = (-m22 * m01 + m02 * m21) * det; + dst[2] = ( m12 * m01 - m02 * m11) * det; + dst[3] = b11 * det; + dst[4] = ( m22 * m00 - m02 * m20) * det; + dst[5] = (-m12 * m00 + m02 * m10) * det; + dst[6] = b21 * det; + dst[7] = (-m21 * m00 + m01 * m20) * det; + dst[8] = ( m11 * m00 - m01 * m10) * det; return dst; }