|
|
@@ -1,5 +1,5 @@
|
|
|
import {
|
|
|
- EventDispatcher,
|
|
|
+ Controls,
|
|
|
MOUSE,
|
|
|
Quaternion,
|
|
|
Spherical,
|
|
|
@@ -23,17 +23,30 @@ const _startEvent = { type: 'start' };
|
|
|
const _endEvent = { type: 'end' };
|
|
|
const _ray = new Ray();
|
|
|
const _plane = new Plane();
|
|
|
-const TILT_LIMIT = Math.cos( 70 * MathUtils.DEG2RAD );
|
|
|
+const _TILT_LIMIT = Math.cos( 70 * MathUtils.DEG2RAD );
|
|
|
|
|
|
-class OrbitControls extends EventDispatcher {
|
|
|
+const _v = new Vector3();
|
|
|
+const _twoPI = 2 * Math.PI;
|
|
|
|
|
|
- constructor( object, domElement ) {
|
|
|
+const _STATE = {
|
|
|
+ NONE: - 1,
|
|
|
+ ROTATE: 0,
|
|
|
+ DOLLY: 1,
|
|
|
+ PAN: 2,
|
|
|
+ TOUCH_ROTATE: 3,
|
|
|
+ TOUCH_PAN: 4,
|
|
|
+ TOUCH_DOLLY_PAN: 5,
|
|
|
+ TOUCH_DOLLY_ROTATE: 6
|
|
|
+};
|
|
|
+const _EPS = 0.000001;
|
|
|
|
|
|
- super();
|
|
|
+class OrbitControls extends Controls {
|
|
|
|
|
|
- this.object = object;
|
|
|
- this.domElement = domElement;
|
|
|
- this.domElement.style.touchAction = 'none'; // disable touch scroll
|
|
|
+ constructor( object, domElement = null ) {
|
|
|
+
|
|
|
+ super( object, domElement );
|
|
|
+
|
|
|
+ this.state = _STATE.NONE;
|
|
|
|
|
|
// Set to false to disable this control
|
|
|
this.enabled = true;
|
|
|
@@ -109,1421 +122,1399 @@ class OrbitControls extends EventDispatcher {
|
|
|
// the target DOM element for key events
|
|
|
this._domElementKeyEvents = null;
|
|
|
|
|
|
- //
|
|
|
- // public methods
|
|
|
- //
|
|
|
+ // internals
|
|
|
|
|
|
- this.getPolarAngle = function () {
|
|
|
+ this._lastPosition = new Vector3();
|
|
|
+ this._lastQuaternion = new Quaternion();
|
|
|
+ this._lastTargetPosition = new Vector3();
|
|
|
|
|
|
- return spherical.phi;
|
|
|
+ // so camera.up is the orbit axis
|
|
|
+ this._quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
|
|
|
+ this._quatInverse = this._quat.clone().invert();
|
|
|
|
|
|
- };
|
|
|
+ // current position in spherical coordinates
|
|
|
+ this._spherical = new Spherical();
|
|
|
+ this._sphericalDelta = new Spherical();
|
|
|
|
|
|
- this.getAzimuthalAngle = function () {
|
|
|
+ this._scale = 1;
|
|
|
+ this._panOffset = new Vector3();
|
|
|
|
|
|
- return spherical.theta;
|
|
|
+ this._rotateStart = new Vector2();
|
|
|
+ this._rotateEnd = new Vector2();
|
|
|
+ this._rotateDelta = new Vector2();
|
|
|
|
|
|
- };
|
|
|
+ this._panStart = new Vector2();
|
|
|
+ this._panEnd = new Vector2();
|
|
|
+ this._panDelta = new Vector2();
|
|
|
|
|
|
- this.getDistance = function () {
|
|
|
+ this._dollyStart = new Vector2();
|
|
|
+ this._dollyEnd = new Vector2();
|
|
|
+ this._dollyDelta = new Vector2();
|
|
|
|
|
|
- return this.object.position.distanceTo( this.target );
|
|
|
+ this._dollyDirection = new Vector3();
|
|
|
+ this._mouse = new Vector2();
|
|
|
+ this._performCursorZoom = false;
|
|
|
|
|
|
- };
|
|
|
+ this._pointers = [];
|
|
|
+ this._pointerPositions = {};
|
|
|
|
|
|
- this.listenToKeyEvents = function ( domElement ) {
|
|
|
+ this._controlActive = false;
|
|
|
|
|
|
- domElement.addEventListener( 'keydown', onKeyDown );
|
|
|
- this._domElementKeyEvents = domElement;
|
|
|
+ // event listeners
|
|
|
|
|
|
- };
|
|
|
+ this._onPointerMove = onPointerMove.bind( this );
|
|
|
+ this._onPointerDown = onPointerDown.bind( this );
|
|
|
+ this._onPointerUp = onPointerUp.bind( this );
|
|
|
+ this._onContextMenu = onContextMenu.bind( this );
|
|
|
+ this._onMouseWheel = onMouseWheel.bind( this );
|
|
|
+ this._onKeyDown = onKeyDown.bind( this );
|
|
|
|
|
|
- this.stopListenToKeyEvents = function () {
|
|
|
+ this._onTouchStart = onTouchStart.bind( this );
|
|
|
+ this._onTouchMove = onTouchMove.bind( this );
|
|
|
|
|
|
- this._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown );
|
|
|
- this._domElementKeyEvents = null;
|
|
|
+ this._onMouseDown = onMouseDown.bind( this );
|
|
|
+ this._onMouseMove = onMouseMove.bind( this );
|
|
|
|
|
|
- };
|
|
|
+ this._interceptControlDown = interceptControlDown.bind( this );
|
|
|
+ this._interceptControlUp = interceptControlUp.bind( this );
|
|
|
|
|
|
- this.saveState = function () {
|
|
|
+ //
|
|
|
|
|
|
- scope.target0.copy( scope.target );
|
|
|
- scope.position0.copy( scope.object.position );
|
|
|
- scope.zoom0 = scope.object.zoom;
|
|
|
+ if ( this.domElement !== null ) {
|
|
|
|
|
|
- };
|
|
|
+ this.connect();
|
|
|
|
|
|
- this.reset = function () {
|
|
|
+ }
|
|
|
|
|
|
- scope.target.copy( scope.target0 );
|
|
|
- scope.object.position.copy( scope.position0 );
|
|
|
- scope.object.zoom = scope.zoom0;
|
|
|
+ this.update();
|
|
|
|
|
|
- scope.object.updateProjectionMatrix();
|
|
|
- scope.dispatchEvent( _changeEvent );
|
|
|
+ }
|
|
|
|
|
|
- scope.update();
|
|
|
+ connect() {
|
|
|
|
|
|
- state = STATE.NONE;
|
|
|
+ this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
|
|
|
+ this.domElement.addEventListener( 'pointercancel', this._onPointerUp );
|
|
|
|
|
|
- };
|
|
|
+ this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
|
|
|
+ this.domElement.addEventListener( 'wheel', this._onMouseWheel, { passive: false } );
|
|
|
|
|
|
- // this method is exposed, but perhaps it would be better if we can make it private...
|
|
|
- this.update = function () {
|
|
|
+ const document = this.domElement.getRootNode(); // offscreen canvas compatibility
|
|
|
+ document.addEventListener( 'keydown', this._interceptControlDown, { passive: true, capture: true } );
|
|
|
|
|
|
- const offset = new Vector3();
|
|
|
+ this.domElement.style.touchAction = 'none'; // disable touch scroll
|
|
|
|
|
|
- // so camera.up is the orbit axis
|
|
|
- const quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
|
|
|
- const quatInverse = quat.clone().invert();
|
|
|
+ }
|
|
|
|
|
|
- const lastPosition = new Vector3();
|
|
|
- const lastQuaternion = new Quaternion();
|
|
|
- const lastTargetPosition = new Vector3();
|
|
|
+ disconnect() {
|
|
|
|
|
|
- const twoPI = 2 * Math.PI;
|
|
|
+ this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
|
|
|
+ this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
|
|
|
+ this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
|
|
|
+ this.domElement.removeEventListener( 'pointercancel', this._onPointerUp );
|
|
|
|
|
|
- return function update( deltaTime = null ) {
|
|
|
+ this.domElement.removeEventListener( 'wheel', this._onMouseWheel );
|
|
|
+ this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
|
|
|
|
|
|
- const position = scope.object.position;
|
|
|
+ this.stopListenToKeyEvents();
|
|
|
|
|
|
- offset.copy( position ).sub( scope.target );
|
|
|
+ const document = this.domElement.getRootNode(); // offscreen canvas compatibility
|
|
|
+ document.removeEventListener( 'keydown', this._interceptControlDown, { capture: true } );
|
|
|
|
|
|
- // rotate offset to "y-axis-is-up" space
|
|
|
- offset.applyQuaternion( quat );
|
|
|
+ this.domElement.style.touchAction = 'auto';
|
|
|
|
|
|
- // angle from z-axis around y-axis
|
|
|
- spherical.setFromVector3( offset );
|
|
|
+ }
|
|
|
|
|
|
- if ( scope.autoRotate && state === STATE.NONE ) {
|
|
|
+ dispose() {
|
|
|
|
|
|
- rotateLeft( getAutoRotationAngle( deltaTime ) );
|
|
|
+ this.disconnect();
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- if ( scope.enableDamping ) {
|
|
|
+ getPolarAngle() {
|
|
|
|
|
|
- spherical.theta += sphericalDelta.theta * scope.dampingFactor;
|
|
|
- spherical.phi += sphericalDelta.phi * scope.dampingFactor;
|
|
|
+ return this._spherical.phi;
|
|
|
|
|
|
- } else {
|
|
|
+ }
|
|
|
|
|
|
- spherical.theta += sphericalDelta.theta;
|
|
|
- spherical.phi += sphericalDelta.phi;
|
|
|
+ getAzimuthalAngle() {
|
|
|
|
|
|
- }
|
|
|
+ return this._spherical.theta;
|
|
|
|
|
|
- // restrict theta to be between desired limits
|
|
|
+ }
|
|
|
|
|
|
- let min = scope.minAzimuthAngle;
|
|
|
- let max = scope.maxAzimuthAngle;
|
|
|
+ getDistance() {
|
|
|
|
|
|
- if ( isFinite( min ) && isFinite( max ) ) {
|
|
|
+ return this.object.position.distanceTo( this.target );
|
|
|
|
|
|
- if ( min < - Math.PI ) min += twoPI; else if ( min > Math.PI ) min -= twoPI;
|
|
|
+ }
|
|
|
|
|
|
- if ( max < - Math.PI ) max += twoPI; else if ( max > Math.PI ) max -= twoPI;
|
|
|
+ listenToKeyEvents( domElement ) {
|
|
|
|
|
|
- if ( min <= max ) {
|
|
|
+ domElement.addEventListener( 'keydown', this._onKeyDown );
|
|
|
+ this._domElementKeyEvents = domElement;
|
|
|
|
|
|
- spherical.theta = Math.max( min, Math.min( max, spherical.theta ) );
|
|
|
+ }
|
|
|
|
|
|
- } else {
|
|
|
+ stopListenToKeyEvents() {
|
|
|
|
|
|
- spherical.theta = ( spherical.theta > ( min + max ) / 2 ) ?
|
|
|
- Math.max( min, spherical.theta ) :
|
|
|
- Math.min( max, spherical.theta );
|
|
|
+ if ( this._domElementKeyEvents !== null ) {
|
|
|
|
|
|
- }
|
|
|
+ this._domElementKeyEvents.removeEventListener( 'keydown', this._onKeyDown );
|
|
|
+ this._domElementKeyEvents = null;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- // restrict phi to be between desired limits
|
|
|
- spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
|
|
|
+ saveState() {
|
|
|
|
|
|
- spherical.makeSafe();
|
|
|
+ this.target0.copy( this.target );
|
|
|
+ this.position0.copy( this.object.position );
|
|
|
+ this.zoom0 = this.object.zoom;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- // move target to panned location
|
|
|
+ reset() {
|
|
|
|
|
|
- if ( scope.enableDamping === true ) {
|
|
|
+ this.target.copy( this.target0 );
|
|
|
+ this.object.position.copy( this.position0 );
|
|
|
+ this.object.zoom = this.zoom0;
|
|
|
|
|
|
- scope.target.addScaledVector( panOffset, scope.dampingFactor );
|
|
|
+ this.object.updateProjectionMatrix();
|
|
|
+ this.dispatchEvent( _changeEvent );
|
|
|
|
|
|
- } else {
|
|
|
+ this.update();
|
|
|
|
|
|
- scope.target.add( panOffset );
|
|
|
+ this.state = _STATE.NONE;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- // Limit the target distance from the cursor to create a sphere around the center of interest
|
|
|
- scope.target.sub( scope.cursor );
|
|
|
- scope.target.clampLength( scope.minTargetRadius, scope.maxTargetRadius );
|
|
|
- scope.target.add( scope.cursor );
|
|
|
+ update( deltaTime = null ) {
|
|
|
|
|
|
- let zoomChanged = false;
|
|
|
- // adjust the camera position based on zoom only if we're not zooming to the cursor or if it's an ortho camera
|
|
|
- // we adjust zoom later in these cases
|
|
|
- if ( scope.zoomToCursor && performCursorZoom || scope.object.isOrthographicCamera ) {
|
|
|
+ const position = this.object.position;
|
|
|
|
|
|
- spherical.radius = clampDistance( spherical.radius );
|
|
|
+ _v.copy( position ).sub( this.target );
|
|
|
|
|
|
- } else {
|
|
|
+ // rotate offset to "y-axis-is-up" space
|
|
|
+ _v.applyQuaternion( this._quat );
|
|
|
|
|
|
- const prevRadius = spherical.radius;
|
|
|
- spherical.radius = clampDistance( spherical.radius * scale );
|
|
|
- zoomChanged = prevRadius != spherical.radius;
|
|
|
+ // angle from z-axis around y-axis
|
|
|
+ this._spherical.setFromVector3( _v );
|
|
|
|
|
|
- }
|
|
|
+ if ( this.autoRotate && this.state === _STATE.NONE ) {
|
|
|
|
|
|
- offset.setFromSpherical( spherical );
|
|
|
+ this._rotateLeft( this._getAutoRotationAngle( deltaTime ) );
|
|
|
|
|
|
- // rotate offset back to "camera-up-vector-is-up" space
|
|
|
- offset.applyQuaternion( quatInverse );
|
|
|
+ }
|
|
|
|
|
|
- position.copy( scope.target ).add( offset );
|
|
|
+ if ( this.enableDamping ) {
|
|
|
|
|
|
- scope.object.lookAt( scope.target );
|
|
|
+ this._spherical.theta += this._sphericalDelta.theta * this.dampingFactor;
|
|
|
+ this._spherical.phi += this._sphericalDelta.phi * this.dampingFactor;
|
|
|
|
|
|
- if ( scope.enableDamping === true ) {
|
|
|
+ } else {
|
|
|
|
|
|
- sphericalDelta.theta *= ( 1 - scope.dampingFactor );
|
|
|
- sphericalDelta.phi *= ( 1 - scope.dampingFactor );
|
|
|
+ this._spherical.theta += this._sphericalDelta.theta;
|
|
|
+ this._spherical.phi += this._sphericalDelta.phi;
|
|
|
|
|
|
- panOffset.multiplyScalar( 1 - scope.dampingFactor );
|
|
|
+ }
|
|
|
|
|
|
- } else {
|
|
|
+ // restrict theta to be between desired limits
|
|
|
|
|
|
- sphericalDelta.set( 0, 0, 0 );
|
|
|
+ let min = this.minAzimuthAngle;
|
|
|
+ let max = this.maxAzimuthAngle;
|
|
|
|
|
|
- panOffset.set( 0, 0, 0 );
|
|
|
+ if ( isFinite( min ) && isFinite( max ) ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( min < - Math.PI ) min += _twoPI; else if ( min > Math.PI ) min -= _twoPI;
|
|
|
|
|
|
- // adjust camera position
|
|
|
- if ( scope.zoomToCursor && performCursorZoom ) {
|
|
|
+ if ( max < - Math.PI ) max += _twoPI; else if ( max > Math.PI ) max -= _twoPI;
|
|
|
|
|
|
- let newRadius = null;
|
|
|
- if ( scope.object.isPerspectiveCamera ) {
|
|
|
+ if ( min <= max ) {
|
|
|
|
|
|
- // move the camera down the pointer ray
|
|
|
- // this method avoids floating point error
|
|
|
- const prevRadius = offset.length();
|
|
|
- newRadius = clampDistance( prevRadius * scale );
|
|
|
+ this._spherical.theta = Math.max( min, Math.min( max, this._spherical.theta ) );
|
|
|
|
|
|
- const radiusDelta = prevRadius - newRadius;
|
|
|
- scope.object.position.addScaledVector( dollyDirection, radiusDelta );
|
|
|
- scope.object.updateMatrixWorld();
|
|
|
+ } else {
|
|
|
|
|
|
- zoomChanged = !! radiusDelta;
|
|
|
+ this._spherical.theta = ( this._spherical.theta > ( min + max ) / 2 ) ?
|
|
|
+ Math.max( min, this._spherical.theta ) :
|
|
|
+ Math.min( max, this._spherical.theta );
|
|
|
|
|
|
- } else if ( scope.object.isOrthographicCamera ) {
|
|
|
+ }
|
|
|
|
|
|
- // adjust the ortho camera position based on zoom changes
|
|
|
- const mouseBefore = new Vector3( mouse.x, mouse.y, 0 );
|
|
|
- mouseBefore.unproject( scope.object );
|
|
|
+ }
|
|
|
|
|
|
- const prevZoom = scope.object.zoom;
|
|
|
- scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / scale ) );
|
|
|
- scope.object.updateProjectionMatrix();
|
|
|
+ // restrict phi to be between desired limits
|
|
|
+ this._spherical.phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, this._spherical.phi ) );
|
|
|
|
|
|
- zoomChanged = prevZoom !== scope.object.zoom;
|
|
|
+ this._spherical.makeSafe();
|
|
|
|
|
|
- const mouseAfter = new Vector3( mouse.x, mouse.y, 0 );
|
|
|
- mouseAfter.unproject( scope.object );
|
|
|
|
|
|
- scope.object.position.sub( mouseAfter ).add( mouseBefore );
|
|
|
- scope.object.updateMatrixWorld();
|
|
|
+ // move target to panned location
|
|
|
|
|
|
- newRadius = offset.length();
|
|
|
+ if ( this.enableDamping === true ) {
|
|
|
|
|
|
- } else {
|
|
|
+ this.target.addScaledVector( this._panOffset, this.dampingFactor );
|
|
|
|
|
|
- console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - zoom to cursor disabled.' );
|
|
|
- scope.zoomToCursor = false;
|
|
|
+ } else {
|
|
|
|
|
|
- }
|
|
|
+ this.target.add( this._panOffset );
|
|
|
|
|
|
- // handle the placement of the target
|
|
|
- if ( newRadius !== null ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( this.screenSpacePanning ) {
|
|
|
+ // Limit the target distance from the cursor to create a sphere around the center of interest
|
|
|
+ this.target.sub( this.cursor );
|
|
|
+ this.target.clampLength( this.minTargetRadius, this.maxTargetRadius );
|
|
|
+ this.target.add( this.cursor );
|
|
|
|
|
|
- // position the orbit target in front of the new camera position
|
|
|
- scope.target.set( 0, 0, - 1 )
|
|
|
- .transformDirection( scope.object.matrix )
|
|
|
- .multiplyScalar( newRadius )
|
|
|
- .add( scope.object.position );
|
|
|
+ let zoomChanged = false;
|
|
|
+ // adjust the camera position based on zoom only if we're not zooming to the cursor or if it's an ortho camera
|
|
|
+ // we adjust zoom later in these cases
|
|
|
+ if ( this.zoomToCursor && this._performCursorZoom || this.object.isOrthographicCamera ) {
|
|
|
|
|
|
- } else {
|
|
|
+ this._spherical.radius = this._clampDistance( this._spherical.radius );
|
|
|
|
|
|
- // get the ray and translation plane to compute target
|
|
|
- _ray.origin.copy( scope.object.position );
|
|
|
- _ray.direction.set( 0, 0, - 1 ).transformDirection( scope.object.matrix );
|
|
|
+ } else {
|
|
|
|
|
|
- // if the camera is 20 degrees above the horizon then don't adjust the focus target to avoid
|
|
|
- // extremely large values
|
|
|
- if ( Math.abs( scope.object.up.dot( _ray.direction ) ) < TILT_LIMIT ) {
|
|
|
+ const prevRadius = this._spherical.radius;
|
|
|
+ this._spherical.radius = this._clampDistance( this._spherical.radius * this._scale );
|
|
|
+ zoomChanged = prevRadius != this._spherical.radius;
|
|
|
|
|
|
- object.lookAt( scope.target );
|
|
|
+ }
|
|
|
|
|
|
- } else {
|
|
|
+ _v.setFromSpherical( this._spherical );
|
|
|
|
|
|
- _plane.setFromNormalAndCoplanarPoint( scope.object.up, scope.target );
|
|
|
- _ray.intersectPlane( _plane, scope.target );
|
|
|
+ // rotate offset back to "camera-up-vector-is-up" space
|
|
|
+ _v.applyQuaternion( this._quatInverse );
|
|
|
|
|
|
- }
|
|
|
+ position.copy( this.target ).add( _v );
|
|
|
|
|
|
- }
|
|
|
+ this.object.lookAt( this.target );
|
|
|
|
|
|
- }
|
|
|
+ if ( this.enableDamping === true ) {
|
|
|
|
|
|
- } else if ( scope.object.isOrthographicCamera ) {
|
|
|
+ this._sphericalDelta.theta *= ( 1 - this.dampingFactor );
|
|
|
+ this._sphericalDelta.phi *= ( 1 - this.dampingFactor );
|
|
|
|
|
|
- const prevZoom = scope.object.zoom;
|
|
|
- scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / scale ) );
|
|
|
+ this._panOffset.multiplyScalar( 1 - this.dampingFactor );
|
|
|
|
|
|
- if ( prevZoom !== scope.object.zoom ) {
|
|
|
+ } else {
|
|
|
|
|
|
- scope.object.updateProjectionMatrix();
|
|
|
- zoomChanged = true;
|
|
|
+ this._sphericalDelta.set( 0, 0, 0 );
|
|
|
|
|
|
- }
|
|
|
+ this._panOffset.set( 0, 0, 0 );
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- scale = 1;
|
|
|
- performCursorZoom = false;
|
|
|
+ // adjust camera position
|
|
|
+ if ( this.zoomToCursor && this._performCursorZoom ) {
|
|
|
|
|
|
- // update condition is:
|
|
|
- // min(camera displacement, camera rotation in radians)^2 > EPS
|
|
|
- // using small-angle approximation cos(x/2) = 1 - x^2 / 8
|
|
|
+ let newRadius = null;
|
|
|
+ if ( this.object.isPerspectiveCamera ) {
|
|
|
|
|
|
- if ( zoomChanged ||
|
|
|
- lastPosition.distanceToSquared( scope.object.position ) > EPS ||
|
|
|
- 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ||
|
|
|
- lastTargetPosition.distanceToSquared( scope.target ) > EPS ) {
|
|
|
+ // move the camera down the pointer ray
|
|
|
+ // this method avoids floating point error
|
|
|
+ const prevRadius = _v.length();
|
|
|
+ newRadius = this._clampDistance( prevRadius * this._scale );
|
|
|
|
|
|
- scope.dispatchEvent( _changeEvent );
|
|
|
+ const radiusDelta = prevRadius - newRadius;
|
|
|
+ this.object.position.addScaledVector( this._dollyDirection, radiusDelta );
|
|
|
+ this.object.updateMatrixWorld();
|
|
|
|
|
|
- lastPosition.copy( scope.object.position );
|
|
|
- lastQuaternion.copy( scope.object.quaternion );
|
|
|
- lastTargetPosition.copy( scope.target );
|
|
|
+ zoomChanged = !! radiusDelta;
|
|
|
|
|
|
- return true;
|
|
|
+ } else if ( this.object.isOrthographicCamera ) {
|
|
|
|
|
|
- }
|
|
|
+ // adjust the ortho camera position based on zoom changes
|
|
|
+ const mouseBefore = new Vector3( this._mouse.x, this._mouse.y, 0 );
|
|
|
+ mouseBefore.unproject( this.object );
|
|
|
|
|
|
- return false;
|
|
|
+ const prevZoom = this.object.zoom;
|
|
|
+ this.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / this._scale ) );
|
|
|
+ this.object.updateProjectionMatrix();
|
|
|
|
|
|
- };
|
|
|
+ zoomChanged = prevZoom !== this.object.zoom;
|
|
|
|
|
|
- }();
|
|
|
+ const mouseAfter = new Vector3( this._mouse.x, this._mouse.y, 0 );
|
|
|
+ mouseAfter.unproject( this.object );
|
|
|
|
|
|
- this.dispose = function () {
|
|
|
+ this.object.position.sub( mouseAfter ).add( mouseBefore );
|
|
|
+ this.object.updateMatrixWorld();
|
|
|
|
|
|
- scope.domElement.removeEventListener( 'contextmenu', onContextMenu );
|
|
|
+ newRadius = _v.length();
|
|
|
|
|
|
- scope.domElement.removeEventListener( 'pointerdown', onPointerDown );
|
|
|
- scope.domElement.removeEventListener( 'pointercancel', onPointerUp );
|
|
|
- scope.domElement.removeEventListener( 'wheel', onMouseWheel );
|
|
|
+ } else {
|
|
|
|
|
|
- scope.domElement.removeEventListener( 'pointermove', onPointerMove );
|
|
|
- scope.domElement.removeEventListener( 'pointerup', onPointerUp );
|
|
|
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - zoom to cursor disabled.' );
|
|
|
+ this.zoomToCursor = false;
|
|
|
|
|
|
- const document = scope.domElement.getRootNode(); // offscreen canvas compatibility
|
|
|
+ }
|
|
|
|
|
|
- document.removeEventListener( 'keydown', interceptControlDown, { capture: true } );
|
|
|
+ // handle the placement of the target
|
|
|
+ if ( newRadius !== null ) {
|
|
|
|
|
|
- if ( scope._domElementKeyEvents !== null ) {
|
|
|
+ if ( this.screenSpacePanning ) {
|
|
|
|
|
|
- scope._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown );
|
|
|
- scope._domElementKeyEvents = null;
|
|
|
+ // position the orbit target in front of the new camera position
|
|
|
+ this.target.set( 0, 0, - 1 )
|
|
|
+ .transformDirection( this.object.matrix )
|
|
|
+ .multiplyScalar( newRadius )
|
|
|
+ .add( this.object.position );
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
|
|
|
+ // get the ray and translation plane to compute target
|
|
|
+ _ray.origin.copy( this.object.position );
|
|
|
+ _ray.direction.set( 0, 0, - 1 ).transformDirection( this.object.matrix );
|
|
|
|
|
|
- };
|
|
|
+ // if the camera is 20 degrees above the horizon then don't adjust the focus target to avoid
|
|
|
+ // extremely large values
|
|
|
+ if ( Math.abs( this.object.up.dot( _ray.direction ) ) < _TILT_LIMIT ) {
|
|
|
|
|
|
- //
|
|
|
- // internals
|
|
|
- //
|
|
|
+ this.object.lookAt( this.target );
|
|
|
|
|
|
- const scope = this;
|
|
|
-
|
|
|
- const STATE = {
|
|
|
- NONE: - 1,
|
|
|
- ROTATE: 0,
|
|
|
- DOLLY: 1,
|
|
|
- PAN: 2,
|
|
|
- TOUCH_ROTATE: 3,
|
|
|
- TOUCH_PAN: 4,
|
|
|
- TOUCH_DOLLY_PAN: 5,
|
|
|
- TOUCH_DOLLY_ROTATE: 6
|
|
|
- };
|
|
|
+ } else {
|
|
|
|
|
|
- let state = STATE.NONE;
|
|
|
+ _plane.setFromNormalAndCoplanarPoint( this.object.up, this.target );
|
|
|
+ _ray.intersectPlane( _plane, this.target );
|
|
|
|
|
|
- const EPS = 0.000001;
|
|
|
+ }
|
|
|
|
|
|
- // current position in spherical coordinates
|
|
|
- const spherical = new Spherical();
|
|
|
- const sphericalDelta = new Spherical();
|
|
|
+ }
|
|
|
|
|
|
- let scale = 1;
|
|
|
- const panOffset = new Vector3();
|
|
|
+ }
|
|
|
|
|
|
- const rotateStart = new Vector2();
|
|
|
- const rotateEnd = new Vector2();
|
|
|
- const rotateDelta = new Vector2();
|
|
|
+ } else if ( this.object.isOrthographicCamera ) {
|
|
|
|
|
|
- const panStart = new Vector2();
|
|
|
- const panEnd = new Vector2();
|
|
|
- const panDelta = new Vector2();
|
|
|
+ const prevZoom = this.object.zoom;
|
|
|
+ this.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / this._scale ) );
|
|
|
|
|
|
- const dollyStart = new Vector2();
|
|
|
- const dollyEnd = new Vector2();
|
|
|
- const dollyDelta = new Vector2();
|
|
|
+ if ( prevZoom !== this.object.zoom ) {
|
|
|
|
|
|
- const dollyDirection = new Vector3();
|
|
|
- const mouse = new Vector2();
|
|
|
- let performCursorZoom = false;
|
|
|
+ this.object.updateProjectionMatrix();
|
|
|
+ zoomChanged = true;
|
|
|
|
|
|
- const pointers = [];
|
|
|
- const pointerPositions = {};
|
|
|
+ }
|
|
|
|
|
|
- let controlActive = false;
|
|
|
+ }
|
|
|
|
|
|
- function getAutoRotationAngle( deltaTime ) {
|
|
|
+ this._scale = 1;
|
|
|
+ this._performCursorZoom = false;
|
|
|
|
|
|
- if ( deltaTime !== null ) {
|
|
|
+ // update condition is:
|
|
|
+ // min(camera displacement, camera rotation in radians)^2 > EPS
|
|
|
+ // using small-angle approximation cos(x/2) = 1 - x^2 / 8
|
|
|
|
|
|
- return ( 2 * Math.PI / 60 * scope.autoRotateSpeed ) * deltaTime;
|
|
|
+ if ( zoomChanged ||
|
|
|
+ this._lastPosition.distanceToSquared( this.object.position ) > _EPS ||
|
|
|
+ 8 * ( 1 - this._lastQuaternion.dot( this.object.quaternion ) ) > _EPS ||
|
|
|
+ this._lastTargetPosition.distanceToSquared( this.target ) > _EPS ) {
|
|
|
|
|
|
- } else {
|
|
|
+ this.dispatchEvent( _changeEvent );
|
|
|
|
|
|
- return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
|
|
|
+ this._lastPosition.copy( this.object.position );
|
|
|
+ this._lastQuaternion.copy( this.object.quaternion );
|
|
|
+ this._lastTargetPosition.copy( this.target );
|
|
|
|
|
|
- }
|
|
|
+ return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
- function getZoomScale( delta ) {
|
|
|
+ return false;
|
|
|
|
|
|
- const normalizedDelta = Math.abs( delta * 0.01 );
|
|
|
- return Math.pow( 0.95, scope.zoomSpeed * normalizedDelta );
|
|
|
-
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- function rotateLeft( angle ) {
|
|
|
+ _getAutoRotationAngle( deltaTime ) {
|
|
|
|
|
|
- sphericalDelta.theta -= angle;
|
|
|
+ if ( deltaTime !== null ) {
|
|
|
|
|
|
- }
|
|
|
+ return ( _twoPI / 60 * this.autoRotateSpeed ) * deltaTime;
|
|
|
|
|
|
- function rotateUp( angle ) {
|
|
|
+ } else {
|
|
|
|
|
|
- sphericalDelta.phi -= angle;
|
|
|
+ return _twoPI / 60 / 60 * this.autoRotateSpeed;
|
|
|
|
|
|
}
|
|
|
|
|
|
- const panLeft = function () {
|
|
|
+ }
|
|
|
|
|
|
- const v = new Vector3();
|
|
|
+ _getZoomScale( delta ) {
|
|
|
|
|
|
- return function panLeft( distance, objectMatrix ) {
|
|
|
+ const normalizedDelta = Math.abs( delta * 0.01 );
|
|
|
+ return Math.pow( 0.95, this.zoomSpeed * normalizedDelta );
|
|
|
|
|
|
- v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
|
|
|
- v.multiplyScalar( - distance );
|
|
|
+ }
|
|
|
|
|
|
- panOffset.add( v );
|
|
|
+ _rotateLeft( angle ) {
|
|
|
|
|
|
- };
|
|
|
+ this._sphericalDelta.theta -= angle;
|
|
|
|
|
|
- }();
|
|
|
+ }
|
|
|
|
|
|
- const panUp = function () {
|
|
|
+ _rotateUp( angle ) {
|
|
|
|
|
|
- const v = new Vector3();
|
|
|
+ this._sphericalDelta.phi -= angle;
|
|
|
|
|
|
- return function panUp( distance, objectMatrix ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( scope.screenSpacePanning === true ) {
|
|
|
+ _panLeft( distance, objectMatrix ) {
|
|
|
|
|
|
- v.setFromMatrixColumn( objectMatrix, 1 );
|
|
|
+ _v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
|
|
|
+ _v.multiplyScalar( - distance );
|
|
|
|
|
|
- } else {
|
|
|
+ this._panOffset.add( _v );
|
|
|
|
|
|
- v.setFromMatrixColumn( objectMatrix, 0 );
|
|
|
- v.crossVectors( scope.object.up, v );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ _panUp( distance, objectMatrix ) {
|
|
|
|
|
|
- v.multiplyScalar( distance );
|
|
|
+ if ( this.screenSpacePanning === true ) {
|
|
|
|
|
|
- panOffset.add( v );
|
|
|
+ _v.setFromMatrixColumn( objectMatrix, 1 );
|
|
|
|
|
|
- };
|
|
|
+ } else {
|
|
|
|
|
|
- }();
|
|
|
+ _v.setFromMatrixColumn( objectMatrix, 0 );
|
|
|
+ _v.crossVectors( this.object.up, _v );
|
|
|
|
|
|
- // deltaX and deltaY are in pixels; right and down are positive
|
|
|
- const pan = function () {
|
|
|
+ }
|
|
|
|
|
|
- const offset = new Vector3();
|
|
|
+ _v.multiplyScalar( distance );
|
|
|
|
|
|
- return function pan( deltaX, deltaY ) {
|
|
|
+ this._panOffset.add( _v );
|
|
|
|
|
|
- const element = scope.domElement;
|
|
|
+ }
|
|
|
|
|
|
- if ( scope.object.isPerspectiveCamera ) {
|
|
|
+ // deltaX and deltaY are in pixels; right and down are positive
|
|
|
+ _pan( deltaX, deltaY ) {
|
|
|
|
|
|
- // perspective
|
|
|
- const position = scope.object.position;
|
|
|
- offset.copy( position ).sub( scope.target );
|
|
|
- let targetDistance = offset.length();
|
|
|
+ const element = this.domElement;
|
|
|
|
|
|
- // half of the fov is center to top of screen
|
|
|
- targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
|
|
|
+ if ( this.object.isPerspectiveCamera ) {
|
|
|
|
|
|
- // we use only clientHeight here so aspect ratio does not distort speed
|
|
|
- panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
|
|
|
- panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
|
|
|
+ // perspective
|
|
|
+ const position = this.object.position;
|
|
|
+ _v.copy( position ).sub( this.target );
|
|
|
+ let targetDistance = _v.length();
|
|
|
|
|
|
- } else if ( scope.object.isOrthographicCamera ) {
|
|
|
+ // half of the fov is center to top of screen
|
|
|
+ targetDistance *= Math.tan( ( this.object.fov / 2 ) * Math.PI / 180.0 );
|
|
|
|
|
|
- // orthographic
|
|
|
- panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
|
|
|
- panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
|
|
|
+ // we use only clientHeight here so aspect ratio does not distort speed
|
|
|
+ this._panLeft( 2 * deltaX * targetDistance / element.clientHeight, this.object.matrix );
|
|
|
+ this._panUp( 2 * deltaY * targetDistance / element.clientHeight, this.object.matrix );
|
|
|
|
|
|
- } else {
|
|
|
+ } else if ( this.object.isOrthographicCamera ) {
|
|
|
|
|
|
- // camera neither orthographic nor perspective
|
|
|
- console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
|
|
|
- scope.enablePan = false;
|
|
|
+ // orthographic
|
|
|
+ this._panLeft( deltaX * ( this.object.right - this.object.left ) / this.object.zoom / element.clientWidth, this.object.matrix );
|
|
|
+ this._panUp( deltaY * ( this.object.top - this.object.bottom ) / this.object.zoom / element.clientHeight, this.object.matrix );
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- };
|
|
|
+ // camera neither orthographic nor perspective
|
|
|
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
|
|
|
+ this.enablePan = false;
|
|
|
|
|
|
- }();
|
|
|
+ }
|
|
|
|
|
|
- function dollyOut( dollyScale ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( scope.object.isPerspectiveCamera || scope.object.isOrthographicCamera ) {
|
|
|
+ _dollyOut( dollyScale ) {
|
|
|
|
|
|
- scale /= dollyScale;
|
|
|
+ if ( this.object.isPerspectiveCamera || this.object.isOrthographicCamera ) {
|
|
|
|
|
|
- } else {
|
|
|
+ this._scale /= dollyScale;
|
|
|
|
|
|
- console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
|
|
|
- scope.enableZoom = false;
|
|
|
+ } else {
|
|
|
|
|
|
- }
|
|
|
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
|
|
|
+ this.enableZoom = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
- function dollyIn( dollyScale ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( scope.object.isPerspectiveCamera || scope.object.isOrthographicCamera ) {
|
|
|
+ _dollyIn( dollyScale ) {
|
|
|
|
|
|
- scale *= dollyScale;
|
|
|
+ if ( this.object.isPerspectiveCamera || this.object.isOrthographicCamera ) {
|
|
|
|
|
|
- } else {
|
|
|
+ this._scale *= dollyScale;
|
|
|
|
|
|
- console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
|
|
|
- scope.enableZoom = false;
|
|
|
+ } else {
|
|
|
|
|
|
- }
|
|
|
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
|
|
|
+ this.enableZoom = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
- function updateZoomParameters( x, y ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( ! scope.zoomToCursor ) {
|
|
|
+ _updateZoomParameters( x, y ) {
|
|
|
|
|
|
- return;
|
|
|
-
|
|
|
- }
|
|
|
+ if ( ! this.zoomToCursor ) {
|
|
|
|
|
|
- performCursorZoom = true;
|
|
|
+ return;
|
|
|
|
|
|
- const rect = scope.domElement.getBoundingClientRect();
|
|
|
- const dx = x - rect.left;
|
|
|
- const dy = y - rect.top;
|
|
|
- const w = rect.width;
|
|
|
- const h = rect.height;
|
|
|
+ }
|
|
|
|
|
|
- mouse.x = ( dx / w ) * 2 - 1;
|
|
|
- mouse.y = - ( dy / h ) * 2 + 1;
|
|
|
+ this._performCursorZoom = true;
|
|
|
|
|
|
- dollyDirection.set( mouse.x, mouse.y, 1 ).unproject( scope.object ).sub( scope.object.position ).normalize();
|
|
|
+ const rect = this.domElement.getBoundingClientRect();
|
|
|
+ const dx = x - rect.left;
|
|
|
+ const dy = y - rect.top;
|
|
|
+ const w = rect.width;
|
|
|
+ const h = rect.height;
|
|
|
|
|
|
- }
|
|
|
+ this._mouse.x = ( dx / w ) * 2 - 1;
|
|
|
+ this._mouse.y = - ( dy / h ) * 2 + 1;
|
|
|
|
|
|
- function clampDistance( dist ) {
|
|
|
+ this._dollyDirection.set( this._mouse.x, this._mouse.y, 1 ).unproject( this.object ).sub( this.object.position ).normalize();
|
|
|
|
|
|
- return Math.max( scope.minDistance, Math.min( scope.maxDistance, dist ) );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ _clampDistance( dist ) {
|
|
|
|
|
|
- //
|
|
|
- // event callbacks - update the object state
|
|
|
- //
|
|
|
-
|
|
|
- function handleMouseDownRotate( event ) {
|
|
|
+ return Math.max( this.minDistance, Math.min( this.maxDistance, dist ) );
|
|
|
|
|
|
- rotateStart.set( event.clientX, event.clientY );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ //
|
|
|
+ // event callbacks - update the object state
|
|
|
+ //
|
|
|
|
|
|
- function handleMouseDownDolly( event ) {
|
|
|
+ _handleMouseDownRotate( event ) {
|
|
|
|
|
|
- updateZoomParameters( event.clientX, event.clientX );
|
|
|
- dollyStart.set( event.clientX, event.clientY );
|
|
|
+ this._rotateStart.set( event.clientX, event.clientY );
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- function handleMouseDownPan( event ) {
|
|
|
+ _handleMouseDownDolly( event ) {
|
|
|
|
|
|
- panStart.set( event.clientX, event.clientY );
|
|
|
+ this._updateZoomParameters( event.clientX, event.clientX );
|
|
|
+ this._dollyStart.set( event.clientX, event.clientY );
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- function handleMouseMoveRotate( event ) {
|
|
|
+ _handleMouseDownPan( event ) {
|
|
|
|
|
|
- rotateEnd.set( event.clientX, event.clientY );
|
|
|
+ this._panStart.set( event.clientX, event.clientY );
|
|
|
|
|
|
- rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
|
|
|
+ }
|
|
|
|
|
|
- const element = scope.domElement;
|
|
|
+ _handleMouseMoveRotate( event ) {
|
|
|
|
|
|
- rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
|
|
|
+ this._rotateEnd.set( event.clientX, event.clientY );
|
|
|
|
|
|
- rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
|
|
|
+ this._rotateDelta.subVectors( this._rotateEnd, this._rotateStart ).multiplyScalar( this.rotateSpeed );
|
|
|
|
|
|
- rotateStart.copy( rotateEnd );
|
|
|
+ const element = this.domElement;
|
|
|
|
|
|
- scope.update();
|
|
|
+ this._rotateLeft( _twoPI * this._rotateDelta.x / element.clientHeight ); // yes, height
|
|
|
|
|
|
- }
|
|
|
+ this._rotateUp( _twoPI * this._rotateDelta.y / element.clientHeight );
|
|
|
|
|
|
- function handleMouseMoveDolly( event ) {
|
|
|
+ this._rotateStart.copy( this._rotateEnd );
|
|
|
|
|
|
- dollyEnd.set( event.clientX, event.clientY );
|
|
|
+ this.update();
|
|
|
|
|
|
- dollyDelta.subVectors( dollyEnd, dollyStart );
|
|
|
+ }
|
|
|
|
|
|
- if ( dollyDelta.y > 0 ) {
|
|
|
+ _handleMouseMoveDolly( event ) {
|
|
|
|
|
|
- dollyOut( getZoomScale( dollyDelta.y ) );
|
|
|
+ this._dollyEnd.set( event.clientX, event.clientY );
|
|
|
|
|
|
- } else if ( dollyDelta.y < 0 ) {
|
|
|
+ this._dollyDelta.subVectors( this._dollyEnd, this._dollyStart );
|
|
|
|
|
|
- dollyIn( getZoomScale( dollyDelta.y ) );
|
|
|
+ if ( this._dollyDelta.y > 0 ) {
|
|
|
|
|
|
- }
|
|
|
+ this._dollyOut( this._getZoomScale( this._dollyDelta.y ) );
|
|
|
|
|
|
- dollyStart.copy( dollyEnd );
|
|
|
+ } else if ( this._dollyDelta.y < 0 ) {
|
|
|
|
|
|
- scope.update();
|
|
|
+ this._dollyIn( this._getZoomScale( this._dollyDelta.y ) );
|
|
|
|
|
|
}
|
|
|
|
|
|
- function handleMouseMovePan( event ) {
|
|
|
+ this._dollyStart.copy( this._dollyEnd );
|
|
|
|
|
|
- panEnd.set( event.clientX, event.clientY );
|
|
|
+ this.update();
|
|
|
|
|
|
- panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
|
|
|
+ }
|
|
|
|
|
|
- pan( panDelta.x, panDelta.y );
|
|
|
+ _handleMouseMovePan( event ) {
|
|
|
|
|
|
- panStart.copy( panEnd );
|
|
|
+ this._panEnd.set( event.clientX, event.clientY );
|
|
|
|
|
|
- scope.update();
|
|
|
+ this._panDelta.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.panSpeed );
|
|
|
|
|
|
- }
|
|
|
+ this._pan( this._panDelta.x, this._panDelta.y );
|
|
|
|
|
|
- function handleMouseWheel( event ) {
|
|
|
+ this._panStart.copy( this._panEnd );
|
|
|
|
|
|
- updateZoomParameters( event.clientX, event.clientY );
|
|
|
+ this.update();
|
|
|
|
|
|
- if ( event.deltaY < 0 ) {
|
|
|
+ }
|
|
|
|
|
|
- dollyIn( getZoomScale( event.deltaY ) );
|
|
|
+ _handleMouseWheel( event ) {
|
|
|
|
|
|
- } else if ( event.deltaY > 0 ) {
|
|
|
+ this._updateZoomParameters( event.clientX, event.clientY );
|
|
|
|
|
|
- dollyOut( getZoomScale( event.deltaY ) );
|
|
|
+ if ( event.deltaY < 0 ) {
|
|
|
|
|
|
- }
|
|
|
+ this._dollyIn( this._getZoomScale( event.deltaY ) );
|
|
|
|
|
|
- scope.update();
|
|
|
+ } else if ( event.deltaY > 0 ) {
|
|
|
+
|
|
|
+ this._dollyOut( this._getZoomScale( event.deltaY ) );
|
|
|
|
|
|
}
|
|
|
|
|
|
- function handleKeyDown( event ) {
|
|
|
+ this.update();
|
|
|
|
|
|
- let needsUpdate = false;
|
|
|
+ }
|
|
|
|
|
|
- switch ( event.code ) {
|
|
|
+ _handleKeyDown( event ) {
|
|
|
|
|
|
- case scope.keys.UP:
|
|
|
+ let needsUpdate = false;
|
|
|
|
|
|
- if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
|
|
+ switch ( event.code ) {
|
|
|
|
|
|
- rotateUp( 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
|
|
|
+ case this.keys.UP:
|
|
|
|
|
|
- } else {
|
|
|
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
|
|
|
|
|
- pan( 0, scope.keyPanSpeed );
|
|
|
+ this._rotateUp( _twoPI * this.rotateSpeed / this.domElement.clientHeight );
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- needsUpdate = true;
|
|
|
- break;
|
|
|
+ this._pan( 0, this.keyPanSpeed );
|
|
|
|
|
|
- case scope.keys.BOTTOM:
|
|
|
+ }
|
|
|
|
|
|
- if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
|
|
+ needsUpdate = true;
|
|
|
+ break;
|
|
|
|
|
|
- rotateUp( - 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
|
|
|
+ case this.keys.BOTTOM:
|
|
|
|
|
|
- } else {
|
|
|
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
|
|
|
|
|
- pan( 0, - scope.keyPanSpeed );
|
|
|
+ this._rotateUp( - _twoPI * this.rotateSpeed / this.domElement.clientHeight );
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- needsUpdate = true;
|
|
|
- break;
|
|
|
+ this._pan( 0, - this.keyPanSpeed );
|
|
|
|
|
|
- case scope.keys.LEFT:
|
|
|
+ }
|
|
|
|
|
|
- if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
|
|
+ needsUpdate = true;
|
|
|
+ break;
|
|
|
|
|
|
- rotateLeft( 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
|
|
|
+ case this.keys.LEFT:
|
|
|
|
|
|
- } else {
|
|
|
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
|
|
|
|
|
- pan( scope.keyPanSpeed, 0 );
|
|
|
+ this._rotateLeft( _twoPI * this.rotateSpeed / this.domElement.clientHeight );
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- needsUpdate = true;
|
|
|
- break;
|
|
|
+ this._pan( this.keyPanSpeed, 0 );
|
|
|
|
|
|
- case scope.keys.RIGHT:
|
|
|
+ }
|
|
|
|
|
|
- if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
|
|
+ needsUpdate = true;
|
|
|
+ break;
|
|
|
|
|
|
- rotateLeft( - 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
|
|
|
+ case this.keys.RIGHT:
|
|
|
|
|
|
- } else {
|
|
|
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
|
|
|
|
|
- pan( - scope.keyPanSpeed, 0 );
|
|
|
+ this._rotateLeft( - _twoPI * this.rotateSpeed / this.domElement.clientHeight );
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- needsUpdate = true;
|
|
|
- break;
|
|
|
+ this._pan( - this.keyPanSpeed, 0 );
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- if ( needsUpdate ) {
|
|
|
+ needsUpdate = true;
|
|
|
+ break;
|
|
|
|
|
|
- // prevent the browser from scrolling on cursor keys
|
|
|
- event.preventDefault();
|
|
|
+ }
|
|
|
|
|
|
- scope.update();
|
|
|
+ if ( needsUpdate ) {
|
|
|
|
|
|
- }
|
|
|
+ // prevent the browser from scrolling on cursor keys
|
|
|
+ event.preventDefault();
|
|
|
|
|
|
+ this.update();
|
|
|
|
|
|
}
|
|
|
|
|
|
- function handleTouchStartRotate( event ) {
|
|
|
|
|
|
- if ( pointers.length === 1 ) {
|
|
|
+ }
|
|
|
+
|
|
|
+ _handleTouchStartRotate( event ) {
|
|
|
|
|
|
- rotateStart.set( event.pageX, event.pageY );
|
|
|
+ if ( this._pointers.length === 1 ) {
|
|
|
|
|
|
- } else {
|
|
|
+ this._rotateStart.set( event.pageX, event.pageY );
|
|
|
|
|
|
- const position = getSecondPointerPosition( event );
|
|
|
+ } else {
|
|
|
|
|
|
- const x = 0.5 * ( event.pageX + position.x );
|
|
|
- const y = 0.5 * ( event.pageY + position.y );
|
|
|
+ const position = this._getSecondPointerPosition( event );
|
|
|
|
|
|
- rotateStart.set( x, y );
|
|
|
+ const x = 0.5 * ( event.pageX + position.x );
|
|
|
+ const y = 0.5 * ( event.pageY + position.y );
|
|
|
|
|
|
- }
|
|
|
+ this._rotateStart.set( x, y );
|
|
|
|
|
|
}
|
|
|
|
|
|
- function handleTouchStartPan( event ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( pointers.length === 1 ) {
|
|
|
+ _handleTouchStartPan( event ) {
|
|
|
|
|
|
- panStart.set( event.pageX, event.pageY );
|
|
|
+ if ( this._pointers.length === 1 ) {
|
|
|
|
|
|
- } else {
|
|
|
+ this._panStart.set( event.pageX, event.pageY );
|
|
|
|
|
|
- const position = getSecondPointerPosition( event );
|
|
|
+ } else {
|
|
|
|
|
|
- const x = 0.5 * ( event.pageX + position.x );
|
|
|
- const y = 0.5 * ( event.pageY + position.y );
|
|
|
+ const position = this._getSecondPointerPosition( event );
|
|
|
|
|
|
- panStart.set( x, y );
|
|
|
+ const x = 0.5 * ( event.pageX + position.x );
|
|
|
+ const y = 0.5 * ( event.pageY + position.y );
|
|
|
|
|
|
- }
|
|
|
+ this._panStart.set( x, y );
|
|
|
|
|
|
}
|
|
|
|
|
|
- function handleTouchStartDolly( event ) {
|
|
|
-
|
|
|
- const position = getSecondPointerPosition( event );
|
|
|
-
|
|
|
- const dx = event.pageX - position.x;
|
|
|
- const dy = event.pageY - position.y;
|
|
|
-
|
|
|
- const distance = Math.sqrt( dx * dx + dy * dy );
|
|
|
-
|
|
|
- dollyStart.set( 0, distance );
|
|
|
-
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- function handleTouchStartDollyPan( event ) {
|
|
|
+ _handleTouchStartDolly( event ) {
|
|
|
|
|
|
- if ( scope.enableZoom ) handleTouchStartDolly( event );
|
|
|
+ const position = this._getSecondPointerPosition( event );
|
|
|
|
|
|
- if ( scope.enablePan ) handleTouchStartPan( event );
|
|
|
+ const dx = event.pageX - position.x;
|
|
|
+ const dy = event.pageY - position.y;
|
|
|
|
|
|
- }
|
|
|
+ const distance = Math.sqrt( dx * dx + dy * dy );
|
|
|
|
|
|
- function handleTouchStartDollyRotate( event ) {
|
|
|
+ this._dollyStart.set( 0, distance );
|
|
|
|
|
|
- if ( scope.enableZoom ) handleTouchStartDolly( event );
|
|
|
+ }
|
|
|
|
|
|
- if ( scope.enableRotate ) handleTouchStartRotate( event );
|
|
|
+ _handleTouchStartDollyPan( event ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( this.enableZoom ) this._handleTouchStartDolly( event );
|
|
|
|
|
|
- function handleTouchMoveRotate( event ) {
|
|
|
+ if ( this.enablePan ) this._handleTouchStartPan( event );
|
|
|
|
|
|
- if ( pointers.length == 1 ) {
|
|
|
+ }
|
|
|
|
|
|
- rotateEnd.set( event.pageX, event.pageY );
|
|
|
+ _handleTouchStartDollyRotate( event ) {
|
|
|
|
|
|
- } else {
|
|
|
+ if ( this.enableZoom ) this._handleTouchStartDolly( event );
|
|
|
|
|
|
- const position = getSecondPointerPosition( event );
|
|
|
+ if ( this.enableRotate ) this._handleTouchStartRotate( event );
|
|
|
|
|
|
- const x = 0.5 * ( event.pageX + position.x );
|
|
|
- const y = 0.5 * ( event.pageY + position.y );
|
|
|
+ }
|
|
|
|
|
|
- rotateEnd.set( x, y );
|
|
|
+ _handleTouchMoveRotate( event ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( this._pointers.length == 1 ) {
|
|
|
|
|
|
- rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
|
|
|
+ this._rotateEnd.set( event.pageX, event.pageY );
|
|
|
|
|
|
- const element = scope.domElement;
|
|
|
+ } else {
|
|
|
|
|
|
- rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
|
|
|
+ const position = this._getSecondPointerPosition( event );
|
|
|
|
|
|
- rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
|
|
|
+ const x = 0.5 * ( event.pageX + position.x );
|
|
|
+ const y = 0.5 * ( event.pageY + position.y );
|
|
|
|
|
|
- rotateStart.copy( rotateEnd );
|
|
|
+ this._rotateEnd.set( x, y );
|
|
|
|
|
|
}
|
|
|
|
|
|
- function handleTouchMovePan( event ) {
|
|
|
+ this._rotateDelta.subVectors( this._rotateEnd, this._rotateStart ).multiplyScalar( this.rotateSpeed );
|
|
|
|
|
|
- if ( pointers.length === 1 ) {
|
|
|
+ const element = this.domElement;
|
|
|
|
|
|
- panEnd.set( event.pageX, event.pageY );
|
|
|
+ this._rotateLeft( _twoPI * this._rotateDelta.x / element.clientHeight ); // yes, height
|
|
|
|
|
|
- } else {
|
|
|
+ this._rotateUp( _twoPI * this._rotateDelta.y / element.clientHeight );
|
|
|
|
|
|
- const position = getSecondPointerPosition( event );
|
|
|
+ this._rotateStart.copy( this._rotateEnd );
|
|
|
|
|
|
- const x = 0.5 * ( event.pageX + position.x );
|
|
|
- const y = 0.5 * ( event.pageY + position.y );
|
|
|
+ }
|
|
|
|
|
|
- panEnd.set( x, y );
|
|
|
+ _handleTouchMovePan( event ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( this._pointers.length === 1 ) {
|
|
|
|
|
|
- panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
|
|
|
+ this._panEnd.set( event.pageX, event.pageY );
|
|
|
|
|
|
- pan( panDelta.x, panDelta.y );
|
|
|
+ } else {
|
|
|
|
|
|
- panStart.copy( panEnd );
|
|
|
+ const position = this._getSecondPointerPosition( event );
|
|
|
|
|
|
- }
|
|
|
+ const x = 0.5 * ( event.pageX + position.x );
|
|
|
+ const y = 0.5 * ( event.pageY + position.y );
|
|
|
|
|
|
- function handleTouchMoveDolly( event ) {
|
|
|
+ this._panEnd.set( x, y );
|
|
|
|
|
|
- const position = getSecondPointerPosition( event );
|
|
|
+ }
|
|
|
|
|
|
- const dx = event.pageX - position.x;
|
|
|
- const dy = event.pageY - position.y;
|
|
|
+ this._panDelta.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.panSpeed );
|
|
|
|
|
|
- const distance = Math.sqrt( dx * dx + dy * dy );
|
|
|
+ this._pan( this._panDelta.x, this._panDelta.y );
|
|
|
|
|
|
- dollyEnd.set( 0, distance );
|
|
|
+ this._panStart.copy( this._panEnd );
|
|
|
|
|
|
- dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
|
|
|
+ }
|
|
|
|
|
|
- dollyOut( dollyDelta.y );
|
|
|
+ _handleTouchMoveDolly( event ) {
|
|
|
|
|
|
- dollyStart.copy( dollyEnd );
|
|
|
+ const position = this._getSecondPointerPosition( event );
|
|
|
|
|
|
- const centerX = ( event.pageX + position.x ) * 0.5;
|
|
|
- const centerY = ( event.pageY + position.y ) * 0.5;
|
|
|
+ const dx = event.pageX - position.x;
|
|
|
+ const dy = event.pageY - position.y;
|
|
|
|
|
|
- updateZoomParameters( centerX, centerY );
|
|
|
+ const distance = Math.sqrt( dx * dx + dy * dy );
|
|
|
|
|
|
- }
|
|
|
+ this._dollyEnd.set( 0, distance );
|
|
|
|
|
|
- function handleTouchMoveDollyPan( event ) {
|
|
|
+ this._dollyDelta.set( 0, Math.pow( this._dollyEnd.y / this._dollyStart.y, this.zoomSpeed ) );
|
|
|
|
|
|
- if ( scope.enableZoom ) handleTouchMoveDolly( event );
|
|
|
+ this._dollyOut( this._dollyDelta.y );
|
|
|
|
|
|
- if ( scope.enablePan ) handleTouchMovePan( event );
|
|
|
+ this._dollyStart.copy( this._dollyEnd );
|
|
|
|
|
|
- }
|
|
|
+ const centerX = ( event.pageX + position.x ) * 0.5;
|
|
|
+ const centerY = ( event.pageY + position.y ) * 0.5;
|
|
|
|
|
|
- function handleTouchMoveDollyRotate( event ) {
|
|
|
+ this._updateZoomParameters( centerX, centerY );
|
|
|
|
|
|
- if ( scope.enableZoom ) handleTouchMoveDolly( event );
|
|
|
+ }
|
|
|
|
|
|
- if ( scope.enableRotate ) handleTouchMoveRotate( event );
|
|
|
+ _handleTouchMoveDollyPan( event ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( this.enableZoom ) this._handleTouchMoveDolly( event );
|
|
|
|
|
|
- //
|
|
|
- // event handlers - FSM: listen for events and reset state
|
|
|
- //
|
|
|
+ if ( this.enablePan ) this._handleTouchMovePan( event );
|
|
|
|
|
|
- function onPointerDown( event ) {
|
|
|
+ }
|
|
|
|
|
|
- if ( scope.enabled === false ) return;
|
|
|
+ _handleTouchMoveDollyRotate( event ) {
|
|
|
|
|
|
- if ( pointers.length === 0 ) {
|
|
|
+ if ( this.enableZoom ) this._handleTouchMoveDolly( event );
|
|
|
|
|
|
- scope.domElement.setPointerCapture( event.pointerId );
|
|
|
+ if ( this.enableRotate ) this._handleTouchMoveRotate( event );
|
|
|
|
|
|
- scope.domElement.addEventListener( 'pointermove', onPointerMove );
|
|
|
- scope.domElement.addEventListener( 'pointerup', onPointerUp );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ // pointers
|
|
|
|
|
|
- //
|
|
|
+ _addPointer( event ) {
|
|
|
|
|
|
- if ( isTrackingPointer( event ) ) return;
|
|
|
+ this._pointers.push( event.pointerId );
|
|
|
|
|
|
- //
|
|
|
+ }
|
|
|
|
|
|
- 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 ] == event.pointerId ) {
|
|
|
|
|
|
- onMouseDown( event );
|
|
|
+ this._pointers.splice( i, 1 );
|
|
|
+ return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- function onPointerMove( event ) {
|
|
|
-
|
|
|
- if ( scope.enabled === false ) return;
|
|
|
+ }
|
|
|
|
|
|
- if ( event.pointerType === 'touch' ) {
|
|
|
+ _isTrackingPointer( event ) {
|
|
|
|
|
|
- onTouchMove( event );
|
|
|
+ for ( let i = 0; i < this._pointers.length; i ++ ) {
|
|
|
|
|
|
- } else {
|
|
|
+ if ( this._pointers[ i ] == event.pointerId ) return true;
|
|
|
|
|
|
- onMouseMove( event );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ return false;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- function onPointerUp( event ) {
|
|
|
+ _trackPointer( event ) {
|
|
|
|
|
|
- removePointer( event );
|
|
|
+ let position = this._pointerPositions[ event.pointerId ];
|
|
|
|
|
|
- switch ( pointers.length ) {
|
|
|
+ if ( position === undefined ) {
|
|
|
|
|
|
- case 0:
|
|
|
+ position = new Vector2();
|
|
|
+ this._pointerPositions[ event.pointerId ] = position;
|
|
|
|
|
|
- scope.domElement.releasePointerCapture( event.pointerId );
|
|
|
+ }
|
|
|
|
|
|
- scope.domElement.removeEventListener( 'pointermove', onPointerMove );
|
|
|
- scope.domElement.removeEventListener( 'pointerup', onPointerUp );
|
|
|
+ position.set( event.pageX, event.pageY );
|
|
|
|
|
|
- scope.dispatchEvent( _endEvent );
|
|
|
+ }
|
|
|
|
|
|
- state = STATE.NONE;
|
|
|
+ _getSecondPointerPosition( event ) {
|
|
|
|
|
|
- break;
|
|
|
+ const pointerId = ( event.pointerId === this._pointers[ 0 ] ) ? this._pointers[ 1 ] : this._pointers[ 0 ];
|
|
|
|
|
|
- case 1:
|
|
|
+ return this._pointerPositions[ pointerId ];
|
|
|
|
|
|
- const pointerId = pointers[ 0 ];
|
|
|
- const position = pointerPositions[ pointerId ];
|
|
|
+ }
|
|
|
|
|
|
- // minimal placeholder event - allows state correction on pointer-up
|
|
|
- onTouchStart( { pointerId: pointerId, pageX: position.x, pageY: position.y } );
|
|
|
+ //
|
|
|
|
|
|
- break;
|
|
|
+ _customWheelEvent( event ) {
|
|
|
|
|
|
- }
|
|
|
+ const mode = event.deltaMode;
|
|
|
|
|
|
- }
|
|
|
+ // minimal wheel event altered to meet delta-zoom demand
|
|
|
+ const newEvent = {
|
|
|
+ clientX: event.clientX,
|
|
|
+ clientY: event.clientY,
|
|
|
+ deltaY: event.deltaY,
|
|
|
+ };
|
|
|
|
|
|
- function onMouseDown( event ) {
|
|
|
+ switch ( mode ) {
|
|
|
|
|
|
- let mouseAction;
|
|
|
+ case 1: // LINE_MODE
|
|
|
+ newEvent.deltaY *= 16;
|
|
|
+ break;
|
|
|
|
|
|
- switch ( event.button ) {
|
|
|
+ case 2: // PAGE_MODE
|
|
|
+ newEvent.deltaY *= 100;
|
|
|
+ break;
|
|
|
|
|
|
- case 0:
|
|
|
+ }
|
|
|
|
|
|
- mouseAction = scope.mouseButtons.LEFT;
|
|
|
- break;
|
|
|
+ // detect if event was triggered by pinching
|
|
|
+ if ( event.ctrlKey && ! this._controlActive ) {
|
|
|
|
|
|
- case 1:
|
|
|
+ newEvent.deltaY *= 10;
|
|
|
|
|
|
- mouseAction = scope.mouseButtons.MIDDLE;
|
|
|
- break;
|
|
|
+ }
|
|
|
|
|
|
- case 2:
|
|
|
+ return newEvent;
|
|
|
|
|
|
- mouseAction = scope.mouseButtons.RIGHT;
|
|
|
- break;
|
|
|
+ }
|
|
|
|
|
|
- default:
|
|
|
+}
|
|
|
|
|
|
- mouseAction = - 1;
|
|
|
+function onPointerDown( event ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( this.enabled === false ) return;
|
|
|
|
|
|
- switch ( mouseAction ) {
|
|
|
+ if ( this._pointers.length === 0 ) {
|
|
|
|
|
|
- case MOUSE.DOLLY:
|
|
|
+ this.domElement.setPointerCapture( event.pointerId );
|
|
|
|
|
|
- if ( scope.enableZoom === false ) return;
|
|
|
+ this.domElement.addEventListener( 'pointermove', this._onPointerMove );
|
|
|
+ this.domElement.addEventListener( 'pointerup', this._onPointerUp );
|
|
|
|
|
|
- handleMouseDownDolly( event );
|
|
|
+ }
|
|
|
|
|
|
- state = STATE.DOLLY;
|
|
|
+ //
|
|
|
|
|
|
- break;
|
|
|
+ if ( this._isTrackingPointer( event ) ) return;
|
|
|
|
|
|
- case MOUSE.ROTATE:
|
|
|
+ //
|
|
|
|
|
|
- if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
|
|
+ this._addPointer( event );
|
|
|
|
|
|
- if ( scope.enablePan === false ) return;
|
|
|
+ if ( event.pointerType === 'touch' ) {
|
|
|
|
|
|
- handleMouseDownPan( event );
|
|
|
+ this._onTouchStart( event );
|
|
|
|
|
|
- state = STATE.PAN;
|
|
|
+ } else {
|
|
|
|
|
|
- } else {
|
|
|
+ this._onMouseDown( event );
|
|
|
|
|
|
- if ( scope.enableRotate === false ) return;
|
|
|
+ }
|
|
|
|
|
|
- handleMouseDownRotate( event );
|
|
|
+}
|
|
|
|
|
|
- state = STATE.ROTATE;
|
|
|
+function onPointerMove( event ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( this.enabled === false ) return;
|
|
|
|
|
|
- break;
|
|
|
+ if ( event.pointerType === 'touch' ) {
|
|
|
|
|
|
- case MOUSE.PAN:
|
|
|
+ this._onTouchMove( event );
|
|
|
|
|
|
- if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
|
|
+ } else {
|
|
|
|
|
|
- if ( scope.enableRotate === false ) return;
|
|
|
+ this._onMouseMove( event );
|
|
|
|
|
|
- handleMouseDownRotate( event );
|
|
|
+ }
|
|
|
|
|
|
- state = STATE.ROTATE;
|
|
|
+}
|
|
|
|
|
|
- } else {
|
|
|
+function onPointerUp( event ) {
|
|
|
|
|
|
- if ( scope.enablePan === false ) return;
|
|
|
+ this._removePointer( event );
|
|
|
|
|
|
- handleMouseDownPan( event );
|
|
|
+ switch ( this._pointers.length ) {
|
|
|
|
|
|
- state = STATE.PAN;
|
|
|
+ case 0:
|
|
|
|
|
|
- }
|
|
|
+ this.domElement.releasePointerCapture( event.pointerId );
|
|
|
|
|
|
- break;
|
|
|
+ this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
|
|
|
+ this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
|
|
|
|
|
|
- default:
|
|
|
+ this.dispatchEvent( _endEvent );
|
|
|
|
|
|
- state = STATE.NONE;
|
|
|
+ this.state = _STATE.NONE;
|
|
|
|
|
|
- }
|
|
|
+ break;
|
|
|
|
|
|
- if ( state !== STATE.NONE ) {
|
|
|
+ case 1:
|
|
|
|
|
|
- scope.dispatchEvent( _startEvent );
|
|
|
+ const pointerId = this._pointers[ 0 ];
|
|
|
+ const position = this._pointerPositions[ pointerId ];
|
|
|
|
|
|
- }
|
|
|
+ // minimal placeholder event - allows state correction on pointer-up
|
|
|
+ this._onTouchStart( { pointerId: pointerId, pageX: position.x, pageY: position.y } );
|
|
|
|
|
|
- }
|
|
|
+ break;
|
|
|
|
|
|
- function onMouseMove( event ) {
|
|
|
+ }
|
|
|
|
|
|
- switch ( state ) {
|
|
|
+}
|
|
|
|
|
|
- case STATE.ROTATE:
|
|
|
+function onMouseDown( event ) {
|
|
|
|
|
|
- if ( scope.enableRotate === false ) return;
|
|
|
+ let mouseAction;
|
|
|
|
|
|
- handleMouseMoveRotate( event );
|
|
|
+ switch ( event.button ) {
|
|
|
|
|
|
- break;
|
|
|
+ case 0:
|
|
|
|
|
|
- case STATE.DOLLY:
|
|
|
+ mouseAction = this.mouseButtons.LEFT;
|
|
|
+ break;
|
|
|
|
|
|
- if ( scope.enableZoom === false ) return;
|
|
|
+ case 1:
|
|
|
|
|
|
- handleMouseMoveDolly( event );
|
|
|
+ mouseAction = this.mouseButtons.MIDDLE;
|
|
|
+ break;
|
|
|
|
|
|
- break;
|
|
|
+ case 2:
|
|
|
|
|
|
- case STATE.PAN:
|
|
|
+ mouseAction = this.mouseButtons.RIGHT;
|
|
|
+ break;
|
|
|
|
|
|
- if ( scope.enablePan === false ) return;
|
|
|
+ default:
|
|
|
|
|
|
- handleMouseMovePan( event );
|
|
|
+ mouseAction = - 1;
|
|
|
|
|
|
- break;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ switch ( mouseAction ) {
|
|
|
|
|
|
- }
|
|
|
+ case MOUSE.DOLLY:
|
|
|
|
|
|
- function onMouseWheel( event ) {
|
|
|
+ if ( this.enableZoom === false ) return;
|
|
|
|
|
|
- if ( scope.enabled === false || scope.enableZoom === false || state !== STATE.NONE ) return;
|
|
|
+ this._handleMouseDownDolly( event );
|
|
|
|
|
|
- event.preventDefault();
|
|
|
+ this.state = _STATE.DOLLY;
|
|
|
|
|
|
- scope.dispatchEvent( _startEvent );
|
|
|
+ break;
|
|
|
|
|
|
- handleMouseWheel( customWheelEvent( event ) );
|
|
|
+ case MOUSE.ROTATE:
|
|
|
|
|
|
- scope.dispatchEvent( _endEvent );
|
|
|
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( this.enablePan === false ) return;
|
|
|
|
|
|
- function customWheelEvent( event ) {
|
|
|
+ this._handleMouseDownPan( event );
|
|
|
|
|
|
- const mode = event.deltaMode;
|
|
|
+ this.state = _STATE.PAN;
|
|
|
|
|
|
- // minimal wheel event altered to meet delta-zoom demand
|
|
|
- const newEvent = {
|
|
|
- clientX: event.clientX,
|
|
|
- clientY: event.clientY,
|
|
|
- deltaY: event.deltaY,
|
|
|
- };
|
|
|
+ } else {
|
|
|
|
|
|
- switch ( mode ) {
|
|
|
+ if ( this.enableRotate === false ) return;
|
|
|
|
|
|
- case 1: // LINE_MODE
|
|
|
- newEvent.deltaY *= 16;
|
|
|
- break;
|
|
|
+ this._handleMouseDownRotate( event );
|
|
|
|
|
|
- case 2: // PAGE_MODE
|
|
|
- newEvent.deltaY *= 100;
|
|
|
- break;
|
|
|
+ this.state = _STATE.ROTATE;
|
|
|
|
|
|
}
|
|
|
|
|
|
- // detect if event was triggered by pinching
|
|
|
- if ( event.ctrlKey && ! controlActive ) {
|
|
|
+ break;
|
|
|
|
|
|
- newEvent.deltaY *= 10;
|
|
|
+ case MOUSE.PAN:
|
|
|
|
|
|
- }
|
|
|
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
|
|
|
|
|
|
- return newEvent;
|
|
|
+ if ( this.enableRotate === false ) return;
|
|
|
|
|
|
- }
|
|
|
+ this._handleMouseDownRotate( event );
|
|
|
|
|
|
- function interceptControlDown( event ) {
|
|
|
+ this.state = _STATE.ROTATE;
|
|
|
|
|
|
- if ( event.key === 'Control' ) {
|
|
|
-
|
|
|
- controlActive = true;
|
|
|
+ } else {
|
|
|
|
|
|
+ if ( this.enablePan === false ) return;
|
|
|
|
|
|
- const document = scope.domElement.getRootNode(); // offscreen canvas compatibility
|
|
|
+ this._handleMouseDownPan( event );
|
|
|
|
|
|
- document.addEventListener( 'keyup', interceptControlUp, { passive: true, capture: true } );
|
|
|
+ this.state = _STATE.PAN;
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- function interceptControlUp( event ) {
|
|
|
+ break;
|
|
|
|
|
|
- if ( event.key === 'Control' ) {
|
|
|
+ default:
|
|
|
|
|
|
- controlActive = false;
|
|
|
+ this.state = _STATE.NONE;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- const document = scope.domElement.getRootNode(); // offscreen canvas compatibility
|
|
|
+ if ( this.state !== _STATE.NONE ) {
|
|
|
|
|
|
- document.removeEventListener( 'keyup', interceptControlUp, { passive: true, capture: true } );
|
|
|
+ this.dispatchEvent( _startEvent );
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+}
|
|
|
|
|
|
- function onKeyDown( event ) {
|
|
|
+function onMouseMove( event ) {
|
|
|
|
|
|
- if ( scope.enabled === false || scope.enablePan === false ) return;
|
|
|
+ switch ( this.state ) {
|
|
|
|
|
|
- handleKeyDown( event );
|
|
|
+ case _STATE.ROTATE:
|
|
|
|
|
|
- }
|
|
|
+ if ( this.enableRotate === false ) return;
|
|
|
|
|
|
- function onTouchStart( event ) {
|
|
|
+ this._handleMouseMoveRotate( event );
|
|
|
|
|
|
- trackPointer( event );
|
|
|
+ break;
|
|
|
|
|
|
- switch ( pointers.length ) {
|
|
|
+ case _STATE.DOLLY:
|
|
|
|
|
|
- case 1:
|
|
|
+ if ( this.enableZoom === false ) return;
|
|
|
|
|
|
- switch ( scope.touches.ONE ) {
|
|
|
+ this._handleMouseMoveDolly( event );
|
|
|
|
|
|
- case TOUCH.ROTATE:
|
|
|
+ break;
|
|
|
|
|
|
- if ( scope.enableRotate === false ) return;
|
|
|
+ case _STATE.PAN:
|
|
|
|
|
|
- handleTouchStartRotate( event );
|
|
|
+ if ( this.enablePan === false ) return;
|
|
|
|
|
|
- state = STATE.TOUCH_ROTATE;
|
|
|
+ this._handleMouseMovePan( event );
|
|
|
|
|
|
- break;
|
|
|
+ break;
|
|
|
|
|
|
- case TOUCH.PAN:
|
|
|
+ }
|
|
|
|
|
|
- if ( scope.enablePan === false ) return;
|
|
|
+}
|
|
|
|
|
|
- handleTouchStartPan( event );
|
|
|
+function onMouseWheel( event ) {
|
|
|
|
|
|
- state = STATE.TOUCH_PAN;
|
|
|
+ if ( this.enabled === false || this.enableZoom === false || this.state !== _STATE.NONE ) return;
|
|
|
|
|
|
- break;
|
|
|
+ event.preventDefault();
|
|
|
|
|
|
- default:
|
|
|
+ this.dispatchEvent( _startEvent );
|
|
|
|
|
|
- state = STATE.NONE;
|
|
|
+ this._handleMouseWheel( this._customWheelEvent( event ) );
|
|
|
|
|
|
- }
|
|
|
+ this.dispatchEvent( _endEvent );
|
|
|
|
|
|
- break;
|
|
|
+}
|
|
|
|
|
|
- case 2:
|
|
|
+function onKeyDown( event ) {
|
|
|
|
|
|
- switch ( scope.touches.TWO ) {
|
|
|
+ if ( this.enabled === false || this.enablePan === false ) return;
|
|
|
|
|
|
- case TOUCH.DOLLY_PAN:
|
|
|
+ this._handleKeyDown( event );
|
|
|
|
|
|
- if ( scope.enableZoom === false && scope.enablePan === false ) return;
|
|
|
+}
|
|
|
|
|
|
- handleTouchStartDollyPan( event );
|
|
|
+function onTouchStart( event ) {
|
|
|
|
|
|
- state = STATE.TOUCH_DOLLY_PAN;
|
|
|
+ this._trackPointer( event );
|
|
|
|
|
|
- break;
|
|
|
+ switch ( this._pointers.length ) {
|
|
|
|
|
|
- case TOUCH.DOLLY_ROTATE:
|
|
|
+ case 1:
|
|
|
|
|
|
- if ( scope.enableZoom === false && scope.enableRotate === false ) return;
|
|
|
+ switch ( this.touches.ONE ) {
|
|
|
|
|
|
- handleTouchStartDollyRotate( event );
|
|
|
+ case TOUCH.ROTATE:
|
|
|
|
|
|
- state = STATE.TOUCH_DOLLY_ROTATE;
|
|
|
+ if ( this.enableRotate === false ) return;
|
|
|
|
|
|
- break;
|
|
|
+ this._handleTouchStartRotate( event );
|
|
|
|
|
|
- default:
|
|
|
+ this.state = _STATE.TOUCH_ROTATE;
|
|
|
|
|
|
- state = STATE.NONE;
|
|
|
+ break;
|
|
|
|
|
|
- }
|
|
|
+ case TOUCH.PAN:
|
|
|
|
|
|
- break;
|
|
|
+ if ( this.enablePan === false ) return;
|
|
|
|
|
|
- default:
|
|
|
+ this._handleTouchStartPan( event );
|
|
|
|
|
|
- state = STATE.NONE;
|
|
|
+ this.state = _STATE.TOUCH_PAN;
|
|
|
|
|
|
- }
|
|
|
+ break;
|
|
|
|
|
|
- if ( state !== STATE.NONE ) {
|
|
|
+ default:
|
|
|
|
|
|
- scope.dispatchEvent( _startEvent );
|
|
|
+ this.state = _STATE.NONE;
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
+ break;
|
|
|
|
|
|
- function onTouchMove( event ) {
|
|
|
+ case 2:
|
|
|
|
|
|
- trackPointer( event );
|
|
|
+ switch ( this.touches.TWO ) {
|
|
|
|
|
|
- switch ( state ) {
|
|
|
+ case TOUCH.DOLLY_PAN:
|
|
|
|
|
|
- case STATE.TOUCH_ROTATE:
|
|
|
+ if ( this.enableZoom === false && this.enablePan === false ) return;
|
|
|
|
|
|
- if ( scope.enableRotate === false ) return;
|
|
|
+ this._handleTouchStartDollyPan( event );
|
|
|
|
|
|
- handleTouchMoveRotate( event );
|
|
|
-
|
|
|
- scope.update();
|
|
|
+ this.state = _STATE.TOUCH_DOLLY_PAN;
|
|
|
|
|
|
break;
|
|
|
|
|
|
- case STATE.TOUCH_PAN:
|
|
|
+ case TOUCH.DOLLY_ROTATE:
|
|
|
|
|
|
- if ( scope.enablePan === false ) return;
|
|
|
+ if ( this.enableZoom === false && this.enableRotate === false ) return;
|
|
|
|
|
|
- handleTouchMovePan( event );
|
|
|
+ this._handleTouchStartDollyRotate( event );
|
|
|
|
|
|
- scope.update();
|
|
|
+ this.state = _STATE.TOUCH_DOLLY_ROTATE;
|
|
|
|
|
|
break;
|
|
|
|
|
|
- case STATE.TOUCH_DOLLY_PAN:
|
|
|
+ default:
|
|
|
|
|
|
- if ( scope.enableZoom === false && scope.enablePan === false ) return;
|
|
|
+ this.state = _STATE.NONE;
|
|
|
|
|
|
- handleTouchMoveDollyPan( event );
|
|
|
+ }
|
|
|
|
|
|
- scope.update();
|
|
|
+ break;
|
|
|
|
|
|
- break;
|
|
|
+ default:
|
|
|
|
|
|
- case STATE.TOUCH_DOLLY_ROTATE:
|
|
|
+ this.state = _STATE.NONE;
|
|
|
|
|
|
- if ( scope.enableZoom === false && scope.enableRotate === false ) return;
|
|
|
+ }
|
|
|
|
|
|
- handleTouchMoveDollyRotate( event );
|
|
|
+ if ( this.state !== _STATE.NONE ) {
|
|
|
|
|
|
- scope.update();
|
|
|
+ this.dispatchEvent( _startEvent );
|
|
|
|
|
|
- break;
|
|
|
+ }
|
|
|
|
|
|
- default:
|
|
|
+}
|
|
|
|
|
|
- state = STATE.NONE;
|
|
|
+function onTouchMove( event ) {
|
|
|
|
|
|
- }
|
|
|
+ this._trackPointer( event );
|
|
|
|
|
|
- }
|
|
|
+ switch ( this.state ) {
|
|
|
|
|
|
- function onContextMenu( event ) {
|
|
|
+ case _STATE.TOUCH_ROTATE:
|
|
|
|
|
|
- if ( scope.enabled === false ) return;
|
|
|
+ if ( this.enableRotate === false ) return;
|
|
|
|
|
|
- event.preventDefault();
|
|
|
+ this._handleTouchMoveRotate( event );
|
|
|
|
|
|
- }
|
|
|
+ this.update();
|
|
|
|
|
|
- function addPointer( event ) {
|
|
|
+ break;
|
|
|
|
|
|
- pointers.push( event.pointerId );
|
|
|
+ case _STATE.TOUCH_PAN:
|
|
|
|
|
|
- }
|
|
|
+ if ( this.enablePan === false ) return;
|
|
|
|
|
|
- function removePointer( event ) {
|
|
|
+ this._handleTouchMovePan( event );
|
|
|
|
|
|
- delete pointerPositions[ event.pointerId ];
|
|
|
+ this.update();
|
|
|
|
|
|
- for ( let i = 0; i < pointers.length; i ++ ) {
|
|
|
+ break;
|
|
|
|
|
|
- if ( pointers[ i ] == event.pointerId ) {
|
|
|
+ case _STATE.TOUCH_DOLLY_PAN:
|
|
|
|
|
|
- pointers.splice( i, 1 );
|
|
|
- return;
|
|
|
+ if ( this.enableZoom === false && this.enablePan === false ) return;
|
|
|
|
|
|
- }
|
|
|
+ this._handleTouchMoveDollyPan( event );
|
|
|
|
|
|
- }
|
|
|
+ this.update();
|
|
|
|
|
|
- }
|
|
|
+ break;
|
|
|
|
|
|
- function isTrackingPointer( event ) {
|
|
|
+ case _STATE.TOUCH_DOLLY_ROTATE:
|
|
|
|
|
|
- for ( let i = 0; i < pointers.length; i ++ ) {
|
|
|
+ if ( this.enableZoom === false && this.enableRotate === false ) return;
|
|
|
|
|
|
- if ( pointers[ i ] == event.pointerId ) return true;
|
|
|
+ this._handleTouchMoveDollyRotate( event );
|
|
|
|
|
|
- }
|
|
|
+ this.update();
|
|
|
|
|
|
- return false;
|
|
|
+ break;
|
|
|
|
|
|
- }
|
|
|
+ default:
|
|
|
|
|
|
- function trackPointer( event ) {
|
|
|
+ this.state = _STATE.NONE;
|
|
|
|
|
|
- let position = pointerPositions[ event.pointerId ];
|
|
|
+ }
|
|
|
|
|
|
- if ( position === undefined ) {
|
|
|
+}
|
|
|
|
|
|
- position = new Vector2();
|
|
|
- pointerPositions[ event.pointerId ] = position;
|
|
|
+function onContextMenu( event ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( this.enabled === false ) return;
|
|
|
|
|
|
- position.set( event.pageX, event.pageY );
|
|
|
+ event.preventDefault();
|
|
|
|
|
|
- }
|
|
|
+}
|
|
|
|
|
|
- function getSecondPointerPosition( event ) {
|
|
|
+function interceptControlDown( event ) {
|
|
|
|
|
|
- const pointerId = ( event.pointerId === pointers[ 0 ] ) ? pointers[ 1 ] : pointers[ 0 ];
|
|
|
+ if ( event.key === 'Control' ) {
|
|
|
|
|
|
- return pointerPositions[ pointerId ];
|
|
|
+ this._controlActive = true;
|
|
|
|
|
|
- }
|
|
|
+ const document = this.domElement.getRootNode(); // offscreen canvas compatibility
|
|
|
|
|
|
- //
|
|
|
+ document.addEventListener( 'keyup', this._interceptControlUp, { passive: true, capture: true } );
|
|
|
|
|
|
- scope.domElement.addEventListener( 'contextmenu', onContextMenu );
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
- scope.domElement.addEventListener( 'pointerdown', onPointerDown );
|
|
|
- scope.domElement.addEventListener( 'pointercancel', onPointerUp );
|
|
|
- scope.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
|
|
|
+function interceptControlUp( event ) {
|
|
|
|
|
|
- const document = scope.domElement.getRootNode(); // offscreen canvas compatibility
|
|
|
+ if ( event.key === 'Control' ) {
|
|
|
|
|
|
- document.addEventListener( 'keydown', interceptControlDown, { passive: true, capture: true } );
|
|
|
+ this._controlActive = false;
|
|
|
|
|
|
- // force an update at start
|
|
|
+ const document = this.domElement.getRootNode(); // offscreen canvas compatibility
|
|
|
|
|
|
- this.update();
|
|
|
+ document.removeEventListener( 'keyup', this._interceptControlUp, { passive: true, capture: true } );
|
|
|
|
|
|
}
|
|
|
|