Browse Source

Merge pull request #6888 from dubejf/orb

Extract OrbitConstraint from OrbitControls
Mr.doob 10 years ago
parent
commit
08815bc10a
1 changed files with 630 additions and 417 deletions
  1. 630 417
      examples/js/controls/OrbitControls.js

+ 630 - 417
examples/js/controls/OrbitControls.js

@@ -7,700 +7,913 @@
  */
 /*global THREE, console */
 
-// This set of controls performs orbiting, dollying (zooming), and panning. It maintains
-// the "up" direction as +Y, unlike the TrackballControls. Touch on tablet and phones is
-// supported.
-//
-//    Orbit - left mouse / touch: one finger move
-//    Zoom - middle mouse, or mousewheel / touch: two finger spread or squish
-//    Pan - right mouse, or arrow keys / touch: three finter swipe
+(function () {
 
-THREE.OrbitControls = function ( object, domElement ) {
+	function OrbitConstraint ( object ) {
 
-	this.object = object;
-	this.domElement = ( domElement !== undefined ) ? domElement : document;
+		this.object = object;
 
-	// API
+		// "target" sets the location of focus, where the object orbits around
+		// and where it pans with respect to.
+		this.target = new THREE.Vector3();
 
-	// Set to false to disable this control
-	this.enabled = true;
+		// Limits to how far you can dolly in and out ( PerspectiveCamera only )
+		this.minDistance = 0;
+		this.maxDistance = Infinity;
 
-	// "target" sets the location of focus, where the control orbits around
-	// and where it pans with respect to.
-	this.target = new THREE.Vector3();
+		// Limits to how far you can zoom in and out ( OrthographicCamera only )
+		this.minZoom = 0;
+		this.maxZoom = Infinity;
 
-	// center is old, deprecated; use "target" instead
-	this.center = this.target;
+		// How far you can orbit vertically, upper and lower limits.
+		// Range is 0 to Math.PI radians.
+		this.minPolarAngle = 0; // radians
+		this.maxPolarAngle = Math.PI; // radians
 
-	// This option actually enables dollying in and out; left as "zoom" for
-	// backwards compatibility
-	this.noZoom = false;
-	this.zoomSpeed = 1.0;
+		// How far you can orbit horizontally, upper and lower limits.
+		// If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
+		this.minAzimuthAngle = - Infinity; // radians
+		this.maxAzimuthAngle = Infinity; // radians
 
-	// Limits to how far you can dolly in and out ( PerspectiveCamera only )
-	this.minDistance = 0;
-	this.maxDistance = Infinity;
+		////////////
+		// internals
 
-	// Limits to how far you can zoom in and out ( OrthographicCamera only )
-	this.minZoom = 0;
-	this.maxZoom = Infinity;
+		var scope = this;
 
-	// Set to true to disable this control
-	this.noRotate = false;
-	this.rotateSpeed = 1.0;
+		var EPS = 0.000001;
 
-	// Set to true to disable this control
-	this.noPan = false;
-	this.keyPanSpeed = 7.0;	// pixels moved per arrow key push
+		// Current position in spherical coordinate system.
+		var theta;
+		var phi;
 
-	// Set to true to automatically rotate around the target
-	this.autoRotate = false;
-	this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
+		// Pending changes
+		var phiDelta = 0;
+		var thetaDelta = 0;
+		var scale = 1;
+		var panOffset = new THREE.Vector3();
+		var zoomChanged = false;
 
-	// How far you can orbit vertically, upper and lower limits.
-	// Range is 0 to Math.PI radians.
-	this.minPolarAngle = 0; // radians
-	this.maxPolarAngle = Math.PI; // radians
+		// API
 
-	// How far you can orbit horizontally, upper and lower limits.
-	// If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
-	this.minAzimuthAngle = - Infinity; // radians
-	this.maxAzimuthAngle = Infinity; // radians
+		this.getPolarAngle = function () {
 
-	// Set to true to disable use of the keys
-	this.noKeys = false;
+			return phi;
 
-	// The four arrow keys
-	this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
+		};
 
-	// Mouse buttons
-	this.mouseButtons = { ORBIT: THREE.MOUSE.LEFT, ZOOM: THREE.MOUSE.MIDDLE, PAN: THREE.MOUSE.RIGHT };
+		this.getAzimuthalAngle = function () {
 
-	////////////
-	// internals
+			return theta;
 
-	var scope = this;
+		};
 
-	var EPS = 0.000001;
+		this.rotateLeft = function ( angle ) {
 
-	var rotateStart = new THREE.Vector2();
-	var rotateEnd = new THREE.Vector2();
-	var rotateDelta = new THREE.Vector2();
+			thetaDelta -= angle;
 
-	var panStart = new THREE.Vector2();
-	var panEnd = new THREE.Vector2();
-	var panDelta = new THREE.Vector2();
-	var panOffset = new THREE.Vector3();
+		};
 
-	var offset = new THREE.Vector3();
+		this.rotateUp = function ( angle ) {
 
-	var dollyStart = new THREE.Vector2();
-	var dollyEnd = new THREE.Vector2();
-	var dollyDelta = new THREE.Vector2();
+			phiDelta -= angle;
 
-	var theta;
-	var phi;
-	var phiDelta = 0;
-	var thetaDelta = 0;
-	var scale = 1;
-	var pan = new THREE.Vector3();
+		};
 
-	var lastPosition = new THREE.Vector3();
-	var lastQuaternion = new THREE.Quaternion();
+		// pass in distance in world space to move left
+		this.panLeft = function() {
 
-	var STATE = { NONE : -1, ROTATE : 0, DOLLY : 1, PAN : 2, TOUCH_ROTATE : 3, TOUCH_DOLLY : 4, TOUCH_PAN : 5 };
+			var v = new THREE.Vector3();
 
-	var state = STATE.NONE;
+			return function panLeft ( distance ) {
 
-	// for reset
+				var te = this.object.matrix.elements;
 
-	this.target0 = this.target.clone();
-	this.position0 = this.object.position.clone();
-	this.zoom0 = this.object.zoom;
+				// get X column of matrix
+				v.set( te[ 0 ], te[ 1 ], te[ 2 ] );
+				v.multiplyScalar( - distance );
 
-	// so camera.up is the orbit axis
+				panOffset.add( v );
 
-	var quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
-	var quatInverse = quat.clone().inverse();
+			};
 
-	// events
+		}();
 
-	var changeEvent = { type: 'change' };
-	var startEvent = { type: 'start' };
-	var endEvent = { type: 'end' };
+		// pass in distance in world space to move up
+		this.panUp = function() {
 
-	this.rotateLeft = function ( angle ) {
+			var v = new THREE.Vector3();
 
-		if ( angle === undefined ) {
+			return function panUp ( distance ) {
 
-			angle = getAutoRotationAngle();
+				var te = this.object.matrix.elements;
 
-		}
+				// get Y column of matrix
+				v.set( te[ 4 ], te[ 5 ], te[ 6 ] );
+				v.multiplyScalar( distance );
 
-		thetaDelta -= angle;
+				panOffset.add( v );
 
-	};
+			};
 
-	this.rotateUp = function ( angle ) {
+		}();
 
-		if ( angle === undefined ) {
+		// pass in x,y of change desired in pixel space,
+		// right and down are positive
+		this.pan = function ( deltaX, deltaY, screenWidth, screenHeight ) {
 
-			angle = getAutoRotationAngle();
+			if ( scope.object instanceof THREE.PerspectiveCamera ) {
 
-		}
+				// perspective
+				var position = scope.object.position;
+				var offset = position.clone().sub( scope.target );
+				var targetDistance = offset.length();
 
-		phiDelta -= angle;
+				// half of the fov is center to top of screen
+				targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
 
-	};
+				// we actually don't use screenWidth, since perspective camera is fixed to screen height
+				scope.panLeft( 2 * deltaX * targetDistance / screenHeight );
+				scope.panUp( 2 * deltaY * targetDistance / screenHeight );
 
-	// pass in distance in world space to move left
-	this.panLeft = function ( distance ) {
+			} else if ( scope.object instanceof THREE.OrthographicCamera ) {
 
-		var te = this.object.matrix.elements;
+				// orthographic
+				scope.panLeft( deltaX * ( scope.object.right - scope.object.left ) / screenWidth );
+				scope.panUp( deltaY * ( scope.object.top - scope.object.bottom ) / screenHeight );
 
-		// get X column of matrix
-		panOffset.set( te[ 0 ], te[ 1 ], te[ 2 ] );
-		panOffset.multiplyScalar( - distance );
+			} else {
 
-		pan.add( panOffset );
+				// camera neither orthographic or perspective
+				console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
 
-	};
+			}
 
-	// pass in distance in world space to move up
-	this.panUp = function ( distance ) {
+		};
 
-		var te = this.object.matrix.elements;
+		this.dollyIn = function ( dollyScale ) {
 
-		// get Y column of matrix
-		panOffset.set( te[ 4 ], te[ 5 ], te[ 6 ] );
-		panOffset.multiplyScalar( distance );
+			if ( scope.object instanceof THREE.PerspectiveCamera ) {
 
-		pan.add( panOffset );
+				scale /= dollyScale;
 
-	};
+			} else if ( scope.object instanceof THREE.OrthographicCamera ) {
 
-	// pass in x,y of change desired in pixel space,
-	// right and down are positive
-	this.pan = function ( deltaX, deltaY ) {
+				scope.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom * dollyScale ) );
+				scope.object.updateProjectionMatrix();
+				zoomChanged = true;
 
-		var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
+			} else {
 
-		if ( scope.object instanceof THREE.PerspectiveCamera ) {
+				console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
 
-			// perspective
-			var position = scope.object.position;
-			var offset = position.clone().sub( scope.target );
-			var targetDistance = offset.length();
+			}
 
-			// half of the fov is center to top of screen
-			targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
+		};
 
-			// we actually don't use screenWidth, since perspective camera is fixed to screen height
-			scope.panLeft( 2 * deltaX * targetDistance / element.clientHeight );
-			scope.panUp( 2 * deltaY * targetDistance / element.clientHeight );
+		this.dollyOut = function ( dollyScale ) {
 
-		} else if ( scope.object instanceof THREE.OrthographicCamera ) {
+			if ( scope.object instanceof THREE.PerspectiveCamera ) {
 
-			// orthographic
-			scope.panLeft( deltaX * (scope.object.right - scope.object.left) / element.clientWidth );
-			scope.panUp( deltaY * (scope.object.top - scope.object.bottom) / element.clientHeight );
+				scale *= dollyScale;
 
-		} else {
+			} else if ( scope.object instanceof THREE.OrthographicCamera ) {
 
-			// camera neither orthographic or perspective
-			console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
+				scope.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / dollyScale ) );
+				scope.object.updateProjectionMatrix();
+				zoomChanged = true;
 
-		}
+			} else {
 
-	};
+				console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
 
-	this.dollyIn = function ( dollyScale ) {
+			}
 
-		if ( dollyScale === undefined ) {
+		};
 
-			dollyScale = getZoomScale();
+		this.update = function() {
 
-		}
+			var offset = new THREE.Vector3();
 
-		if ( scope.object instanceof THREE.PerspectiveCamera ) {
+			// so camera.up is the orbit axis
+			var quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
+			var quatInverse = quat.clone().inverse();
 
-			scale /= dollyScale;
+			var lastPosition = new THREE.Vector3();
+			var lastQuaternion = new THREE.Quaternion();
 
-		} else if ( scope.object instanceof THREE.OrthographicCamera ) {
+			return function () {
 
-			scope.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom * dollyScale ) );
-			scope.object.updateProjectionMatrix();
-			scope.dispatchEvent( changeEvent );
+				var position = this.object.position;
 
-		} else {
+				offset.copy( position ).sub( this.target );
 
-			console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
+				// rotate offset to "y-axis-is-up" space
+				offset.applyQuaternion( quat );
 
-		}
+				// angle from z-axis around y-axis
 
-	};
+				theta = Math.atan2( offset.x, offset.z );
 
