Browse Source

Fixed the inverse(mat3) polyfill for GLES2

Marcus Brummer 4 years ago
parent
commit
b02900478f
1 changed files with 9 additions and 5 deletions
  1. 9 5
      drivers/gles2/shaders/stdlib.glsl

+ 9 - 5
drivers/gles2/shaders/stdlib.glsl

@@ -309,11 +309,15 @@ highp mat2 inverse(highp mat2 m) {
 }
 
 highp mat3 inverse(highp mat3 m) {
-	highp float d = 1.0 / (m[0].x * (m[1].y * m[2].z - m[2].y * m[1].z) - m[1].x * (m[0].y * m[2].z - m[2].y * m[0].z) + m[2].x * (m[0].y * m[1].z - m[1].y * m[0].z));
-	return mat3(
-			vec3((m[1].y * m[2].z - m[2].y * m[1].z), -(m[1].x * m[2].z - m[2].x * m[1].z), (m[1].x * m[2].y - m[2].x * m[1].y)) * d,
-			vec3(-(m[0].y * m[2].z - m[2].y * m[0].z), (m[0].x * m[2].z - m[2].x * m[0].z), -(m[0].x * m[2].y - m[2].x * m[0].y)) * d,
-			vec3((m[0].y * m[1].z - m[1].y * m[0].z), -(m[0].x * m[1].z - m[1].x * m[0].z), (m[0].x * m[1].y - m[1].x * m[0].y)) * d);
+	highp float c01 = m[2].z * m[1].y - m[1].z * m[2].y;
+	highp float c11 = -m[2].z * m[1].x + m[1].z * m[2].x;
+	highp float c21 = m[2].y * m[1].x - m[1].y * m[2].x;
+	highp float d = 1.0 / (m[0].x * c01 + m[0].y * c11 + m[0].z * c21);
+
+	return mat3(c01, (-m[2].z * m[0].y + m[0].z * m[2].y), (m[1].z * m[0].y - m[0].z * m[1].y),
+				   c11, (m[2].z * m[0].x - m[0].z * m[2].x), (-m[1].z * m[0].x + m[0].z * m[1].x),
+				   c21, (-m[2].y * m[0].x + m[0].y * m[2].x), (m[1].y * m[0].x - m[0].y * m[1].x)) *
+		   d;
 }
 
 highp mat4 inverse(highp mat4 m) {