Browse Source

Merge pull request #14044 from mquander/updatematrixworld-microoptimize

Make Matrix4.compose more efficient
Mr.doob 7 years ago
parent
commit
bc44c95def
1 changed files with 39 additions and 37 deletions
  1. 39 37
      src/math/Matrix4.js

+ 39 - 37
src/math/Matrix4.js

@@ -263,12 +263,12 @@ Object.assign( Matrix4.prototype, {
 
 
 		}
 		}
 
 
-		// last column
+		// bottom row
 		te[ 3 ] = 0;
 		te[ 3 ] = 0;
 		te[ 7 ] = 0;
 		te[ 7 ] = 0;
 		te[ 11 ] = 0;
 		te[ 11 ] = 0;
 
 
-		// bottom row
+		// last column
 		te[ 12 ] = 0;
 		te[ 12 ] = 0;
 		te[ 13 ] = 0;
 		te[ 13 ] = 0;
 		te[ 14 ] = 0;
 		te[ 14 ] = 0;
@@ -278,42 +278,18 @@ Object.assign( Matrix4.prototype, {
 
 
 	},
 	},
 
 
-	makeRotationFromQuaternion: function ( q ) {
-
-		var te = this.elements;
-
-		var x = q._x, y = q._y, z = q._z, w = q._w;
-		var x2 = x + x, y2 = y + y, z2 = z + z;
-		var xx = x * x2, xy = x * y2, xz = x * z2;
-		var yy = y * y2, yz = y * z2, zz = z * z2;
-		var wx = w * x2, wy = w * y2, wz = w * z2;
-
-		te[ 0 ] = 1 - ( yy + zz );
-		te[ 4 ] = xy - wz;
-		te[ 8 ] = xz + wy;
+	makeRotationFromQuaternion: function () {
 
 
-		te[ 1 ] = xy + wz;
-		te[ 5 ] = 1 - ( xx + zz );
-		te[ 9 ] = yz - wx;
+		var zero = new Vector3( 0, 0, 0 );
+		var one = new Vector3( 1, 1, 1 );
 
 
-		te[ 2 ] = xz - wy;
-		te[ 6 ] = yz + wx;
-		te[ 10 ] = 1 - ( xx + yy );
+		return function makeRotationFromQuaternion( q ) {
 
 
-		// last column
-		te[ 3 ] = 0;
-		te[ 7 ] = 0;
-		te[ 11 ] = 0;
-
-		// bottom row
-		te[ 12 ] = 0;
-		te[ 13 ] = 0;
-		te[ 14 ] = 0;
-		te[ 15 ] = 1;
+			return this.compose( zero, q, one );
 
 
-		return this;
+		};
 
 
-	},
+	}(),
 
 
 	lookAt: function () {
 	lookAt: function () {
 
 
@@ -754,11 +730,37 @@ Object.assign( Matrix4.prototype, {
 
 
 	compose: function ( position, quaternion, scale ) {
 	compose: function ( position, quaternion, scale ) {
 
 
-		this.makeRotationFromQuaternion( quaternion );
-		this.scale( scale );
-		this.setPosition( position );
+		var te = this.elements;
 
 
-		return this;
+		var x = quaternion._x, y = quaternion._y, z = quaternion._z, w = quaternion._w;
+		var x2 = x + x,	y2 = y + y, z2 = z + z;
+		var xx = x * x2, xy = x * y2, xz = x * z2;
+		var yy = y * y2, yz = y * z2, zz = z * z2;
+		var wx = w * x2, wy = w * y2, wz = w * z2;
+
+		var sx = scale.x, sy = scale.y, sz = scale.z;
+
+	        te[ 0 ] = ( 1 - ( yy + zz ) ) * sx;
+	        te[ 1 ] = ( xy + wz ) * sx;
+	        te[ 2 ] = ( xz - wy ) * sx;
+	        te[ 3 ] = 0;
+
+	        te[ 4 ] = ( xy - wz ) * sy;
+	        te[ 5 ] = ( 1 - ( xx + zz ) ) * sy;
+	        te[ 6 ] = ( yz + wx ) * sy;
+	        te[ 7 ] = 0;
+
+	        te[ 8 ] = ( xz + wy ) * sz;
+	        te[ 9 ] = ( yz - wx ) * sz;
+	        te[ 10 ] = ( 1 - ( xx + yy ) ) * sz;
+	        te[ 11 ] = 0;
+
+	        te[ 12 ] = position.x;
+	        te[ 13 ] = position.y;
+	        te[ 14 ] = position.z;
+	        te[ 15 ] = 1;
+
+	        return this;
 
 
 	},
 	},