-	this.dollyOut = function ( dollyScale ) {
+				// angle from y-axis
 
-		if ( dollyScale === undefined ) {
+				phi = Math.atan2( Math.sqrt( offset.x * offset.x + offset.z * offset.z ), offset.y );
 
-			dollyScale = getZoomScale();
+				theta += thetaDelta;
+				phi += phiDelta;
 
-		}
+				// restrict theta to be between desired limits
+				theta = Math.max( this.minAzimuthAngle, Math.min( this.maxAzimuthAngle, theta ) );
 
-		if ( scope.object instanceof THREE.PerspectiveCamera ) {
+				// restrict phi to be between desired limits
+				phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, phi ) );
 
-			scale *= dollyScale;
+				// restrict phi to be betwee EPS and PI-EPS
+				phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) );
 
-		} else if ( scope.object instanceof THREE.OrthographicCamera ) {
+				var radius = offset.length() * scale;
 
-			scope.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / dollyScale ) );
-			scope.object.updateProjectionMatrix();
-			scope.dispatchEvent( changeEvent );
+				// restrict radius to be between desired limits
+				radius = Math.max( this.minDistance, Math.min( this.maxDistance, radius ) );
 
-		} else {
+				// move target to panned location
+				this.target.add( panOffset );
 
-			console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
+				offset.x = radius * Math.sin( phi ) * Math.sin( theta );
+				offset.y = radius * Math.cos( phi );
+				offset.z = radius * Math.sin( phi ) * Math.cos( theta );
 
-		}
+				// rotate offset back to "camera-up-vector-is-up" space
+				offset.applyQuaternion( quatInverse );
 
