Browse Source

Make Matrix4.compose more efficient

Marshall Quander 7 years ago
parent
commit
7f0a1234c2
1 changed files with 39 additions and 36 deletions
  1. 39 36
      src/math/Matrix4.js

+ 39 - 36
src/math/Matrix4.js

@@ -278,42 +278,17 @@ Object.assign( Matrix4.prototype, {
 
 
 	},
 	},
 
 
-	makeRotationFromQuaternion: function ( q ) {
+	makeRotationFromQuaternion: function () {
 
 
-		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;
-
-		te[ 1 ] = xy + wz;
-		te[ 5 ] = 1 - ( xx + zz );
-		te[ 9 ] = yz - wx;
-
-		te[ 2 ] = xz - wy;
-		te[ 6 ] = yz + wx;
-		te[ 10 ] = 1 - ( xx + yy );
-
-		// last column
-		te[ 3 ] = 0;
-		te[ 7 ] = 0;
-		te[ 11 ] = 0;
+		var zero = new Vector3( 0, 0, 0 );
+		var one = new Vector3( 1, 1, 1 );
 
 
-		// bottom row
-		te[ 12 ] = 0;
-		te[ 13 ] = 0;
-		te[ 14 ] = 0;
-		te[ 15 ] = 1;
+		return function makeRotationFromQuaternion( q ) {
 
 
-		return this;
+			return this.compose( zero, q, one );
 
 
-	},
+		};
+	}(),
 
 
 	lookAt: function () {
 	lookAt: function () {
 
 
@@ -754,12 +729,40 @@ 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[ 4 ] = (xy - wz) * sy;
+		te[ 8 ] = (xz + wy) * sz;
+
+		te[ 1 ] = (xy + wz) * sx;
+		te[ 5 ] = (1 - ( xx + zz )) * sy;
+		te[ 9 ] = (yz - wx) * sz;
 
 
+		te[ 2 ] = (xz - wy) * sx;
+		te[ 6 ] = (yz + wx) * sy;
+		te[ 10 ] = (1 - ( xx + yy )) * sz;
+
+		// last column
+		te[ 3 ] = 0;
+		te[ 7 ] = 0;
+		te[ 11 ] = 0;
+
+		// bottom row
+		te[ 12 ] = position.x;
+		te[ 13 ] = position.y;
+		te[ 14 ] = position.z;
+		te[ 15 ] = 1;
+
+		return this;
 	},
 	},
 
 
 	decompose: function () {
 	decompose: function () {