فهرست منبع

TrackballControls: Derive from `Controls`. (#29124)

* TrackballControls: Derive from `Controls`.

* TrackballControls: Clean up.
Michael Herzog 1 سال پیش
والد
کامیت
649b99272b
2فایلهای تغییر یافته به همراه457 افزوده شده و 498 حذف شده
  1. 4 46
      docs/examples/en/controls/TrackballControls.html
  2. 453 452
      examples/jsm/controls/TrackballControls.js

+ 4 - 46
docs/examples/en/controls/TrackballControls.html

@@ -7,7 +7,7 @@
 		<link type="text/css" rel="stylesheet" href="page.css" />
 	</head>
 	<body>
-		[page:EventDispatcher] &rarr;
+		[page:Controls] &rarr;
 
 		<h1>[name]</h1>
 
@@ -41,7 +41,7 @@
 				[page:Camera camera]: The camera of the rendered scene.
 			</p>
 			<p>
-				[page:HTMLDOMElement domElement]: The HTML element used for event listeners.
+				[page:HTMLDOMElement domElement]: The HTML element used for event listeners. (optional)
 			</p>
 			<p>
 				Creates a new instance of [name].
@@ -67,22 +67,13 @@
 
 		<h2>Properties</h2>
 
-		<h3>[property:HTMLDOMElement domElement]</h3>
-		<p>
-			The HTMLDOMElement used to listen for mouse / touch events. This must be passed in the constructor; changing it here will
-			not set up new event listeners.
-		</p>
+		<p>See the base [page:Controls] class for common properties.</p>
 
 		<h3>[property:Number dynamicDampingFactor]</h3>
 		<p>
 			Defines the intensity of damping. Only considered if [page:.staticMoving staticMoving] is set to `false`. Default is `0.2`.
 		</p>
 
-		<h3>[property:Boolean enabled]</h3>
-		<p>
-			Whether or not the controls are enabled.
-		</p>
-
 		<h3>[property:Array keys]</h3>
 	
 			This array holds keycodes for controlling interactions.
@@ -140,11 +131,6 @@
 			Whether or not zooming is disabled. Default is `false`.
 		</p>
 
-		<h3>[property:Camera object]</h3>
-		<p>
-			The camera being controlled.
-		</p>
-
 		<h3>[property:Number panSpeed]</h3>
 		<p>
 			The pan speed. Default is `0.3`.
@@ -183,46 +169,18 @@
 
 		<h2>Methods</h2>
 
-		<h3>[method:undefined checkDistances] ()</h3>
-		<p>
-			Ensures the controls stay in the range [minDistance, maxDistance]. Called by [page:.update update]().
-		</p>
-
-		<h3>[method:undefined dispose] ()</h3>
-		<p>
-			Should be called if the controls is no longer required.
-		</p>
+		<p>See the base [page:Controls] class for common methods.</p>
 
 		<h3>[method:undefined handleResize] ()</h3>
 		<p>
 			Should be called if the application window is resized.
 		</p>
 
-		<h3>[method:undefined panCamera] ()</h3>
-		<p>
-			Performs panning if necessary. Called by [page:.update update]().
-		</p>
-
 		<h3>[method:undefined reset] ()</h3>
 		<p>
 			Resets the controls to its initial state.
 		</p>
 
-		<h3>[method:undefined rotateCamera] ()</h3>
-		<p>
-			Rotates the camera if necessary. Called by [page:.update update]().
-		</p>
-
-		<h3>[method:undefined update] ()</h3>
-		<p>
-			Updates the controls. Usually called in the animation loop.
-		</p>
-
-		<h3>[method:undefined zoomCamera] ()</h3>
-		<p>
-			Performs zooming if necessary. Called by [page:.update update]().
-		</p>
-
 		<h2>Source</h2>
 
 		<p>

+ 453 - 452
examples/jsm/controls/TrackballControls.js

@@ -1,28 +1,35 @@
 import {
-	EventDispatcher,
 	MathUtils,
 	MOUSE,
 	Quaternion,
 	Vector2,
 	Vector3
 } from 'three';
+import { Controls } from './Controls.js';
 
 const _changeEvent = { type: 'change' };
 const _startEvent = { type: 'start' };
 const _endEvent = { type: 'end' };
 
-class TrackballControls extends EventDispatcher {
+const _EPS = 0.000001;
+const _STATE = { NONE: - 1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4 };
 
-	constructor( object, domElement ) {
+const _v2 = new Vector2();
+const _mouseChange = new Vector2();
+const _objectUp = new Vector3();
+const _pan = new Vector3();
+const _axis = new Vector3();
+const _quaternion = new Quaternion();
+const _eyeDirection = new Vector3();
+const _objectUpDirection = new Vector3();
+const _objectSidewaysDirection = new Vector3();
+const _moveDirection = new Vector3();
 
-		super();
+class TrackballControls extends Controls {
 
-		const scope = this;
-		const STATE = { NONE: - 1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4 };
+	constructor( object, domElement = null ) {
 
-		this.object = object;
-		this.domElement = domElement;
-		this.domElement.style.touchAction = 'none'; // disable touch scroll
+		super( object, domElement );
 
 		// API
 
@@ -50,779 +57,773 @@ class TrackballControls extends EventDispatcher {
 		this.keys = [ 'KeyA' /*A*/, 'KeyS' /*S*/, 'KeyD' /*D*/ ];
 
 		this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
-
-		// internals
+		this.state = _STATE.NONE;
+		this.keyState = _STATE.NONE;
 
 		this.target = new Vector3();
 
-		const EPS = 0.000001;
-
-		const lastPosition = new Vector3();
-		let lastZoom = 1;
-
-		let _state = STATE.NONE,
-			_keyState = STATE.NONE,
-
-			_touchZoomDistanceStart = 0,
-			_touchZoomDistanceEnd = 0,
+		// internals
 
-			_lastAngle = 0;
+		this._lastPosition = new Vector3();
+		this._lastZoom = 1;
+		this._touchZoomDistanceStart = 0;
+		this._touchZoomDistanceEnd = 0;
+		this._lastAngle = 0;
 
-		const _eye = new Vector3(),
+		this._eye = new Vector3();
 
-			_movePrev = new Vector2(),
-			_moveCurr = new Vector2(),
+		this._movePrev = new Vector2();
+		this._moveCurr = new Vector2();
 
-			_lastAxis = new Vector3(),
+		this._lastAxis = new Vector3();
 
-			_zoomStart = new Vector2(),
-			_zoomEnd = new Vector2(),
+		this._zoomStart = new Vector2();
+		this._zoomEnd = new Vector2();
 
-			_panStart = new Vector2(),
-			_panEnd = new Vector2(),
+		this._panStart = new Vector2();
+		this._panEnd = new Vector2();
 
-			_pointers = [],
-			_pointerPositions = {};
+		this._pointers = [];
+		this._pointerPositions = {};
 
-		// for reset
+		// event listeners
 
-		this.target0 = this.target.clone();
-		this.position0 = this.object.position.clone();
-		this.up0 = this.object.up.clone();
-		this.zoom0 = this.object.zoom;
+		this._onPointerMove = onPointerMove.bind( this );
+		this._onPointerDown = onPointerDown.bind( this );
+		this._onPointerUp = onPointerUp.bind( this );
+		this._onPointerCancel = onPointerCancel.bind( this );
+		this._onContextMenu = onContextMenu.bind( this );
+		this._onMouseWheel = onMouseWheel.bind( this );
+		this._onKeyDown = onKeyDown.bind( this );
+		this._onKeyUp = onKeyUp.bind( this );
 
-		// methods
+		this._onTouchStart = onTouchStart.bind( this );
+		this._onTouchMove = onTouchMove.bind( this );
+		this._onTouchEnd = onTouchEnd.bind( this );
 
-		this.handleResize = function () {
+		this._onMouseDown = onMouseDown.bind( this );
+		this._onMouseMove = onMouseMove.bind( this );
+		this._onMouseUp = onMouseUp.bind( this );
 
-			const box = scope.domElement.getBoundingClientRect();
-			// adjustments come from similar code in the jquery offset() function
-			const d = scope.domElement.ownerDocument.documentElement;
-			scope.screen.left = box.left + window.pageXOffset - d.clientLeft;
-			scope.screen.top = box.top + window.pageYOffset - d.clientTop;
-			scope.screen.width = box.width;
-			scope.screen.height = box.height;
+		// for reset
 
-		};
+		this._target0 = this.target.clone();
+		this._position0 = this.object.position.clone();
+		this._up0 = this.object.up.clone();
+		this._zoom0 = this.object.zoom;
 
-		const getMouseOnScreen = ( function () {
+		if ( domElement !== null ) {
 
-			const vector = new Vector2();
+			this.connect();
 
-			return function getMouseOnScreen( pageX, pageY ) {
+			this.handleResize();
 
-				vector.set(
-					( pageX - scope.screen.left ) / scope.screen.width,
-					( pageY - scope.screen.top ) / scope.screen.height
-				);
+		}
 
-				return vector;
+		// force an update at start
+		this.update();
 
-			};
+	}
 
-		}() );
+	connect() {
 
-		const getMouseOnCircle = ( function () {
+		window.addEventListener( 'keydown', this._onKeyDown );
+		window.addEventListener( 'keyup', this._onKeyUp );
 
-			const vector = new Vector2();
+		this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
+		this.domElement.addEventListener( 'pointercancel', this._onPointerCancel );
+		this.domElement.addEventListener( 'wheel', this._onMouseWheel, { passive: false } );
+		this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
 
-			return function getMouseOnCircle( pageX, pageY ) {
+		this.domElement.style.touchAction = 'none'; // disable touch scroll
 
-				vector.set(
-					( ( pageX - scope.screen.width * 0.5 - scope.screen.left ) / ( scope.screen.width * 0.5 ) ),
-					( ( scope.screen.height + 2 * ( scope.screen.top - pageY ) ) / scope.screen.width ) // screen.width intentional
-				);
+	}
 
-				return vector;
+	disconnect() {
 
-			};
+		window.removeEventListener( 'keydown', this._onKeyDown );
+		window.removeEventListener( 'keyup', this._onKeyUp );
 
-		}() );
+		this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
+		this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
+		this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
+		this.domElement.removeEventListener( 'pointercancel', this._onPointerCancel );
+		this.domElement.removeEventListener( 'wheel', this._onMouseWheel );
+		this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
 
-		this.rotateCamera = ( function () {
+		this.domElement.style.touchAction = 'auto'; // disable touch scroll
 
-			const axis = new Vector3(),
-				quaternion = new Quaternion(),
-				eyeDirection = new Vector3(),
-				objectUpDirection = new Vector3(),
-				objectSidewaysDirection = new Vector3(),
-				moveDirection = new Vector3();
+	}
 
-			return function rotateCamera() {
+	dispose() {
 
-				moveDirection.set( _moveCurr.x - _movePrev.x, _moveCurr.y - _movePrev.y, 0 );
-				let angle = moveDirection.length();
+		this.disconnect();
 
-				if ( angle ) {
+	}
 
-					_eye.copy( scope.object.position ).sub( scope.target );
+	handleResize() {
 
-					eyeDirection.copy( _eye ).normalize();
-					objectUpDirection.copy( scope.object.up ).normalize();
-					objectSidewaysDirection.crossVectors( objectUpDirection, eyeDirection ).normalize();
+		const box = this.domElement.getBoundingClientRect();
+		// adjustments come from similar code in the jquery offset() function
+		const d = this.domElement.ownerDocument.documentElement;
 
-					objectUpDirection.setLength( _moveCurr.y - _movePrev.y );
-					objectSidewaysDirection.setLength( _moveCurr.x - _movePrev.x );
+		this.screen.left = box.left + window.pageXOffset - d.clientLeft;
+		this.screen.top = box.top + window.pageYOffset - d.clientTop;
+		this.screen.width = box.width;
+		this.screen.height = box.height;
 
-					moveDirection.copy( objectUpDirection.add( objectSidewaysDirection ) );
+	}
 
-					axis.crossVectors( moveDirection, _eye ).normalize();
+	update() {
 
-					angle *= scope.rotateSpeed;
-					quaternion.setFromAxisAngle( axis, angle );
+		this._eye.subVectors( this.object.position, this.target );
 
-					_eye.applyQuaternion( quaternion );
-					scope.object.up.applyQuaternion( quaternion );
+		if ( ! this.noRotate ) {
 
-					_lastAxis.copy( axis );
-					_lastAngle = angle;
+			this._rotateCamera();
 
-				} else if ( ! scope.staticMoving && _lastAngle ) {
+		}
 
-					_lastAngle *= Math.sqrt( 1.0 - scope.dynamicDampingFactor );
-					_eye.copy( scope.object.position ).sub( scope.target );
-					quaternion.setFromAxisAngle( _lastAxis, _lastAngle );
-					_eye.applyQuaternion( quaternion );
-					scope.object.up.applyQuaternion( quaternion );
+		if ( ! this.noZoom ) {
 
-				}
+			this._zoomCamera();
 
-				_movePrev.copy( _moveCurr );
+		}
 
-			};
+		if ( ! this.noPan ) {
 
-		}() );
+			this._panCamera();
 
+		}
 
-		this.zoomCamera = function () {
+		this.object.position.addVectors( this.target, this._eye );
 
-			let factor;
+		if ( this.object.isPerspectiveCamera ) {
 
-			if ( _state === STATE.TOUCH_ZOOM_PAN ) {
+			this._checkDistances();
 
-				factor = _touchZoomDistanceStart / _touchZoomDistanceEnd;
-				_touchZoomDistanceStart = _touchZoomDistanceEnd;
+			this.object.lookAt( this.target );
 
-				if ( scope.object.isPerspectiveCamera ) {
+			if ( this._lastPosition.distanceToSquared( this.object.position ) > _EPS ) {
 
-					_eye.multiplyScalar( factor );
+				this.dispatchEvent( _changeEvent );
 
-				} else if ( scope.object.isOrthographicCamera ) {
+				this._lastPosition.copy( this.object.position );
 
-					scope.object.zoom = MathUtils.clamp( scope.object.zoom / factor, scope.minZoom, scope.maxZoom );
+			}
 
-					if ( lastZoom !== scope.object.zoom ) {
+		} else if ( this.object.isOrthographicCamera ) {
 
-						scope.object.updateProjectionMatrix();
+			this.object.lookAt( this.target );
 
-					}
+			if ( this._lastPosition.distanceToSquared( this.object.position ) > _EPS || this._lastZoom !== this.object.zoom ) {
 
-				} else {
+				this.dispatchEvent( _changeEvent );
 
-					console.warn( 'THREE.TrackballControls: Unsupported camera type' );
+				this._lastPosition.copy( this.object.position );
+				this._lastZoom = this.object.zoom;
 
-				}
+			}
 
-			} else {
+		} else {
 
-				factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * scope.zoomSpeed;
+			console.warn( 'THREE.TrackballControls: Unsupported camera type.' );
 
-				if ( factor !== 1.0 && factor > 0.0 ) {
+		}
 
-					if ( scope.object.isPerspectiveCamera ) {
+	}
 
-						_eye.multiplyScalar( factor );
+	reset() {
 
-					} else if ( scope.object.isOrthographicCamera ) {
+		this.state = _STATE.NONE;
+		this.keyState = _STATE.NONE;
 
-						scope.object.zoom = MathUtils.clamp( scope.object.zoom / factor, scope.minZoom, scope.maxZoom );
+		this.target.copy( this._target0 );
+		this.object.position.copy( this._position0 );
+		this.object.up.copy( this._up0 );
+		this.object.zoom = this._zoom0;
 
-						if ( lastZoom !== scope.object.zoom ) {
+		this.object.updateProjectionMatrix();
 
-							scope.object.updateProjectionMatrix();
+		this._eye.subVectors( this.object.position, this.target );
 
-						}
+		this.object.lookAt( this.target );
 
-					} else {
+		this.dispatchEvent( _changeEvent );
 
-						console.warn( 'THREE.TrackballControls: Unsupported camera type' );
+		this._lastPosition.copy( this.object.position );
+		this._lastZoom = this.object.zoom;
 
-					}
+	}
 
-				}
+	_panCamera() {
 
-				if ( scope.staticMoving ) {
+		_mouseChange.copy( this._panEnd ).sub( this._panStart );
 
-					_zoomStart.copy( _zoomEnd );
+		if ( _mouseChange.lengthSq() ) {
 
-				} else {
+			if ( this.object.isOrthographicCamera ) {
 
-					_zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
+				const scale_x = ( this.object.right - this.object.left ) / this.object.zoom / this.domElement.clientWidth;
+				const scale_y = ( this.object.top - this.object.bottom ) / this.object.zoom / this.domElement.clientWidth;
 
-				}
+				_mouseChange.x *= scale_x;
+				_mouseChange.y *= scale_y;
 
 			}
 
-		};
-
-		this.panCamera = ( function () {
-
-			const mouseChange = new Vector2(),
-				objectUp = new Vector3(),
-				pan = new Vector3();
+			_mouseChange.multiplyScalar( this._eye.length() * this.panSpeed );
 
-			return function panCamera() {
+			_pan.copy( this._eye ).cross( this.object.up ).setLength( _mouseChange.x );
+			_pan.add( _objectUp.copy( this.object.up ).setLength( _mouseChange.y ) );
 
-				mouseChange.copy( _panEnd ).sub( _panStart );
+			this.object.position.add( _pan );
+			this.target.add( _pan );
 
-				if ( mouseChange.lengthSq() ) {
+			if ( this.staticMoving ) {
 
-					if ( scope.object.isOrthographicCamera ) {
+				this._panStart.copy( this._panEnd );
 
-						const scale_x = ( scope.object.right - scope.object.left ) / scope.object.zoom / scope.domElement.clientWidth;
-						const scale_y = ( scope.object.top - scope.object.bottom ) / scope.object.zoom / scope.domElement.clientWidth;
+			} else {
 
-						mouseChange.x *= scale_x;
-						mouseChange.y *= scale_y;
+				this._panStart.add( _mouseChange.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.dynamicDampingFactor ) );
 
-					}
+			}
 
-					mouseChange.multiplyScalar( _eye.length() * scope.panSpeed );
+		}
 
-					pan.copy( _eye ).cross( scope.object.up ).setLength( mouseChange.x );
-					pan.add( objectUp.copy( scope.object.up ).setLength( mouseChange.y ) );
+	}
 
-					scope.object.position.add( pan );
-					scope.target.add( pan );
+	_rotateCamera() {
 
-					if ( scope.staticMoving ) {
+		_moveDirection.set( this._moveCurr.x - this._movePrev.x, this._moveCurr.y - this._movePrev.y, 0 );
+		let angle = _moveDirection.length();
 
-						_panStart.copy( _panEnd );
+		if ( angle ) {
 
-					} else {
+			this._eye.copy( this.object.position ).sub( this.target );
 
-						_panStart.add( mouseChange.subVectors( _panEnd, _panStart ).multiplyScalar( scope.dynamicDampingFactor ) );
+			_eyeDirection.copy( this._eye ).normalize();
+			_objectUpDirection.copy( this.object.up ).normalize();
+			_objectSidewaysDirection.crossVectors( _objectUpDirection, _eyeDirection ).normalize();
 
-					}
+			_objectUpDirection.setLength( this._moveCurr.y - this._movePrev.y );
+			_objectSidewaysDirection.setLength( this._moveCurr.x - this._movePrev.x );
 
-				}
+			_moveDirection.copy( _objectUpDirection.add( _objectSidewaysDirection ) );
 
-			};
+			_axis.crossVectors( _moveDirection, this._eye ).normalize();
 
-		}() );
+			angle *= this.rotateSpeed;
+			_quaternion.setFromAxisAngle( _axis, angle );
 
-		this.checkDistances = function () {
+			this._eye.applyQuaternion( _quaternion );
+			this.object.up.applyQuaternion( _quaternion );
 
-			if ( ! scope.noZoom || ! scope.noPan ) {
+			this._lastAxis.copy( _axis );
+			this._lastAngle = angle;
 
-				if ( _eye.lengthSq() > scope.maxDistance * scope.maxDistance ) {
+		} else if ( ! this.staticMoving && this._lastAngle ) {
 
-					scope.object.position.addVectors( scope.target, _eye.setLength( scope.maxDistance ) );
-					_zoomStart.copy( _zoomEnd );
+			this._lastAngle *= Math.sqrt( 1.0 - this.dynamicDampingFactor );
+			this._eye.copy( this.object.position ).sub( this.target );
+			_quaternion.setFromAxisAngle( this._lastAxis, this._lastAngle );
+			this._eye.applyQuaternion( _quaternion );
+			this.object.up.applyQuaternion( _quaternion );
 
-				}
+		}
 
-				if ( _eye.lengthSq() < scope.minDistance * scope.minDistance ) {
+		this._movePrev.copy( this._moveCurr );
 
-					scope.object.position.addVectors( scope.target, _eye.setLength( scope.minDistance ) );
-					_zoomStart.copy( _zoomEnd );
+	}
 
-				}
+	_zoomCamera() {
 
-			}
+		let factor;
 
-		};
+		if ( this.state === _STATE.TOUCH_ZOOM_PAN ) {
 
-		this.update = function () {
+			factor = this._touchZoomDistanceStart / this._touchZoomDistanceEnd;
+			this._touchZoomDistanceStart = this._touchZoomDistanceEnd;
 
-			_eye.subVectors( scope.object.position, scope.target );
+			if ( this.object.isPerspectiveCamera ) {
 
-			if ( ! scope.noRotate ) {
+				this._eye.multiplyScalar( factor );
 
-				scope.rotateCamera();
+			} else if ( this.object.isOrthographicCamera ) {
 
-			}
+				this.object.zoom = MathUtils.clamp( this.object.zoom / factor, this.minZoom, this.maxZoom );
 
-			if ( ! scope.noZoom ) {
+				if ( this._lastZoom !== this.object.zoom ) {
 
-				scope.zoomCamera();
+					this.object.updateProjectionMatrix();
 
-			}
+				}
 
-			if ( ! scope.noPan ) {
+			} else {
 
-				scope.panCamera();
+				console.warn( 'THREE.TrackballControls: Unsupported camera type' );
 
 			}
 
-			scope.object.position.addVectors( scope.target, _eye );
+		} else {
 
-			if ( scope.object.isPerspectiveCamera ) {
+			factor = 1.0 + ( this._zoomEnd.y - this._zoomStart.y ) * this.zoomSpeed;
 
-				scope.checkDistances();
+			if ( factor !== 1.0 && factor > 0.0 ) {
 
-				scope.object.lookAt( scope.target );
+				if ( this.object.isPerspectiveCamera ) {
 
-				if ( lastPosition.distanceToSquared( scope.object.position ) > EPS ) {
+					this._eye.multiplyScalar( factor );
 
-					scope.dispatchEvent( _changeEvent );
+				} else if ( this.object.isOrthographicCamera ) {
 
-					lastPosition.copy( scope.object.position );
+					this.object.zoom = MathUtils.clamp( this.object.zoom / factor, this.minZoom, this.maxZoom );
 
-				}
-
-			} else if ( scope.object.isOrthographicCamera ) {
+					if ( this._lastZoom !== this.object.zoom ) {
 
-				scope.object.lookAt( scope.target );
+						this.object.updateProjectionMatrix();
 
-				if ( lastPosition.distanceToSquared( scope.object.position ) > EPS || lastZoom !== scope.object.zoom ) {
+					}
 
-					scope.dispatchEvent( _changeEvent );
+				} else {
 
-					lastPosition.copy( scope.object.position );
-					lastZoom = scope.object.zoom;
+					console.warn( 'THREE.TrackballControls: Unsupported camera type' );
 
 				}
 
-			} else {
-
-				console.warn( 'THREE.TrackballControls: Unsupported camera type' );
-
 			}
 
-		};
+			if ( this.staticMoving ) {
 
-		this.reset = function () {
+				this._zoomStart.copy( this._zoomEnd );
 
-			_state = STATE.NONE;
-			_keyState = STATE.NONE;
+			} else {
 
-			scope.target.copy( scope.target0 );
-			scope.object.position.copy( scope.position0 );
-			scope.object.up.copy( scope.up0 );
-			scope.object.zoom = scope.zoom0;
+				this._zoomStart.y += ( this._zoomEnd.y - this._zoomStart.y ) * this.dynamicDampingFactor;
 
-			scope.object.updateProjectionMatrix();
+			}
 
-			_eye.subVectors( scope.object.position, scope.target );
+		}
 
-			scope.object.lookAt( scope.target );
+	}
 
-			scope.dispatchEvent( _changeEvent );
+	_getMouseOnScreen( pageX, pageY ) {
 
-			lastPosition.copy( scope.object.position );
-			lastZoom = scope.object.zoom;
+		_v2.set(
+			( pageX - this.screen.left ) / this.screen.width,
+			( pageY - this.screen.top ) / this.screen.height
+		);
 
-		};
+		return _v2;
 
-		// listeners
+	}
 
-		function onPointerDown( event ) {
+	_getMouseOnCircle( pageX, pageY ) {
 
-			if ( scope.enabled === false ) return;
+		_v2.set(
+			( ( pageX - this.screen.width * 0.5 - this.screen.left ) / ( this.screen.width * 0.5 ) ),
+			( ( this.screen.height + 2 * ( this.screen.top - pageY ) ) / this.screen.width ) // screen.width intentional
+		);
 
-			if ( _pointers.length === 0 ) {
+		return _v2;
 
-				scope.domElement.setPointerCapture( event.pointerId );
+	}
 
-				scope.domElement.addEventListener( 'pointermove', onPointerMove );
-				scope.domElement.addEventListener( 'pointerup', onPointerUp );
+	_addPointer( event ) {
 
-			}
+		this._pointers.push( event );
 
-			//
+	}
 
-			addPointer( event );
+	_removePointer( event ) {
 
-			if ( event.pointerType === 'touch' ) {
+		delete this._pointerPositions[ event.pointerId ];
 
-				onTouchStart( event );
+		for ( let i = 0; i < this._pointers.length; i ++ ) {
 
-			} else {
+			if ( this._pointers[ i ].pointerId == event.pointerId ) {
 
-				onMouseDown( event );
+				this._pointers.splice( i, 1 );
+				return;
 
 			}
 
 		}
 
-		function onPointerMove( event ) {
-
-			if ( scope.enabled === false ) return;
+	}
 
-			if ( event.pointerType === 'touch' ) {
+	_trackPointer( event ) {
 
-				onTouchMove( event );
+		let position = this._pointerPositions[ event.pointerId ];
 
-			} else {
+		if ( position === undefined ) {
 
-				onMouseMove( event );
-
-			}
+			position = new Vector2();
+			this._pointerPositions[ event.pointerId ] = position;
 
 		}
 
-		function onPointerUp( event ) {
+		position.set( event.pageX, event.pageY );
+
+	}
 
-			if ( scope.enabled === false ) return;
+	_getSecondPointerPosition( event ) {
 
-			if ( event.pointerType === 'touch' ) {
+		const pointer = ( event.pointerId === this._pointers[ 0 ].pointerId ) ? this._pointers[ 1 ] : this._pointers[ 0 ];
 
-				onTouchEnd( event );
+		return this._pointerPositions[ pointer.pointerId ];
 
-			} else {
+	}
 
-				onMouseUp();
+	_checkDistances() {
 
-			}
+		if ( ! this.noZoom || ! this.noPan ) {
 
-			//
+			if ( this._eye.lengthSq() > this.maxDistance * this.maxDistance ) {
 
-			removePointer( event );
+				this.object.position.addVectors( this.target, this._eye.setLength( this.maxDistance ) );
+				this._zoomStart.copy( this._zoomEnd );
 
-			if ( _pointers.length === 0 ) {
+			}
 
-				scope.domElement.releasePointerCapture( event.pointerId );
+			if ( this._eye.lengthSq() < this.minDistance * this.minDistance ) {
 
-				scope.domElement.removeEventListener( 'pointermove', onPointerMove );
-				scope.domElement.removeEventListener( 'pointerup', onPointerUp );
+				this.object.position.addVectors( this.target, this._eye.setLength( this.minDistance ) );
+				this._zoomStart.copy( this._zoomEnd );
 
 			}
 
-
 		}
 
-		function onPointerCancel( event ) {
+	}
 
-			removePointer( event );
+}
 
-		}
+function onPointerDown( event ) {
 
-		function keydown( event ) {
+	if ( this.enabled === false ) return;
 
-			if ( scope.enabled === false ) return;
+	if ( this._pointers.length === 0 ) {
 
-			window.removeEventListener( 'keydown', keydown );
+		this.domElement.setPointerCapture( event.pointerId );
 
-			if ( _keyState !== STATE.NONE ) {
+		this.domElement.addEventListener( 'pointermove', this._onPointerMove );
+		this.domElement.addEventListener( 'pointerup', this._onPointerUp );
 
-				return;
+	}
 
-			} else if ( event.code === scope.keys[ STATE.ROTATE ] && ! scope.noRotate ) {
+	//
 
-				_keyState = STATE.ROTATE;
+	this._addPointer( event );
 
-			} else if ( event.code === scope.keys[ STATE.ZOOM ] && ! scope.noZoom ) {
+	if ( event.pointerType === 'touch' ) {
 
-				_keyState = STATE.ZOOM;
+		this._onTouchStart( event );
 
-			} else if ( event.code === scope.keys[ STATE.PAN ] && ! scope.noPan ) {
+	} else {
 
-				_keyState = STATE.PAN;
+		this._onMouseDown( event );
 
-			}
+	}
 
-		}
+}
 
-		function keyup() {
+function onPointerMove( event ) {
 
-			if ( scope.enabled === false ) return;
+	if ( this.enabled === false ) return;
 
-			_keyState = STATE.NONE;
+	if ( event.pointerType === 'touch' ) {
 
-			window.addEventListener( 'keydown', keydown );
+		this._onTouchMove( event );
 
-		}
+	} else {
 
-		function onMouseDown( event ) {
+		this._onMouseMove( event );
 
-			if ( _state === STATE.NONE ) {
+	}
 
-				switch ( event.button ) {
+}
 
-					case scope.mouseButtons.LEFT:
-						_state = STATE.ROTATE;
-						break;
+function onPointerUp( event ) {
 
-					case scope.mouseButtons.MIDDLE:
-						_state = STATE.ZOOM;
-						break;
+	if ( this.enabled === false ) return;
 
-					case scope.mouseButtons.RIGHT:
-						_state = STATE.PAN;
-						break;
+	if ( event.pointerType === 'touch' ) {
 
-				}
+		this._onTouchEnd( event );
 
-			}
+	} else {
 
-			const state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
+		this._onMouseUp();
 
-			if ( state === STATE.ROTATE && ! scope.noRotate ) {
+	}
 
-				_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
-				_movePrev.copy( _moveCurr );
+	//
 
-			} else if ( state === STATE.ZOOM && ! scope.noZoom ) {
+	this._removePointer( event );
 
-				_zoomStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
-				_zoomEnd.copy( _zoomStart );
+	if ( this._pointers.length === 0 ) {
 
-			} else if ( state === STATE.PAN && ! scope.noPan ) {
+		this.domElement.releasePointerCapture( event.pointerId );
 
-				_panStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
-				_panEnd.copy( _panStart );
+		this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
+		this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
 
-			}
+	}
 
-			scope.dispatchEvent( _startEvent );
+}
 
-		}
+function onPointerCancel( event ) {
 
-		function onMouseMove( event ) {
+	this._removePointer( event );
 
-			const state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
+}
 
-			if ( state === STATE.ROTATE && ! scope.noRotate ) {
+function onKeyUp() {
 
-				_movePrev.copy( _moveCurr );
-				_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
+	if ( this.enabled === false ) return;
 
-			} else if ( state === STATE.ZOOM && ! scope.noZoom ) {
+	this.keyState = _STATE.NONE;
 
-				_zoomEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
+	window.addEventListener( 'keydown', this._onKeyDown );
 
-			} else if ( state === STATE.PAN && ! scope.noPan ) {
+}
 
-				_panEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
+function onKeyDown( event ) {
 
-			}
+	if ( this.enabled === false ) return;
 
-		}
+	window.removeEventListener( 'keydown', this._onKeyDown );
 
-		function onMouseUp() {
+	if ( this.keyState !== _STATE.NONE ) {
 
-			_state = STATE.NONE;
+		return;
 
-			scope.dispatchEvent( _endEvent );
+	} else if ( event.code === this.keys[ _STATE.ROTATE ] && ! this.noRotate ) {
 
-		}
+		this.keyState = _STATE.ROTATE;
 
-		function onMouseWheel( event ) {
+	} else if ( event.code === this.keys[ _STATE.ZOOM ] && ! this.noZoom ) {
 
-			if ( scope.enabled === false ) return;
+		this.keyState = _STATE.ZOOM;
 
-			if ( scope.noZoom === true ) return;
+	} else if ( event.code === this.keys[ _STATE.PAN ] && ! this.noPan ) {
 
-			event.preventDefault();
+		this.keyState = _STATE.PAN;
 
-			switch ( event.deltaMode ) {
+	}
 
-				case 2:
-					// Zoom in pages
-					_zoomStart.y -= event.deltaY * 0.025;
-					break;
+}
 
-				case 1:
-					// Zoom in lines
-					_zoomStart.y -= event.deltaY * 0.01;
-					break;
+function onMouseDown( event ) {
 
-				default:
-					// undefined, 0, assume pixels
-					_zoomStart.y -= event.deltaY * 0.00025;
-					break;
+	if ( this.state === _STATE.NONE ) {
 
-			}
+		switch ( event.button ) {
 
-			scope.dispatchEvent( _startEvent );
-			scope.dispatchEvent( _endEvent );
+			case this.mouseButtons.LEFT:
+				this.state = _STATE.ROTATE;
+				break;
 
-		}
+			case this.mouseButtons.MIDDLE:
+				this.state = _STATE.ZOOM;
+				break;
 
-		function onTouchStart( event ) {
+			case this.mouseButtons.RIGHT:
+				this.state = _STATE.PAN;
+				break;
 
-			trackPointer( event );
+		}
 
-			switch ( _pointers.length ) {
+	}
 
-				case 1:
-					_state = STATE.TOUCH_ROTATE;
-					_moveCurr.copy( getMouseOnCircle( _pointers[ 0 ].pageX, _pointers[ 0 ].pageY ) );
-					_movePrev.copy( _moveCurr );
-					break;
+	const state = ( this.keyState !== _STATE.NONE ) ? this.keyState : this.state;
 
-				default: // 2 or more
-					_state = STATE.TOUCH_ZOOM_PAN;
-					const dx = _pointers[ 0 ].pageX - _pointers[ 1 ].pageX;
-					const dy = _pointers[ 0 ].pageY - _pointers[ 1 ].pageY;
-					_touchZoomDistanceEnd = _touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
+	if ( state === _STATE.ROTATE && ! this.noRotate ) {
 
-					const x = ( _pointers[ 0 ].pageX + _pointers[ 1 ].pageX ) / 2;
-					const y = ( _pointers[ 0 ].pageY + _pointers[ 1 ].pageY ) / 2;
-					_panStart.copy( getMouseOnScreen( x, y ) );
-					_panEnd.copy( _panStart );
-					break;
+		this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
+		this._movePrev.copy( this._moveCurr );
 
-			}
+	} else if ( state === _STATE.ZOOM && ! this.noZoom ) {
 
-			scope.dispatchEvent( _startEvent );
+		this._zoomStart.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
+		this._zoomEnd.copy( this._zoomStart );
 
-		}
+	} else if ( state === _STATE.PAN && ! this.noPan ) {
 
-		function onTouchMove( event ) {
+		this._panStart.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
+		this._panEnd.copy( this._panStart );
 
-			trackPointer( event );
+	}
 
-			switch ( _pointers.length ) {
+	this.dispatchEvent( _startEvent );
 
-				case 1:
-					_movePrev.copy( _moveCurr );
-					_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
-					break;
+}
 
-				default: // 2 or more
+function onMouseMove( event ) {
 
-					const position = getSecondPointerPosition( event );
+	const state = ( this.keyState !== _STATE.NONE ) ? this.keyState : this.state;
 
-					const dx = event.pageX - position.x;
-					const dy = event.pageY - position.y;
-					_touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
+	if ( state === _STATE.ROTATE && ! this.noRotate ) {
 
-					const x = ( event.pageX + position.x ) / 2;
-					const y = ( event.pageY + position.y ) / 2;
-					_panEnd.copy( getMouseOnScreen( x, y ) );
-					break;
+		this._movePrev.copy( this._moveCurr );
+		this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
 
-			}
+	} else if ( state === _STATE.ZOOM && ! this.noZoom ) {
 
-		}
+		this._zoomEnd.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
 
-		function onTouchEnd( event ) {
+	} else if ( state === _STATE.PAN && ! this.noPan ) {
 
-			switch ( _pointers.length ) {
+		this._panEnd.copy( this._getMouseOnScreen( event.pageX, event.pageY ) );
 
-				case 0:
-					_state = STATE.NONE;
-					break;
+	}
 
-				case 1:
-					_state = STATE.TOUCH_ROTATE;
-					_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
-					_movePrev.copy( _moveCurr );
-					break;
+}
 
-				case 2:
-					_state = STATE.TOUCH_ZOOM_PAN;
+function onMouseUp() {
 
-					for ( let i = 0; i < _pointers.length; i ++ ) {
+	this.state = _STATE.NONE;
 
-						if ( _pointers[ i ].pointerId !== event.pointerId ) {
+	this.dispatchEvent( _endEvent );
 
-							const position = _pointerPositions[ _pointers[ i ].pointerId ];
-							_moveCurr.copy( getMouseOnCircle( position.x, position.y ) );
-							_movePrev.copy( _moveCurr );
-							break;
+}
 
-						}
+function onMouseWheel( event ) {
 
-					}
+	if ( this.enabled === false ) return;
 
-					break;
+	if ( this.noZoom === true ) return;
 
-			}
+	event.preventDefault();
 
-			scope.dispatchEvent( _endEvent );
+	switch ( event.deltaMode ) {
 
-		}
+		case 2:
+			// Zoom in pages
+			this._zoomStart.y -= event.deltaY * 0.025;
+			break;
 
-		function contextmenu( event ) {
+		case 1:
+			// Zoom in lines
+			this._zoomStart.y -= event.deltaY * 0.01;
+			break;
 
-			if ( scope.enabled === false ) return;
+		default:
+			// undefined, 0, assume pixels
+			this._zoomStart.y -= event.deltaY * 0.00025;
+			break;
 
-			event.preventDefault();
+	}
 
-		}
+	this.dispatchEvent( _startEvent );
+	this.dispatchEvent( _endEvent );
 
-		function addPointer( event ) {
+}
 
-			_pointers.push( event );
+function onContextMenu( event ) {
 
-		}
+	if ( this.enabled === false ) return;
 
-		function removePointer( event ) {
+	event.preventDefault();
 
-			delete _pointerPositions[ event.pointerId ];
+}
 
-			for ( let i = 0; i < _pointers.length; i ++ ) {
+function onTouchStart( event ) {
 
-				if ( _pointers[ i ].pointerId == event.pointerId ) {
+	this._trackPointer( event );
 
-					_pointers.splice( i, 1 );
-					return;
+	switch ( this._pointers.length ) {
 
-				}
+		case 1:
+			this.state = _STATE.TOUCH_ROTATE;
+			this._moveCurr.copy( this._getMouseOnCircle( this._pointers[ 0 ].pageX, this._pointers[ 0 ].pageY ) );
+			this._movePrev.copy( this._moveCurr );
+			break;
 
-			}
+		default: // 2 or more
+			this.state = _STATE.TOUCH_ZOOM_PAN;
+			const dx = this._pointers[ 0 ].pageX - this._pointers[ 1 ].pageX;
+			const dy = this._pointers[ 0 ].pageY - this._pointers[ 1 ].pageY;
+			this._touchZoomDistanceEnd = this._touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
 
-		}
+			const x = ( this._pointers[ 0 ].pageX + this._pointers[ 1 ].pageX ) / 2;
+			const y = ( this._pointers[ 0 ].pageY + this._pointers[ 1 ].pageY ) / 2;
+			this._panStart.copy( this._getMouseOnScreen( x, y ) );
+			this._panEnd.copy( this._panStart );
+			break;
 
-		function trackPointer( event ) {
+	}
 
-			let position = _pointerPositions[ event.pointerId ];
+	this.dispatchEvent( _startEvent );
 
-			if ( position === undefined ) {
+}
 
-				position = new Vector2();
-				_pointerPositions[ event.pointerId ] = position;
+function onTouchMove( event ) {
 
-			}
+	this._trackPointer( event );
 
-			position.set( event.pageX, event.pageY );
+	switch ( this._pointers.length ) {
 
-		}
+		case 1:
+			this._movePrev.copy( this._moveCurr );
+			this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
+			break;
 
-		function getSecondPointerPosition( event ) {
+		default: // 2 or more
 
-			const pointer = ( event.pointerId === _pointers[ 0 ].pointerId ) ? _pointers[ 1 ] : _pointers[ 0 ];
+			const position = this._getSecondPointerPosition( event );
 
-			return _pointerPositions[ pointer.pointerId ];
+			const dx = event.pageX - position.x;
+			const dy = event.pageY - position.y;
+			this._touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
 
-		}
+			const x = ( event.pageX + position.x ) / 2;
+			const y = ( event.pageY + position.y ) / 2;
+			this._panEnd.copy( this._getMouseOnScreen( x, y ) );
+			break;
 
-		this.dispose = function () {
+	}
 
-			scope.domElement.removeEventListener( 'contextmenu', contextmenu );
+}
 
-			scope.domElement.removeEventListener( 'pointerdown', onPointerDown );
-			scope.domElement.removeEventListener( 'pointercancel', onPointerCancel );
-			scope.domElement.removeEventListener( 'wheel', onMouseWheel );
+function onTouchEnd( event ) {
 
-			scope.domElement.removeEventListener( 'pointermove', onPointerMove );
-			scope.domElement.removeEventListener( 'pointerup', onPointerUp );
+	switch ( this._pointers.length ) {
 
-			window.removeEventListener( 'keydown', keydown );
-			window.removeEventListener( 'keyup', keyup );
+		case 0:
+			this.state = _STATE.NONE;
+			break;
 
-		};
+		case 1:
+			this.state = _STATE.TOUCH_ROTATE;
+			this._moveCurr.copy( this._getMouseOnCircle( event.pageX, event.pageY ) );
+			this._movePrev.copy( this._moveCurr );
+			break;
 
-		this.domElement.addEventListener( 'contextmenu', contextmenu );
+		case 2:
+			this.state = _STATE.TOUCH_ZOOM_PAN;
 
-		this.domElement.addEventListener( 'pointerdown', onPointerDown );
-		this.domElement.addEventListener( 'pointercancel', onPointerCancel );
-		this.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
+			for ( let i = 0; i < this._pointers.length; i ++ ) {
 
+				if ( this._pointers[ i ].pointerId !== event.pointerId ) {
+
+					const position = this._pointerPositions[ this._pointers[ i ].pointerId ];
+					this._moveCurr.copy( this._getMouseOnCircle( position.x, position.y ) );
+					this._movePrev.copy( this._moveCurr );
+					break;
 
-		window.addEventListener( 'keydown', keydown );
-		window.addEventListener( 'keyup', keyup );
+				}
 
-		this.handleResize();
+			}
 
-		// force an update at start
-		this.update();
+			break;
 
 	}
 
+	this.dispatchEvent( _endEvent );
+
 }
 
 export { TrackballControls };

粤ICP备19079148号