-	};
+				position.copy( this.target ).add( offset );
 
-	this.update = function () {
+				this.object.lookAt( this.target );
 
-		var position = this.object.position;
+				thetaDelta = 0;
+				phiDelta = 0;
+				scale = 1;
+				panOffset.set( 0, 0, 0 );
 
-		offset.copy( position ).sub( this.target );
+				// update condition is:
+				// min(camera displacement, camera rotation in radians)^2 > EPS
+				// using small-angle approximation cos(x/2) = 1 - x^2 / 8
 
-		// rotate offset to "y-axis-is-up" space
-		offset.applyQuaternion( quat );
+				if ( zoomChanged ||
+					 lastPosition.distanceToSquared( this.object.position ) > EPS ||
+				    8 * ( 1 - lastQuaternion.dot( this.object.quaternion) ) > EPS ) {
 
-		// angle from z-axis around y-axis
+					lastPosition.copy( this.object.position );
+					lastQuaternion.copy( this.object.quaternion );
+					zoomChanged = false;
 
-		theta = Math.atan2( offset.x, offset.z );
+					return true;
 
-		// angle from y-axis
+				}
 
-		phi = Math.atan2( Math.sqrt( offset.x * offset.x + offset.z * offset.z ), offset.y );
+				return false;
 
-		if ( this.autoRotate && state === STATE.NONE ) {
+			};
 
-			this.rotateLeft( getAutoRotationAngle() );
+		}();
 
