BlueGit

timfa filemanager

Files

filemanager / public / vendor / three / addons / controls / OrbitControls.js

OrbitControls.js

ff762d33 History
OrbitControls.js 31.5 KiB Download Raw
1import {
2 EventDispatcher,
3 MOUSE,
4 Quaternion,
5 Spherical,
6 TOUCH,
7 Vector2,
8 Vector3,
9 Plane,
10 Ray,
11 MathUtils
12} from 'three';
13
14// OrbitControls performs orbiting, dollying (zooming), and panning.
15// Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
16//
17// Orbit - left mouse / touch: one-finger move
18// Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
19// Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move
20
21const _changeEvent = { type: 'change' };
22const _startEvent = { type: 'start' };
23const _endEvent = { type: 'end' };
24const _ray = new Ray();
25const _plane = new Plane();
26const TILT_LIMIT = Math.cos( 70 * MathUtils.DEG2RAD );
27
28class OrbitControls extends EventDispatcher {
29
30 constructor( object, domElement ) {
31
32 super();
33
34 this.object = object;
35 this.domElement = domElement;
36 this.domElement.style.touchAction = 'none'; // disable touch scroll
37
38 // Set to false to disable this control
39 this.enabled = true;
40
41 // "target" sets the location of focus, where the object orbits around
42 this.target = new Vector3();
43
44 // Sets the 3D cursor (similar to Blender), from which the maxTargetRadius takes effect
45 this.cursor = new Vector3();
46
47 // How far you can dolly in and out ( PerspectiveCamera only )
48 this.minDistance = 0;
49 this.maxDistance = Infinity;
50
51 // How far you can zoom in and out ( OrthographicCamera only )
52 this.minZoom = 0;
53 this.maxZoom = Infinity;
54
55 // Limit camera target within a spherical area around the cursor
56 this.minTargetRadius = 0;
57 this.maxTargetRadius = Infinity;
58
59 // How far you can orbit vertically, upper and lower limits.
60 // Range is 0 to Math.PI radians.
61 this.minPolarAngle = 0; // radians
62 this.maxPolarAngle = Math.PI; // radians
63
64 // How far you can orbit horizontally, upper and lower limits.
65 // If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI )
66 this.minAzimuthAngle = - Infinity; // radians
67 this.maxAzimuthAngle = Infinity; // radians
68
69 // Set to true to enable damping (inertia)
70 // If damping is enabled, you must call controls.update() in your animation loop
71 this.enableDamping = false;
72 this.dampingFactor = 0.05;
73
74 // This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
75 // Set to false to disable zooming
76 this.enableZoom = true;
77 this.zoomSpeed = 1.0;
78
79 // Set to false to disable rotating
80 this.enableRotate = true;
81 this.rotateSpeed = 1.0;
82
83 // Set to false to disable panning
84 this.enablePan = true;
85 this.panSpeed = 1.0;
86 this.screenSpacePanning = true; // if false, pan orthogonal to world-space direction camera.up
87 this.keyPanSpeed = 7.0; // pixels moved per arrow key push
88 this.zoomToCursor = false;
89
90 // Set to true to automatically rotate around the target
91 // If auto-rotate is enabled, you must call controls.update() in your animation loop
92 this.autoRotate = false;
93 this.autoRotateSpeed = 2.0; // 30 seconds per orbit when fps is 60
94
95 // The four arrow keys
96 this.keys = { LEFT: 'ArrowLeft', UP: 'ArrowUp', RIGHT: 'ArrowRight', BOTTOM: 'ArrowDown' };
97
98 // Mouse buttons
99 this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
100
101 // Touch fingers
102 this.touches = { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN };
103
104 // for reset
105 this.target0 = this.target.clone();
106 this.position0 = this.object.position.clone();
107 this.zoom0 = this.object.zoom;
108
109 // the target DOM element for key events
110 this._domElementKeyEvents = null;
111
112 //
113 // public methods
114 //
115
116 this.getPolarAngle = function () {
117
118 return spherical.phi;
119
120 };
121
122 this.getAzimuthalAngle = function () {
123
124 return spherical.theta;
125
126 };
127
128 this.getDistance = function () {
129
130 return this.object.position.distanceTo( this.target );
131
132 };
133
134 this.listenToKeyEvents = function ( domElement ) {
135
136 domElement.addEventListener( 'keydown', onKeyDown );
137 this._domElementKeyEvents = domElement;
138
139 };
140
141 this.stopListenToKeyEvents = function () {
142
143 this._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown );
144 this._domElementKeyEvents = null;
145
146 };
147
148 this.saveState = function () {
149
150 scope.target0.copy( scope.target );
151 scope.position0.copy( scope.object.position );
152 scope.zoom0 = scope.object.zoom;
153
154 };
155
156 this.reset = function () {
157
158 scope.target.copy( scope.target0 );
159 scope.object.position.copy( scope.position0 );
160 scope.object.zoom = scope.zoom0;
161
162 scope.object.updateProjectionMatrix();
163 scope.dispatchEvent( _changeEvent );
164
165 scope.update();
166
167 state = STATE.NONE;
168
169 };
170
171 // this method is exposed, but perhaps it would be better if we can make it private...
172 this.update = function () {
173
174 const offset = new Vector3();
175
176 // so camera.up is the orbit axis
177 const quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
178 const quatInverse = quat.clone().invert();
179
180 const lastPosition = new Vector3();
181 const lastQuaternion = new Quaternion();
182 const lastTargetPosition = new Vector3();
183
184 const twoPI = 2 * Math.PI;
185
186 return function update( deltaTime = null ) {
187
188 const position = scope.object.position;
189
190 offset.copy( position ).sub( scope.target );
191
192 // rotate offset to "y-axis-is-up" space
193 offset.applyQuaternion( quat );
194
195 // angle from z-axis around y-axis
196 spherical.setFromVector3( offset );
197
198 if ( scope.autoRotate && state === STATE.NONE ) {
199
200 rotateLeft( getAutoRotationAngle( deltaTime ) );
201
202 }
203
204 if ( scope.enableDamping ) {
205
206 spherical.theta += sphericalDelta.theta * scope.dampingFactor;
207 spherical.phi += sphericalDelta.phi * scope.dampingFactor;
208
209 } else {
210
211 spherical.theta += sphericalDelta.theta;
212 spherical.phi += sphericalDelta.phi;
213
214 }
215
216 // restrict theta to be between desired limits
217
218 let min = scope.minAzimuthAngle;
219 let max = scope.maxAzimuthAngle;
220
221 if ( isFinite( min ) && isFinite( max ) ) {
222
223 if ( min < - Math.PI ) min += twoPI; else if ( min > Math.PI ) min -= twoPI;
224
225 if ( max < - Math.PI ) max += twoPI; else if ( max > Math.PI ) max -= twoPI;
226
227 if ( min <= max ) {
228
229 spherical.theta = Math.max( min, Math.min( max, spherical.theta ) );
230
231 } else {
232
233 spherical.theta = ( spherical.theta > ( min + max ) / 2 ) ?
234 Math.max( min, spherical.theta ) :
235 Math.min( max, spherical.theta );
236
237 }
238
239 }
240
241 // restrict phi to be between desired limits
242 spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
243
244 spherical.makeSafe();
245
246
247 // move target to panned location
248
249 if ( scope.enableDamping === true ) {
250
251 scope.target.addScaledVector( panOffset, scope.dampingFactor );
252
253 } else {
254
255 scope.target.add( panOffset );
256
257 }
258
259 // Limit the target distance from the cursor to create a sphere around the center of interest
260 scope.target.sub( scope.cursor );
261 scope.target.clampLength( scope.minTargetRadius, scope.maxTargetRadius );
262 scope.target.add( scope.cursor );
263
264 let zoomChanged = false;
265 // adjust the camera position based on zoom only if we're not zooming to the cursor or if it's an ortho camera
266 // we adjust zoom later in these cases
267 if ( scope.zoomToCursor && performCursorZoom || scope.object.isOrthographicCamera ) {
268
269 spherical.radius = clampDistance( spherical.radius );
270
271 } else {
272
273 const prevRadius = spherical.radius;
274 spherical.radius = clampDistance( spherical.radius * scale );
275 zoomChanged = prevRadius != spherical.radius;
276
277 }
278
279 offset.setFromSpherical( spherical );
280
281 // rotate offset back to "camera-up-vector-is-up" space
282 offset.applyQuaternion( quatInverse );
283
284 position.copy( scope.target ).add( offset );
285
286 scope.object.lookAt( scope.target );
287
288 if ( scope.enableDamping === true ) {
289
290 sphericalDelta.theta *= ( 1 - scope.dampingFactor );
291 sphericalDelta.phi *= ( 1 - scope.dampingFactor );
292
293 panOffset.multiplyScalar( 1 - scope.dampingFactor );
294
295 } else {
296
297 sphericalDelta.set( 0, 0, 0 );
298
299 panOffset.set( 0, 0, 0 );
300
301 }
302
303 // adjust camera position
304 if ( scope.zoomToCursor && performCursorZoom ) {
305
306 let newRadius = null;
307 if ( scope.object.isPerspectiveCamera ) {
308
309 // move the camera down the pointer ray
310 // this method avoids floating point error
311 const prevRadius = offset.length();
312 newRadius = clampDistance( prevRadius * scale );
313
314 const radiusDelta = prevRadius - newRadius;
315 scope.object.position.addScaledVector( dollyDirection, radiusDelta );
316 scope.object.updateMatrixWorld();
317
318 zoomChanged = !! radiusDelta;
319
320 } else if ( scope.object.isOrthographicCamera ) {
321
322 // adjust the ortho camera position based on zoom changes
323 const mouseBefore = new Vector3( mouse.x, mouse.y, 0 );
324 mouseBefore.unproject( scope.object );
325
326 const prevZoom = scope.object.zoom;
327 scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / scale ) );
328 scope.object.updateProjectionMatrix();
329
330 zoomChanged = prevZoom !== scope.object.zoom;
331
332 const mouseAfter = new Vector3( mouse.x, mouse.y, 0 );
333 mouseAfter.unproject( scope.object );
334
335 scope.object.position.sub( mouseAfter ).add( mouseBefore );
336 scope.object.updateMatrixWorld();
337
338 newRadius = offset.length();
339
340 } else {
341
342 console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - zoom to cursor disabled.' );
343 scope.zoomToCursor = false;
344
345 }
346
347 // handle the placement of the target
348 if ( newRadius !== null ) {
349
350 if ( this.screenSpacePanning ) {
351
352 // position the orbit target in front of the new camera position
353 scope.target.set( 0, 0, - 1 )
354 .transformDirection( scope.object.matrix )
355 .multiplyScalar( newRadius )
356 .add( scope.object.position );
357
358 } else {
359
360 // get the ray and translation plane to compute target
361 _ray.origin.copy( scope.object.position );
362 _ray.direction.set( 0, 0, - 1 ).transformDirection( scope.object.matrix );
363
364 // if the camera is 20 degrees above the horizon then don't adjust the focus target to avoid
365 // extremely large values
366 if ( Math.abs( scope.object.up.dot( _ray.direction ) ) < TILT_LIMIT ) {
367
368 object.lookAt( scope.target );
369
370 } else {
371
372 _plane.setFromNormalAndCoplanarPoint( scope.object.up, scope.target );
373 _ray.intersectPlane( _plane, scope.target );
374
375 }
376
377 }
378
379 }
380
381 } else if ( scope.object.isOrthographicCamera ) {
382
383 const prevZoom = scope.object.zoom;
384 scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / scale ) );
385
386 if ( prevZoom !== scope.object.zoom ) {
387
388 scope.object.updateProjectionMatrix();
389 zoomChanged = true;
390
391 }
392
393 }
394
395 scale = 1;
396 performCursorZoom = false;
397
398 // update condition is:
399 // min(camera displacement, camera rotation in radians)^2 > EPS
400 // using small-angle approximation cos(x/2) = 1 - x^2 / 8
401
402 if ( zoomChanged ||
403 lastPosition.distanceToSquared( scope.object.position ) > EPS ||
404 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ||
405 lastTargetPosition.distanceToSquared( scope.target ) > EPS ) {
406
407 scope.dispatchEvent( _changeEvent );
408
409 lastPosition.copy( scope.object.position );
410 lastQuaternion.copy( scope.object.quaternion );
411 lastTargetPosition.copy( scope.target );
412
413 return true;
414
415 }
416
417 return false;
418
419 };
420
421 }();
422
423 this.dispose = function () {
424
425 scope.domElement.removeEventListener( 'contextmenu', onContextMenu );
426
427 scope.domElement.removeEventListener( 'pointerdown', onPointerDown );
428 scope.domElement.removeEventListener( 'pointercancel', onPointerUp );
429 scope.domElement.removeEventListener( 'wheel', onMouseWheel );
430
431 scope.domElement.removeEventListener( 'pointermove', onPointerMove );
432 scope.domElement.removeEventListener( 'pointerup', onPointerUp );
433
434 const document = scope.domElement.getRootNode(); // offscreen canvas compatibility
435
436 document.removeEventListener( 'keydown', interceptControlDown, { capture: true } );
437
438 if ( scope._domElementKeyEvents !== null ) {
439
440 scope._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown );
441 scope._domElementKeyEvents = null;
442
443 }
444
445 //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
446
447 };
448
449 //
450 // internals
451 //
452
453 const scope = this;
454
455 const STATE = {
456 NONE: - 1,
457 ROTATE: 0,
458 DOLLY: 1,
459 PAN: 2,
460 TOUCH_ROTATE: 3,
461 TOUCH_PAN: 4,
462 TOUCH_DOLLY_PAN: 5,
463 TOUCH_DOLLY_ROTATE: 6
464 };
465
466 let state = STATE.NONE;
467
468 const EPS = 0.000001;
469
470 // current position in spherical coordinates
471 const spherical = new Spherical();
472 const sphericalDelta = new Spherical();
473
474 let scale = 1;
475 const panOffset = new Vector3();
476
477 const rotateStart = new Vector2();
478 const rotateEnd = new Vector2();
479 const rotateDelta = new Vector2();
480
481 const panStart = new Vector2();
482 const panEnd = new Vector2();
483 const panDelta = new Vector2();
484
485 const dollyStart = new Vector2();
486 const dollyEnd = new Vector2();
487 const dollyDelta = new Vector2();
488
489 const dollyDirection = new Vector3();
490 const mouse = new Vector2();
491 let performCursorZoom = false;
492
493 const pointers = [];
494 const pointerPositions = {};
495
496 let controlActive = false;
497
498 function getAutoRotationAngle( deltaTime ) {
499
500 if ( deltaTime !== null ) {
501
502 return ( 2 * Math.PI / 60 * scope.autoRotateSpeed ) * deltaTime;
503
504 } else {
505
506 return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
507
508 }
509
510 }
511
512 function getZoomScale( delta ) {
513
514 const normalizedDelta = Math.abs( delta * 0.01 );
515 return Math.pow( 0.95, scope.zoomSpeed * normalizedDelta );
516
517 }
518
519 function rotateLeft( angle ) {
520
521 sphericalDelta.theta -= angle;
522
523 }
524
525 function rotateUp( angle ) {
526
527 sphericalDelta.phi -= angle;
528
529 }
530
531 const panLeft = function () {
532
533 const v = new Vector3();
534
535 return function panLeft( distance, objectMatrix ) {
536
537 v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
538 v.multiplyScalar( - distance );
539
540 panOffset.add( v );
541
542 };
543
544 }();
545
546 const panUp = function () {
547
548 const v = new Vector3();
549
550 return function panUp( distance, objectMatrix ) {
551
552 if ( scope.screenSpacePanning === true ) {
553
554 v.setFromMatrixColumn( objectMatrix, 1 );
555
556 } else {
557
558 v.setFromMatrixColumn( objectMatrix, 0 );
559 v.crossVectors( scope.object.up, v );
560
561 }
562
563 v.multiplyScalar( distance );
564
565 panOffset.add( v );
566
567 };
568
569 }();
570
571 // deltaX and deltaY are in pixels; right and down are positive
572 const pan = function () {
573
574 const offset = new Vector3();
575
576 return function pan( deltaX, deltaY ) {
577
578 const element = scope.domElement;
579
580 if ( scope.object.isPerspectiveCamera ) {
581
582 // perspective
583 const position = scope.object.position;
584 offset.copy( position ).sub( scope.target );
585 let targetDistance = offset.length();
586
587 // half of the fov is center to top of screen
588 targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
589
590 // we use only clientHeight here so aspect ratio does not distort speed
591 panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
592 panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
593
594 } else if ( scope.object.isOrthographicCamera ) {
595
596 // orthographic
597 panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
598 panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
599
600 } else {
601
602 // camera neither orthographic nor perspective
603 console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
604 scope.enablePan = false;
605
606 }
607
608 };
609
610 }();
611
612 function dollyOut( dollyScale ) {
613
614 if ( scope.object.isPerspectiveCamera || scope.object.isOrthographicCamera ) {
615
616 scale /= dollyScale;
617
618 } else {
619
620 console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
621 scope.enableZoom = false;
622
623 }
624
625 }
626
627 function dollyIn( dollyScale ) {
628
629 if ( scope.object.isPerspectiveCamera || scope.object.isOrthographicCamera ) {
630
631 scale *= dollyScale;
632
633 } else {
634
635 console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
636 scope.enableZoom = false;
637
638 }
639
640 }
641
642 function updateZoomParameters( x, y ) {
643
644 if ( ! scope.zoomToCursor ) {
645
646 return;
647
648 }
649
650 performCursorZoom = true;
651
652 const rect = scope.domElement.getBoundingClientRect();
653 const dx = x - rect.left;
654 const dy = y - rect.top;
655 const w = rect.width;
656 const h = rect.height;
657
658 mouse.x = ( dx / w ) * 2 - 1;
659 mouse.y = - ( dy / h ) * 2 + 1;
660
661 dollyDirection.set( mouse.x, mouse.y, 1 ).unproject( scope.object ).sub( scope.object.position ).normalize();
662
663 }
664
665 function clampDistance( dist ) {
666
667 return Math.max( scope.minDistance, Math.min( scope.maxDistance, dist ) );
668
669 }
670
671 //
672 // event callbacks - update the object state
673 //
674
675 function handleMouseDownRotate( event ) {
676
677 rotateStart.set( event.clientX, event.clientY );
678
679 }
680
681 function handleMouseDownDolly( event ) {
682
683 updateZoomParameters( event.clientX, event.clientX );
684 dollyStart.set( event.clientX, event.clientY );
685
686 }
687
688 function handleMouseDownPan( event ) {
689
690 panStart.set( event.clientX, event.clientY );
691
692 }
693
694 function handleMouseMoveRotate( event ) {
695
696 rotateEnd.set( event.clientX, event.clientY );
697
698 rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
699
700 const element = scope.domElement;
701
702 rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
703
704 rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
705
706 rotateStart.copy( rotateEnd );
707
708 scope.update();
709
710 }
711
712 function handleMouseMoveDolly( event ) {
713
714 dollyEnd.set( event.clientX, event.clientY );
715
716 dollyDelta.subVectors( dollyEnd, dollyStart );
717
718 if ( dollyDelta.y > 0 ) {
719
720 dollyOut( getZoomScale( dollyDelta.y ) );
721
722 } else if ( dollyDelta.y < 0 ) {
723
724 dollyIn( getZoomScale( dollyDelta.y ) );
725
726 }
727
728 dollyStart.copy( dollyEnd );
729
730 scope.update();
731
732 }
733
734 function handleMouseMovePan( event ) {
735
736 panEnd.set( event.clientX, event.clientY );
737
738 panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
739
740 pan( panDelta.x, panDelta.y );
741
742 panStart.copy( panEnd );
743
744 scope.update();
745
746 }
747
748 function handleMouseWheel( event ) {
749
750 updateZoomParameters( event.clientX, event.clientY );
751
752 if ( event.deltaY < 0 ) {
753
754 dollyIn( getZoomScale( event.deltaY ) );
755
756 } else if ( event.deltaY > 0 ) {
757
758 dollyOut( getZoomScale( event.deltaY ) );
759
760 }
761
762 scope.update();
763
764 }
765
766 function handleKeyDown( event ) {
767
768 let needsUpdate = false;
769
770 switch ( event.code ) {
771
772 case scope.keys.UP:
773
774 if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
775
776 rotateUp( 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
777
778 } else {
779
780 pan( 0, scope.keyPanSpeed );
781
782 }
783
784 needsUpdate = true;
785 break;
786
787 case scope.keys.BOTTOM:
788
789 if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
790
791 rotateUp( - 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
792
793 } else {
794
795 pan( 0, - scope.keyPanSpeed );
796
797 }
798
799 needsUpdate = true;
800 break;
801
802 case scope.keys.LEFT:
803
804 if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
805
806 rotateLeft( 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
807
808 } else {
809
810 pan( scope.keyPanSpeed, 0 );
811
812 }
813
814 needsUpdate = true;
815 break;
816
817 case scope.keys.RIGHT:
818
819 if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
820
821 rotateLeft( - 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
822
823 } else {
824
825 pan( - scope.keyPanSpeed, 0 );
826
827 }
828
829 needsUpdate = true;
830 break;
831
832 }
833
834 if ( needsUpdate ) {
835
836 // prevent the browser from scrolling on cursor keys
837 event.preventDefault();
838
839 scope.update();
840
841 }
842
843
844 }
845
846 function handleTouchStartRotate( event ) {
847
848 if ( pointers.length === 1 ) {
849
850 rotateStart.set( event.pageX, event.pageY );
851
852 } else {
853
854 const position = getSecondPointerPosition( event );
855
856 const x = 0.5 * ( event.pageX + position.x );
857 const y = 0.5 * ( event.pageY + position.y );
858
859 rotateStart.set( x, y );
860
861 }
862
863 }
864
865 function handleTouchStartPan( event ) {
866
867 if ( pointers.length === 1 ) {
868
869 panStart.set( event.pageX, event.pageY );
870
871 } else {
872
873 const position = getSecondPointerPosition( event );
874
875 const x = 0.5 * ( event.pageX + position.x );
876 const y = 0.5 * ( event.pageY + position.y );
877
878 panStart.set( x, y );
879
880 }
881
882 }
883
884 function handleTouchStartDolly( event ) {
885
886 const position = getSecondPointerPosition( event );
887
888 const dx = event.pageX - position.x;
889 const dy = event.pageY - position.y;
890
891 const distance = Math.sqrt( dx * dx + dy * dy );
892
893 dollyStart.set( 0, distance );
894
895 }
896
897 function handleTouchStartDollyPan( event ) {
898
899 if ( scope.enableZoom ) handleTouchStartDolly( event );
900
901 if ( scope.enablePan ) handleTouchStartPan( event );
902
903 }
904
905 function handleTouchStartDollyRotate( event ) {
906
907 if ( scope.enableZoom ) handleTouchStartDolly( event );
908
909 if ( scope.enableRotate ) handleTouchStartRotate( event );
910
911 }
912
913 function handleTouchMoveRotate( event ) {
914
915 if ( pointers.length == 1 ) {
916
917 rotateEnd.set( event.pageX, event.pageY );
918
919 } else {
920
921 const position = getSecondPointerPosition( event );
922
923 const x = 0.5 * ( event.pageX + position.x );
924 const y = 0.5 * ( event.pageY + position.y );
925
926 rotateEnd.set( x, y );
927
928 }
929
930 rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
931
932 const element = scope.domElement;
933
934 rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
935
936 rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
937
938 rotateStart.copy( rotateEnd );
939
940 }
941
942 function handleTouchMovePan( event ) {
943
944 if ( pointers.length === 1 ) {
945
946 panEnd.set( event.pageX, event.pageY );
947
948 } else {
949
950 const position = getSecondPointerPosition( event );
951
952 const x = 0.5 * ( event.pageX + position.x );
953 const y = 0.5 * ( event.pageY + position.y );
954
955 panEnd.set( x, y );
956
957 }
958
959 panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
960
961 pan( panDelta.x, panDelta.y );
962
963 panStart.copy( panEnd );
964
965 }
966
967 function handleTouchMoveDolly( event ) {
968
969 const position = getSecondPointerPosition( event );
970
971 const dx = event.pageX - position.x;
972 const dy = event.pageY - position.y;
973
974 const distance = Math.sqrt( dx * dx + dy * dy );
975
976 dollyEnd.set( 0, distance );
977
978 dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
979
980 dollyOut( dollyDelta.y );
981
982 dollyStart.copy( dollyEnd );
983
984 const centerX = ( event.pageX + position.x ) * 0.5;
985 const centerY = ( event.pageY + position.y ) * 0.5;
986
987 updateZoomParameters( centerX, centerY );
988
989 }
990
991 function handleTouchMoveDollyPan( event ) {
992
993 if ( scope.enableZoom ) handleTouchMoveDolly( event );
994
995 if ( scope.enablePan ) handleTouchMovePan( event );
996
997 }
998
999 function handleTouchMoveDollyRotate( event ) {
1000
1001 if ( scope.enableZoom ) handleTouchMoveDolly( event );
1002
1003 if ( scope.enableRotate ) handleTouchMoveRotate( event );
1004
1005 }
1006
1007 //
1008 // event handlers - FSM: listen for events and reset state
1009 //
1010
1011 function onPointerDown( event ) {
1012
1013 if ( scope.enabled === false ) return;
1014
1015 if ( pointers.length === 0 ) {
1016
1017 scope.domElement.setPointerCapture( event.pointerId );
1018
1019 scope.domElement.addEventListener( 'pointermove', onPointerMove );
1020 scope.domElement.addEventListener( 'pointerup', onPointerUp );
1021
1022 }
1023
1024 //
1025
1026 if ( isTrackingPointer( event ) ) return;
1027
1028 //
1029
1030 addPointer( event );
1031
1032 if ( event.pointerType === 'touch' ) {
1033
1034 onTouchStart( event );
1035
1036 } else {
1037
1038 onMouseDown( event );
1039
1040 }
1041
1042 }
1043
1044 function onPointerMove( event ) {
1045
1046 if ( scope.enabled === false ) return;
1047
1048 if ( event.pointerType === 'touch' ) {
1049
1050 onTouchMove( event );
1051
1052 } else {
1053
1054 onMouseMove( event );
1055
1056 }
1057
1058 }
1059
1060 function onPointerUp( event ) {
1061
1062 removePointer( event );
1063
1064 switch ( pointers.length ) {
1065
1066 case 0:
1067
1068 scope.domElement.releasePointerCapture( event.pointerId );
1069
1070 scope.domElement.removeEventListener( 'pointermove', onPointerMove );
1071 scope.domElement.removeEventListener( 'pointerup', onPointerUp );
1072
1073 scope.dispatchEvent( _endEvent );
1074
1075 state = STATE.NONE;
1076
1077 break;
1078
1079 case 1:
1080
1081 const pointerId = pointers[ 0 ];
1082 const position = pointerPositions[ pointerId ];
1083
1084 // minimal placeholder event - allows state correction on pointer-up
1085 onTouchStart( { pointerId: pointerId, pageX: position.x, pageY: position.y } );
1086
1087 break;
1088
1089 }
1090
1091 }
1092
1093 function onMouseDown( event ) {
1094
1095 let mouseAction;
1096
1097 switch ( event.button ) {
1098
1099 case 0:
1100
1101 mouseAction = scope.mouseButtons.LEFT;
1102 break;
1103
1104 case 1:
1105
1106 mouseAction = scope.mouseButtons.MIDDLE;
1107 break;
1108
1109 case 2:
1110
1111 mouseAction = scope.mouseButtons.RIGHT;
1112 break;
1113
1114 default:
1115
1116 mouseAction = - 1;
1117
1118 }
1119
1120 switch ( mouseAction ) {
1121
1122 case MOUSE.DOLLY:
1123
1124 if ( scope.enableZoom === false ) return;
1125
1126 handleMouseDownDolly( event );
1127
1128 state = STATE.DOLLY;
1129
1130 break;
1131
1132 case MOUSE.ROTATE:
1133
1134 if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
1135
1136 if ( scope.enablePan === false ) return;
1137
1138 handleMouseDownPan( event );
1139
1140 state = STATE.PAN;
1141
1142 } else {
1143
1144 if ( scope.enableRotate === false ) return;
1145
1146 handleMouseDownRotate( event );
1147
1148 state = STATE.ROTATE;
1149
1150 }
1151
1152 break;
1153
1154 case MOUSE.PAN:
1155
1156 if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
1157
1158 if ( scope.enableRotate === false ) return;
1159
1160 handleMouseDownRotate( event );
1161
1162 state = STATE.ROTATE;
1163
1164 } else {
1165
1166 if ( scope.enablePan === false ) return;
1167
1168 handleMouseDownPan( event );
1169
1170 state = STATE.PAN;
1171
1172 }
1173
1174 break;
1175
1176 default:
1177
1178 state = STATE.NONE;
1179
1180 }
1181
1182 if ( state !== STATE.NONE ) {
1183
1184 scope.dispatchEvent( _startEvent );
1185
1186 }
1187
1188 }
1189
1190 function onMouseMove( event ) {
1191
1192 switch ( state ) {
1193
1194 case STATE.ROTATE:
1195
1196 if ( scope.enableRotate === false ) return;
1197
1198 handleMouseMoveRotate( event );
1199
1200 break;
1201
1202 case STATE.DOLLY:
1203
1204 if ( scope.enableZoom === false ) return;
1205
1206 handleMouseMoveDolly( event );
1207
1208 break;
1209
1210 case STATE.PAN:
1211
1212 if ( scope.enablePan === false ) return;
1213
1214 handleMouseMovePan( event );
1215
1216 break;
1217
1218 }
1219
1220 }
1221
1222 function onMouseWheel( event ) {
1223
1224 if ( scope.enabled === false || scope.enableZoom === false || state !== STATE.NONE ) return;
1225
1226 event.preventDefault();
1227
1228 scope.dispatchEvent( _startEvent );
1229
1230 handleMouseWheel( customWheelEvent( event ) );
1231
1232 scope.dispatchEvent( _endEvent );
1233
1234 }
1235
1236 function customWheelEvent( event ) {
1237
1238 const mode = event.deltaMode;
1239
1240 // minimal wheel event altered to meet delta-zoom demand
1241 const newEvent = {
1242 clientX: event.clientX,
1243 clientY: event.clientY,
1244 deltaY: event.deltaY,
1245 };
1246
1247 switch ( mode ) {
1248
1249 case 1: // LINE_MODE
1250 newEvent.deltaY *= 16;
1251 break;
1252
1253 case 2: // PAGE_MODE
1254 newEvent.deltaY *= 100;
1255 break;
1256
1257 }
1258
1259 // detect if event was triggered by pinching
1260 if ( event.ctrlKey && ! controlActive ) {
1261
1262 newEvent.deltaY *= 10;
1263
1264 }
1265
1266 return newEvent;
1267
1268 }
1269
1270 function interceptControlDown( event ) {
1271
1272 if ( event.key === 'Control' ) {
1273
1274 controlActive = true;
1275
1276
1277 const document = scope.domElement.getRootNode(); // offscreen canvas compatibility
1278
1279 document.addEventListener( 'keyup', interceptControlUp, { passive: true, capture: true } );
1280
1281 }
1282
1283 }
1284
1285 function interceptControlUp( event ) {
1286
1287 if ( event.key === 'Control' ) {
1288
1289 controlActive = false;
1290
1291
1292 const document = scope.domElement.getRootNode(); // offscreen canvas compatibility
1293
1294 document.removeEventListener( 'keyup', interceptControlUp, { passive: true, capture: true } );
1295
1296 }
1297
1298 }
1299
1300 function onKeyDown( event ) {
1301
1302 if ( scope.enabled === false || scope.enablePan === false ) return;
1303
1304 handleKeyDown( event );
1305
1306 }
1307
1308 function onTouchStart( event ) {
1309
1310 trackPointer( event );
1311
1312 switch ( pointers.length ) {
1313
1314 case 1:
1315
1316 switch ( scope.touches.ONE ) {
1317
1318 case TOUCH.ROTATE:
1319
1320 if ( scope.enableRotate === false ) return;
1321
1322 handleTouchStartRotate( event );
1323
1324 state = STATE.TOUCH_ROTATE;
1325
1326 break;
1327
1328 case TOUCH.PAN:
1329
1330 if ( scope.enablePan === false ) return;
1331
1332 handleTouchStartPan( event );
1333
1334 state = STATE.TOUCH_PAN;
1335
1336 break;
1337
1338 default:
1339
1340 state = STATE.NONE;
1341
1342 }
1343
1344 break;
1345
1346 case 2:
1347
1348 switch ( scope.touches.TWO ) {
1349
1350 case TOUCH.DOLLY_PAN:
1351
1352 if ( scope.enableZoom === false && scope.enablePan === false ) return;
1353
1354 handleTouchStartDollyPan( event );
1355
1356 state = STATE.TOUCH_DOLLY_PAN;
1357
1358 break;
1359
1360 case TOUCH.DOLLY_ROTATE:
1361
1362 if ( scope.enableZoom === false && scope.enableRotate === false ) return;
1363
1364 handleTouchStartDollyRotate( event );
1365
1366 state = STATE.TOUCH_DOLLY_ROTATE;
1367
1368 break;
1369
1370 default:
1371
1372 state = STATE.NONE;
1373
1374 }
1375
1376 break;
1377
1378 default:
1379
1380 state = STATE.NONE;
1381
1382 }
1383
1384 if ( state !== STATE.NONE ) {
1385
1386 scope.dispatchEvent( _startEvent );
1387
1388 }
1389
1390 }
1391
1392 function onTouchMove( event ) {
1393
1394 trackPointer( event );
1395
1396 switch ( state ) {
1397
1398 case STATE.TOUCH_ROTATE:
1399
1400 if ( scope.enableRotate === false ) return;
1401
1402 handleTouchMoveRotate( event );
1403
1404 scope.update();
1405
1406 break;
1407
1408 case STATE.TOUCH_PAN:
1409
1410 if ( scope.enablePan === false ) return;
1411
1412 handleTouchMovePan( event );
1413
1414 scope.update();
1415
1416 break;
1417
1418 case STATE.TOUCH_DOLLY_PAN:
1419
1420 if ( scope.enableZoom === false && scope.enablePan === false ) return;
1421
1422 handleTouchMoveDollyPan( event );
1423
1424 scope.update();
1425
1426 break;
1427
1428 case STATE.TOUCH_DOLLY_ROTATE:
1429
1430 if ( scope.enableZoom === false && scope.enableRotate === false ) return;
1431
1432 handleTouchMoveDollyRotate( event );
1433
1434 scope.update();
1435
1436 break;
1437
1438 default:
1439
1440 state = STATE.NONE;
1441
1442 }
1443
1444 }
1445
1446 function onContextMenu( event ) {
1447
1448 if ( scope.enabled === false ) return;
1449
1450 event.preventDefault();
1451
1452 }
1453
1454 function addPointer( event ) {
1455
1456 pointers.push( event.pointerId );
1457
1458 }
1459
1460 function removePointer( event ) {
1461
1462 delete pointerPositions[ event.pointerId ];
1463
1464 for ( let i = 0; i < pointers.length; i ++ ) {
1465
1466 if ( pointers[ i ] == event.pointerId ) {
1467
1468 pointers.splice( i, 1 );
1469 return;
1470
1471 }
1472
1473 }
1474
1475 }
1476
1477 function isTrackingPointer( event ) {
1478
1479 for ( let i = 0; i < pointers.length; i ++ ) {
1480
1481 if ( pointers[ i ] == event.pointerId ) return true;
1482
1483 }
1484
1485 return false;
1486
1487 }
1488
1489 function trackPointer( event ) {
1490
1491 let position = pointerPositions[ event.pointerId ];
1492
1493 if ( position === undefined ) {
1494
1495 position = new Vector2();
1496 pointerPositions[ event.pointerId ] = position;
1497
1498 }
1499
1500 position.set( event.pageX, event.pageY );
1501
1502 }
1503
1504 function getSecondPointerPosition( event ) {
1505
1506 const pointerId = ( event.pointerId === pointers[ 0 ] ) ? pointers[ 1 ] : pointers[ 0 ];
1507
1508 return pointerPositions[ pointerId ];
1509
1510 }
1511
1512 //
1513
1514 scope.domElement.addEventListener( 'contextmenu', onContextMenu );
1515
1516 scope.domElement.addEventListener( 'pointerdown', onPointerDown );
1517 scope.domElement.addEventListener( 'pointercancel', onPointerUp );
1518 scope.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
1519
1520 const document = scope.domElement.getRootNode(); // offscreen canvas compatibility
1521
1522 document.addEventListener( 'keydown', interceptControlDown, { passive: true, capture: true } );
1523
1524 // force an update at start
1525
1526 this.update();
1527
1528 }
1529
1530}
1531
1532export { OrbitControls };