| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808 | import {	EventDispatcher,	MOUSE,	Quaternion,	Vector2,	Vector3} from '../../../build/three.module.js';const _changeEvent = { type: 'change' };const _startEvent = { type: 'start' };const _endEvent = { type: 'end' };class TrackballControls extends EventDispatcher {	constructor( object, domElement ) {		super();		if ( domElement === undefined ) console.warn( 'THREE.TrackballControls: The second parameter "domElement" is now mandatory.' );		if ( domElement === document ) console.error( 'THREE.TrackballControls: "document" should not be used as the target "domElement". Please use "renderer.domElement" instead.' );		const scope = this;		const STATE = { NONE: - 1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4 };		this.object = object;		this.domElement = domElement;		this.domElement.style.touchAction = 'none'; // disable touch scroll		// API		this.enabled = true;		this.screen = { left: 0, top: 0, width: 0, height: 0 };		this.rotateSpeed = 1.0;		this.zoomSpeed = 1.2;		this.panSpeed = 0.3;		this.noRotate = false;		this.noZoom = false;		this.noPan = false;		this.staticMoving = false;		this.dynamicDampingFactor = 0.2;		this.minDistance = 0;		this.maxDistance = Infinity;		this.keys = [ 'KeyA' /*A*/, 'KeyS' /*S*/, 'KeyD' /*D*/ ];		this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };		// internals		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,			_lastAngle = 0;		const _eye = new Vector3(),			_movePrev = new Vector2(),			_moveCurr = new Vector2(),			_lastAxis = new Vector3(),			_zoomStart = new Vector2(),			_zoomEnd = new Vector2(),			_panStart = new Vector2(),			_panEnd = new Vector2(),			_pointers = [],			_pointerPositions = {};		// for reset		this.target0 = this.target.clone();		this.position0 = this.object.position.clone();		this.up0 = this.object.up.clone();		this.zoom0 = this.object.zoom;		// methods		this.handleResize = function () {			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;		};		const getMouseOnScreen = ( function () {			const vector = new Vector2();			return function getMouseOnScreen( pageX, pageY ) {				vector.set(					( pageX - scope.screen.left ) / scope.screen.width,					( pageY - scope.screen.top ) / scope.screen.height				);				return vector;			};		}() );		const getMouseOnCircle = ( function () {			const vector = new Vector2();			return function getMouseOnCircle( pageX, pageY ) {				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;			};		}() );		this.rotateCamera = ( function () {			const axis = new Vector3(),				quaternion = new Quaternion(),				eyeDirection = new Vector3(),				objectUpDirection = new Vector3(),				objectSidewaysDirection = new Vector3(),				moveDirection = new Vector3();			return function rotateCamera() {				moveDirection.set( _moveCurr.x - _movePrev.x, _moveCurr.y - _movePrev.y, 0 );				let angle = moveDirection.length();				if ( angle ) {					_eye.copy( scope.object.position ).sub( scope.target );					eyeDirection.copy( _eye ).normalize();					objectUpDirection.copy( scope.object.up ).normalize();					objectSidewaysDirection.crossVectors( objectUpDirection, eyeDirection ).normalize();					objectUpDirection.setLength( _moveCurr.y - _movePrev.y );					objectSidewaysDirection.setLength( _moveCurr.x - _movePrev.x );					moveDirection.copy( objectUpDirection.add( objectSidewaysDirection ) );					axis.crossVectors( moveDirection, _eye ).normalize();					angle *= scope.rotateSpeed;					quaternion.setFromAxisAngle( axis, angle );					_eye.applyQuaternion( quaternion );					scope.object.up.applyQuaternion( quaternion );					_lastAxis.copy( axis );					_lastAngle = angle;				} 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 );				}				_movePrev.copy( _moveCurr );			};		}() );		this.zoomCamera = function () {			let factor;			if ( _state === STATE.TOUCH_ZOOM_PAN ) {				factor = _touchZoomDistanceStart / _touchZoomDistanceEnd;				_touchZoomDistanceStart = _touchZoomDistanceEnd;				if ( scope.object.isPerspectiveCamera ) {					_eye.multiplyScalar( factor );				} else if ( scope.object.isOrthographicCamera ) {					scope.object.zoom /= factor;					scope.object.updateProjectionMatrix();				} else {					console.warn( 'THREE.TrackballControls: Unsupported camera type' );				}			} else {				factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * scope.zoomSpeed;				if ( factor !== 1.0 && factor > 0.0 ) {					if ( scope.object.isPerspectiveCamera ) {						_eye.multiplyScalar( factor );					} else if ( scope.object.isOrthographicCamera ) {						scope.object.zoom /= factor;						scope.object.updateProjectionMatrix();					} else {						console.warn( 'THREE.TrackballControls: Unsupported camera type' );					}				}				if ( scope.staticMoving ) {					_zoomStart.copy( _zoomEnd );				} else {					_zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;				}			}		};		this.panCamera = ( function () {			const mouseChange = new Vector2(),				objectUp = new Vector3(),				pan = new Vector3();			return function panCamera() {				mouseChange.copy( _panEnd ).sub( _panStart );				if ( mouseChange.lengthSq() ) {					if ( scope.object.isOrthographicCamera ) {						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;						mouseChange.x *= scale_x;						mouseChange.y *= scale_y;					}					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 );					if ( scope.staticMoving ) {						_panStart.copy( _panEnd );					} else {						_panStart.add( mouseChange.subVectors( _panEnd, _panStart ).multiplyScalar( scope.dynamicDampingFactor ) );					}				}			};		}() );		this.checkDistances = function () {			if ( ! scope.noZoom || ! scope.noPan ) {				if ( _eye.lengthSq() > scope.maxDistance * scope.maxDistance ) {					scope.object.position.addVectors( scope.target, _eye.setLength( scope.maxDistance ) );					_zoomStart.copy( _zoomEnd );				}				if ( _eye.lengthSq() < scope.minDistance * scope.minDistance ) {					scope.object.position.addVectors( scope.target, _eye.setLength( scope.minDistance ) );					_zoomStart.copy( _zoomEnd );				}			}		};		this.update = function () {			_eye.subVectors( scope.object.position, scope.target );			if ( ! scope.noRotate ) {				scope.rotateCamera();			}			if ( ! scope.noZoom ) {				scope.zoomCamera();			}			if ( ! scope.noPan ) {				scope.panCamera();			}			scope.object.position.addVectors( scope.target, _eye );			if ( scope.object.isPerspectiveCamera ) {				scope.checkDistances();				scope.object.lookAt( scope.target );				if ( lastPosition.distanceToSquared( scope.object.position ) > EPS ) {					scope.dispatchEvent( _changeEvent );					lastPosition.copy( scope.object.position );				}			} else if ( scope.object.isOrthographicCamera ) {				scope.object.lookAt( scope.target );				if ( lastPosition.distanceToSquared( scope.object.position ) > EPS || lastZoom !== scope.object.zoom ) {					scope.dispatchEvent( _changeEvent );					lastPosition.copy( scope.object.position );					lastZoom = scope.object.zoom;				}			} else {				console.warn( 'THREE.TrackballControls: Unsupported camera type' );			}		};		this.reset = function () {			_state = STATE.NONE;			_keyState = STATE.NONE;			scope.target.copy( scope.target0 );			scope.object.position.copy( scope.position0 );			scope.object.up.copy( scope.up0 );			scope.object.zoom = scope.zoom0;			scope.object.updateProjectionMatrix();			_eye.subVectors( scope.object.position, scope.target );			scope.object.lookAt( scope.target );			scope.dispatchEvent( _changeEvent );			lastPosition.copy( scope.object.position );			lastZoom = scope.object.zoom;		};		// listeners		function onPointerDown( event ) {			if ( scope.enabled === false ) return;			if ( _pointers.length === 0 ) {				scope.domElement.setPointerCapture( event.pointerId );				scope.domElement.addEventListener( 'pointermove', onPointerMove );				scope.domElement.addEventListener( 'pointerup', onPointerUp );			}			//			addPointer( event );			if ( event.pointerType === 'touch' ) {				onTouchStart( event );			} else {				onMouseDown( event );			}		}		function onPointerMove( event ) {			if ( scope.enabled === false ) return;			if ( event.pointerType === 'touch' ) {				onTouchMove( event );			} else {				onMouseMove( event );			}		}		function onPointerUp( event ) {			if ( scope.enabled === false ) return;			if ( event.pointerType === 'touch' ) {				onTouchEnd( event );			} else {				onMouseUp();			}			//			removePointer( event );			if ( _pointers.length === 0 ) {				scope.domElement.releasePointerCapture( event.pointerId );				scope.domElement.removeEventListener( 'pointermove', onPointerMove );				scope.domElement.removeEventListener( 'pointerup', onPointerUp );			}		}		function onPointerCancel( event ) {			removePointer( event );		}		function keydown( event ) {			if ( scope.enabled === false ) return;			window.removeEventListener( 'keydown', keydown );			if ( _keyState !== STATE.NONE ) {				return;			} else if ( event.code === scope.keys[ STATE.ROTATE ] && ! scope.noRotate ) {				_keyState = STATE.ROTATE;			} else if ( event.code === scope.keys[ STATE.ZOOM ] && ! scope.noZoom ) {				_keyState = STATE.ZOOM;			} else if ( event.code === scope.keys[ STATE.PAN ] && ! scope.noPan ) {				_keyState = STATE.PAN;			}		}		function keyup() {			if ( scope.enabled === false ) return;			_keyState = STATE.NONE;			window.addEventListener( 'keydown', keydown );		}		function onMouseDown( event ) {			if ( _state === STATE.NONE ) {				switch ( event.button ) {					case scope.mouseButtons.LEFT:						_state = STATE.ROTATE;						break;					case scope.mouseButtons.MIDDLE:						_state = STATE.ZOOM;						break;					case scope.mouseButtons.RIGHT:						_state = STATE.PAN;						break;					default:						_state = STATE.NONE;				}			}			const state = ( _keyState !== STATE.NONE ) ? _keyState : _state;			if ( state === STATE.ROTATE && ! scope.noRotate ) {				_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );				_movePrev.copy( _moveCurr );			} else if ( state === STATE.ZOOM && ! scope.noZoom ) {				_zoomStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );				_zoomEnd.copy( _zoomStart );			} else if ( state === STATE.PAN && ! scope.noPan ) {				_panStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );				_panEnd.copy( _panStart );			}			scope.dispatchEvent( _startEvent );		}		function onMouseMove( event ) {			const state = ( _keyState !== STATE.NONE ) ? _keyState : _state;			if ( state === STATE.ROTATE && ! scope.noRotate ) {				_movePrev.copy( _moveCurr );				_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );			} else if ( state === STATE.ZOOM && ! scope.noZoom ) {				_zoomEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );			} else if ( state === STATE.PAN && ! scope.noPan ) {				_panEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );			}		}		function onMouseUp() {			_state = STATE.NONE;			scope.dispatchEvent( _endEvent );		}		function onMouseWheel( event ) {			if ( scope.enabled === false ) return;			if ( scope.noZoom === true ) return;			event.preventDefault();			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;				default:					// undefined, 0, assume pixels					_zoomStart.y -= event.deltaY * 0.00025;					break;			}			scope.dispatchEvent( _startEvent );			scope.dispatchEvent( _endEvent );		}		function onTouchStart( event ) {			trackPointer( event );			switch ( _pointers.length ) {				case 1:					_state = STATE.TOUCH_ROTATE;					_moveCurr.copy( getMouseOnCircle( _pointers[ 0 ].pageX, _pointers[ 0 ].pageY ) );					_movePrev.copy( _moveCurr );					break;				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 );					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;			}			scope.dispatchEvent( _startEvent );		}		function onTouchMove( event ) {			trackPointer( event );			switch ( _pointers.length ) {				case 1:					_movePrev.copy( _moveCurr );					_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );					break;				default: // 2 or more					const position = getSecondPointerPosition( event );					const dx = event.pageX - position.x;					const dy = event.pageY - position.y;					_touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );					const x = ( event.pageX + position.x ) / 2;					const y = ( event.pageY + position.y ) / 2;					_panEnd.copy( getMouseOnScreen( x, y ) );					break;			}		}		function onTouchEnd( event ) {			switch ( _pointers.length ) {				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;					_moveCurr.copy( getMouseOnCircle( event.pageX - _movePrev.pageX, event.pageY - _movePrev.pageY ) );					_movePrev.copy( _moveCurr );					break;			}			scope.dispatchEvent( _endEvent );		}		function contextmenu( event ) {			if ( scope.enabled === false ) return;			event.preventDefault();		}		function addPointer( event ) {			_pointers.push( event );		}		function removePointer( event ) {			delete _pointerPositions[ event.pointerId ];			for ( let i = 0; i < _pointers.length; i ++ ) {				if ( _pointers[ i ].pointerId == event.pointerId ) {					_pointers.splice( i, 1 );					return;				}			}		}		function trackPointer( event ) {			let position = _pointerPositions[ event.pointerId ];			if ( position === undefined ) {				position = new Vector2();				_pointerPositions[ event.pointerId ] = position;			}			position.set( event.pageX, event.pageY );		}		function getSecondPointerPosition( event ) {			const pointer = ( event.pointerId === _pointers[ 0 ].pointerId ) ? _pointers[ 1 ] : _pointers[ 0 ];			return _pointerPositions[ pointer.pointerId ];		}		this.dispose = function () {			scope.domElement.removeEventListener( 'contextmenu', contextmenu );			scope.domElement.removeEventListener( 'pointerdown', onPointerDown );			scope.domElement.removeEventListener( 'pointercancel', onPointerCancel );			scope.domElement.removeEventListener( 'wheel', onMouseWheel );			scope.domElement.removeEventListener( 'pointermove', onPointerMove );			scope.domElement.removeEventListener( 'pointerup', onPointerUp );			window.removeEventListener( 'keydown', keydown );			window.removeEventListener( 'keyup', keyup );		};		this.domElement.addEventListener( 'contextmenu', contextmenu );		this.domElement.addEventListener( 'pointerdown', onPointerDown );		this.domElement.addEventListener( 'pointercancel', onPointerCancel );		this.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );		window.addEventListener( 'keydown', keydown );		window.addEventListener( 'keyup', keyup );		this.handleResize();		// force an update at start		this.update();	}}export { TrackballControls };
 |