-		}
+	};
 
-		theta += thetaDelta;
-		phi += phiDelta;
 
-		// restrict theta to be between desired limits
-		theta = Math.max( this.minAzimuthAngle, Math.min( this.maxAzimuthAngle, theta ) );
+	// This set of controls performs orbiting, dollying (zooming), and panning. It maintains
+	// the "up" direction as +Y, unlike the TrackballControls. Touch on tablet and phones is
+	// supported.
+	//
+	//    Orbit - left mouse / touch: one finger move
+	//    Zoom - middle mouse, or mousewheel / touch: two finger spread or squish
+	//    Pan - right mouse, or arrow keys / touch: three finter swipe
 
-		// restrict phi to be between desired limits
-		phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, phi ) );
+	THREE.OrbitControls = function ( object, domElement ) {
 
-		// restrict phi to be betwee EPS and PI-EPS
-		phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) );
+		var constraint = new OrbitConstraint( object );
 
-		var radius = offset.length() * scale;
+		this.domElement = ( domElement !== undefined ) ? domElement : document;
 
-		// restrict radius to be between desired limits
-		radius = Math.max( this.minDistance, Math.min( this.maxDistance, radius ) );
+		// API
 
-		// move target to panned location
-		this.target.add( pan );
+		Object.defineProperty(this, 'constraint', {
 
-		offset.x = radius * Math.sin( phi ) * Math.sin( theta );
-		offset.y = radius * Math.cos( phi );
-		offset.z = radius * Math.sin( phi ) * Math.cos( theta );
+		    get: function() {
 
-		// rotate offset back to "camera-up-vector-is-up" space
-		offset.applyQuaternion( quatInverse );
+		        return constraint;
 
-		position.copy( this.target ).add( offset );
+		    }
 
-		this.object.lookAt( this.target );
+		});
 
-		thetaDelta = 0;
-		phiDelta = 0;
-		scale = 1;
-		pan.set( 0, 0, 0 );
+		this.getPolarAngle = function () {
 
-		// update condition is:
-		// min(camera displacement, camera rotation in radians)^2 > EPS
-		// using small-angle approximation cos(x/2) = 1 - x^2 / 8
+			return constraint.phi;
 
-		if ( lastPosition.distanceToSquared( this.object.position ) > EPS
-		    || 8 * (1 - lastQuaternion.dot(this.object.quaternion)) > EPS ) {
+		};
 
-			this.dispatchEvent( changeEvent );
+		this.getAzimuthalAngle = function () {
 
-			lastPosition.copy( this.object.position );
-			lastQuaternion.copy (this.object.quaternion );
+			return constraint.theta;
 
-		}
+		};
 
-	};
+		// Set to false to disable this control
+		this.enabled = true;
 
+		// center is old, deprecated; use "target" instead
+		this.center = this.target;
 
-	this.reset = function () {
+		// This option actually enables dollying in and out; left as "zoom" for
+		// backwards compatibility
+		this.noZoom = false;
+		this.zoomSpeed = 1.0;
 
-		state = STATE.NONE;
+		// Set to true to disable this control
+		this.noRotate = false;
+		this.rotateSpeed = 1.0;
 
-		this.target.copy( this.target0 );
-		this.object.position.copy( this.position0 );
-		this.object.zoom = this.zoom0;
+		// Set to true to disable this control
+		this.noPan = false;
+		this.keyPanSpeed = 7.0;	// pixels moved per arrow key push
 
-		this.object.updateProjectionMatrix();
-		this.dispatchEvent( changeEvent );
+		// Set to true to automatically rotate around the target
+		this.autoRotate = false;
+		this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
 
-		this.update();
+		// Set to true to disable use of the keys
+		this.noKeys = false;
 
-	};
+		// The four arrow keys
+		this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
 
-	this.getPolarAngle = function () {
+		// Mouse buttons
+		this.mouseButtons = { ORBIT: THREE.MOUSE.LEFT, ZOOM: THREE.MOUSE.MIDDLE, PAN: THREE.MOUSE.RIGHT };
 
-		return phi;
+		////////////
+		// internals
 
-	};
+		var scope = this;
 
