Przeglądaj źródła

Merge remote-tracking branch 'bhouston/Object3D-Matrix4-compose' into dev

Mr.doob 12 lat temu
rodzic
commit
8b577fbdc7

+ 10 - 5
docs/api/math/Matrix4.html

@@ -170,19 +170,19 @@
 		Based on [link:http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm].
 		</div>
 
-		<h3>.setRotationFromEuler( [page:Vector3 v], [page:String order] ) [page:Matrix4]</h3>
+		<h3>.makeRotationFromEuler( [page:Vector3 v], [page:String order] ) [page:Matrix4]</h3>
 		<div>
 		v — Rotation vector.
 		order — The order of rotations. Eg. "XYZ".
 		</div>
 		<div>
-		Sets the rotation submatrix of this matrix to the rotation specified by Euler angles.<br />
+		Sets the rotation submatrix of this matrix to the rotation specified by Euler angles, the rest of the matrix is identity.<br />
 		Default order is *"XYZ"*.
 		</div>
 
-		<h3>.setRotationFromQuaternion( [page:Quaternion q] ) [page:Matrix4]</h3>
+		<h3>.makeRotationFromQuaternion( [page:Quaternion q] ) [page:Matrix4]</h3>
 		<div>
-		Sets the rotation submatrix of this matrix to the rotation specified by *q*.
+		Sets the rotation submatrix of this matrix to the rotation specified by *q*. The rest of the matrix is identity.
 		</div>
 
 		<h3>.scale( [page:Vector3 v] ) [page:Matrix4]</h3>
@@ -190,11 +190,16 @@
 		Multiplies the columns of this matrix by vector *v*.
 		</div>
 
-		<h3>.compose( [page:Vector3 translation], [page:Quaternion rotation], [page:Vector3 scale] ) [page:Matrix4]</h3>
+		<h3>.makeFromPositionQuaternionScale( [page:Vector3 translation], [page:Quaternion rotation], [page:Vector3 scale] ) [page:Matrix4]</h3>
 		<div>
 		Sets this matrix to the transformation composed of *translation*, *rotation* and *scale*.
 		</div>
 
+		<h3>.makeFromPositionEulerScale( [page:Vector3 translation], [page:Vector3 rotation], eulerOrder, [page:Vector3 scale] ) [page:Matrix4]</h3>
+		<div>
+		Sets this matrix to the transformation composed of *translation*, *rotation* (as euler angle triple and order) and *scale*.
+		</div>
+
 		<h3>.decompose( [page:Vector3 translation], [page:Quaternion rotation], [page:Vector3 scale] ) [page:Array]</h3>
 		<div>
 		Decomposes this matrix into the *translation*, *rotation* and *scale* components.<br />

+ 1 - 1
examples/misc_lookat.html

@@ -68,7 +68,7 @@
 				scene.add( sphere );
 
 				var geometry = new THREE.CylinderGeometry( 0, 10, 100, 3 );
-				geometry.applyMatrix( new THREE.Matrix4().setRotationFromEuler( new THREE.Vector3( Math.PI / 2, Math.PI, 0 ) ) );
+				geometry.applyMatrix( new THREE.Matrix4().makeRotationFromEuler( new THREE.Vector3( Math.PI / 2, Math.PI, 0 ) ) );
 
 				var material = new THREE.MeshNormalMaterial();
 

+ 6 - 12
src/core/Object3D.js

