Skip to content

Commit bc44c95

Browse files
authored
Merge pull request #14044 from mquander/updatematrixworld-microoptimize
Make Matrix4.compose more efficient
2 parents efeb95f + fdde74a commit bc44c95

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

src/math/Matrix4.js

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ Object.assign( Matrix4.prototype, {
263263

264264
}
265265

266-
// last column
266+
// bottom row
267267
te[ 3 ] = 0;
268268
te[ 7 ] = 0;
269269
te[ 11 ] = 0;
270270

271-
// bottom row
271+
// last column
272272
te[ 12 ] = 0;
273273
te[ 13 ] = 0;
274274
te[ 14 ] = 0;
@@ -278,42 +278,18 @@ Object.assign( Matrix4.prototype, {
278278

279279
},
280280

281-
makeRotationFromQuaternion: function ( q ) {
282-
283-
var te = this.elements;
284-
285-
var x = q._x, y = q._y, z = q._z, w = q._w;
286-
var x2 = x + x, y2 = y + y, z2 = z + z;
287-
var xx = x * x2, xy = x * y2, xz = x * z2;
288-
var yy = y * y2, yz = y * z2, zz = z * z2;
289-
var wx = w * x2, wy = w * y2, wz = w * z2;
290-
291-
te[ 0 ] = 1 - ( yy + zz );
292-
te[ 4 ] = xy - wz;
293-
te[ 8 ] = xz + wy;
281+
makeRotationFromQuaternion: function () {
294282

295-
te[ 1 ] = xy + wz;
296-
te[ 5 ] = 1 - ( xx + zz );
297-
te[ 9 ] = yz - wx;
283+
var zero = new Vector3( 0, 0, 0 );
284+
var one = new Vector3( 1, 1, 1 );
298285

299-
te[ 2 ] = xz - wy;
300-
te[ 6 ] = yz + wx;
301-
te[ 10 ] = 1 - ( xx + yy );
286+
return function makeRotationFromQuaternion( q ) {
302287

303-
// last column
304-
te[ 3 ] = 0;
305-
te[ 7 ] = 0;
306-
te[ 11 ] = 0;
307-
308-
// bottom row
309-
te[ 12 ] = 0;
310-
te[ 13 ] = 0;
311-
te[ 14 ] = 0;
312-
te[ 15 ] = 1;
288+
return this.compose( zero, q, one );
313289

314-
return this;
290+
};
315291

316-
},
292+
}(),
317293

318294
lookAt: function () {
319295

@@ -754,11 +730,37 @@ Object.assign( Matrix4.prototype, {
754730

755731
compose: function ( position, quaternion, scale ) {
756732

757-
this.makeRotationFromQuaternion( quaternion );
758-
this.scale( scale );
759-
this.setPosition( position );
733+
var te = this.elements;
760734

761-
return this;
735+
var x = quaternion._x, y = quaternion._y, z = quaternion._z, w = quaternion._w;
736+
var x2 = x + x, y2 = y + y, z2 = z + z;
737+
var xx = x * x2, xy = x * y2, xz = x * z2;
738+
var yy = y * y2, yz = y * z2, zz = z * z2;
739+
var wx = w * x2, wy = w * y2, wz = w * z2;
740+
741+
var sx = scale.x, sy = scale.y, sz = scale.z;
742+
743+
te[ 0 ] = ( 1 - ( yy + zz ) ) * sx;
744+
te[ 1 ] = ( xy + wz ) * sx;
745+
te[ 2 ] = ( xz - wy ) * sx;
746+
te[ 3 ] = 0;
747+
748+
te[ 4 ] = ( xy - wz ) * sy;
749+
te[ 5 ] = ( 1 - ( xx + zz ) ) * sy;
750+
te[ 6 ] = ( yz + wx ) * sy;
751+
te[ 7 ] = 0;
752+
753+
te[ 8 ] = ( xz + wy ) * sz;
754+
te[ 9 ] = ( yz - wx ) * sz;
755+
te[ 10 ] = ( 1 - ( xx + yy ) ) * sz;
756+
te[ 11 ] = 0;
757+
758+
te[ 12 ] = position.x;
759+
te[ 13 ] = position.y;
760+
te[ 14 ] = position.z;
761+
te[ 15 ] = 1;
762+
763+
return this;
762764

763765
},
764766

0 commit comments

Comments
 (0)