Browse Source

Optimized Object3D.updateMatrix to (1) cut down where possible on new Matrix4 allocation, (2) only compute
rotation/scale matrices if needed (if no rotation, no need t compute); and (3) no longer require
storage of translationMatrix or scaleMatrix (1 per object). Uses a single tmpMatrix instead.

astrodud 14 years ago
parent
commit
c9706f85bf
1 changed files with 21 additions and 11 deletions
  1. 21 11
      src/objects/Object3D.js

+ 21 - 11
src/objects/Object3D.js

@@ -11,9 +11,7 @@ THREE.Object3D = function () {
 	this.scale = new THREE.Vector3( 1, 1, 1 );
 
 	this.matrix = new THREE.Matrix4();
-	this.translationMatrix = new THREE.Matrix4();
-	this.rotationMatrix = new THREE.Matrix4();
-	this.scaleMatrix = new THREE.Matrix4();
+	this.tmpMatrix = new THREE.Matrix4();
 
 	this.screen = new THREE.Vector3();
 
@@ -26,18 +24,30 @@ THREE.Object3D.prototype = {
 
 	updateMatrix: function () {
 
-		this.matrixPosition = THREE.Matrix4.translationMatrix( this.position.x, this.position.y, this.position.z );
+		var p = this.position, r = this.rotation, s = this.scale, m = this.tmpMatrix;
 
-		this.rotationMatrix = THREE.Matrix4.rotationXMatrix( this.rotation.x );
-		this.rotationMatrix.multiplySelf( THREE.Matrix4.rotationYMatrix( this.rotation.y ) );
-		this.rotationMatrix.multiplySelf( THREE.Matrix4.rotationZMatrix( this.rotation.z ) );
+		this.matrix.setTranslation( p.x, p.y, p.z );
 
-		this.scaleMatrix = THREE.Matrix4.scaleMatrix( this.scale.x, this.scale.y, this.scale.z );
+		if ( r.x != 0 ) {
+		       m.setRotX( r.x );
+		       this.matrix.multiplySelf( m );
+		}
 
-		this.matrix.copy( this.matrixPosition );
-		this.matrix.multiplySelf( this.rotationMatrix );
-		this.matrix.multiplySelf( this.scaleMatrix );
+		if ( r.y != 0 ) {
+		       m.setRotY( r.y );
+		       this.matrix.multiplySelf( m );
+		}
 
+		if ( r.z != 0 ) {
+		       m.setRotZ( r.z );
+		       this.matrix.multiplySelf( m );
+		}
+
+		if ( s.x != 0 || s.y != 0 || s.z != 0 ) {
+		       m.setScale( s.x, s.y, s.z );
+		       this.matrix.multiplySelf( m );
+		}
+		
 	}
 
 };