فهرست منبع

fixed changed event up update only on visual change
changed conditional statements to be more explicit

Aleksandar Rodic 12 سال پیش
والد
کامیت
59db2ace44
1فایلهای تغییر یافته به همراه117 افزوده شده و 123 حذف شده
  1. 117 123
      examples/js/controls/TransformControls.js

+ 117 - 123
examples/js/controls/TransformControls.js

@@ -40,6 +40,7 @@ THREE.TransformGizmo = function () {
 
 	var showPickers = false; //debug
 	var showActivePlane = false; //debug
+	this.activePlane = undefined;
 
 	this.init = function () {
 
@@ -144,7 +145,7 @@ THREE.TransformGizmo = function () {
 
 		}
 
-		if (this.activePlane) this.activePlane.visible = showActivePlane;
+		if ( this.activePlane !== undefined ) this.activePlane.visible = showActivePlane;
 
 	}
 
@@ -156,7 +157,7 @@ THREE.TransformGizmo = function () {
 
 			handle = this.handleGizmos[ i ][0];
 
-			if ( handle.material.oldColor ) {
+			if ( handle.material.oldColor !== undefined ) {
 
 				handle.material.color.copy( handle.material.oldColor );
 				handle.material.opacity = handle.material.oldOpacity;
@@ -165,7 +166,7 @@ THREE.TransformGizmo = function () {
 
 		}
 
-		if ( this.handleGizmos[ axis ] ) {
+		if ( this.handleGizmos[ axis ] !== undefined ) {
 		
 			handle = this.handleGizmos[ axis ][0];
 
@@ -574,11 +575,11 @@ THREE.TransformControls = function ( camera, domElement ) {
 	this.gizmo["rotate"].hide();
 	this.gizmo["scale"].hide();
 
-	this.object = false;
+	this.object = undefined;
 	this.snap = false;
 	this.space = "world";
 	this.size = 1;
-	this.axis = false;
+	this.axis = undefined;
 
 	var scope = this;
 	
@@ -658,8 +659,8 @@ THREE.TransformControls = function ( camera, domElement ) {
 
 	this.detach = function ( object ) {
 
-		scope.object = false;
-		this.axis = false;
+		scope.object = undefined;
+		this.axis = undefined;
 
 	 	this.gizmo["translate"].hide();
 	 	this.gizmo["rotate"].hide();
@@ -705,7 +706,7 @@ THREE.TransformControls = function ( camera, domElement ) {
 
 	this.update = function () {
 
-		if ( !scope.object ) return;
+		if ( scope.object === undefined ) return;
 
 		scope.object.updateMatrixWorld();
 		worldPosition.getPositionFromMatrix( scope.object.matrixWorld );
@@ -729,13 +730,11 @@ THREE.TransformControls = function ( camera, domElement ) {
 
 		this.gizmo[_mode].highlight( scope.axis );
 
-		scope.dispatchEvent( changeEvent );
-
 	}
 
 	function onPointerHover( event ) {
 
-		if ( !scope.object || _dragging ) return;
+		if ( scope.object === undefined || _dragging == true ) return;
 
 		event.preventDefault();
 		event.stopPropagation();
@@ -748,11 +747,13 @@ THREE.TransformControls = function ( camera, domElement ) {
 
 			scope.axis = intersect.object.name;
 			scope.update();
+			scope.dispatchEvent( changeEvent );
 
-		} else {
+		} else if ( scope.axis !== undefined ) {
 
-			scope.axis = false;
+			scope.axis = undefined;
 			scope.update();
+			scope.dispatchEvent( changeEvent );
 
 		}
 
@@ -760,14 +761,14 @@ THREE.TransformControls = function ( camera, domElement ) {
 
 	function onPointerDown( event ) {
 
-		if ( !scope.object || _dragging ) return;
+		if ( scope.object === undefined || _dragging == true ) return;
 
 		event.preventDefault();
 		event.stopPropagation();
 
 		var pointer = event.touches? event.touches[0] : event;
 
-		if ( pointer.button === 0 || pointer.button == undefined ) {
+		if ( pointer.button === 0 || pointer.button === undefined ) {
 
 			var intersect = intersectObjects( pointer, scope.gizmo[_mode].pickers.children );
 
@@ -783,20 +784,16 @@ THREE.TransformControls = function ( camera, domElement ) {
 
 				var planeIntersect = intersectObjects( pointer, [scope.gizmo[_mode].activePlane] );
 
-				if ( planeIntersect ) {
-
-					oldPosition.copy( scope.object.position );
-					oldScale.copy( scope.object.scale );
-
-					oldRotationMatrix.extractRotation( scope.object.matrix );
-					worldRotationMatrix.extractRotation( scope.object.matrixWorld );
+				oldPosition.copy( scope.object.position );
+				oldScale.copy( scope.object.scale );
 
-					parentRotationMatrix.extractRotation( scope.object.parent.matrixWorld );
-					parentScale.getScaleFromMatrix( tempMatrix.getInverse( scope.object.parent.matrixWorld ) );
+				oldRotationMatrix.extractRotation( scope.object.matrix );
+				worldRotationMatrix.extractRotation( scope.object.matrixWorld );
 
-					offset.copy( planeIntersect.point );
+				parentRotationMatrix.extractRotation( scope.object.parent.matrixWorld );
+				parentScale.getScaleFromMatrix( tempMatrix.getInverse( scope.object.parent.matrixWorld ) );
 
-				}
+				offset.copy( planeIntersect.point );
 
 			}
 
@@ -808,7 +805,7 @@ THREE.TransformControls = function ( camera, domElement ) {
 
 	function onPointerMove( event ) {
 
-		if ( !scope.object || !scope.axis || !_dragging ) return;
+		if ( scope.object === undefined || scope.axis === undefined || _dragging == false ) return;
 
 		event.preventDefault();
 		event.stopPropagation();
@@ -817,169 +814,166 @@ THREE.TransformControls = function ( camera, domElement ) {
 
 		var planeIntersect = intersectObjects( pointer, [scope.gizmo[_mode].activePlane] );
 
-		if ( planeIntersect ) {
-
-			point.copy( planeIntersect.point );
+		point.copy( planeIntersect.point );
 
-			if ( _mode == "translate" ) {
+		if ( _mode == "translate" ) {
 
-				point.sub( offset );
-				point.multiply(parentScale);
+			point.sub( offset );
+			point.multiply(parentScale);
 
-				if ( scope.space == "local" ) {
-
-					point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
+			if ( scope.space == "local" ) {
 
-					if ( scope.axis.search("X") == -1 ) point.x = 0;
-					if ( scope.axis.search("Y") == -1 ) point.y = 0;
-					if ( scope.axis.search("Z") == -1 ) point.z = 0;
+				point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
 
-					point.applyMatrix4( oldRotationMatrix );
+				if ( scope.axis.search("X") == -1 ) point.x = 0;
+				if ( scope.axis.search("Y") == -1 ) point.y = 0;
+				if ( scope.axis.search("Z") == -1 ) point.z = 0;
 
-					scope.object.position.copy( oldPosition );
-					scope.object.position.add( point );
+				point.applyMatrix4( oldRotationMatrix );
 
-				} 
+				scope.object.position.copy( oldPosition );
+				scope.object.position.add( point );
 
-				if ( scope.space == "world" || scope.axis.search("XYZ") != -1 ) {
+			} 
 
-					if ( scope.axis.search("X") == -1 ) point.x = 0;
-					if ( scope.axis.search("Y") == -1 ) point.y = 0;
-					if ( scope.axis.search("Z") == -1 ) point.z = 0;
+			if ( scope.space == "world" || scope.axis.search("XYZ") != -1 ) {
 
-					point.applyMatrix4( tempMatrix.getInverse( parentRotationMatrix ) );
+				if ( scope.axis.search("X") == -1 ) point.x = 0;
+				if ( scope.axis.search("Y") == -1 ) point.y = 0;
+				if ( scope.axis.search("Z") == -1 ) point.z = 0;
 
-					scope.object.position.copy( oldPosition );
-					scope.object.position.add( point );
+				point.applyMatrix4( tempMatrix.getInverse( parentRotationMatrix ) );
 
-					if ( scope.snap ) {
+				scope.object.position.copy( oldPosition );
+				scope.object.position.add( point );
 
-						if ( scope.axis.search("X") != -1 ) scope.object.position.x = Math.round( scope.object.position.x / scope.snap ) * scope.snap;
-						if ( scope.axis.search("Y") != -1 ) scope.object.position.y = Math.round( scope.object.position.y / scope.snap ) * scope.snap;
-						if ( scope.axis.search("Z") != -1 ) scope.object.position.z = Math.round( scope.object.position.z / scope.snap ) * scope.snap;
-					
-					}
+				if ( scope.snap == true ) {
 
+					if ( scope.axis.search("X") != -1 ) scope.object.position.x = Math.round( scope.object.position.x / scope.snap ) * scope.snap;
+					if ( scope.axis.search("Y") != -1 ) scope.object.position.y = Math.round( scope.object.position.y / scope.snap ) * scope.snap;
+					if ( scope.axis.search("Z") != -1 ) scope.object.position.z = Math.round( scope.object.position.z / scope.snap ) * scope.snap;
+				
 				}
 
-			} else if ( _mode == "scale" ) {
+			}
 
-				point.sub( offset );
-				point.multiply(parentScale);
+		} else if ( _mode == "scale" ) {
 
-				if ( scope.space == "local" ) {
+			point.sub( offset );
+			point.multiply(parentScale);
 
-					if ( scope.axis == "XYZ") {
+			if ( scope.space == "local" ) {
 
-						scale = 1 + ( ( point.y ) / 50 );
+				if ( scope.axis == "XYZ") {
 
-						scope.object.scale.x = oldScale.x * scale;
-						scope.object.scale.y = oldScale.y * scale;
-						scope.object.scale.z = oldScale.z * scale;
+					scale = 1 + ( ( point.y ) / 50 );
 
-					} else {
+					scope.object.scale.x = oldScale.x * scale;
+					scope.object.scale.y = oldScale.y * scale;
+					scope.object.scale.z = oldScale.z * scale;
 
-						point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
+				} else {
 
-						if ( scope.axis == "X" ) scope.object.scale.x = oldScale.x * ( 1 + point.x / 50 );
-						if ( scope.axis == "Y" ) scope.object.scale.y = oldScale.y * ( 1 + point.y / 50 );
-						if ( scope.axis == "Z" ) scope.object.scale.z = oldScale.z * ( 1 + point.z / 50 );
+					point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
 
-					}
+					if ( scope.axis == "X" ) scope.object.scale.x = oldScale.x * ( 1 + point.x / 50 );
+					if ( scope.axis == "Y" ) scope.object.scale.y = oldScale.y * ( 1 + point.y / 50 );
+					if ( scope.axis == "Z" ) scope.object.scale.z = oldScale.z * ( 1 + point.z / 50 );
 
 				}
 
-			} else if ( _mode == "rotate" ) {
+			}
 
-				point.sub( worldPosition );
-				point.multiply(parentScale);
-				tempVector.copy(offset).sub( worldPosition );
-				tempVector.multiply(parentScale);
+		} else if ( _mode == "rotate" ) {
 
-				if ( scope.axis == "E" ) {
+			point.sub( worldPosition );
+			point.multiply(parentScale);
+			tempVector.copy(offset).sub( worldPosition );
+			tempVector.multiply(parentScale);
 
-					point.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
-					tempVector.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
+			if ( scope.axis == "E" ) {
 
-					rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
-					offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
+				point.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
+				tempVector.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
 
-					tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
+				rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
+				offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
 
-					quaternionE.setFromAxisAngle( eye, rotation.z - offsetRotation.z );
-					quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
+				tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
 
-					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionE );
-					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
+				quaternionE.setFromAxisAngle( eye, rotation.z - offsetRotation.z );
+				quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
 
-					scope.object.quaternion.copy( tempQuaternion );
+				tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionE );
+				tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
 
-				} else if ( scope.axis == "XYZE" ) {
+				scope.object.quaternion.copy( tempQuaternion );
 
-					quaternionE.setFromEuler( point.clone().cross(tempVector).normalize() ); // rotation axis
+			} else if ( scope.axis == "XYZE" ) {
 
-					tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
-					quaternionX.setFromAxisAngle( quaternionE, - point.clone().angleTo(tempVector) );
-					quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
+				quaternionE.setFromEuler( point.clone().cross(tempVector).normalize() ); // rotation axis
 
-					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
-					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
+				tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
+				quaternionX.setFromAxisAngle( quaternionE, - point.clone().angleTo(tempVector) );
+				quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
 
-					scope.object.quaternion.copy( tempQuaternion );
+				tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
+				tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
 
-				} else if ( scope.space == "local" ) {
+				scope.object.quaternion.copy( tempQuaternion );
 
-					point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
+			} else if ( scope.space == "local" ) {
 
-					tempVector.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
+				point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
 
-					rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
-					offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
+				tempVector.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
 
-					quaternionXYZ.setFromRotationMatrix( oldRotationMatrix );
-					quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
-					quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
-					quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
+				rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
+				offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
 
-					if ( scope.axis == "X" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX );
-					if ( scope.axis == "Y" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY );
-					if ( scope.axis == "Z" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionZ );
+				quaternionXYZ.setFromRotationMatrix( oldRotationMatrix );
+				quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
+				quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
+				quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
 
-					scope.object.quaternion.copy( quaternionXYZ );
+				if ( scope.axis == "X" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX );
+				if ( scope.axis == "Y" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY );
+				if ( scope.axis == "Z" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionZ );
 
-				} else if ( scope.space == "world" ) {
+				scope.object.quaternion.copy( quaternionXYZ );
 
-					rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
-					offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
+			} else if ( scope.space == "world" ) {
 
-					tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
+				rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
+				offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
 
-					quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
-					quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
-					quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
-					quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
+				tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
 
-					if ( scope.axis == "X" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
-					if ( scope.axis == "Y" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
-					if ( scope.axis == "Z" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
+				quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
+				quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
+				quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
+				quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
 
-					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
+				if ( scope.axis == "X" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
+				if ( scope.axis == "Y" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
+				if ( scope.axis == "Z" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
 
-					scope.object.quaternion.copy( tempQuaternion );
+				tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
 
-				}
+				scope.object.quaternion.copy( tempQuaternion );
 
 			}
 
 		}
 
 		scope.update();
+		scope.dispatchEvent( changeEvent );
 
 	}
 
 	function onPointerUp( event ) {
 
-		scope.axis = false;
+		scope.axis = undefined;
 		_dragging = false;
 		scope.update();