sceneview-web 1.1.0 → 1.4.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,1417 @@
1
+ import {
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
+
21
+ const _changeEvent = { type: 'change' };
22
+ const _startEvent = { type: 'start' };
23
+ const _endEvent = { type: 'end' };
24
+ const _ray = new Ray();
25
+ const _plane = new Plane();
26
+ const TILT_LIMIT = Math.cos( 70 * MathUtils.DEG2RAD );
27
+
28
+ class 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
+ // adjust the camera position based on zoom only if we're not zooming to the cursor or if it's an ortho camera
265
+ // we adjust zoom later in these cases
266
+ if ( scope.zoomToCursor && performCursorZoom || scope.object.isOrthographicCamera ) {
267
+
268
+ spherical.radius = clampDistance( spherical.radius );
269
+
270
+ } else {
271
+
272
+ spherical.radius = clampDistance( spherical.radius * scale );
273
+
274
+ }
275
+
276
+ offset.setFromSpherical( spherical );
277
+
278
+ // rotate offset back to "camera-up-vector-is-up" space
279
+ offset.applyQuaternion( quatInverse );
280
+
281
+ position.copy( scope.target ).add( offset );
282
+
283
+ scope.object.lookAt( scope.target );
284
+
285
+ if ( scope.enableDamping === true ) {
286
+
287
+ sphericalDelta.theta *= ( 1 - scope.dampingFactor );
288
+ sphericalDelta.phi *= ( 1 - scope.dampingFactor );
289
+
290
+ panOffset.multiplyScalar( 1 - scope.dampingFactor );
291
+
292
+ } else {
293
+
294
+ sphericalDelta.set( 0, 0, 0 );
295
+
296
+ panOffset.set( 0, 0, 0 );
297
+
298
+ }
299
+
300
+ // adjust camera position
301
+ let zoomChanged = false;
302
+ if ( scope.zoomToCursor && performCursorZoom ) {
303
+
304
+ let newRadius = null;
305
+ if ( scope.object.isPerspectiveCamera ) {
306
+
307
+ // move the camera down the pointer ray
308
+ // this method avoids floating point error
309
+ const prevRadius = offset.length();
310
+ newRadius = clampDistance( prevRadius * scale );
311
+
312
+ const radiusDelta = prevRadius - newRadius;
313
+ scope.object.position.addScaledVector( dollyDirection, radiusDelta );
314
+ scope.object.updateMatrixWorld();
315
+
316
+ } else if ( scope.object.isOrthographicCamera ) {
317
+
318
+ // adjust the ortho camera position based on zoom changes
319
+ const mouseBefore = new Vector3( mouse.x, mouse.y, 0 );
320
+ mouseBefore.unproject( scope.object );
321
+
322
+ scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / scale ) );
323
+ scope.object.updateProjectionMatrix();
324
+ zoomChanged = true;
325
+
326
+ const mouseAfter = new Vector3( mouse.x, mouse.y, 0 );
327
+ mouseAfter.unproject( scope.object );
328
+
329
+ scope.object.position.sub( mouseAfter ).add( mouseBefore );
330
+ scope.object.updateMatrixWorld();
331
+
332
+ newRadius = offset.length();
333
+
334
+ } else {
335
+
336
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - zoom to cursor disabled.' );
337
+ scope.zoomToCursor = false;
338
+
339
+ }
340
+
341
+ // handle the placement of the target
342
+ if ( newRadius !== null ) {
343
+
344
+ if ( this.screenSpacePanning ) {
345
+
346
+ // position the orbit target in front of the new camera position
347
+ scope.target.set( 0, 0, - 1 )
348
+ .transformDirection( scope.object.matrix )
349
+ .multiplyScalar( newRadius )
350
+ .add( scope.object.position );
351
+
352
+ } else {
353
+
354
+ // get the ray and translation plane to compute target
355
+ _ray.origin.copy( scope.object.position );
356
+ _ray.direction.set( 0, 0, - 1 ).transformDirection( scope.object.matrix );
357
+
358
+ // if the camera is 20 degrees above the horizon then don't adjust the focus target to avoid
359
+ // extremely large values
360
+ if ( Math.abs( scope.object.up.dot( _ray.direction ) ) < TILT_LIMIT ) {
361
+
362
+ object.lookAt( scope.target );
363
+
364
+ } else {
365
+
366
+ _plane.setFromNormalAndCoplanarPoint( scope.object.up, scope.target );
367
+ _ray.intersectPlane( _plane, scope.target );
368
+
369
+ }
370
+
371
+ }
372
+
373
+ }
374
+
375
+ } else if ( scope.object.isOrthographicCamera ) {
376
+
377
+ scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / scale ) );
378
+ scope.object.updateProjectionMatrix();
379
+ zoomChanged = true;
380
+
381
+ }
382
+
383
+ scale = 1;
384
+ performCursorZoom = false;
385
+
386
+ // update condition is:
387
+ // min(camera displacement, camera rotation in radians)^2 > EPS
388
+ // using small-angle approximation cos(x/2) = 1 - x^2 / 8
389
+
390
+ if ( zoomChanged ||
391
+ lastPosition.distanceToSquared( scope.object.position ) > EPS ||
392
+ 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ||
393
+ lastTargetPosition.distanceToSquared( scope.target ) > 0 ) {
394
+
395
+ scope.dispatchEvent( _changeEvent );
396
+
397
+ lastPosition.copy( scope.object.position );
398
+ lastQuaternion.copy( scope.object.quaternion );
399
+ lastTargetPosition.copy( scope.target );
400
+
401
+ return true;
402
+
403
+ }
404
+
405
+ return false;
406
+
407
+ };
408
+
409
+ }();
410
+
411
+ this.dispose = function () {
412
+
413
+ scope.domElement.removeEventListener( 'contextmenu', onContextMenu );
414
+
415
+ scope.domElement.removeEventListener( 'pointerdown', onPointerDown );
416
+ scope.domElement.removeEventListener( 'pointercancel', onPointerUp );
417
+ scope.domElement.removeEventListener( 'wheel', onMouseWheel );
418
+
419
+ scope.domElement.removeEventListener( 'pointermove', onPointerMove );
420
+ scope.domElement.removeEventListener( 'pointerup', onPointerUp );
421
+
422
+
423
+ if ( scope._domElementKeyEvents !== null ) {
424
+
425
+ scope._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown );
426
+ scope._domElementKeyEvents = null;
427
+
428
+ }
429
+
430
+ //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
431
+
432
+ };
433
+
434
+ //
435
+ // internals
436
+ //
437
+
438
+ const scope = this;
439
+
440
+ const STATE = {
441
+ NONE: - 1,
442
+ ROTATE: 0,
443
+ DOLLY: 1,
444
+ PAN: 2,
445
+ TOUCH_ROTATE: 3,
446
+ TOUCH_PAN: 4,
447
+ TOUCH_DOLLY_PAN: 5,
448
+ TOUCH_DOLLY_ROTATE: 6
449
+ };
450
+
451
+ let state = STATE.NONE;
452
+
453
+ const EPS = 0.000001;
454
+
455
+ // current position in spherical coordinates
456
+ const spherical = new Spherical();
457
+ const sphericalDelta = new Spherical();
458
+
459
+ let scale = 1;
460
+ const panOffset = new Vector3();
461
+
462
+ const rotateStart = new Vector2();
463
+ const rotateEnd = new Vector2();
464
+ const rotateDelta = new Vector2();
465
+
466
+ const panStart = new Vector2();
467
+ const panEnd = new Vector2();
468
+ const panDelta = new Vector2();
469
+
470
+ const dollyStart = new Vector2();
471
+ const dollyEnd = new Vector2();
472
+ const dollyDelta = new Vector2();
473
+
474
+ const dollyDirection = new Vector3();
475
+ const mouse = new Vector2();
476
+ let performCursorZoom = false;
477
+
478
+ const pointers = [];
479
+ const pointerPositions = {};
480
+
481
+ function getAutoRotationAngle( deltaTime ) {
482
+
483
+ if ( deltaTime !== null ) {
484
+
485
+ return ( 2 * Math.PI / 60 * scope.autoRotateSpeed ) * deltaTime;
486
+
487
+ } else {
488
+
489
+ return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
490
+
491
+ }
492
+
493
+ }
494
+
495
+ function getZoomScale( delta ) {
496
+
497
+ const normalized_delta = Math.abs( delta ) / ( 100 * ( window.devicePixelRatio | 0 ) );
498
+ return Math.pow( 0.95, scope.zoomSpeed * normalized_delta );
499
+
500
+ }
501
+
502
+ function rotateLeft( angle ) {
503
+
504
+ sphericalDelta.theta -= angle;
505
+
506
+ }
507
+
508
+ function rotateUp( angle ) {
509
+
510
+ sphericalDelta.phi -= angle;
511
+
512
+ }
513
+
514
+ const panLeft = function () {
515
+
516
+ const v = new Vector3();
517
+
518
+ return function panLeft( distance, objectMatrix ) {
519
+
520
+ v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
521
+ v.multiplyScalar( - distance );
522
+
523
+ panOffset.add( v );
524
+
525
+ };
526
+
527
+ }();
528
+
529
+ const panUp = function () {
530
+
531
+ const v = new Vector3();
532
+
533
+ return function panUp( distance, objectMatrix ) {
534
+
535
+ if ( scope.screenSpacePanning === true ) {
536
+
537
+ v.setFromMatrixColumn( objectMatrix, 1 );
538
+
539
+ } else {
540
+
541
+ v.setFromMatrixColumn( objectMatrix, 0 );
542
+ v.crossVectors( scope.object.up, v );
543
+
544
+ }
545
+
546
+ v.multiplyScalar( distance );
547
+
548
+ panOffset.add( v );
549
+
550
+ };
551
+
552
+ }();
553
+
554
+ // deltaX and deltaY are in pixels; right and down are positive
555
+ const pan = function () {
556
+
557
+ const offset = new Vector3();
558
+
559
+ return function pan( deltaX, deltaY ) {
560
+
561
+ const element = scope.domElement;
562
+
563
+ if ( scope.object.isPerspectiveCamera ) {
564
+
565
+ // perspective
566
+ const position = scope.object.position;
567
+ offset.copy( position ).sub( scope.target );
568
+ let targetDistance = offset.length();
569
+
570
+ // half of the fov is center to top of screen
571
+ targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
572
+
573
+ // we use only clientHeight here so aspect ratio does not distort speed
574
+ panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
575
+ panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
576
+
577
+ } else if ( scope.object.isOrthographicCamera ) {
578
+
579
+ // orthographic
580
+ panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
581
+ panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
582
+
583
+ } else {
584
+
585
+ // camera neither orthographic nor perspective
586
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
587
+ scope.enablePan = false;
588
+
589
+ }
590
+
591
+ };
592
+
593
+ }();
594
+
595
+ function dollyOut( dollyScale ) {
596
+
597
+ if ( scope.object.isPerspectiveCamera || scope.object.isOrthographicCamera ) {
598
+
599
+ scale /= dollyScale;
600
+
601
+ } else {
602
+
603
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
604
+ scope.enableZoom = false;
605
+
606
+ }
607
+
608
+ }
609
+
610
+ function dollyIn( dollyScale ) {
611
+
612
+ if ( scope.object.isPerspectiveCamera || scope.object.isOrthographicCamera ) {
613
+
614
+ scale *= dollyScale;
615
+
616
+ } else {
617
+
618
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
619
+ scope.enableZoom = false;
620
+
621
+ }
622
+
623
+ }
624
+
625
+ function updateZoomParameters( x, y ) {
626
+
627
+ if ( ! scope.zoomToCursor ) {
628
+
629
+ return;
630
+
631
+ }
632
+
633
+ performCursorZoom = true;
634
+
635
+ const rect = scope.domElement.getBoundingClientRect();
636
+ const dx = x - rect.left;
637
+ const dy = y - rect.top;
638
+ const w = rect.width;
639
+ const h = rect.height;
640
+
641
+ mouse.x = ( dx / w ) * 2 - 1;
642
+ mouse.y = - ( dy / h ) * 2 + 1;
643
+
644
+ dollyDirection.set( mouse.x, mouse.y, 1 ).unproject( scope.object ).sub( scope.object.position ).normalize();
645
+
646
+ }
647
+
648
+ function clampDistance( dist ) {
649
+
650
+ return Math.max( scope.minDistance, Math.min( scope.maxDistance, dist ) );
651
+
652
+ }
653
+
654
+ //
655
+ // event callbacks - update the object state
656
+ //
657
+
658
+ function handleMouseDownRotate( event ) {
659
+
660
+ rotateStart.set( event.clientX, event.clientY );
661
+
662
+ }
663
+
664
+ function handleMouseDownDolly( event ) {
665
+
666
+ updateZoomParameters( event.clientX, event.clientX );
667
+ dollyStart.set( event.clientX, event.clientY );
668
+
669
+ }
670
+
671
+ function handleMouseDownPan( event ) {
672
+
673
+ panStart.set( event.clientX, event.clientY );
674
+
675
+ }
676
+
677
+ function handleMouseMoveRotate( event ) {
678
+
679
+ rotateEnd.set( event.clientX, event.clientY );
680
+
681
+ rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
682
+
683
+ const element = scope.domElement;
684
+
685
+ rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
686
+
687
+ rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
688
+
689
+ rotateStart.copy( rotateEnd );
690
+
691
+ scope.update();
692
+
693
+ }
694
+
695
+ function handleMouseMoveDolly( event ) {
696
+
697
+ dollyEnd.set( event.clientX, event.clientY );
698
+
699
+ dollyDelta.subVectors( dollyEnd, dollyStart );
700
+
701
+ if ( dollyDelta.y > 0 ) {
702
+
703
+ dollyOut( getZoomScale( dollyDelta.y ) );
704
+
705
+ } else if ( dollyDelta.y < 0 ) {
706
+
707
+ dollyIn( getZoomScale( dollyDelta.y ) );
708
+
709
+ }
710
+
711
+ dollyStart.copy( dollyEnd );
712
+
713
+ scope.update();
714
+
715
+ }
716
+
717
+ function handleMouseMovePan( event ) {
718
+
719
+ panEnd.set( event.clientX, event.clientY );
720
+
721
+ panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
722
+
723
+ pan( panDelta.x, panDelta.y );
724
+
725
+ panStart.copy( panEnd );
726
+
727
+ scope.update();
728
+
729
+ }
730
+
731
+ function handleMouseWheel( event ) {
732
+
733
+ updateZoomParameters( event.clientX, event.clientY );
734
+
735
+ if ( event.deltaY < 0 ) {
736
+
737
+ dollyIn( getZoomScale( event.deltaY ) );
738
+
739
+ } else if ( event.deltaY > 0 ) {
740
+
741
+ dollyOut( getZoomScale( event.deltaY ) );
742
+
743
+ }
744
+
745
+ scope.update();
746
+
747
+ }
748
+
749
+ function handleKeyDown( event ) {
750
+
751
+ let needsUpdate = false;
752
+
753
+ switch ( event.code ) {
754
+
755
+ case scope.keys.UP:
756
+
757
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
758
+
759
+ rotateUp( 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
760
+
761
+ } else {
762
+
763
+ pan( 0, scope.keyPanSpeed );
764
+
765
+ }
766
+
767
+ needsUpdate = true;
768
+ break;
769
+
770
+ case scope.keys.BOTTOM:
771
+
772
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
773
+
774
+ rotateUp( - 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
775
+
776
+ } else {
777
+
778
+ pan( 0, - scope.keyPanSpeed );
779
+
780
+ }
781
+
782
+ needsUpdate = true;
783
+ break;
784
+
785
+ case scope.keys.LEFT:
786
+
787
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
788
+
789
+ rotateLeft( 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
790
+
791
+ } else {
792
+
793
+ pan( scope.keyPanSpeed, 0 );
794
+
795
+ }
796
+
797
+ needsUpdate = true;
798
+ break;
799
+
800
+ case scope.keys.RIGHT:
801
+
802
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
803
+
804
+ rotateLeft( - 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
805
+
806
+ } else {
807
+
808
+ pan( - scope.keyPanSpeed, 0 );
809
+
810
+ }
811
+
812
+ needsUpdate = true;
813
+ break;
814
+
815
+ }
816
+
817
+ if ( needsUpdate ) {
818
+
819
+ // prevent the browser from scrolling on cursor keys
820
+ event.preventDefault();
821
+
822
+ scope.update();
823
+
824
+ }
825
+
826
+
827
+ }
828
+
829
+ function handleTouchStartRotate( event ) {
830
+
831
+ if ( pointers.length === 1 ) {
832
+
833
+ rotateStart.set( event.pageX, event.pageY );
834
+
835
+ } else {
836
+
837
+ const position = getSecondPointerPosition( event );
838
+
839
+ const x = 0.5 * ( event.pageX + position.x );
840
+ const y = 0.5 * ( event.pageY + position.y );
841
+
842
+ rotateStart.set( x, y );
843
+
844
+ }
845
+
846
+ }
847
+
848
+ function handleTouchStartPan( event ) {
849
+
850
+ if ( pointers.length === 1 ) {
851
+
852
+ panStart.set( event.pageX, event.pageY );
853
+
854
+ } else {
855
+
856
+ const position = getSecondPointerPosition( event );
857
+
858
+ const x = 0.5 * ( event.pageX + position.x );
859
+ const y = 0.5 * ( event.pageY + position.y );
860
+
861
+ panStart.set( x, y );
862
+
863
+ }
864
+
865
+ }
866
+
867
+ function handleTouchStartDolly( event ) {
868
+
869
+ const position = getSecondPointerPosition( event );
870
+
871
+ const dx = event.pageX - position.x;
872
+ const dy = event.pageY - position.y;
873
+
874
+ const distance = Math.sqrt( dx * dx + dy * dy );
875
+
876
+ dollyStart.set( 0, distance );
877
+
878
+ }
879
+
880
+ function handleTouchStartDollyPan( event ) {
881
+
882
+ if ( scope.enableZoom ) handleTouchStartDolly( event );
883
+
884
+ if ( scope.enablePan ) handleTouchStartPan( event );
885
+
886
+ }
887
+
888
+ function handleTouchStartDollyRotate( event ) {
889
+
890
+ if ( scope.enableZoom ) handleTouchStartDolly( event );
891
+
892
+ if ( scope.enableRotate ) handleTouchStartRotate( event );
893
+
894
+ }
895
+
896
+ function handleTouchMoveRotate( event ) {
897
+
898
+ if ( pointers.length == 1 ) {
899
+
900
+ rotateEnd.set( event.pageX, event.pageY );
901
+
902
+ } else {
903
+
904
+ const position = getSecondPointerPosition( event );
905
+
906
+ const x = 0.5 * ( event.pageX + position.x );
907
+ const y = 0.5 * ( event.pageY + position.y );
908
+
909
+ rotateEnd.set( x, y );
910
+
911
+ }
912
+
913
+ rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
914
+
915
+ const element = scope.domElement;
916
+
917
+ rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
918
+
919
+ rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
920
+
921
+ rotateStart.copy( rotateEnd );
922
+
923
+ }
924
+
925
+ function handleTouchMovePan( event ) {
926
+
927
+ if ( pointers.length === 1 ) {
928
+
929
+ panEnd.set( event.pageX, event.pageY );
930
+
931
+ } else {
932
+
933
+ const position = getSecondPointerPosition( event );
934
+
935
+ const x = 0.5 * ( event.pageX + position.x );
936
+ const y = 0.5 * ( event.pageY + position.y );
937
+
938
+ panEnd.set( x, y );
939
+
940
+ }
941
+
942
+ panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
943
+
944
+ pan( panDelta.x, panDelta.y );
945
+
946
+ panStart.copy( panEnd );
947
+
948
+ }
949
+
950
+ function handleTouchMoveDolly( event ) {
951
+
952
+ const position = getSecondPointerPosition( event );
953
+
954
+ const dx = event.pageX - position.x;
955
+ const dy = event.pageY - position.y;
956
+
957
+ const distance = Math.sqrt( dx * dx + dy * dy );
958
+
959
+ dollyEnd.set( 0, distance );
960
+
961
+ dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
962
+
963
+ dollyOut( dollyDelta.y );
964
+
965
+ dollyStart.copy( dollyEnd );
966
+
967
+ const centerX = ( event.pageX + position.x ) * 0.5;
968
+ const centerY = ( event.pageY + position.y ) * 0.5;
969
+
970
+ updateZoomParameters( centerX, centerY );
971
+
972
+ }
973
+
974
+ function handleTouchMoveDollyPan( event ) {
975
+
976
+ if ( scope.enableZoom ) handleTouchMoveDolly( event );
977
+
978
+ if ( scope.enablePan ) handleTouchMovePan( event );
979
+
980
+ }
981
+
982
+ function handleTouchMoveDollyRotate( event ) {
983
+
984
+ if ( scope.enableZoom ) handleTouchMoveDolly( event );
985
+
986
+ if ( scope.enableRotate ) handleTouchMoveRotate( event );
987
+
988
+ }
989
+
990
+ //
991
+ // event handlers - FSM: listen for events and reset state
992
+ //
993
+
994
+ function onPointerDown( event ) {
995
+
996
+ if ( scope.enabled === false ) return;
997
+
998
+ if ( pointers.length === 0 ) {
999
+
1000
+ scope.domElement.setPointerCapture( event.pointerId );
1001
+
1002
+ scope.domElement.addEventListener( 'pointermove', onPointerMove );
1003
+ scope.domElement.addEventListener( 'pointerup', onPointerUp );
1004
+
1005
+ }
1006
+
1007
+ //
1008
+
1009
+ addPointer( event );
1010
+
1011
+ if ( event.pointerType === 'touch' ) {
1012
+
1013
+ onTouchStart( event );
1014
+
1015
+ } else {
1016
+
1017
+ onMouseDown( event );
1018
+
1019
+ }
1020
+
1021
+ }
1022
+
1023
+ function onPointerMove( event ) {
1024
+
1025
+ if ( scope.enabled === false ) return;
1026
+
1027
+ if ( event.pointerType === 'touch' ) {
1028
+
1029
+ onTouchMove( event );
1030
+
1031
+ } else {
1032
+
1033
+ onMouseMove( event );
1034
+
1035
+ }
1036
+
1037
+ }
1038
+
1039
+ function onPointerUp( event ) {
1040
+
1041
+ removePointer( event );
1042
+
1043
+ if ( pointers.length === 0 ) {
1044
+
1045
+ scope.domElement.releasePointerCapture( event.pointerId );
1046
+
1047
+ scope.domElement.removeEventListener( 'pointermove', onPointerMove );
1048
+ scope.domElement.removeEventListener( 'pointerup', onPointerUp );
1049
+
1050
+ }
1051
+
1052
+ scope.dispatchEvent( _endEvent );
1053
+
1054
+ state = STATE.NONE;
1055
+
1056
+ }
1057
+
1058
+ function onMouseDown( event ) {
1059
+
1060
+ let mouseAction;
1061
+
1062
+ switch ( event.button ) {
1063
+
1064
+ case 0:
1065
+
1066
+ mouseAction = scope.mouseButtons.LEFT;
1067
+ break;
1068
+
1069
+ case 1:
1070
+
1071
+ mouseAction = scope.mouseButtons.MIDDLE;
1072
+ break;
1073
+
1074
+ case 2:
1075
+
1076
+ mouseAction = scope.mouseButtons.RIGHT;
1077
+ break;
1078
+
1079
+ default:
1080
+
1081
+ mouseAction = - 1;
1082
+
1083
+ }
1084
+
1085
+ switch ( mouseAction ) {
1086
+
1087
+ case MOUSE.DOLLY:
1088
+
1089
+ if ( scope.enableZoom === false ) return;
1090
+
1091
+ handleMouseDownDolly( event );
1092
+
1093
+ state = STATE.DOLLY;
1094
+
1095
+ break;
1096
+
1097
+ case MOUSE.ROTATE:
1098
+
1099
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
1100
+
1101
+ if ( scope.enablePan === false ) return;
1102
+
1103
+ handleMouseDownPan( event );
1104
+
1105
+ state = STATE.PAN;
1106
+
1107
+ } else {
1108
+
1109
+ if ( scope.enableRotate === false ) return;
1110
+
1111
+ handleMouseDownRotate( event );
1112
+
1113
+ state = STATE.ROTATE;
1114
+
1115
+ }
1116
+
1117
+ break;
1118
+
1119
+ case MOUSE.PAN:
1120
+
1121
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
1122
+
1123
+ if ( scope.enableRotate === false ) return;
1124
+
1125
+ handleMouseDownRotate( event );
1126
+
1127
+ state = STATE.ROTATE;
1128
+
1129
+ } else {
1130
+
1131
+ if ( scope.enablePan === false ) return;
1132
+
1133
+ handleMouseDownPan( event );
1134
+
1135
+ state = STATE.PAN;
1136
+
1137
+ }
1138
+
1139
+ break;
1140
+
1141
+ default:
1142
+
1143
+ state = STATE.NONE;
1144
+
1145
+ }
1146
+
1147
+ if ( state !== STATE.NONE ) {
1148
+
1149
+ scope.dispatchEvent( _startEvent );
1150
+
1151
+ }
1152
+
1153
+ }
1154
+
1155
+ function onMouseMove( event ) {
1156
+
1157
+ switch ( state ) {
1158
+
1159
+ case STATE.ROTATE:
1160
+
1161
+ if ( scope.enableRotate === false ) return;
1162
+
1163
+ handleMouseMoveRotate( event );
1164
+
1165
+ break;
1166
+
1167
+ case STATE.DOLLY:
1168
+
1169
+ if ( scope.enableZoom === false ) return;
1170
+
1171
+ handleMouseMoveDolly( event );
1172
+
1173
+ break;
1174
+
1175
+ case STATE.PAN:
1176
+
1177
+ if ( scope.enablePan === false ) return;
1178
+
1179
+ handleMouseMovePan( event );
1180
+
1181
+ break;
1182
+
1183
+ }
1184
+
1185
+ }
1186
+
1187
+ function onMouseWheel( event ) {
1188
+
1189
+ if ( scope.enabled === false || scope.enableZoom === false || state !== STATE.NONE ) return;
1190
+
1191
+ event.preventDefault();
1192
+
1193
+ scope.dispatchEvent( _startEvent );
1194
+
1195
+ handleMouseWheel( event );
1196
+
1197
+ scope.dispatchEvent( _endEvent );
1198
+
1199
+ }
1200
+
1201
+ function onKeyDown( event ) {
1202
+
1203
+ if ( scope.enabled === false || scope.enablePan === false ) return;
1204
+
1205
+ handleKeyDown( event );
1206
+
1207
+ }
1208
+
1209
+ function onTouchStart( event ) {
1210
+
1211
+ trackPointer( event );
1212
+
1213
+ switch ( pointers.length ) {
1214
+
1215
+ case 1:
1216
+
1217
+ switch ( scope.touches.ONE ) {
1218
+
1219
+ case TOUCH.ROTATE:
1220
+
1221
+ if ( scope.enableRotate === false ) return;
1222
+
1223
+ handleTouchStartRotate( event );
1224
+
1225
+ state = STATE.TOUCH_ROTATE;
1226
+
1227
+ break;
1228
+
1229
+ case TOUCH.PAN:
1230
+
1231
+ if ( scope.enablePan === false ) return;
1232
+
1233
+ handleTouchStartPan( event );
1234
+
1235
+ state = STATE.TOUCH_PAN;
1236
+
1237
+ break;
1238
+
1239
+ default:
1240
+
1241
+ state = STATE.NONE;
1242
+
1243
+ }
1244
+
1245
+ break;
1246
+
1247
+ case 2:
1248
+
1249
+ switch ( scope.touches.TWO ) {
1250
+
1251
+ case TOUCH.DOLLY_PAN:
1252
+
1253
+ if ( scope.enableZoom === false && scope.enablePan === false ) return;
1254
+
1255
+ handleTouchStartDollyPan( event );
1256
+
1257
+ state = STATE.TOUCH_DOLLY_PAN;
1258
+
1259
+ break;
1260
+
1261
+ case TOUCH.DOLLY_ROTATE:
1262
+
1263
+ if ( scope.enableZoom === false && scope.enableRotate === false ) return;
1264
+
1265
+ handleTouchStartDollyRotate( event );
1266
+
1267
+ state = STATE.TOUCH_DOLLY_ROTATE;
1268
+
1269
+ break;
1270
+
1271
+ default:
1272
+
1273
+ state = STATE.NONE;
1274
+
1275
+ }
1276
+
1277
+ break;
1278
+
1279
+ default:
1280
+
1281
+ state = STATE.NONE;
1282
+
1283
+ }
1284
+
1285
+ if ( state !== STATE.NONE ) {
1286
+
1287
+ scope.dispatchEvent( _startEvent );
1288
+
1289
+ }
1290
+
1291
+ }
1292
+
1293
+ function onTouchMove( event ) {
1294
+
1295
+ trackPointer( event );
1296
+
1297
+ switch ( state ) {
1298
+
1299
+ case STATE.TOUCH_ROTATE:
1300
+
1301
+ if ( scope.enableRotate === false ) return;
1302
+
1303
+ handleTouchMoveRotate( event );
1304
+
1305
+ scope.update();
1306
+
1307
+ break;
1308
+
1309
+ case STATE.TOUCH_PAN:
1310
+
1311
+ if ( scope.enablePan === false ) return;
1312
+
1313
+ handleTouchMovePan( event );
1314
+
1315
+ scope.update();
1316
+
1317
+ break;
1318
+
1319
+ case STATE.TOUCH_DOLLY_PAN:
1320
+
1321
+ if ( scope.enableZoom === false && scope.enablePan === false ) return;
1322
+
1323
+ handleTouchMoveDollyPan( event );
1324
+
1325
+ scope.update();
1326
+
1327
+ break;
1328
+
1329
+ case STATE.TOUCH_DOLLY_ROTATE:
1330
+
1331
+ if ( scope.enableZoom === false && scope.enableRotate === false ) return;
1332
+
1333
+ handleTouchMoveDollyRotate( event );
1334
+
1335
+ scope.update();
1336
+
1337
+ break;
1338
+
1339
+ default:
1340
+
1341
+ state = STATE.NONE;
1342
+
1343
+ }
1344
+
1345
+ }
1346
+
1347
+ function onContextMenu( event ) {
1348
+
1349
+ if ( scope.enabled === false ) return;
1350
+
1351
+ event.preventDefault();
1352
+
1353
+ }
1354
+
1355
+ function addPointer( event ) {
1356
+
1357
+ pointers.push( event.pointerId );
1358
+
1359
+ }
1360
+
1361
+ function removePointer( event ) {
1362
+
1363
+ delete pointerPositions[ event.pointerId ];
1364
+
1365
+ for ( let i = 0; i < pointers.length; i ++ ) {
1366
+
1367
+ if ( pointers[ i ] == event.pointerId ) {
1368
+
1369
+ pointers.splice( i, 1 );
1370
+ return;
1371
+
1372
+ }
1373
+
1374
+ }
1375
+
1376
+ }
1377
+
1378
+ function trackPointer( event ) {
1379
+
1380
+ let position = pointerPositions[ event.pointerId ];
1381
+
1382
+ if ( position === undefined ) {
1383
+
1384
+ position = new Vector2();
1385
+ pointerPositions[ event.pointerId ] = position;
1386
+
1387
+ }
1388
+
1389
+ position.set( event.pageX, event.pageY );
1390
+
1391
+ }
1392
+
1393
+ function getSecondPointerPosition( event ) {
1394
+
1395
+ const pointerId = ( event.pointerId === pointers[ 0 ] ) ? pointers[ 1 ] : pointers[ 0 ];
1396
+
1397
+ return pointerPositions[ pointerId ];
1398
+
1399
+ }
1400
+
1401
+ //
1402
+
1403
+ scope.domElement.addEventListener( 'contextmenu', onContextMenu );
1404
+
1405
+ scope.domElement.addEventListener( 'pointerdown', onPointerDown );
1406
+ scope.domElement.addEventListener( 'pointercancel', onPointerUp );
1407
+ scope.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
1408
+
1409
+ // force an update at start
1410
+
1411
+ this.update();
1412
+
1413
+ }
1414
+
1415
+ }
1416
+
1417
+ export { OrbitControls };