@@ -389,21 +389,15 @@ THREE.Object3D.prototype = {
 
 	updateMatrix: function () {
 
-		this.matrix.setPosition( this.position );
-
+		// if we are not using a quaternion directly, convert Euler rotation to this.quaternion.
 		if ( this.useQuaternion === false )  {
 
-			this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
-
-		} else {
-
-			this.matrix.setRotationFromQuaternion( this.quaternion );
-
-		}
-
-		if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
+			this.matrix.makeFromPositionEulerScale( this.position, this.rotation, this.eulerOrder, this.scale );
 
-			this.matrix.scale( this.scale );
+		} 
+		else {
+		
+			this.matrix.makeFromPositionQuaternionScale( this.position, this.quaternion, this.scale );
 
 		}
 

+ 1 - 1
src/extras/core/Gyroscope.js

@@ -25,7 +25,7 @@ THREE.Gyroscope.prototype.updateMatrixWorld = function ( force ) {
 			this.matrixWorld.decompose( this.translationWorld, this.rotationWorld, this.scaleWorld );
 			this.matrix.decompose( this.translationObject, this.rotationObject, this.scaleObject );
 
-			this.matrixWorld.compose( this.translationWorld, this.rotationObject, this.scaleWorld );
+			this.matrixWorld.makeFromPositionQuaternionScale( this.translationWorld, this.rotationObject, this.scaleWorld );
 
 
 		} else {

+ 55 - 15
src/math/Matrix4.js

@@ -128,6 +128,14 @@ THREE.Matrix4.prototype = {
 
 	setRotationFromEuler: function ( v, order ) {
 
+		console.warn( 'DEPRECATED: Matrix4\'s .setRotationFromEuler() has been deprecated in favor of makeRotationFromEuler.  Please update your code.' );
+		
+		return this.makeRotationFromEuler( v, order );
+
+	},
+
+	makeRotationFromEuler: function ( v, order ) {
+
 		var te = this.elements;
 
 		var x = v.x, y = v.y, z = v.z;
@@ -233,12 +241,31 @@ THREE.Matrix4.prototype = {
 
 		}
 
+		// 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;
 
 	},
 
 	setRotationFromQuaternion: function ( q ) {
 
+		console.warn( 'DEPRECATED: Matrix4\'s .setRotationFromQuaternion() has been deprecated in favor of makeRotationFromQuaternion.  Please update your code.' );
+		
+		return this.makeRotationFromQuaternion( q );
+
+	},
+
+	makeRotationFromQuaternion: function ( q ) {
+
 		var te = this.elements;
 
 		var x = q.x, y = q.y, z = q.z, w = q.w;
@@ -259,6 +286,17 @@ THREE.Matrix4.prototype = {
 		te[6] = yz + wx;
 		te[10] = 1 - ( xx + yy );
 
+		// 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;
 
 	},
@@ -857,31 +895,33 @@ THREE.Matrix4.prototype = {
 
 THREE.extend( THREE.Matrix4.prototype, {
 
-	compose: function() {
+	compose: function ( position, quaternion, scale ) {
 
-		var mRotation = new THREE.Matrix4();
-		var mScale = new THREE.Matrix4();
+		console.warn( 'DEPRECATED: Matrix4\'s .compose() has been deprecated in favor of makeFromPositionQuaternionScale.  Please update your code.' );
+		
+		return this.makeFromPositionQuaternionScale( position, quaternion, scale );
 
-		return function ( position, quaternion, scale ) {
+	},
 
-			var te = this.elements;
+	makeFromPositionQuaternionScale: function ( position, quaternion, scale ) {
 
-			mRotation.identity();
-			mRotation.setRotationFromQuaternion( quaternion );
+		this.makeRotationFromQuaternion( quaternion );
+		this.scale( scale );
+		this.setPosition( position );
 
-			mScale.makeScale( scale.x, scale.y, scale.z );
+		return this;
 
-			this.multiplyMatrices( mRotation, mScale );
+	},
 
-			te[12] = position.x;
-			te[13] = position.y;
-			te[14] = position.z;
+	makeFromPositionEulerScale: function ( position, rotation, eulerOrder, scale ) {
 
-			return this;
+		this.makeRotationFromEuler( rotation, eulerOrder );
+		this.scale( scale );
+		this.setPosition( position );
 
-		};
+		return this;
 
-	}(),
+	},
 
 	decompose: function() {
 

+ 2 - 9
src/objects/Sprite.js

@@ -22,16 +22,9 @@ THREE.Sprite.prototype = Object.create( THREE.Object3D.prototype );
 
 THREE.Sprite.prototype.updateMatrix = function () {
 
-	this.matrix.setPosition( this.position );
-
 	this.rotation3d.set( 0, 0, this.rotation );
-	this.matrix.setRotationFromEuler( this.rotation3d );
-
-	if ( this.scale.x !== 1 || this.scale.y !== 1 ) {
-
-		this.matrix.scale( this.scale );
-
-	}
+	this.quaterion.setFromEuler( this.rotation3d, this.eulerOrder );
+	this.matrix.makeFromPositionQuaternionScale( this.position, this.quaternion, this.scale );
 
 	this.matrixWorldNeedsUpdate = true;
 

+ 5 - 5
test/unit/math/Quaternion.js

@@ -111,7 +111,7 @@ test( "setFromEuler/setFromRotationMatrix", function() {
 	// ensure euler conversion for Quaternion matches that of Matrix4
 	for( var i = 0; i < orders.length; i ++ ) {
 		var q = new THREE.Quaternion().setFromEuler( eulerAngles, orders[i] );
-		var m = new THREE.Matrix4().setRotationFromEuler( eulerAngles, orders[i] );
+		var m = new THREE.Matrix4().makeRotationFromEuler( eulerAngles, orders[i] );
 		var q2 = new THREE.Quaternion().setFromRotationMatrix( m );
 
 		ok( qSub( q, q2 ).length() < 0.001, "Passed!" );
@@ -161,9 +161,9 @@ test( "multiplyQuaternions/multiply", function() {
 
 	var q = new THREE.Quaternion().multiplyQuaternions( q1, q2 ).multiply( q3 );
 
-	var m1 = new THREE.Matrix4().setRotationFromEuler( angles[0], "XYZ" );
-	var m2 = new THREE.Matrix4().setRotationFromEuler( angles[1], "XYZ" );
-	var m3 = new THREE.Matrix4().setRotationFromEuler( angles[2], "XYZ" );
+	var m1 = new THREE.Matrix4().makeRotationFromEuler( angles[0], "XYZ" );
+	var m2 = new THREE.Matrix4().makeRotationFromEuler( angles[1], "XYZ" );
+	var m3 = new THREE.Matrix4().makeRotationFromEuler( angles[2], "XYZ" );
 
 	var m = new THREE.Matrix4().multiplyMatrices( m1, m2 ).multiply( m3 );
 
@@ -180,7 +180,7 @@ test( "multiplyVector3", function() {
 	for( var i = 0; i < orders.length; i ++ ) {
 		for( var j = 0; j < angles.length; j ++ ) {
 			var q = new THREE.Quaternion().setFromEuler( angles[j], orders[i] );
-			var m = new THREE.Matrix4().setRotationFromEuler( angles[j], orders[i] );
+			var m = new THREE.Matrix4().makeRotationFromEuler( angles[j], orders[i] );
 
 			var v0 = new THREE.Vector3(1, 0, 0);
 			var qv = v0.clone().applyQuaternion( q );