@floless/app 0.34.2 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1963 @@
1
+ import {
2
+ Controls,
3
+ MOUSE,
4
+ Quaternion,
5
+ Spherical,
6
+ TOUCH,
7
+ Vector2,
8
+ Vector3,
9
+ Plane,
10
+ Ray,
11
+ MathUtils
12
+ } from 'three';
13
+
14
+ /**
15
+ * Fires when the camera has been transformed by the controls.
16
+ *
17
+ * @event OrbitControls#change
18
+ * @type {Object}
19
+ */
20
+ const _changeEvent = { type: 'change' };
21
+
22
+ /**
23
+ * Fires when an interaction was initiated.
24
+ *
25
+ * @event OrbitControls#start
26
+ * @type {Object}
27
+ */
28
+ const _startEvent = { type: 'start' };
29
+
30
+ /**
31
+ * Fires when an interaction has finished.
32
+ *
33
+ * @event OrbitControls#end
34
+ * @type {Object}
35
+ */
36
+ const _endEvent = { type: 'end' };
37
+
38
+ const _ray = new Ray();
39
+ const _plane = new Plane();
40
+ const _TILT_LIMIT = Math.cos( 70 * MathUtils.DEG2RAD );
41
+
42
+ const _v = new Vector3();
43
+ const _twoPI = 2 * Math.PI;
44
+
45
+ const _STATE = {
46
+ NONE: - 1,
47
+ ROTATE: 0,
48
+ DOLLY: 1,
49
+ PAN: 2,
50
+ TOUCH_ROTATE: 3,
51
+ TOUCH_PAN: 4,
52
+ TOUCH_DOLLY_PAN: 5,
53
+ TOUCH_DOLLY_ROTATE: 6
54
+ };
55
+ const _EPS = 0.000001;
56
+
57
+
58
+ /**
59
+ * Orbit controls allow the camera to orbit around a target.
60
+ *
61
+ * OrbitControls performs orbiting, dollying (zooming), and panning. Unlike {@link TrackballControls},
62
+ * it maintains the "up" direction `object.up` (+Y by default).
63
+ *
64
+ * - Orbit: Left mouse / touch: one-finger move.
65
+ * - Zoom: Middle mouse, or mousewheel / touch: two-finger spread or squish.
66
+ * - Pan: Right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move.
67
+ *
68
+ * ```js
69
+ * const controls = new OrbitControls( camera, renderer.domElement );
70
+ *
71
+ * // controls.update() must be called after any manual changes to the camera's transform
72
+ * camera.position.set( 0, 20, 100 );
73
+ * controls.update();
74
+ *
75
+ * function animate() {
76
+ *
77
+ * // required if controls.enableDamping or controls.autoRotate are set to true
78
+ * controls.update();
79
+ *
80
+ * renderer.render( scene, camera );
81
+ *
82
+ * }
83
+ * ```
84
+ *
85
+ * @augments Controls
86
+ * @three_import import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
87
+ */
88
+ class OrbitControls extends Controls {
89
+
90
+ /**
91
+ * Constructs a new controls instance.
92
+ *
93
+ * @param {Object3D} object - The object that is managed by the controls.
94
+ * @param {?HTMLElement} domElement - The HTML element used for event listeners.
95
+ */
96
+ constructor( object, domElement = null ) {
97
+
98
+ super( object, domElement );
99
+
100
+ this.state = _STATE.NONE;
101
+
102
+ /**
103
+ * The focus point of the controls, the `object` orbits around this.
104
+ * It can be updated manually at any point to change the focus of the controls.
105
+ *
106
+ * @type {Vector3}
107
+ */
108
+ this.target = new Vector3();
109
+
110
+ /**
111
+ * The focus point of the `minTargetRadius` and `maxTargetRadius` limits.
112
+ * It can be updated manually at any point to change the center of interest
113
+ * for the `target`.
114
+ *
115
+ * @type {Vector3}
116
+ */
117
+ this.cursor = new Vector3();
118
+
119
+ /**
120
+ * How far you can dolly in (perspective camera only).
121
+ *
122
+ * @type {number}
123
+ * @default 0
124
+ */
125
+ this.minDistance = 0;
126
+
127
+ /**
128
+ * How far you can dolly out (perspective camera only).
129
+ *
130
+ * @type {number}
131
+ * @default Infinity
132
+ */
133
+ this.maxDistance = Infinity;
134
+
135
+ /**
136
+ * How far you can zoom in (orthographic camera only).
137
+ *
138
+ * @type {number}
139
+ * @default 0
140
+ */
141
+ this.minZoom = 0;
142
+
143
+ /**
144
+ * How far you can zoom out (orthographic camera only).
145
+ *
146
+ * @type {number}
147
+ * @default Infinity
148
+ */
149
+ this.maxZoom = Infinity;
150
+
151
+ /**
152
+ * How close you can get the target to the 3D `cursor`.
153
+ *
154
+ * @type {number}
155
+ * @default 0
156
+ */
157
+ this.minTargetRadius = 0;
158
+
159
+ /**
160
+ * How far you can move the target from the 3D `cursor`.
161
+ *
162
+ * @type {number}
163
+ * @default Infinity
164
+ */
165
+ this.maxTargetRadius = Infinity;
166
+
167
+ /**
168
+ * How far you can orbit vertically, lower limit. Range is `[0, Math.PI]` radians.
169
+ *
170
+ * @type {number}
171
+ * @default 0
172
+ */
173
+ this.minPolarAngle = 0;
174
+
175
+ /**
176
+ * How far you can orbit vertically, upper limit. Range is `[0, Math.PI]` radians.
177
+ *
178
+ * @type {number}
179
+ * @default Math.PI
180
+ */
181
+ this.maxPolarAngle = Math.PI;
182
+
183
+ /**
184
+ * How far you can orbit horizontally, lower limit. If set, the interval `[ min, max ]`
185
+ * must be a sub-interval of `[ - 2 PI, 2 PI ]`, with `( max - min < 2 PI )`.
186
+ *
187
+ * @type {number}
188
+ * @default -Infinity
189
+ */
190
+ this.minAzimuthAngle = - Infinity;
191
+
192
+ /**
193
+ * How far you can orbit horizontally, upper limit. If set, the interval `[ min, max ]`
194
+ * must be a sub-interval of `[ - 2 PI, 2 PI ]`, with `( max - min < 2 PI )`.
195
+ *
196
+ * @type {number}
197
+ * @default -Infinity
198
+ */
199
+ this.maxAzimuthAngle = Infinity;
200
+
201
+ /**
202
+ * Set to `true` to enable damping (inertia), which can be used to give a sense of weight
203
+ * to the controls. Note that if this is enabled, you must call `update()` in your animation
204
+ * loop.
205
+ *
206
+ * @type {boolean}
207
+ * @default false
208
+ */
209
+ this.enableDamping = false;
210
+
211
+ /**
212
+ * The damping inertia used if `enableDamping` is set to `true`.
213
+ *
214
+ * Note that for this to work, you must call `update()` in your animation loop.
215
+ *
216
+ * @type {number}
217
+ * @default 0.05
218
+ */
219
+ this.dampingFactor = 0.05;
220
+
221
+ /**
222
+ * Enable or disable zooming (dollying) of the camera.
223
+ *
224
+ * @type {boolean}
225
+ * @default true
226
+ */
227
+ this.enableZoom = true;
228
+
229
+ /**
230
+ * Speed of zooming / dollying.
231
+ *
232
+ * @type {number}
233
+ * @default 1
234
+ */
235
+ this.zoomSpeed = 1.0;
236
+
237
+ /**
238
+ * Enable or disable horizontal and vertical rotation of the camera.
239
+ *
240
+ * Note that it is possible to disable a single axis by setting the min and max of the
241
+ * `minPolarAngle` or `minAzimuthAngle` to the same value, which will cause the vertical
242
+ * or horizontal rotation to be fixed at that value.
243
+ *
244
+ * @type {boolean}
245
+ * @default true
246
+ */
247
+ this.enableRotate = true;
248
+
249
+ /**
250
+ * Speed of rotation.
251
+ *
252
+ * @type {number}
253
+ * @default 1
254
+ */
255
+ this.rotateSpeed = 1.0;
256
+
257
+ /**
258
+ * How fast to rotate the camera when the keyboard is used.
259
+ *
260
+ * @type {number}
261
+ * @default 1
262
+ */
263
+ this.keyRotateSpeed = 1.0;
264
+
265
+ /**
266
+ * Enable or disable camera panning.
267
+ *
268
+ * @type {boolean}
269
+ * @default true
270
+ */
271
+ this.enablePan = true;
272
+
273
+ /**
274
+ * Speed of panning.
275
+ *
276
+ * @type {number}
277
+ * @default 1
278
+ */
279
+ this.panSpeed = 1.0;
280
+
281
+ /**
282
+ * Defines how the camera's position is translated when panning. If `true`, the camera pans
283
+ * in screen space. Otherwise, the camera pans in the plane orthogonal to the camera's up
284
+ * direction.
285
+ *
286
+ * @type {boolean}
287
+ * @default true
288
+ */
289
+ this.screenSpacePanning = true;
290
+
291
+ /**
292
+ * How fast to pan the camera when the keyboard is used in
293
+ * pixels per keypress.
294
+ *
295
+ * @type {number}
296
+ * @default 7
297
+ */
298
+ this.keyPanSpeed = 7.0;
299
+
300
+ /**
301
+ * Setting this property to `true` allows to zoom to the cursor's position.
302
+ *
303
+ * @type {boolean}
304
+ * @default false
305
+ */
306
+ this.zoomToCursor = false;
307
+
308
+ /**
309
+ * Set to true to automatically rotate around the target
310
+ *
311
+ * Note that if this is enabled, you must call `update()` in your animation loop.
312
+ * If you want the auto-rotate speed to be independent of the frame rate (the refresh
313
+ * rate of the display), you must pass the time `deltaTime`, in seconds, to `update()`.
314
+ *
315
+ * @type {boolean}
316
+ * @default false
317
+ */
318
+ this.autoRotate = false;
319
+
320
+ /**
321
+ * How fast to rotate around the target if `autoRotate` is `true`. The default equates to 30 seconds
322
+ * per orbit at 60fps.
323
+ *
324
+ * Note that if `autoRotate` is enabled, you must call `update()` in your animation loop.
325
+ *
326
+ * @type {number}
327
+ * @default 2
328
+ */
329
+ this.autoRotateSpeed = 2.0;
330
+
331
+ /**
332
+ * This object contains references to the keycodes for controlling camera panning.
333
+ *
334
+ * ```js
335
+ * controls.keys = {
336
+ * LEFT: 'ArrowLeft', //left arrow
337
+ * UP: 'ArrowUp', // up arrow
338
+ * RIGHT: 'ArrowRight', // right arrow
339
+ * BOTTOM: 'ArrowDown' // down arrow
340
+ * }
341
+ * ```
342
+ * @type {Object}
343
+ */
344
+ this.keys = { LEFT: 'ArrowLeft', UP: 'ArrowUp', RIGHT: 'ArrowRight', BOTTOM: 'ArrowDown' };
345
+
346
+ /**
347
+ * This object contains references to the mouse actions used by the controls.
348
+ *
349
+ * ```js
350
+ * controls.mouseButtons = {
351
+ * LEFT: THREE.MOUSE.ROTATE,
352
+ * MIDDLE: THREE.MOUSE.DOLLY,
353
+ * RIGHT: THREE.MOUSE.PAN
354
+ * }
355
+ * ```
356
+ * @type {Object}
357
+ */
358
+ this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
359
+
360
+ /**
361
+ * This object contains references to the touch actions used by the controls.
362
+ *
363
+ * ```js
364
+ * controls.mouseButtons = {
365
+ * ONE: THREE.TOUCH.ROTATE,
366
+ * TWO: THREE.TOUCH.DOLLY_PAN
367
+ * }
368
+ * ```
369
+ * @type {Object}
370
+ */
371
+ this.touches = { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN };
372
+
373
+ /**
374
+ * Used internally by `saveState()` and `reset()`.
375
+ *
376
+ * @type {Vector3}
377
+ */
378
+ this.target0 = this.target.clone();
379
+
380
+ /**
381
+ * Used internally by `saveState()` and `reset()`.
382
+ *
383
+ * @type {Vector3}
384
+ */
385
+ this.position0 = this.object.position.clone();
386
+
387
+ /**
388
+ * Used internally by `saveState()` and `reset()`.
389
+ *
390
+ * @type {number}
391
+ */
392
+ this.zoom0 = this.object.zoom;
393
+
394
+ this._cursorStyle = 'auto';
395
+
396
+ // the target DOM element for key events
397
+ this._domElementKeyEvents = null;
398
+
399
+ // internals
400
+
401
+ this._lastPosition = new Vector3();
402
+ this._lastQuaternion = new Quaternion();
403
+ this._lastTargetPosition = new Vector3();
404
+
405
+ // so camera.up is the orbit axis
406
+ this._quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
407
+ this._quatInverse = this._quat.clone().invert();
408
+
409
+ // current position in spherical coordinates
410
+ this._spherical = new Spherical();
411
+ this._sphericalDelta = new Spherical();
412
+
413
+ this._scale = 1;
414
+ this._panOffset = new Vector3();
415
+
416
+ this._rotateStart = new Vector2();
417
+ this._rotateEnd = new Vector2();
418
+ this._rotateDelta = new Vector2();
419
+
420
+ this._panStart = new Vector2();
421
+ this._panEnd = new Vector2();
422
+ this._panDelta = new Vector2();
423
+
424
+ this._dollyStart = new Vector2();
425
+ this._dollyEnd = new Vector2();
426
+ this._dollyDelta = new Vector2();
427
+
428
+ this._dollyDirection = new Vector3();
429
+ this._mouse = new Vector2();
430
+ this._performCursorZoom = false;
431
+
432
+ this._pointers = [];
433
+ this._pointerPositions = {};
434
+
435
+ this._controlActive = false;
436
+
437
+ // event listeners
438
+
439
+ this._onPointerMove = onPointerMove.bind( this );
440
+ this._onPointerDown = onPointerDown.bind( this );
441
+ this._onPointerUp = onPointerUp.bind( this );
442
+ this._onContextMenu = onContextMenu.bind( this );
443
+ this._onMouseWheel = onMouseWheel.bind( this );
444
+ this._onKeyDown = onKeyDown.bind( this );
445
+
446
+ this._onTouchStart = onTouchStart.bind( this );
447
+ this._onTouchMove = onTouchMove.bind( this );
448
+
449
+ this._onMouseDown = onMouseDown.bind( this );
450
+ this._onMouseMove = onMouseMove.bind( this );
451
+
452
+ this._interceptControlDown = interceptControlDown.bind( this );
453
+ this._interceptControlUp = interceptControlUp.bind( this );
454
+
455
+ //
456
+
457
+ if ( this.domElement !== null ) {
458
+
459
+ this.connect( this.domElement );
460
+
461
+ }
462
+
463
+ this.update();
464
+
465
+ }
466
+
467
+ /**
468
+ * Defines the visual representation of the cursor.
469
+ *
470
+ * @type {('auto'|'grab')}
471
+ * @default 'auto'
472
+ */
473
+ set cursorStyle( type ) {
474
+
475
+ this._cursorStyle = type;
476
+
477
+ if ( type === 'grab' ) {
478
+
479
+ this.domElement.style.cursor = 'grab';
480
+
481
+ } else {
482
+
483
+ this.domElement.style.cursor = 'auto';
484
+
485
+ }
486
+
487
+ }
488
+
489
+ get cursorStyle() {
490
+
491
+ return this._cursorStyle;
492
+
493
+ }
494
+
495
+ connect( element ) {
496
+
497
+ super.connect( element );
498
+
499
+ this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
500
+ this.domElement.addEventListener( 'pointercancel', this._onPointerUp );
501
+
502
+ this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
503
+ this.domElement.addEventListener( 'wheel', this._onMouseWheel, { passive: false } );
504
+
505
+ const document = this.domElement.getRootNode(); // offscreen canvas compatibility
506
+ document.addEventListener( 'keydown', this._interceptControlDown, { passive: true, capture: true } );
507
+
508
+ this.domElement.style.touchAction = 'none'; // Disable touch scroll
509
+
510
+ }
511
+
512
+ disconnect() {
513
+
514
+ this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
515
+ this.domElement.ownerDocument.removeEventListener( 'pointermove', this._onPointerMove );
516
+ this.domElement.ownerDocument.removeEventListener( 'pointerup', this._onPointerUp );
517
+ this.domElement.removeEventListener( 'pointercancel', this._onPointerUp );
518
+
519
+ this.domElement.removeEventListener( 'wheel', this._onMouseWheel );
520
+ this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
521
+
522
+ this.stopListenToKeyEvents();
523
+
524
+ const document = this.domElement.getRootNode(); // offscreen canvas compatibility
525
+ document.removeEventListener( 'keydown', this._interceptControlDown, { capture: true } );
526
+
527
+ this.domElement.style.touchAction = ''; // Restore touch scroll
528
+
529
+ }
530
+
531
+ dispose() {
532
+
533
+ this.disconnect();
534
+
535
+ }
536
+
537
+ /**
538
+ * Get the current vertical rotation, in radians.
539
+ *
540
+ * @return {number} The current vertical rotation, in radians.
541
+ */
542
+ getPolarAngle() {
543
+
544
+ return this._spherical.phi;
545
+
546
+ }
547
+
548
+ /**
549
+ * Get the current horizontal rotation, in radians.
550
+ *
551
+ * @return {number} The current horizontal rotation, in radians.
552
+ */
553
+ getAzimuthalAngle() {
554
+
555
+ return this._spherical.theta;
556
+
557
+ }
558
+
559
+ /**
560
+ * Returns the distance from the camera to the target.
561
+ *
562
+ * @return {number} The distance from the camera to the target.
563
+ */
564
+ getDistance() {
565
+
566
+ return this.object.position.distanceTo( this.target );
567
+
568
+ }
569
+
570
+ /**
571
+ * Adds key event listeners to the given DOM element.
572
+ * `window` is a recommended argument for using this method.
573
+ *
574
+ * @param {HTMLElement} domElement - The DOM element
575
+ */
576
+ listenToKeyEvents( domElement ) {
577
+
578
+ domElement.addEventListener( 'keydown', this._onKeyDown );
579
+ this._domElementKeyEvents = domElement;
580
+
581
+ }
582
+
583
+ /**
584
+ * Removes the key event listener previously defined with `listenToKeyEvents()`.
585
+ */
586
+ stopListenToKeyEvents() {
587
+
588
+ if ( this._domElementKeyEvents !== null ) {
589
+
590
+ this._domElementKeyEvents.removeEventListener( 'keydown', this._onKeyDown );
591
+ this._domElementKeyEvents = null;
592
+
593
+ }
594
+
595
+ }
596
+
597
+ /**
598
+ * Save the current state of the controls. This can later be recovered with `reset()`.
599
+ */
600
+ saveState() {
601
+
602
+ this.target0.copy( this.target );
603
+ this.position0.copy( this.object.position );
604
+ this.zoom0 = this.object.zoom;
605
+
606
+ }
607
+
608
+ /**
609
+ * Reset the controls to their state from either the last time the `saveState()`
610
+ * was called, or the initial state.
611
+ */
612
+ reset() {
613
+
614
+ this.target.copy( this.target0 );
615
+ this.object.position.copy( this.position0 );
616
+ this.object.zoom = this.zoom0;
617
+
618
+ this.object.updateProjectionMatrix();
619
+ this.dispatchEvent( _changeEvent );
620
+
621
+ this.update();
622
+
623
+ this.state = _STATE.NONE;
624
+
625
+ }
626
+
627
+ /**
628
+ * Programmatically pan the camera.
629
+ *
630
+ * @param {number} deltaX - The horizontal pan amount in pixels.
631
+ * @param {number} deltaY - The vertical pan amount in pixels.
632
+ */
633
+ pan( deltaX, deltaY ) {
634
+
635
+ this._pan( deltaX, deltaY );
636
+ this.update();
637
+
638
+ }
639
+
640
+ /**
641
+ * Programmatically dolly in (zoom in for perspective camera).
642
+ *
643
+ * @param {number} dollyScale - The dolly scale factor.
644
+ */
645
+ dollyIn( dollyScale ) {
646
+
647
+ this._dollyIn( dollyScale );
648
+ this.update();
649
+
650
+ }
651
+
652
+ /**
653
+ * Programmatically dolly out (zoom out for perspective camera).
654
+ *
655
+ * @param {number} dollyScale - The dolly scale factor.
656
+ */
657
+ dollyOut( dollyScale ) {
658
+
659
+ this._dollyOut( dollyScale );
660
+ this.update();
661
+
662
+ }
663
+
664
+ /**
665
+ * Programmatically rotate the camera left (around the vertical axis).
666
+ *
667
+ * @param {number} angle - The rotation angle in radians.
668
+ */
669
+ rotateLeft( angle ) {
670
+
671
+ this._rotateLeft( angle );
672
+ this.update();
673
+
674
+ }
675
+
676
+ /**
677
+ * Programmatically rotate the camera up (around the horizontal axis).
678
+ *
679
+ * @param {number} angle - The rotation angle in radians.
680
+ */
681
+ rotateUp( angle ) {
682
+
683
+ this._rotateUp( angle );
684
+ this.update();
685
+
686
+ }
687
+
688
+ update( deltaTime = null ) {
689
+
690
+ const position = this.object.position;
691
+
692
+ _v.copy( position ).sub( this.target );
693
+
694
+ // rotate offset to "y-axis-is-up" space
695
+ _v.applyQuaternion( this._quat );
696
+
697
+ // angle from z-axis around y-axis
698
+ this._spherical.setFromVector3( _v );
699
+
700
+ if ( this.autoRotate && this.state === _STATE.NONE ) {
701
+
702
+ this._rotateLeft( this._getAutoRotationAngle( deltaTime ) );
703
+
704
+ }
705
+
706
+ if ( this.enableDamping ) {
707
+
708
+ this._spherical.theta += this._sphericalDelta.theta * this.dampingFactor;
709
+ this._spherical.phi += this._sphericalDelta.phi * this.dampingFactor;
710
+
711
+ } else {
712
+
713
+ this._spherical.theta += this._sphericalDelta.theta;
714
+ this._spherical.phi += this._sphericalDelta.phi;
715
+
716
+ }
717
+
718
+ // restrict theta to be between desired limits
719
+
720
+ let min = this.minAzimuthAngle;
721
+ let max = this.maxAzimuthAngle;
722
+
723
+ if ( isFinite( min ) && isFinite( max ) ) {
724
+
725
+ if ( min < - Math.PI ) min += _twoPI; else if ( min > Math.PI ) min -= _twoPI;
726
+
727
+ if ( max < - Math.PI ) max += _twoPI; else if ( max > Math.PI ) max -= _twoPI;
728
+
729
+ if ( min <= max ) {
730
+
731
+ this._spherical.theta = Math.max( min, Math.min( max, this._spherical.theta ) );
732
+
733
+ } else {
734
+
735
+ this._spherical.theta = ( this._spherical.theta > ( min + max ) / 2 ) ?
736
+ Math.max( min, this._spherical.theta ) :
737
+ Math.min( max, this._spherical.theta );
738
+
739
+ }
740
+
741
+ }
742
+
743
+ // restrict phi to be between desired limits
744
+ this._spherical.phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, this._spherical.phi ) );
745
+
746
+ this._spherical.makeSafe();
747
+
748
+
749
+ // move target to panned location
750
+
751
+ if ( this.enableDamping === true ) {
752
+
753
+ this.target.addScaledVector( this._panOffset, this.dampingFactor );
754
+
755
+ } else {
756
+
757
+ this.target.add( this._panOffset );
758
+
759
+ }
760
+
761
+ // Limit the target distance from the cursor to create a sphere around the center of interest
762
+ this.target.sub( this.cursor );
763
+ this.target.clampLength( this.minTargetRadius, this.maxTargetRadius );
764
+ this.target.add( this.cursor );
765
+
766
+ let zoomChanged = false;
767
+ // adjust the camera position based on zoom only if we're not zooming to the cursor or if it's an ortho camera
768
+ // we adjust zoom later in these cases
769
+ if ( this.zoomToCursor && this._performCursorZoom || this.object.isOrthographicCamera ) {
770
+
771
+ this._spherical.radius = this._clampDistance( this._spherical.radius );
772
+
773
+ } else {
774
+
775
+ const prevRadius = this._spherical.radius;
776
+ this._spherical.radius = this._clampDistance( this._spherical.radius * this._scale );
777
+ zoomChanged = prevRadius != this._spherical.radius;
778
+
779
+ }
780
+
781
+ _v.setFromSpherical( this._spherical );
782
+
783
+ // rotate offset back to "camera-up-vector-is-up" space
784
+ _v.applyQuaternion( this._quatInverse );
785
+
786
+ position.copy( this.target ).add( _v );
787
+
788
+ this.object.lookAt( this.target );
789
+
790
+ if ( this.enableDamping === true ) {
791
+
792
+ this._sphericalDelta.theta *= ( 1 - this.dampingFactor );
793
+ this._sphericalDelta.phi *= ( 1 - this.dampingFactor );
794
+
795
+ this._panOffset.multiplyScalar( 1 - this.dampingFactor );
796
+
797
+ } else {
798
+
799
+ this._sphericalDelta.set( 0, 0, 0 );
800
+
801
+ this._panOffset.set( 0, 0, 0 );
802
+
803
+ }
804
+
805
+ // adjust camera position
806
+ if ( this.zoomToCursor && this._performCursorZoom ) {
807
+
808
+ let newRadius = null;
809
+ if ( this.object.isPerspectiveCamera ) {
810
+
811
+ // move the camera down the pointer ray
812
+ // this method avoids floating point error
813
+ const prevRadius = _v.length();
814
+ newRadius = this._clampDistance( prevRadius * this._scale );
815
+
816
+ const radiusDelta = prevRadius - newRadius;
817
+ this.object.position.addScaledVector( this._dollyDirection, radiusDelta );
818
+ this.object.updateMatrixWorld();
819
+
820
+ zoomChanged = !! radiusDelta;
821
+
822
+ } else if ( this.object.isOrthographicCamera ) {
823
+
824
+ // adjust the ortho camera position based on zoom changes
825
+ const mouseBefore = new Vector3( this._mouse.x, this._mouse.y, 0 );
826
+ mouseBefore.unproject( this.object );
827
+
828
+ const prevZoom = this.object.zoom;
829
+ this.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / this._scale ) );
830
+ this.object.updateProjectionMatrix();
831
+
832
+ zoomChanged = prevZoom !== this.object.zoom;
833
+
834
+ const mouseAfter = new Vector3( this._mouse.x, this._mouse.y, 0 );
835
+ mouseAfter.unproject( this.object );
836
+
837
+ this.object.position.sub( mouseAfter ).add( mouseBefore );
838
+ this.object.updateMatrixWorld();
839
+
840
+ newRadius = _v.length();
841
+
842
+ } else {
843
+
844
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - zoom to cursor disabled.' );
845
+ this.zoomToCursor = false;
846
+
847
+ }
848
+
849
+ // handle the placement of the target
850
+ if ( newRadius !== null ) {
851
+
852
+ if ( this.screenSpacePanning ) {
853
+
854
+ // position the orbit target in front of the new camera position
855
+ this.target.set( 0, 0, - 1 )
856
+ .transformDirection( this.object.matrix )
857
+ .multiplyScalar( newRadius )
858
+ .add( this.object.position );
859
+
860
+ } else {
861
+
862
+ // get the ray and translation plane to compute target
863
+ _ray.origin.copy( this.object.position );
864
+ _ray.direction.set( 0, 0, - 1 ).transformDirection( this.object.matrix );
865
+
866
+ // if the camera is 20 degrees above the horizon then don't adjust the focus target to avoid
867
+ // extremely large values
868
+ if ( Math.abs( this.object.up.dot( _ray.direction ) ) < _TILT_LIMIT ) {
869
+
870
+ this.object.lookAt( this.target );
871
+
872
+ } else {
873
+
874
+ _plane.setFromNormalAndCoplanarPoint( this.object.up, this.target );
875
+ _ray.intersectPlane( _plane, this.target );
876
+
877
+ }
878
+
879
+ }
880
+
881
+ }
882
+
883
+ } else if ( this.object.isOrthographicCamera ) {
884
+
885
+ const prevZoom = this.object.zoom;
886
+ this.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / this._scale ) );
887
+
888
+ if ( prevZoom !== this.object.zoom ) {
889
+
890
+ this.object.updateProjectionMatrix();
891
+ zoomChanged = true;
892
+
893
+ }
894
+
895
+ }
896
+
897
+ this._scale = 1;
898
+ this._performCursorZoom = false;
899
+
900
+ // update condition is:
901
+ // min(camera displacement, camera rotation in radians)^2 > EPS
902
+ // using small-angle approximation cos(x/2) = 1 - x^2 / 8
903
+
904
+ if ( zoomChanged ||
905
+ this._lastPosition.distanceToSquared( this.object.position ) > _EPS ||
906
+ 8 * ( 1 - this._lastQuaternion.dot( this.object.quaternion ) ) > _EPS ||
907
+ this._lastTargetPosition.distanceToSquared( this.target ) > _EPS ) {
908
+
909
+ this.dispatchEvent( _changeEvent );
910
+
911
+ this._lastPosition.copy( this.object.position );
912
+ this._lastQuaternion.copy( this.object.quaternion );
913
+ this._lastTargetPosition.copy( this.target );
914
+
915
+ return true;
916
+
917
+ }
918
+
919
+ return false;
920
+
921
+ }
922
+
923
+ _getAutoRotationAngle( deltaTime ) {
924
+
925
+ if ( deltaTime !== null ) {
926
+
927
+ return ( _twoPI / 60 * this.autoRotateSpeed ) * deltaTime;
928
+
929
+ } else {
930
+
931
+ return _twoPI / 60 / 60 * this.autoRotateSpeed;
932
+
933
+ }
934
+
935
+ }
936
+
937
+ _getZoomScale( delta ) {
938
+
939
+ const normalizedDelta = Math.abs( delta * 0.01 );
940
+ return Math.pow( 0.95, this.zoomSpeed * normalizedDelta );
941
+
942
+ }
943
+
944
+ _rotateLeft( angle ) {
945
+
946
+ this._sphericalDelta.theta -= angle;
947
+
948
+ }
949
+
950
+ _rotateUp( angle ) {
951
+
952
+ this._sphericalDelta.phi -= angle;
953
+
954
+ }
955
+
956
+ _panLeft( distance, objectMatrix ) {
957
+
958
+ _v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
959
+ _v.multiplyScalar( - distance );
960
+
961
+ this._panOffset.add( _v );
962
+
963
+ }
964
+
965
+ _panUp( distance, objectMatrix ) {
966
+
967
+ if ( this.screenSpacePanning === true ) {
968
+
969
+ _v.setFromMatrixColumn( objectMatrix, 1 );
970
+
971
+ } else {
972
+
973
+ _v.setFromMatrixColumn( objectMatrix, 0 );
974
+ _v.crossVectors( this.object.up, _v );
975
+
976
+ }
977
+
978
+ _v.multiplyScalar( distance );
979
+
980
+ this._panOffset.add( _v );
981
+
982
+ }
983
+
984
+ // deltaX and deltaY are in pixels; right and down are positive
985
+ _pan( deltaX, deltaY ) {
986
+
987
+ const element = this.domElement;
988
+
989
+ if ( this.object.isPerspectiveCamera ) {
990
+
991
+ // perspective
992
+ const position = this.object.position;
993
+ _v.copy( position ).sub( this.target );
994
+ let targetDistance = _v.length();
995
+
996
+ // half of the fov is center to top of screen
997
+ targetDistance *= Math.tan( ( this.object.fov / 2 ) * Math.PI / 180.0 );
998
+
999
+ // we use only clientHeight here so aspect ratio does not distort speed
1000
+ this._panLeft( 2 * deltaX * targetDistance / element.clientHeight, this.object.matrix );
1001
+ this._panUp( 2 * deltaY * targetDistance / element.clientHeight, this.object.matrix );
1002
+
1003
+ } else if ( this.object.isOrthographicCamera ) {
1004
+
1005
+ // orthographic
1006
+ this._panLeft( deltaX * ( this.object.right - this.object.left ) / this.object.zoom / element.clientWidth, this.object.matrix );
1007
+ this._panUp( deltaY * ( this.object.top - this.object.bottom ) / this.object.zoom / element.clientHeight, this.object.matrix );
1008
+
1009
+ } else {
1010
+
1011
+ // camera neither orthographic nor perspective
1012
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
1013
+ this.enablePan = false;
1014
+
1015
+ }
1016
+
1017
+ }
1018
+
1019
+ _dollyOut( dollyScale ) {
1020
+
1021
+ if ( this.object.isPerspectiveCamera || this.object.isOrthographicCamera ) {
1022
+
1023
+ this._scale /= dollyScale;
1024
+
1025
+ } else {
1026
+
1027
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
1028
+ this.enableZoom = false;
1029
+
1030
+ }
1031
+
1032
+ }
1033
+
1034
+ _dollyIn( dollyScale ) {
1035
+
1036
+ if ( this.object.isPerspectiveCamera || this.object.isOrthographicCamera ) {
1037
+
1038
+ this._scale *= dollyScale;
1039
+
1040
+ } else {
1041
+
1042
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
1043
+ this.enableZoom = false;
1044
+
1045
+ }
1046
+
1047
+ }
1048
+
1049
+ _updateZoomParameters( x, y ) {
1050
+
1051
+ if ( ! this.zoomToCursor ) {
1052
+
1053
+ return;
1054
+
1055
+ }
1056
+
1057
+ this._performCursorZoom = true;
1058
+
1059
+ const rect = this.domElement.getBoundingClientRect();
1060
+ const dx = x - rect.left;
1061
+ const dy = y - rect.top;
1062
+ const w = rect.width;
1063
+ const h = rect.height;
1064
+
1065
+ this._mouse.x = ( dx / w ) * 2 - 1;
1066
+ this._mouse.y = - ( dy / h ) * 2 + 1;
1067
+
1068
+ this._dollyDirection.set( this._mouse.x, this._mouse.y, 1 ).unproject( this.object ).sub( this.object.position ).normalize();
1069
+
1070
+ }
1071
+
1072
+ _clampDistance( dist ) {
1073
+
1074
+ return Math.max( this.minDistance, Math.min( this.maxDistance, dist ) );
1075
+
1076
+ }
1077
+
1078
+ //
1079
+ // event callbacks - update the object state
1080
+ //
1081
+
1082
+ _handleMouseDownRotate( event ) {
1083
+
1084
+ this._rotateStart.set( event.clientX, event.clientY );
1085
+
1086
+ }
1087
+
1088
+ _handleMouseDownDolly( event ) {
1089
+
1090
+ this._updateZoomParameters( event.clientX, event.clientX );
1091
+ this._dollyStart.set( event.clientX, event.clientY );
1092
+
1093
+ }
1094
+
1095
+ _handleMouseDownPan( event ) {
1096
+
1097
+ this._panStart.set( event.clientX, event.clientY );
1098
+
1099
+ }
1100
+
1101
+ _handleMouseMoveRotate( event ) {
1102
+
1103
+ this._rotateEnd.set( event.clientX, event.clientY );
1104
+
1105
+ this._rotateDelta.subVectors( this._rotateEnd, this._rotateStart ).multiplyScalar( this.rotateSpeed );
1106
+
1107
+ const element = this.domElement;
1108
+
1109
+ this._rotateLeft( _twoPI * this._rotateDelta.x / element.clientHeight ); // yes, height
1110
+
1111
+ this._rotateUp( _twoPI * this._rotateDelta.y / element.clientHeight );
1112
+
1113
+ this._rotateStart.copy( this._rotateEnd );
1114
+
1115
+ this.update();
1116
+
1117
+ }
1118
+
1119
+ _handleMouseMoveDolly( event ) {
1120
+
1121
+ this._dollyEnd.set( event.clientX, event.clientY );
1122
+
1123
+ this._dollyDelta.subVectors( this._dollyEnd, this._dollyStart );
1124
+
1125
+ if ( this._dollyDelta.y > 0 ) {
1126
+
1127
+ this._dollyOut( this._getZoomScale( this._dollyDelta.y ) );
1128
+
1129
+ } else if ( this._dollyDelta.y < 0 ) {
1130
+
1131
+ this._dollyIn( this._getZoomScale( this._dollyDelta.y ) );
1132
+
1133
+ }
1134
+
1135
+ this._dollyStart.copy( this._dollyEnd );
1136
+
1137
+ this.update();
1138
+
1139
+ }
1140
+
1141
+ _handleMouseMovePan( event ) {
1142
+
1143
+ this._panEnd.set( event.clientX, event.clientY );
1144
+
1145
+ this._panDelta.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.panSpeed );
1146
+
1147
+ this._pan( this._panDelta.x, this._panDelta.y );
1148
+
1149
+ this._panStart.copy( this._panEnd );
1150
+
1151
+ this.update();
1152
+
1153
+ }
1154
+
1155
+ _handleMouseWheel( event ) {
1156
+
1157
+ this._updateZoomParameters( event.clientX, event.clientY );
1158
+
1159
+ if ( event.deltaY < 0 ) {
1160
+
1161
+ this._dollyIn( this._getZoomScale( event.deltaY ) );
1162
+
1163
+ } else if ( event.deltaY > 0 ) {
1164
+
1165
+ this._dollyOut( this._getZoomScale( event.deltaY ) );
1166
+
1167
+ }
1168
+
1169
+ this.update();
1170
+
1171
+ }
1172
+
1173
+ _handleKeyDown( event ) {
1174
+
1175
+ let needsUpdate = false;
1176
+
1177
+ switch ( event.code ) {
1178
+
1179
+ case this.keys.UP:
1180
+
1181
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
1182
+
1183
+ if ( this.enableRotate ) {
1184
+
1185
+ this._rotateUp( _twoPI * this.keyRotateSpeed / this.domElement.clientHeight );
1186
+
1187
+ }
1188
+
1189
+ } else {
1190
+
1191
+ if ( this.enablePan ) {
1192
+
1193
+ this._pan( 0, this.keyPanSpeed );
1194
+
1195
+ }
1196
+
1197
+ }
1198
+
1199
+ needsUpdate = true;
1200
+ break;
1201
+
1202
+ case this.keys.BOTTOM:
1203
+
1204
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
1205
+
1206
+ if ( this.enableRotate ) {
1207
+
1208
+ this._rotateUp( - _twoPI * this.keyRotateSpeed / this.domElement.clientHeight );
1209
+
1210
+ }
1211
+
1212
+ } else {
1213
+
1214
+ if ( this.enablePan ) {
1215
+
1216
+ this._pan( 0, - this.keyPanSpeed );
1217
+
1218
+ }
1219
+
1220
+ }
1221
+
1222
+ needsUpdate = true;
1223
+ break;
1224
+
1225
+ case this.keys.LEFT:
1226
+
1227
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
1228
+
1229
+ if ( this.enableRotate ) {
1230
+
1231
+ this._rotateLeft( _twoPI * this.keyRotateSpeed / this.domElement.clientHeight );
1232
+
1233
+ }
1234
+
1235
+ } else {
1236
+
1237
+ if ( this.enablePan ) {
1238
+
1239
+ this._pan( this.keyPanSpeed, 0 );
1240
+
1241
+ }
1242
+
1243
+ }
1244
+
1245
+ needsUpdate = true;
1246
+ break;
1247
+
1248
+ case this.keys.RIGHT:
1249
+
1250
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
1251
+
1252
+ if ( this.enableRotate ) {
1253
+
1254
+ this._rotateLeft( - _twoPI * this.keyRotateSpeed / this.domElement.clientHeight );
1255
+
1256
+ }
1257
+
1258
+ } else {
1259
+
1260
+ if ( this.enablePan ) {
1261
+
1262
+ this._pan( - this.keyPanSpeed, 0 );
1263
+
1264
+ }
1265
+
1266
+ }
1267
+
1268
+ needsUpdate = true;
1269
+ break;
1270
+
1271
+ }
1272
+
1273
+ if ( needsUpdate ) {
1274
+
1275
+ // prevent the browser from scrolling on cursor keys
1276
+ event.preventDefault();
1277
+
1278
+ this.update();
1279
+
1280
+ }
1281
+
1282
+
1283
+ }
1284
+
1285
+ _handleTouchStartRotate( event ) {
1286
+
1287
+ if ( this._pointers.length === 1 ) {
1288
+
1289
+ this._rotateStart.set( event.pageX, event.pageY );
1290
+
1291
+ } else {
1292
+
1293
+ const position = this._getSecondPointerPosition( event );
1294
+
1295
+ const x = 0.5 * ( event.pageX + position.x );
1296
+ const y = 0.5 * ( event.pageY + position.y );
1297
+
1298
+ this._rotateStart.set( x, y );
1299
+
1300
+ }
1301
+
1302
+ }
1303
+
1304
+ _handleTouchStartPan( event ) {
1305
+
1306
+ if ( this._pointers.length === 1 ) {
1307
+
1308
+ this._panStart.set( event.pageX, event.pageY );
1309
+
1310
+ } else {
1311
+
1312
+ const position = this._getSecondPointerPosition( event );
1313
+
1314
+ const x = 0.5 * ( event.pageX + position.x );
1315
+ const y = 0.5 * ( event.pageY + position.y );
1316
+
1317
+ this._panStart.set( x, y );
1318
+
1319
+ }
1320
+
1321
+ }
1322
+
1323
+ _handleTouchStartDolly( event ) {
1324
+
1325
+ const position = this._getSecondPointerPosition( event );
1326
+
1327
+ const dx = event.pageX - position.x;
1328
+ const dy = event.pageY - position.y;
1329
+
1330
+ const distance = Math.sqrt( dx * dx + dy * dy );
1331
+
1332
+ this._dollyStart.set( 0, distance );
1333
+
1334
+ }
1335
+
1336
+ _handleTouchStartDollyPan( event ) {
1337
+
1338
+ if ( this.enableZoom ) this._handleTouchStartDolly( event );
1339
+
1340
+ if ( this.enablePan ) this._handleTouchStartPan( event );
1341
+
1342
+ }
1343
+
1344
+ _handleTouchStartDollyRotate( event ) {
1345
+
1346
+ if ( this.enableZoom ) this._handleTouchStartDolly( event );
1347
+
1348
+ if ( this.enableRotate ) this._handleTouchStartRotate( event );
1349
+
1350
+ }
1351
+
1352
+ _handleTouchMoveRotate( event ) {
1353
+
1354
+ if ( this._pointers.length == 1 ) {
1355
+
1356
+ this._rotateEnd.set( event.pageX, event.pageY );
1357
+
1358
+ } else {
1359
+
1360
+ const position = this._getSecondPointerPosition( event );
1361
+
1362
+ const x = 0.5 * ( event.pageX + position.x );
1363
+ const y = 0.5 * ( event.pageY + position.y );
1364
+
1365
+ this._rotateEnd.set( x, y );
1366
+
1367
+ }
1368
+
1369
+ this._rotateDelta.subVectors( this._rotateEnd, this._rotateStart ).multiplyScalar( this.rotateSpeed );
1370
+
1371
+ const element = this.domElement;
1372
+
1373
+ this._rotateLeft( _twoPI * this._rotateDelta.x / element.clientHeight ); // yes, height
1374
+
1375
+ this._rotateUp( _twoPI * this._rotateDelta.y / element.clientHeight );
1376
+
1377
+ this._rotateStart.copy( this._rotateEnd );
1378
+
1379
+ }
1380
+
1381
+ _handleTouchMovePan( event ) {
1382
+
1383
+ if ( this._pointers.length === 1 ) {
1384
+
1385
+ this._panEnd.set( event.pageX, event.pageY );
1386
+
1387
+ } else {
1388
+
1389
+ const position = this._getSecondPointerPosition( event );
1390
+
1391
+ const x = 0.5 * ( event.pageX + position.x );
1392
+ const y = 0.5 * ( event.pageY + position.y );
1393
+
1394
+ this._panEnd.set( x, y );
1395
+
1396
+ }
1397
+
1398
+ this._panDelta.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.panSpeed );
1399
+
1400
+ this._pan( this._panDelta.x, this._panDelta.y );
1401
+
1402
+ this._panStart.copy( this._panEnd );
1403
+
1404
+ }
1405
+
1406
+ _handleTouchMoveDolly( event ) {
1407
+
1408
+ const position = this._getSecondPointerPosition( event );
1409
+
1410
+ const dx = event.pageX - position.x;
1411
+ const dy = event.pageY - position.y;
1412
+
1413
+ const distance = Math.sqrt( dx * dx + dy * dy );
1414
+
1415
+ this._dollyEnd.set( 0, distance );
1416
+
1417
+ this._dollyDelta.set( 0, Math.pow( this._dollyEnd.y / this._dollyStart.y, this.zoomSpeed ) );
1418
+
1419
+ this._dollyOut( this._dollyDelta.y );
1420
+
1421
+ this._dollyStart.copy( this._dollyEnd );
1422
+
1423
+ const centerX = ( event.pageX + position.x ) * 0.5;
1424
+ const centerY = ( event.pageY + position.y ) * 0.5;
1425
+
1426
+ this._updateZoomParameters( centerX, centerY );
1427
+
1428
+ }
1429
+
1430
+ _handleTouchMoveDollyPan( event ) {
1431
+
1432
+ if ( this.enableZoom ) this._handleTouchMoveDolly( event );
1433
+
1434
+ if ( this.enablePan ) this._handleTouchMovePan( event );
1435
+
1436
+ }
1437
+
1438
+ _handleTouchMoveDollyRotate( event ) {
1439
+
1440
+ if ( this.enableZoom ) this._handleTouchMoveDolly( event );
1441
+
1442
+ if ( this.enableRotate ) this._handleTouchMoveRotate( event );
1443
+
1444
+ }
1445
+
1446
+ // pointers
1447
+
1448
+ _addPointer( event ) {
1449
+
1450
+ this._pointers.push( event.pointerId );
1451
+
1452
+ }
1453
+
1454
+ _removePointer( event ) {
1455
+
1456
+ delete this._pointerPositions[ event.pointerId ];
1457
+
1458
+ for ( let i = 0; i < this._pointers.length; i ++ ) {
1459
+
1460
+ if ( this._pointers[ i ] == event.pointerId ) {
1461
+
1462
+ this._pointers.splice( i, 1 );
1463
+ return;
1464
+
1465
+ }
1466
+
1467
+ }
1468
+
1469
+ }
1470
+
1471
+ _isTrackingPointer( event ) {
1472
+
1473
+ for ( let i = 0; i < this._pointers.length; i ++ ) {
1474
+
1475
+ if ( this._pointers[ i ] == event.pointerId ) return true;
1476
+
1477
+ }
1478
+
1479
+ return false;
1480
+
1481
+ }
1482
+
1483
+ _trackPointer( event ) {
1484
+
1485
+ let position = this._pointerPositions[ event.pointerId ];
1486
+
1487
+ if ( position === undefined ) {
1488
+
1489
+ position = new Vector2();
1490
+ this._pointerPositions[ event.pointerId ] = position;
1491
+
1492
+ }
1493
+
1494
+ position.set( event.pageX, event.pageY );
1495
+
1496
+ }
1497
+
1498
+ _getSecondPointerPosition( event ) {
1499
+
1500
+ const pointerId = ( event.pointerId === this._pointers[ 0 ] ) ? this._pointers[ 1 ] : this._pointers[ 0 ];
1501
+
1502
+ return this._pointerPositions[ pointerId ];
1503
+
1504
+ }
1505
+
1506
+ //
1507
+
1508
+ _customWheelEvent( event ) {
1509
+
1510
+ const mode = event.deltaMode;
1511
+
1512
+ // minimal wheel event altered to meet delta-zoom demand
1513
+ const newEvent = {
1514
+ clientX: event.clientX,
1515
+ clientY: event.clientY,
1516
+ deltaY: event.deltaY,
1517
+ };
1518
+
1519
+ switch ( mode ) {
1520
+
1521
+ case 1: // LINE_MODE
1522
+ newEvent.deltaY *= 16;
1523
+ break;
1524
+
1525
+ case 2: // PAGE_MODE
1526
+ newEvent.deltaY *= 100;
1527
+ break;
1528
+
1529
+ }
1530
+
1531
+ // detect if event was triggered by pinching
1532
+ if ( event.ctrlKey && ! this._controlActive ) {
1533
+
1534
+ newEvent.deltaY *= 10;
1535
+
1536
+ }
1537
+
1538
+ return newEvent;
1539
+
1540
+ }
1541
+
1542
+ }
1543
+
1544
+ function onPointerDown( event ) {
1545
+
1546
+ if ( this.enabled === false ) return;
1547
+
1548
+ if ( this._pointers.length === 0 ) {
1549
+
1550
+ this.domElement.setPointerCapture( event.pointerId );
1551
+
1552
+ this.domElement.ownerDocument.addEventListener( 'pointermove', this._onPointerMove );
1553
+ this.domElement.ownerDocument.addEventListener( 'pointerup', this._onPointerUp );
1554
+
1555
+ }
1556
+
1557
+ //
1558
+
1559
+ if ( this._isTrackingPointer( event ) ) return;
1560
+
1561
+ //
1562
+
1563
+ this._addPointer( event );
1564
+
1565
+ if ( event.pointerType === 'touch' ) {
1566
+
1567
+ this._onTouchStart( event );
1568
+
1569
+ } else {
1570
+
1571
+ this._onMouseDown( event );
1572
+
1573
+ }
1574
+
1575
+ if ( this._cursorStyle === 'grab' ) {
1576
+
1577
+ this.domElement.style.cursor = 'grabbing';
1578
+
1579
+ }
1580
+
1581
+ }
1582
+
1583
+ function onPointerMove( event ) {
1584
+
1585
+ if ( this.enabled === false ) return;
1586
+
1587
+ if ( event.pointerType === 'touch' ) {
1588
+
1589
+ this._onTouchMove( event );
1590
+
1591
+ } else {
1592
+
1593
+ this._onMouseMove( event );
1594
+
1595
+ }
1596
+
1597
+ }
1598
+
1599
+ function onPointerUp( event ) {
1600
+
1601
+ this._removePointer( event );
1602
+
1603
+ switch ( this._pointers.length ) {
1604
+
1605
+ case 0:
1606
+
1607
+ this.domElement.releasePointerCapture( event.pointerId );
1608
+
1609
+ this.domElement.ownerDocument.removeEventListener( 'pointermove', this._onPointerMove );
1610
+ this.domElement.ownerDocument.removeEventListener( 'pointerup', this._onPointerUp );
1611
+
1612
+ this.dispatchEvent( _endEvent );
1613
+
1614
+ this.state = _STATE.NONE;
1615
+
1616
+ if ( this._cursorStyle === 'grab' ) {
1617
+
1618
+ this.domElement.style.cursor = 'grab';
1619
+
1620
+ }
1621
+
1622
+ break;
1623
+
1624
+ case 1:
1625
+
1626
+ const pointerId = this._pointers[ 0 ];
1627
+ const position = this._pointerPositions[ pointerId ];
1628
+
1629
+ // minimal placeholder event - allows state correction on pointer-up
1630
+ this._onTouchStart( { pointerId: pointerId, pageX: position.x, pageY: position.y } );
1631
+
1632
+ break;
1633
+
1634
+ }
1635
+
1636
+ }
1637
+
1638
+ function onMouseDown( event ) {
1639
+
1640
+ let mouseAction;
1641
+
1642
+ switch ( event.button ) {
1643
+
1644
+ case 0:
1645
+
1646
+ mouseAction = this.mouseButtons.LEFT;
1647
+ break;
1648
+
1649
+ case 1:
1650
+
1651
+ mouseAction = this.mouseButtons.MIDDLE;
1652
+ break;
1653
+
1654
+ case 2:
1655
+
1656
+ mouseAction = this.mouseButtons.RIGHT;
1657
+ break;
1658
+
1659
+ default:
1660
+
1661
+ mouseAction = - 1;
1662
+
1663
+ }
1664
+
1665
+ switch ( mouseAction ) {
1666
+
1667
+ case MOUSE.DOLLY:
1668
+
1669
+ if ( this.enableZoom === false ) return;
1670
+
1671
+ this._handleMouseDownDolly( event );
1672
+
1673
+ this.state = _STATE.DOLLY;
1674
+
1675
+ break;
1676
+
1677
+ case MOUSE.ROTATE:
1678
+
1679
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
1680
+
1681
+ if ( this.enablePan === false ) return;
1682
+
1683
+ this._handleMouseDownPan( event );
1684
+
1685
+ this.state = _STATE.PAN;
1686
+
1687
+ } else {
1688
+
1689
+ if ( this.enableRotate === false ) return;
1690
+
1691
+ this._handleMouseDownRotate( event );
1692
+
1693
+ this.state = _STATE.ROTATE;
1694
+
1695
+ }
1696
+
1697
+ break;
1698
+
1699
+ case MOUSE.PAN:
1700
+
1701
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
1702
+
1703
+ if ( this.enableRotate === false ) return;
1704
+
1705
+ this._handleMouseDownRotate( event );
1706
+
1707
+ this.state = _STATE.ROTATE;
1708
+
1709
+ } else {
1710
+
1711
+ if ( this.enablePan === false ) return;
1712
+
1713
+ this._handleMouseDownPan( event );
1714
+
1715
+ this.state = _STATE.PAN;
1716
+
1717
+ }
1718
+
1719
+ break;
1720
+
1721
+ default:
1722
+
1723
+ this.state = _STATE.NONE;
1724
+
1725
+ }
1726
+
1727
+ if ( this.state !== _STATE.NONE ) {
1728
+
1729
+ this.dispatchEvent( _startEvent );
1730
+
1731
+ }
1732
+
1733
+ }
1734
+
1735
+ function onMouseMove( event ) {
1736
+
1737
+ switch ( this.state ) {
1738
+
1739
+ case _STATE.ROTATE:
1740
+
1741
+ if ( this.enableRotate === false ) return;
1742
+
1743
+ this._handleMouseMoveRotate( event );
1744
+
1745
+ break;
1746
+
1747
+ case _STATE.DOLLY:
1748
+
1749
+ if ( this.enableZoom === false ) return;
1750
+
1751
+ this._handleMouseMoveDolly( event );
1752
+
1753
+ break;
1754
+
1755
+ case _STATE.PAN:
1756
+
1757
+ if ( this.enablePan === false ) return;
1758
+
1759
+ this._handleMouseMovePan( event );
1760
+
1761
+ break;
1762
+
1763
+ }
1764
+
1765
+ }
1766
+
1767
+ function onMouseWheel( event ) {
1768
+
1769
+ if ( this.enabled === false || this.enableZoom === false || this.state !== _STATE.NONE ) return;
1770
+
1771
+ event.preventDefault();
1772
+
1773
+ this.dispatchEvent( _startEvent );
1774
+
1775
+ this._handleMouseWheel( this._customWheelEvent( event ) );
1776
+
1777
+ this.dispatchEvent( _endEvent );
1778
+
1779
+ }
1780
+
1781
+ function onKeyDown( event ) {
1782
+
1783
+ if ( this.enabled === false ) return;
1784
+
1785
+ this._handleKeyDown( event );
1786
+
1787
+ }
1788
+
1789
+ function onTouchStart( event ) {
1790
+
1791
+ this._trackPointer( event );
1792
+
1793
+ switch ( this._pointers.length ) {
1794
+
1795
+ case 1:
1796
+
1797
+ switch ( this.touches.ONE ) {
1798
+
1799
+ case TOUCH.ROTATE:
1800
+
1801
+ if ( this.enableRotate === false ) return;
1802
+
1803
+ this._handleTouchStartRotate( event );
1804
+
1805
+ this.state = _STATE.TOUCH_ROTATE;
1806
+
1807
+ break;
1808
+
1809
+ case TOUCH.PAN:
1810
+
1811
+ if ( this.enablePan === false ) return;
1812
+
1813
+ this._handleTouchStartPan( event );
1814
+
1815
+ this.state = _STATE.TOUCH_PAN;
1816
+
1817
+ break;
1818
+
1819
+ default:
1820
+
1821
+ this.state = _STATE.NONE;
1822
+
1823
+ }
1824
+
1825
+ break;
1826
+
1827
+ case 2:
1828
+
1829
+ switch ( this.touches.TWO ) {
1830
+
1831
+ case TOUCH.DOLLY_PAN:
1832
+
1833
+ if ( this.enableZoom === false && this.enablePan === false ) return;
1834
+
1835
+ this._handleTouchStartDollyPan( event );
1836
+
1837
+ this.state = _STATE.TOUCH_DOLLY_PAN;
1838
+
1839
+ break;
1840
+
1841
+ case TOUCH.DOLLY_ROTATE:
1842
+
1843
+ if ( this.enableZoom === false && this.enableRotate === false ) return;
1844
+
1845
+ this._handleTouchStartDollyRotate( event );
1846
+
1847
+ this.state = _STATE.TOUCH_DOLLY_ROTATE;
1848
+
1849
+ break;
1850
+
1851
+ default:
1852
+
1853
+ this.state = _STATE.NONE;
1854
+
1855
+ }
1856
+
1857
+ break;
1858
+
1859
+ default:
1860
+
1861
+ this.state = _STATE.NONE;
1862
+
1863
+ }
1864
+
1865
+ if ( this.state !== _STATE.NONE ) {
1866
+
1867
+ this.dispatchEvent( _startEvent );
1868
+
1869
+ }
1870
+
1871
+ }
1872
+
1873
+ function onTouchMove( event ) {
1874
+
1875
+ this._trackPointer( event );
1876
+
1877
+ switch ( this.state ) {
1878
+
1879
+ case _STATE.TOUCH_ROTATE:
1880
+
1881
+ if ( this.enableRotate === false ) return;
1882
+
1883
+ this._handleTouchMoveRotate( event );
1884
+
1885
+ this.update();
1886
+
1887
+ break;
1888
+
1889
+ case _STATE.TOUCH_PAN:
1890
+
1891
+ if ( this.enablePan === false ) return;
1892
+
1893
+ this._handleTouchMovePan( event );
1894
+
1895
+ this.update();
1896
+
1897
+ break;
1898
+
1899
+ case _STATE.TOUCH_DOLLY_PAN:
1900
+
1901
+ if ( this.enableZoom === false && this.enablePan === false ) return;
1902
+
1903
+ this._handleTouchMoveDollyPan( event );
1904
+
1905
+ this.update();
1906
+
1907
+ break;
1908
+
1909
+ case _STATE.TOUCH_DOLLY_ROTATE:
1910
+
1911
+ if ( this.enableZoom === false && this.enableRotate === false ) return;
1912
+
1913
+ this._handleTouchMoveDollyRotate( event );
1914
+
1915
+ this.update();
1916
+
1917
+ break;
1918
+
1919
+ default:
1920
+
1921
+ this.state = _STATE.NONE;
1922
+
1923
+ }
1924
+
1925
+ }
1926
+
1927
+ function onContextMenu( event ) {
1928
+
1929
+ if ( this.enabled === false ) return;
1930
+
1931
+ event.preventDefault();
1932
+
1933
+ }
1934
+
1935
+ function interceptControlDown( event ) {
1936
+
1937
+ if ( event.key === 'Control' ) {
1938
+
1939
+ this._controlActive = true;
1940
+
1941
+ const document = this.domElement.getRootNode(); // offscreen canvas compatibility
1942
+
1943
+ document.addEventListener( 'keyup', this._interceptControlUp, { passive: true, capture: true } );
1944
+
1945
+ }
1946
+
1947
+ }
1948
+
1949
+ function interceptControlUp( event ) {
1950
+
1951
+ if ( event.key === 'Control' ) {
1952
+
1953
+ this._controlActive = false;
1954
+
1955
+ const document = this.domElement.getRootNode(); // offscreen canvas compatibility
1956
+
1957
+ document.removeEventListener( 'keyup', this._interceptControlUp, { passive: true, capture: true } );
1958
+
1959
+ }
1960
+
1961
+ }
1962
+
1963
+ export { OrbitControls };