-	this.getAzimuthalAngle = function () {
+		var rotateStart = new THREE.Vector2();
+		var rotateEnd = new THREE.Vector2();
+		var rotateDelta = new THREE.Vector2();
 
-		return theta
+		var panStart = new THREE.Vector2();
+		var panEnd = new THREE.Vector2();
+		var panDelta = new THREE.Vector2();
 
-	};
+		var dollyStart = new THREE.Vector2();
+		var dollyEnd = new THREE.Vector2();
+		var dollyDelta = new THREE.Vector2();
+
+		var STATE = { NONE : -1, ROTATE : 0, DOLLY : 1, PAN : 2, TOUCH_ROTATE : 3, TOUCH_DOLLY : 4, TOUCH_PAN : 5 };
+
+		var state = STATE.NONE;
+
+		// for reset
+
+		this.target0 = this.target.clone();
+		this.position0 = this.object.position.clone();
+		this.zoom0 = this.object.zoom;
+
+		// events
+
+		var changeEvent = { type: 'change' };
+		var startEvent = { type: 'start' };
+		var endEvent = { type: 'end' };
+
+		// pass in x,y of change desired in pixel space,
+		// right and down are positive
+		function pan( deltaX, deltaY ) {
+
+			var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
+
+			constraint.pan( deltaX, deltaY, element.clientWidth, element.clientHeight );
 
-	function getAutoRotationAngle() {
+		}
 
-		return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
+		this.update = function() {
 
-	}
+			if ( this.autoRotate && state === STATE.NONE ) {
 
-	function getZoomScale() {
+				constraint.rotateLeft( getAutoRotationAngle() );
 
-		return Math.pow( 0.95, scope.zoomSpeed );
+			}
 
-	}
+			if ( constraint.update() === true ) {
 
-	function onMouseDown( event ) {
+				this.dispatchEvent( changeEvent );
 
-		if ( scope.enabled === false ) return;
-		event.preventDefault();
+			}
 
-		if ( event.button === scope.mouseButtons.ORBIT ) {
-			if ( scope.noRotate === true ) return;
+		};
 
-			state = STATE.ROTATE;
+		this.reset = function () {
 
-			rotateStart.set( event.clientX, event.clientY );
+			state = STATE.NONE;
 
-		} else if ( event.button === scope.mouseButtons.ZOOM ) {
-			if ( scope.noZoom === true ) return;
+			this.target.copy( this.target0 );
+			this.object.position.copy( this.position0 );
+			this.object.zoom = this.zoom0;
 
-			state = STATE.DOLLY;
+			this.object.updateProjectionMatrix();
+			this.dispatchEvent( changeEvent );
 
-			dollyStart.set( event.clientX, event.clientY );
+			this.update();
 
-		} else if ( event.button === scope.mouseButtons.PAN ) {
-			if ( scope.noPan === true ) return;
+		};
 
-			state = STATE.PAN;
+		function getAutoRotationAngle() {
 
-			panStart.set( event.clientX, event.clientY );
+			return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
 
 		}
 
-		if ( state !== STATE.NONE ) {
-			document.addEventListener( 'mousemove', onMouseMove, false );
-			document.addEventListener( 'mouseup', onMouseUp, false );
-			scope.dispatchEvent( startEvent );
+		function getZoomScale() {
+
+			return Math.pow( 0.95, scope.zoomSpeed );
+
 		}
 
-	}
+		function onMouseDown( event ) {
 
-	function onMouseMove( event ) {
+			if ( scope.enabled === false ) return;
 
-		if ( scope.enabled === false ) return;
+			event.preventDefault();
 
-		event.preventDefault();
+			if ( event.button === scope.mouseButtons.ORBIT ) {
 
-		var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
+				if ( scope.noRotate === true ) return;
 
-		if ( state === STATE.ROTATE ) {
+				state = STATE.ROTATE;
 
-			if ( scope.noRotate === true ) return;
+				rotateStart.set( event.clientX, event.clientY );
 
-			rotateEnd.set( event.clientX, event.clientY );
-			rotateDelta.subVectors( rotateEnd, rotateStart );
+			} else if ( event.button === scope.mouseButtons.ZOOM ) {
 
-			// rotating across whole screen goes 360 degrees around
-			scope.rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
+				if ( scope.noZoom === true ) return;
 
-			// rotating up and down along whole screen attempts to go 360, but limited to 180
-			scope.rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
+				state = STATE.DOLLY;
 
-			rotateStart.copy( rotateEnd );
+				dollyStart.set( event.clientX, event.clientY );
 
-		} else if ( state === STATE.DOLLY ) {
+			} else if ( event.button === scope.mouseButtons.PAN ) {
 
-			if ( scope.noZoom === true ) return;
+				if ( scope.noPan === true ) return;
 
-			dollyEnd.set( event.clientX, event.clientY );
-			dollyDelta.subVectors( dollyEnd, dollyStart );
+				state = STATE.PAN;
 
-			if ( dollyDelta.y > 0 ) {
+				panStart.set( event.clientX, event.clientY );
 
-				scope.dollyIn();
+			}
 
-			} else if ( dollyDelta.y < 0 ) {
+			if ( state !== STATE.NONE ) {
 
-				scope.dollyOut();
+				document.addEventListener( 'mousemove', onMouseMove, false );
+				document.addEventListener( 'mouseup', onMouseUp, false );
+				scope.dispatchEvent( startEvent );
 
 			}
 
-			dollyStart.copy( dollyEnd );
+		}
 
-		} else if ( state === STATE.PAN ) {
+		function onMouseMove( event ) {
 
-			if ( scope.noPan === true ) return;
+			if ( scope.enabled === false ) return;
 
-			panEnd.set( event.clientX, event.clientY );
-			panDelta.subVectors( panEnd, panStart );
+			event.preventDefault();
 
-			scope.pan( panDelta.x, panDelta.y );
+			var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
 
-			panStart.copy( panEnd );
+			if ( state === STATE.ROTATE ) {
+
+				if ( scope.noRotate === true ) return;
+
+				rotateEnd.set( event.clientX, event.clientY );
+				rotateDelta.subVectors( rotateEnd, rotateStart );
+
+				// rotating across whole screen goes 360 degrees around
+				constraint.rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
+
+				// rotating up and down along whole screen attempts to go 360, but limited to 180
+				constraint.rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
+
+				rotateStart.copy( rotateEnd );
+
+			} else if ( state === STATE.DOLLY ) {
+
+				if ( scope.noZoom === true ) return;
+
+				dollyEnd.set( event.clientX, event.clientY );
+				dollyDelta.subVectors( dollyEnd, dollyStart );
+
+				if ( dollyDelta.y > 0 ) {
+
+					contraint.dollyIn( getZoomScale() );
+
+				} else if ( dollyDelta.y < 0 ) {
+
+					contraint.dollyOut( getZoomScale() );
+
+				}
+
+				dollyStart.copy( dollyEnd );
+
+			} else if ( state === STATE.PAN ) {
+
+				if ( scope.noPan === true ) return;
+
+				panEnd.set( event.clientX, event.clientY );
+				panDelta.subVectors( panEnd, panStart );
+
+				pan( panDelta.x, panDelta.y );
+
+				panStart.copy( panEnd );
+
+			}
+
+			if ( state !== STATE.NONE ) scope.update();
 
 		}
 
-		if ( state !== STATE.NONE ) scope.update();
+		function onMouseUp( /* event */ ) {
 
-	}
+			if ( scope.enabled === false ) return;
 
-	function onMouseUp( /* event */ ) {
+			document.removeEventListener( 'mousemove', onMouseMove, false );
+			document.removeEventListener( 'mouseup', onMouseUp, false );
+			scope.dispatchEvent( endEvent );
+			state = STATE.NONE;
 
-		if ( scope.enabled === false ) return;
+		}
+
+		function onMouseWheel( event ) {
+
+			if ( scope.enabled === false || scope.noZoom === true || state !== STATE.NONE ) return;
 
-		document.removeEventListener( 'mousemove', onMouseMove, false );
-		document.removeEventListener( 'mouseup', onMouseUp, false );
-		scope.dispatchEvent( endEvent );
-		state = STATE.NONE;
+			event.preventDefault();
+			event.stopPropagation();
 
-	}
+			var delta = 0;
 
-	function onMouseWheel( event ) {
+			if ( event.wheelDelta !== undefined ) { // WebKit / Opera / Explorer 9
 
-		if ( scope.enabled === false || scope.noZoom === true || state !== STATE.NONE ) return;
+				delta = event.wheelDelta;
 
-		event.preventDefault();
-		event.stopPropagation();
+			} else if ( event.detail !== undefined ) { // Firefox
 
-		var delta = 0;
+				delta = - event.detail;
 
-		if ( event.wheelDelta !== undefined ) { // WebKit / Opera / Explorer 9
+			}
+
+			if ( delta > 0 ) {
 
-			delta = event.wheelDelta;
+				constraint.dollyOut( getZoomScale() );
 
-		} else if ( event.detail !== undefined ) { // Firefox
+			} else if ( delta < 0 ) {
 
-			delta = - event.detail;
+				constraint.dollyIn( getZoomScale() );
+
+			}
+
+			scope.update();
+			scope.dispatchEvent( startEvent );
+			scope.dispatchEvent( endEvent );
 
 		}
 
-		if ( delta > 0 ) {
+		function onKeyDown( event ) {
+
+			if ( scope.enabled === false || scope.noKeys === true || scope.noPan === true ) return;
+
+			switch ( event.keyCode ) {
 
-			scope.dollyOut();
+				case scope.keys.UP:
+					pan( 0, scope.keyPanSpeed );
+					scope.update();
+					break;
 
-		} else if ( delta < 0 ) {
+				case scope.keys.BOTTOM:
+					pan( 0, - scope.keyPanSpeed );
+					scope.update();
+					break;
 
-			scope.dollyIn();
+				case scope.keys.LEFT:
+					pan( scope.keyPanSpeed, 0 );
+					scope.update();
+					break;
+
+				case scope.keys.RIGHT:
+					pan( - scope.keyPanSpeed, 0 );
+					scope.update();
+					break;
+
+			}
 
 		}
 
-		scope.update();
-		scope.dispatchEvent( startEvent );
-		scope.dispatchEvent( endEvent );
+		function touchstart( event ) {
+
+			if ( scope.enabled === false ) return;
+
+			switch ( event.touches.length ) {
+
+				case 1:	// one-fingered touch: rotate
+
+					if ( scope.noRotate === true ) return;
+
+					state = STATE.TOUCH_ROTATE;
+
+					rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
+					break;
+
+				case 2:	// two-fingered touch: dolly
+
+					if ( scope.noZoom === true ) return;
+
+					state = STATE.TOUCH_DOLLY;
 
-	}
+					var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
+					var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
+					var distance = Math.sqrt( dx * dx + dy * dy );
+					dollyStart.set( 0, distance );
+					break;
 
-	function onKeyDown( event ) {
+				case 3: // three-fingered touch: pan
 
-		if ( scope.enabled === false || scope.noKeys === true || scope.noPan === true ) return;
+					if ( scope.noPan === true ) return;
 
-		switch ( event.keyCode ) {
+					state = STATE.TOUCH_PAN;
 
-			case scope.keys.UP:
-				scope.pan( 0, scope.keyPanSpeed );
-				scope.update();
-				break;
+					panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
+					break;
 
-			case scope.keys.BOTTOM:
-				scope.pan( 0, - scope.keyPanSpeed );
-				scope.update();
-				break;
+				default:
 
-			case scope.keys.LEFT:
-				scope.pan( scope.keyPanSpeed, 0 );
-				scope.update();
-				break;
+					state = STATE.NONE;
 
-			case scope.keys.RIGHT:
-				scope.pan( - scope.keyPanSpeed, 0 );
-				scope.update();
-				break;
+			}
+
+			if ( state !== STATE.NONE ) scope.dispatchEvent( startEvent );
 
 		}
 
-	}
+		function touchmove( event ) {
 
-	function touchstart( event ) {
+			if ( scope.enabled === false ) return;
 
-		if ( scope.enabled === false ) return;
+			event.preventDefault();
+			event.stopPropagation();
 
-		switch ( event.touches.length ) {
+			var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
 
-			case 1:	// one-fingered touch: rotate
+			switch ( event.touches.length ) {
 
-				if ( scope.noRotate === true ) return;
+				case 1: // one-fingered touch: rotate
 
-				state = STATE.TOUCH_ROTATE;
+					if ( scope.noRotate === true ) return;
+					if ( state !== STATE.TOUCH_ROTATE ) return;
 
-				rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
-				break;
+					rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
+					rotateDelta.subVectors( rotateEnd, rotateStart );
 
-			case 2:	// two-fingered touch: dolly
+					// rotating across whole screen goes 360 degrees around
+					constraint.rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
+					// rotating up and down along whole screen attempts to go 360, but limited to 180
+					constraint.rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
 
-				if ( scope.noZoom === true ) return;
+					rotateStart.copy( rotateEnd );
 
-				state = STATE.TOUCH_DOLLY;
+					scope.update();
+					break;
 
-				var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
-				var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
-				var distance = Math.sqrt( dx * dx + dy * dy );
-				dollyStart.set( 0, distance );
-				break;
+				case 2: // two-fingered touch: dolly
 
-			case 3: // three-fingered touch: pan
+					if ( scope.noZoom === true ) return;
+					if ( state !== STATE.TOUCH_DOLLY ) return;
 
-				if ( scope.noPan === true ) return;
+					var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
+					var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
+					var distance = Math.sqrt( dx * dx + dy * dy );
+
+					dollyEnd.set( 0, distance );
+					dollyDelta.subVectors( dollyEnd, dollyStart );
+
+					if ( dollyDelta.y > 0 ) {
+
+						constraint.dollyOut( getZoomScale() );
+
+					} else if ( dollyDelta.y < 0 ) {
+
+						constraint.dollyIn( getZoomScale() );
+
+					}
+
+					dollyStart.copy( dollyEnd );
 
-				state = STATE.TOUCH_PAN;
+					scope.update();
+					break;
 
-				panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
-				break;
+				case 3: // three-fingered touch: pan
 
-			default:
+					if ( scope.noPan === true ) return;
+					if ( state !== STATE.TOUCH_PAN ) return;
 
-				state = STATE.NONE;
+					panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
+					panDelta.subVectors( panEnd, panStart );
+
+					pan( panDelta.x, panDelta.y );
+
+					panStart.copy( panEnd );
+
+					scope.update();
+					break;
+
+				default:
+
+					state = STATE.NONE;
+
+			}
 
 		}
 
-		if ( state !== STATE.NONE ) scope.dispatchEvent( startEvent );
+		function touchend( /* event */ ) {
 
-	}
+			if ( scope.enabled === false ) return;
 
-	function touchmove( event ) {
+			scope.dispatchEvent( endEvent );
+			state = STATE.NONE;
 
-		if ( scope.enabled === false ) return;
+		}
 
-		event.preventDefault();
-		event.stopPropagation();
+		this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
+		this.domElement.addEventListener( 'mousedown', onMouseDown, false );
+		this.domElement.addEventListener( 'mousewheel', onMouseWheel, false );
+		this.domElement.addEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox
 
-		var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
+		this.domElement.addEventListener( 'touchstart', touchstart, false );
+		this.domElement.addEventListener( 'touchend', touchend, false );
+		this.domElement.addEventListener( 'touchmove', touchmove, false );
 
-		switch ( event.touches.length ) {
+		window.addEventListener( 'keydown', onKeyDown, false );
 
-			case 1: // one-fingered touch: rotate
+		// force an update at start
+		this.update();
 
-				if ( scope.noRotate === true ) return;
-				if ( state !== STATE.TOUCH_ROTATE ) return;
+	};
 
-				rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
-				rotateDelta.subVectors( rotateEnd, rotateStart );
+	THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );
+	THREE.OrbitControls.prototype.constructor = THREE.OrbitControls;
 
-				// rotating across whole screen goes 360 degrees around
-				scope.rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
-				// rotating up and down along whole screen attempts to go 360, but limited to 180
-				scope.rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
+	Object.defineProperties( THREE.OrbitControls.prototype, {
 
-				rotateStart.copy( rotateEnd );
+		object: {
 
-				scope.update();
-				break;
+			get: function () {
 
-			case 2: // two-fingered touch: dolly
+				return this.constraint.object;
+			}
 
-				if ( scope.noZoom === true ) return;
-				if ( state !== STATE.TOUCH_DOLLY ) return;
+		},
 
-				var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
-				var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
-				var distance = Math.sqrt( dx * dx + dy * dy );
+		target: {
 
-				dollyEnd.set( 0, distance );
-				dollyDelta.subVectors( dollyEnd, dollyStart );
+			get: function () {
 
-				if ( dollyDelta.y > 0 ) {
+				return this.constraint.target;
 
-					scope.dollyOut();
+			}
 
-				} else if ( dollyDelta.y < 0 ) {
+		},
 
-					scope.dollyIn();
+		minDistance : {
 
-				}
+			get: function () {
 
-				dollyStart.copy( dollyEnd );
+				return this.constraint.minDistance;
 
-				scope.update();
-				break;
+			},
 
-			case 3: // three-fingered touch: pan
+			set: function ( value ) {
 
-				if ( scope.noPan === true ) return;
-				if ( state !== STATE.TOUCH_PAN ) return;
+				this.constraint.minDistance = value;
 
-				panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
-				panDelta.subVectors( panEnd, panStart );
+			},
 
-				scope.pan( panDelta.x, panDelta.y );
+		},
 
-				panStart.copy( panEnd );
+		maxDistance : {
 
-				scope.update();
-				break;
+			get: function () {
 
-			default:
+				return this.constraint.maxDistance;
 
-				state = STATE.NONE;
+			},
 
-		}
+			set: function ( value ) {
+
+				this.constraint.maxDistance = value;
+
+			},
+
+		},
+
+		minZoom : {
+
+			get: function () {
+
+				return this.constraint.minZoom;
+
+			},
+
+			set: function ( value ) {
+
+				this.constraint.minZoom = value;
+
+			},
+
+		},
+
+		maxZoom : {
+
+			get: function () {
+
+				return this.constraint.maxZoom;
+
+			},
+
+			set: function ( value ) {
+
+				this.constraint.maxZoom = value;
+
+			},
+
+		},
+
+		minPolarAngle : {
+
+			get: function () {
 
-	}
+				return this.constraint.minPolarAngle;
 
-	function touchend( /* event */ ) {
+			},
 
-		if ( scope.enabled === false ) return;
+			set: function ( value ) {
 
-		scope.dispatchEvent( endEvent );
-		state = STATE.NONE;
+				this.constraint.minPolarAngle = value;
 
-	}
+			},
 
-	this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
-	this.domElement.addEventListener( 'mousedown', onMouseDown, false );
-	this.domElement.addEventListener( 'mousewheel', onMouseWheel, false );
-	this.domElement.addEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox
+		},
 
-	this.domElement.addEventListener( 'touchstart', touchstart, false );
-	this.domElement.addEventListener( 'touchend', touchend, false );
-	this.domElement.addEventListener( 'touchmove', touchmove, false );
+		maxPolarAngle : {
 
-	window.addEventListener( 'keydown', onKeyDown, false );
+			get: function () {
 
-	// force an update at start
-	this.update();
+				return this.constraint.maxPolarAngle;
+
+			},
+
+			set: function ( value ) {
+
+				this.constraint.maxPolarAngle = value;
+
+			},
+
+		},
+
+		minAzimuthAngle : {
+
+			get: function () {
+
+				return this.constraint.minAzimuthAngle;
+
+			},
+
+			set: function ( value ) {
+
+				this.constraint.minAzimuthAngle = value;
+
+			},
+
+		},
+
+		maxAzimuthAngle : {
+
+			get: function () {
+
+				return this.constraint.maxAzimuthAngle;
+
+			},
+
+			set: function ( value ) {
+
+				this.constraint.maxAzimuthAngle = value;
+
+			}
+
+		}
 
-};
+	});
 
-THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );
-THREE.OrbitControls.prototype.constructor = THREE.OrbitControls;
+}());