Browse Source

Simplified Euler/Quaternion setup. See #4599.

Mr.doob 11 years ago
parent
commit
fffeeb636e
3 changed files with 50 additions and 82 deletions
  1. 5 33
      src/core/Object3D.js
  2. 21 23
      src/math/Euler.js
  3. 24 26
      src/math/Quaternion.js

+ 5 - 33
src/core/Object3D.js

@@ -7,6 +7,8 @@
 
 
 THREE.Object3D = function () {
 THREE.Object3D = function () {
 
 
+	var scope = this;
+
 	this.id = THREE.Object3DIdCount ++;
 	this.id = THREE.Object3DIdCount ++;
 	this.uuid = THREE.Math.generateUUID();
 	this.uuid = THREE.Math.generateUUID();
 
 
@@ -18,15 +20,11 @@ THREE.Object3D = function () {
 	this.up = new THREE.Vector3( 0, 1, 0 );
 	this.up = new THREE.Vector3( 0, 1, 0 );
 
 
 	this.position = new THREE.Vector3();
 	this.position = new THREE.Vector3();
-	this._rotation = new THREE.Euler();
-	this._quaternion = new THREE.Quaternion();
+	this.rotation = new THREE.Euler().onChange( function () { scope.quaternion.setFromEuler( scope.rotation, false ); } );
+	this.quaternion = new THREE.Quaternion().onChange( function () { scope.rotation.setFromQuaternion( scope.quaternion, undefined, false );
+ } );
 	this.scale = new THREE.Vector3( 1, 1, 1 );
 	this.scale = new THREE.Vector3( 1, 1, 1 );
 
 
-	// keep rotation and quaternion in sync
-
-	this._rotation._quaternion = this.quaternion;
-	this._quaternion._euler = this.rotation;
-
 	this.renderDepth = null;
 	this.renderDepth = null;
 
 
 	this.rotationAutoUpdate = true;
 	this.rotationAutoUpdate = true;
@@ -52,32 +50,6 @@ THREE.Object3D = function () {
 THREE.Object3D.prototype = {
 THREE.Object3D.prototype = {
 
 
 	constructor: THREE.Object3D,
 	constructor: THREE.Object3D,
-	
-	get rotation () { 
-		return this._rotation; 
-	},
-
-	set rotation ( value ) {
-		
-		this._rotation = value;
-		this._rotation._quaternion = this._quaternion;
-		this._quaternion._euler = this._rotation;
-		this._rotation._updateQuaternion();
-		
-	},
-
-	get quaternion () { 
-		return this._quaternion; 
-	},
-	
-	set quaternion ( value ) {
-		
-		this._quaternion = value;
-		this._quaternion._euler = this._rotation;
-		this._rotation._quaternion = this._quaternion;
-		this._quaternion._updateEuler();
-		
-	},
 
 
 	get eulerOrder () {
 	get eulerOrder () {
 
 

+ 21 - 23
src/math/Euler.js

@@ -23,18 +23,6 @@ THREE.Euler.prototype = {
 
 
 	_x: 0, _y: 0, _z: 0, _order: THREE.Euler.DefaultOrder,
 	_x: 0, _y: 0, _z: 0, _order: THREE.Euler.DefaultOrder,
 
 
-	_quaternion: undefined,
-
-	_updateQuaternion: function () {
-
-		if ( this._quaternion !== undefined ) {
-
-			this._quaternion.setFromEuler( this, false );
-
-		}
-
-	},
-
 	get x () {
 	get x () {
 
 
 		return this._x;
 		return this._x;
@@ -44,7 +32,7 @@ THREE.Euler.prototype = {
 	set x ( value ) {
 	set x ( value ) {
 
 
 		this._x = value;
 		this._x = value;
-		this._updateQuaternion();
+		this.onChangeCallback();
 
 
 	},
 	},
 
 
@@ -57,7 +45,7 @@ THREE.Euler.prototype = {
 	set y ( value ) {
 	set y ( value ) {
 
 
 		this._y = value;
 		this._y = value;
-		this._updateQuaternion();
+		this.onChangeCallback();
 
 
 	},
 	},
 
 
@@ -70,7 +58,7 @@ THREE.Euler.prototype = {
 	set z ( value ) {
 	set z ( value ) {
 
 
 		this._z = value;
 		this._z = value;
-		this._updateQuaternion();
+		this.onChangeCallback();
 
 
 	},
 	},
 
 
@@ -83,7 +71,7 @@ THREE.Euler.prototype = {
 	set order ( value ) {
 	set order ( value ) {
 
 
 		this._order = value;
 		this._order = value;
-		this._updateQuaternion();
+		this.onChangeCallback();
 
 
 	},
 	},
 
 
@@ -94,7 +82,7 @@ THREE.Euler.prototype = {
 		this._z = z;
 		this._z = z;
 		this._order = order || this._order;
 		this._order = order || this._order;
 
 
-		this._updateQuaternion();
+		this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -107,7 +95,7 @@ THREE.Euler.prototype = {
 		this._z = euler._z;
 		this._z = euler._z;
 		this._order = euler._order;
 		this._order = euler._order;
 
 
-		this._updateQuaternion();
+		this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -230,7 +218,7 @@ THREE.Euler.prototype = {
 
 
 		this._order = order;
 		this._order = order;
 
 
-		this._updateQuaternion();
+		this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -295,7 +283,7 @@ THREE.Euler.prototype = {
 
 
 		this._order = order;
 		this._order = order;
 
 
-		if ( update !== false ) this._updateQuaternion();
+		if ( update !== false ) this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -317,6 +305,12 @@ THREE.Euler.prototype = {
 
 
 	}(),
 	}(),
 
 
+	equals: function ( euler ) {
+
+		return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
+
+	},
+
 	fromArray: function ( array ) {
 	fromArray: function ( array ) {
 
 
 		this._x = array[ 0 ];
 		this._x = array[ 0 ];
@@ -324,7 +318,7 @@ THREE.Euler.prototype = {
 		this._z = array[ 2 ];
 		this._z = array[ 2 ];
 		if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
 		if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
 
 
-		this._updateQuaternion();
+		this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -336,12 +330,16 @@ THREE.Euler.prototype = {
 
 
 	},
 	},
 
 
-	equals: function ( euler ) {
+	onChange: function ( callback ) {
 
 
-		return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
+		this.onChangeCallback = callback;
+
+		return this;
 
 
 	},
 	},
 
 
+	onChangeCallback: function () {},
+
 	clone: function () {
 	clone: function () {
 
 
 		return new THREE.Euler( this._x, this._y, this._z, this._order );
 		return new THREE.Euler( this._x, this._y, this._z, this._order );

+ 24 - 26
src/math/Quaternion.js

@@ -20,18 +20,6 @@ THREE.Quaternion.prototype = {
 
 
 	_x: 0,_y: 0, _z: 0, _w: 0,
 	_x: 0,_y: 0, _z: 0, _w: 0,
 
 
-	_euler: undefined,
-
-	_updateEuler: function ( callback ) {
-
-		if ( this._euler !== undefined ) {
-
-			this._euler.setFromQuaternion( this, undefined, false );
-
-		}
-
-	},
-
 	get x () {
 	get x () {
 
 
 		return this._x;
 		return this._x;
@@ -41,7 +29,7 @@ THREE.Quaternion.prototype = {
 	set x ( value ) {
 	set x ( value ) {
 
 
 		this._x = value;
 		this._x = value;
-		this._updateEuler();
+		this.onChangeCallback();
 
 
 	},
 	},
 
 
@@ -54,7 +42,7 @@ THREE.Quaternion.prototype = {
 	set y ( value ) {
 	set y ( value ) {
 
 
 		this._y = value;
 		this._y = value;
-		this._updateEuler();
+		this.onChangeCallback();
 
 
 	},
 	},
 
 
@@ -67,7 +55,7 @@ THREE.Quaternion.prototype = {
 	set z ( value ) {
 	set z ( value ) {
 
 
 		this._z = value;
 		this._z = value;
-		this._updateEuler();
+		this.onChangeCallback();
 
 
 	},
 	},
 
 
@@ -80,7 +68,7 @@ THREE.Quaternion.prototype = {
 	set w ( value ) {
 	set w ( value ) {
 
 
 		this._w = value;
 		this._w = value;
-		this._updateEuler();
+		this.onChangeCallback();
 
 
 	},
 	},
 
 
@@ -91,7 +79,7 @@ THREE.Quaternion.prototype = {
 		this._z = z;
 		this._z = z;
 		this._w = w;
 		this._w = w;
 
 
-		this._updateEuler();
+		this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -104,7 +92,7 @@ THREE.Quaternion.prototype = {
 		this._z = quaternion._z;
 		this._z = quaternion._z;
 		this._w = quaternion._w;
 		this._w = quaternion._w;
 
 
-		this._updateEuler();
+		this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -172,7 +160,7 @@ THREE.Quaternion.prototype = {
 
 
 		}
 		}
 
 
-		if ( update !== false ) this._updateEuler();
+		if ( update !== false ) this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -191,7 +179,7 @@ THREE.Quaternion.prototype = {
 		this._z = axis.z * s;
 		this._z = axis.z * s;
 		this._w = Math.cos( halfAngle );
 		this._w = Math.cos( halfAngle );
 
 
-		this._updateEuler();
+		this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -250,7 +238,7 @@ THREE.Quaternion.prototype = {
 
 
 		}
 		}
 
 
-		this._updateEuler();
+		this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -294,7 +282,7 @@ THREE.Quaternion.prototype = {
 
 
 			this.set( v1.x, v1.y, v1.z, r ).normalize();
 			this.set( v1.x, v1.y, v1.z, r ).normalize();
 
 
-			this._updateEuler();
+			this.onChangeCallback();
 
 
 			return this;
 			return this;
 
 
@@ -316,7 +304,7 @@ THREE.Quaternion.prototype = {
 		this._y *= -1;
 		this._y *= -1;
 		this._z *= -1;
 		this._z *= -1;
 
 
-		this._updateEuler();
+		this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -385,7 +373,7 @@ THREE.Quaternion.prototype = {
 		this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
 		this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
 		this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
 		this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
 
 
-		this._updateEuler();
+		this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -454,7 +442,7 @@ THREE.Quaternion.prototype = {
 		this._y = ( y * ratioA + this._y * ratioB );
 		this._y = ( y * ratioA + this._y * ratioB );
 		this._z = ( z * ratioA + this._z * ratioB );
 		this._z = ( z * ratioA + this._z * ratioB );
 
 
-		this._updateEuler();
+		this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -473,7 +461,7 @@ THREE.Quaternion.prototype = {
 		this._z = array[ 2 ];
 		this._z = array[ 2 ];
 		this._w = array[ 3 ];
 		this._w = array[ 3 ];
 
 
-		this._updateEuler();
+		this.onChangeCallback();
 
 
 		return this;
 		return this;
 
 
@@ -485,6 +473,16 @@ THREE.Quaternion.prototype = {
 
 
 	},
 	},
 
 
+	onChange: function ( callback ) {
+
+		this.onChangeCallback = callback;
+
+		return this;
+
+	},
+
+	onChangeCallback: function () {},
+
 	clone: function () {
 	clone: function () {
 
 
 		return new THREE.Quaternion( this._x, this._y, this._z, this._w );
 		return new THREE.Quaternion( this._x, this._y, this._z, this._w );