dsh-cad-studio 0.0.0-stage → 0.2.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,1101 @@
1
+ ( function () {
2
+
3
+ // This set of controls performs orbiting, dollying (zooming), and panning.
4
+ // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
5
+ //
6
+ // Orbit - left mouse / touch: one-finger move
7
+ // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
8
+ // Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move
9
+
10
+ const _changeEvent = {
11
+ type: 'change'
12
+ };
13
+ const _startEvent = {
14
+ type: 'start'
15
+ };
16
+ const _endEvent = {
17
+ type: 'end'
18
+ };
19
+ class OrbitControls extends THREE.EventDispatcher {
20
+
21
+ constructor( object, domElement ) {
22
+
23
+ super();
24
+ this.object = object;
25
+ this.domElement = domElement;
26
+ this.domElement.style.touchAction = 'none'; // disable touch scroll
27
+
28
+ // Set to false to disable this control
29
+ this.enabled = true;
30
+
31
+ // "target" sets the location of focus, where the object orbits around
32
+ this.target = new THREE.Vector3();
33
+
34
+ // How far you can dolly in and out ( PerspectiveCamera only )
35
+ this.minDistance = 0;
36
+ this.maxDistance = Infinity;
37
+
38
+ // How far you can zoom in and out ( OrthographicCamera only )
39
+ this.minZoom = 0;
40
+ this.maxZoom = Infinity;
41
+
42
+ // How far you can orbit vertically, upper and lower limits.
43
+ // Range is 0 to Math.PI radians.
44
+ this.minPolarAngle = 0; // radians
45
+ this.maxPolarAngle = Math.PI; // radians
46
+
47
+ // How far you can orbit horizontally, upper and lower limits.
48
+ // If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI )
49
+ this.minAzimuthAngle = - Infinity; // radians
50
+ this.maxAzimuthAngle = Infinity; // radians
51
+
52
+ // Set to true to enable damping (inertia)
53
+ // If damping is enabled, you must call controls.update() in your animation loop
54
+ this.enableDamping = false;
55
+ this.dampingFactor = 0.05;
56
+
57
+ // This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
58
+ // Set to false to disable zooming
59
+ this.enableZoom = true;
60
+ this.zoomSpeed = 1.0;
61
+
62
+ // Set to false to disable rotating
63
+ this.enableRotate = true;
64
+ this.rotateSpeed = 1.0;
65
+
66
+ // Set to false to disable panning
67
+ this.enablePan = true;
68
+ this.panSpeed = 1.0;
69
+ this.screenSpacePanning = true; // if false, pan orthogonal to world-space direction camera.up
70
+ this.keyPanSpeed = 7.0; // pixels moved per arrow key push
71
+
72
+ // Set to true to automatically rotate around the target
73
+ // If auto-rotate is enabled, you must call controls.update() in your animation loop
74
+ this.autoRotate = false;
75
+ this.autoRotateSpeed = 2.0; // 30 seconds per orbit when fps is 60
76
+
77
+ // The four arrow keys
78
+ this.keys = {
79
+ LEFT: 'ArrowLeft',
80
+ UP: 'ArrowUp',
81
+ RIGHT: 'ArrowRight',
82
+ BOTTOM: 'ArrowDown'
83
+ };
84
+
85
+ // Mouse buttons
86
+ this.mouseButtons = {
87
+ LEFT: THREE.MOUSE.ROTATE,
88
+ MIDDLE: THREE.MOUSE.DOLLY,
89
+ RIGHT: THREE.MOUSE.PAN
90
+ };
91
+
92
+ // Touch fingers
93
+ this.touches = {
94
+ ONE: THREE.TOUCH.ROTATE,
95
+ TWO: THREE.TOUCH.DOLLY_PAN
96
+ };
97
+
98
+ // for reset
99
+ this.target0 = this.target.clone();
100
+ this.position0 = this.object.position.clone();
101
+ this.zoom0 = this.object.zoom;
102
+
103
+ // the target DOM element for key events
104
+ this._domElementKeyEvents = null;
105
+
106
+ //
107
+ // public methods
108
+ //
109
+
110
+ this.getPolarAngle = function () {
111
+
112
+ return spherical.phi;
113
+
114
+ };
115
+
116
+ this.getAzimuthalAngle = function () {
117
+
118
+ return spherical.theta;
119
+
120
+ };
121
+
122
+ this.getDistance = function () {
123
+
124
+ return this.object.position.distanceTo( this.target );
125
+
126
+ };
127
+
128
+ this.listenToKeyEvents = function ( domElement ) {
129
+
130
+ domElement.addEventListener( 'keydown', onKeyDown );
131
+ this._domElementKeyEvents = domElement;
132
+
133
+ };
134
+
135
+ this.saveState = function () {
136
+
137
+ scope.target0.copy( scope.target );
138
+ scope.position0.copy( scope.object.position );
139
+ scope.zoom0 = scope.object.zoom;
140
+
141
+ };
142
+
143
+ this.reset = function () {
144
+
145
+ scope.target.copy( scope.target0 );
146
+ scope.object.position.copy( scope.position0 );
147
+ scope.object.zoom = scope.zoom0;
148
+ scope.object.updateProjectionMatrix();
149
+ scope.dispatchEvent( _changeEvent );
150
+ scope.update();
151
+ state = STATE.NONE;
152
+
153
+ };
154
+
155
+ // this method is exposed, but perhaps it would be better if we can make it private...
156
+ this.update = function () {
157
+
158
+ const offset = new THREE.Vector3();
159
+
160
+ // so camera.up is the orbit axis
161
+ const quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
162
+ const quatInverse = quat.clone().invert();
163
+ const lastPosition = new THREE.Vector3();
164
+ const lastQuaternion = new THREE.Quaternion();
165
+ const twoPI = 2 * Math.PI;
166
+ return function update() {
167
+
168
+ const position = scope.object.position;
169
+ offset.copy( position ).sub( scope.target );
170
+
171
+ // rotate offset to "y-axis-is-up" space
172
+ offset.applyQuaternion( quat );
173
+
174
+ // angle from z-axis around y-axis
175
+ spherical.setFromVector3( offset );
176
+ if ( scope.autoRotate && state === STATE.NONE ) {
177
+
178
+ rotateLeft( getAutoRotationAngle() );
179
+
180
+ }
181
+
182
+ if ( scope.enableDamping ) {
183
+
184
+ spherical.theta += sphericalDelta.theta * scope.dampingFactor;
185
+ spherical.phi += sphericalDelta.phi * scope.dampingFactor;
186
+
187
+ } else {
188
+
189
+ spherical.theta += sphericalDelta.theta;
190
+ spherical.phi += sphericalDelta.phi;
191
+
192
+ }
193
+
194
+ // restrict theta to be between desired limits
195
+
196
+ let min = scope.minAzimuthAngle;
197
+ let max = scope.maxAzimuthAngle;
198
+ if ( isFinite( min ) && isFinite( max ) ) {
199
+
200
+ if ( min < - Math.PI ) min += twoPI; else if ( min > Math.PI ) min -= twoPI;
201
+ if ( max < - Math.PI ) max += twoPI; else if ( max > Math.PI ) max -= twoPI;
202
+ if ( min <= max ) {
203
+
204
+ spherical.theta = Math.max( min, Math.min( max, spherical.theta ) );
205
+
206
+ } else {
207
+
208
+ spherical.theta = spherical.theta > ( min + max ) / 2 ? Math.max( min, spherical.theta ) : Math.min( max, spherical.theta );
209
+
210
+ }
211
+
212
+ }
213
+
214
+ // restrict phi to be between desired limits
215
+ spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
216
+ spherical.makeSafe();
217
+ spherical.radius *= scale;
218
+
219
+ // restrict radius to be between desired limits
220
+ spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
221
+
222
+ // move target to panned location
223
+
224
+ if ( scope.enableDamping === true ) {
225
+
226
+ scope.target.addScaledVector( panOffset, scope.dampingFactor );
227
+
228
+ } else {
229
+
230
+ scope.target.add( panOffset );
231
+
232
+ }
233
+
234
+ offset.setFromSpherical( spherical );
235
+
236
+ // rotate offset back to "camera-up-vector-is-up" space
237
+ offset.applyQuaternion( quatInverse );
238
+ position.copy( scope.target ).add( offset );
239
+ scope.object.lookAt( scope.target );
240
+ if ( scope.enableDamping === true ) {
241
+
242
+ sphericalDelta.theta *= 1 - scope.dampingFactor;
243
+ sphericalDelta.phi *= 1 - scope.dampingFactor;
244
+ panOffset.multiplyScalar( 1 - scope.dampingFactor );
245
+
246
+ } else {
247
+
248
+ sphericalDelta.set( 0, 0, 0 );
249
+ panOffset.set( 0, 0, 0 );
250
+
251
+ }
252
+
253
+ scale = 1;
254
+
255
+ // update condition is:
256
+ // min(camera displacement, camera rotation in radians)^2 > EPS
257
+ // using small-angle approximation cos(x/2) = 1 - x^2 / 8
258
+
259
+ if ( zoomChanged || lastPosition.distanceToSquared( scope.object.position ) > EPS || 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
260
+
261
+ scope.dispatchEvent( _changeEvent );
262
+ lastPosition.copy( scope.object.position );
263
+ lastQuaternion.copy( scope.object.quaternion );
264
+ zoomChanged = false;
265
+ return true;
266
+
267
+ }
268
+
269
+ return false;
270
+
271
+ };
272
+
273
+ }();
274
+ this.dispose = function () {
275
+
276
+ scope.domElement.removeEventListener( 'contextmenu', onContextMenu );
277
+ scope.domElement.removeEventListener( 'pointerdown', onPointerDown );
278
+ scope.domElement.removeEventListener( 'pointercancel', onPointerCancel );
279
+ scope.domElement.removeEventListener( 'wheel', onMouseWheel );
280
+ scope.domElement.removeEventListener( 'pointermove', onPointerMove );
281
+ scope.domElement.removeEventListener( 'pointerup', onPointerUp );
282
+ if ( scope._domElementKeyEvents !== null ) {
283
+
284
+ scope._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown );
285
+
286
+ }
287
+
288
+ //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
289
+
290
+ };
291
+
292
+ //
293
+ // internals
294
+ //
295
+
296
+ const scope = this;
297
+ const STATE = {
298
+ NONE: - 1,
299
+ ROTATE: 0,
300
+ DOLLY: 1,
301
+ PAN: 2,
302
+ TOUCH_ROTATE: 3,
303
+ TOUCH_PAN: 4,
304
+ TOUCH_DOLLY_PAN: 5,
305
+ TOUCH_DOLLY_ROTATE: 6
306
+ };
307
+ let state = STATE.NONE;
308
+ const EPS = 0.000001;
309
+
310
+ // current position in spherical coordinates
311
+ const spherical = new THREE.Spherical();
312
+ const sphericalDelta = new THREE.Spherical();
313
+ let scale = 1;
314
+ const panOffset = new THREE.Vector3();
315
+ let zoomChanged = false;
316
+ const rotateStart = new THREE.Vector2();
317
+ const rotateEnd = new THREE.Vector2();
318
+ const rotateDelta = new THREE.Vector2();
319
+ const panStart = new THREE.Vector2();
320
+ const panEnd = new THREE.Vector2();
321
+ const panDelta = new THREE.Vector2();
322
+ const dollyStart = new THREE.Vector2();
323
+ const dollyEnd = new THREE.Vector2();
324
+ const dollyDelta = new THREE.Vector2();
325
+ const pointers = [];
326
+ const pointerPositions = {};
327
+ function getAutoRotationAngle() {
328
+
329
+ return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
330
+
331
+ }
332
+
333
+ function getZoomScale() {
334
+
335
+ return Math.pow( 0.95, scope.zoomSpeed );
336
+
337
+ }
338
+
339
+ function rotateLeft( angle ) {
340
+
341
+ sphericalDelta.theta -= angle;
342
+
343
+ }
344
+
345
+ function rotateUp( angle ) {
346
+
347
+ sphericalDelta.phi -= angle;
348
+
349
+ }
350
+
351
+ const panLeft = function () {
352
+
353
+ const v = new THREE.Vector3();
354
+ return function panLeft( distance, objectMatrix ) {
355
+
356
+ v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
357
+ v.multiplyScalar( - distance );
358
+ panOffset.add( v );
359
+
360
+ };
361
+
362
+ }();
363
+ const panUp = function () {
364
+
365
+ const v = new THREE.Vector3();
366
+ return function panUp( distance, objectMatrix ) {
367
+
368
+ if ( scope.screenSpacePanning === true ) {
369
+
370
+ v.setFromMatrixColumn( objectMatrix, 1 );
371
+
372
+ } else {
373
+
374
+ v.setFromMatrixColumn( objectMatrix, 0 );
375
+ v.crossVectors( scope.object.up, v );
376
+
377
+ }
378
+
379
+ v.multiplyScalar( distance );
380
+ panOffset.add( v );
381
+
382
+ };
383
+
384
+ }();
385
+
386
+ // deltaX and deltaY are in pixels; right and down are positive
387
+ const pan = function () {
388
+
389
+ const offset = new THREE.Vector3();
390
+ return function pan( deltaX, deltaY ) {
391
+
392
+ const element = scope.domElement;
393
+ if ( scope.object.isPerspectiveCamera ) {
394
+
395
+ // perspective
396
+ const position = scope.object.position;
397
+ offset.copy( position ).sub( scope.target );
398
+ let targetDistance = offset.length();
399
+
400
+ // half of the fov is center to top of screen
401
+ targetDistance *= Math.tan( scope.object.fov / 2 * Math.PI / 180.0 );
402
+
403
+ // we use only clientHeight here so aspect ratio does not distort speed
404
+ panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
405
+ panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
406
+
407
+ } else if ( scope.object.isOrthographicCamera ) {
408
+
409
+ // orthographic
410
+ panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
411
+ panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
412
+
413
+ } else {
414
+
415
+ // camera neither orthographic nor perspective
416
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
417
+ scope.enablePan = false;
418
+
419
+ }
420
+
421
+ };
422
+
423
+ }();
424
+ function dollyOut( dollyScale ) {
425
+
426
+ if ( scope.object.isPerspectiveCamera ) {
427
+
428
+ scale /= dollyScale;
429
+
430
+ } else if ( scope.object.isOrthographicCamera ) {
431
+
432
+ scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
433
+ scope.object.updateProjectionMatrix();
434
+ zoomChanged = true;
435
+
436
+ } else {
437
+
438
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
439
+ scope.enableZoom = false;
440
+
441
+ }
442
+
443
+ }
444
+
445
+ function dollyIn( dollyScale ) {
446
+
447
+ if ( scope.object.isPerspectiveCamera ) {
448
+
449
+ scale *= dollyScale;
450
+
451
+ } else if ( scope.object.isOrthographicCamera ) {
452
+
453
+ scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
454
+ scope.object.updateProjectionMatrix();
455
+ zoomChanged = true;
456
+
457
+ } else {
458
+
459
+ console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
460
+ scope.enableZoom = false;
461
+
462
+ }
463
+
464
+ }
465
+
466
+ //
467
+ // event callbacks - update the object state
468
+ //
469
+
470
+ function handleMouseDownRotate( event ) {
471
+
472
+ rotateStart.set( event.clientX, event.clientY );
473
+
474
+ }
475
+
476
+ function handleMouseDownDolly( event ) {
477
+
478
+ dollyStart.set( event.clientX, event.clientY );
479
+
480
+ }
481
+
482
+ function handleMouseDownPan( event ) {
483
+
484
+ panStart.set( event.clientX, event.clientY );
485
+
486
+ }
487
+
488
+ function handleMouseMoveRotate( event ) {
489
+
490
+ rotateEnd.set( event.clientX, event.clientY );
491
+ rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
492
+ const element = scope.domElement;
493
+ rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
494
+
495
+ rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
496
+ rotateStart.copy( rotateEnd );
497
+ scope.update();
498
+
499
+ }
500
+
501
+ function handleMouseMoveDolly( event ) {
502
+
503
+ dollyEnd.set( event.clientX, event.clientY );
504
+ dollyDelta.subVectors( dollyEnd, dollyStart );
505
+ if ( dollyDelta.y > 0 ) {
506
+
507
+ dollyOut( getZoomScale() );
508
+
509
+ } else if ( dollyDelta.y < 0 ) {
510
+
511
+ dollyIn( getZoomScale() );
512
+
513
+ }
514
+
515
+ dollyStart.copy( dollyEnd );
516
+ scope.update();
517
+
518
+ }
519
+
520
+ function handleMouseMovePan( event ) {
521
+
522
+ panEnd.set( event.clientX, event.clientY );
523
+ panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
524
+ pan( panDelta.x, panDelta.y );
525
+ panStart.copy( panEnd );
526
+ scope.update();
527
+
528
+ }
529
+
530
+ function handleMouseWheel( event ) {
531
+
532
+ if ( event.deltaY < 0 ) {
533
+
534
+ dollyIn( getZoomScale() );
535
+
536
+ } else if ( event.deltaY > 0 ) {
537
+
538
+ dollyOut( getZoomScale() );
539
+
540
+ }
541
+
542
+ scope.update();
543
+
544
+ }
545
+
546
+ function handleKeyDown( event ) {
547
+
548
+ let needsUpdate = false;
549
+ switch ( event.code ) {
550
+
551
+ case scope.keys.UP:
552
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
553
+
554
+ rotateUp( 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
555
+
556
+ } else {
557
+
558
+ pan( 0, scope.keyPanSpeed );
559
+
560
+ }
561
+
562
+ needsUpdate = true;
563
+ break;
564
+ case scope.keys.BOTTOM:
565
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
566
+
567
+ rotateUp( - 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
568
+
569
+ } else {
570
+
571
+ pan( 0, - scope.keyPanSpeed );
572
+
573
+ }
574
+
575
+ needsUpdate = true;
576
+ break;
577
+ case scope.keys.LEFT:
578
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
579
+
580
+ rotateLeft( 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
581
+
582
+ } else {
583
+
584
+ pan( scope.keyPanSpeed, 0 );
585
+
586
+ }
587
+
588
+ needsUpdate = true;
589
+ break;
590
+ case scope.keys.RIGHT:
591
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
592
+
593
+ rotateLeft( - 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight );
594
+
595
+ } else {
596
+
597
+ pan( - scope.keyPanSpeed, 0 );
598
+
599
+ }
600
+
601
+ needsUpdate = true;
602
+ break;
603
+
604
+ }
605
+
606
+ if ( needsUpdate ) {
607
+
608
+ // prevent the browser from scrolling on cursor keys
609
+ event.preventDefault();
610
+ scope.update();
611
+
612
+ }
613
+
614
+ }
615
+
616
+ function handleTouchStartRotate() {
617
+
618
+ if ( pointers.length === 1 ) {
619
+
620
+ rotateStart.set( pointers[ 0 ].pageX, pointers[ 0 ].pageY );
621
+
622
+ } else {
623
+
624
+ const x = 0.5 * ( pointers[ 0 ].pageX + pointers[ 1 ].pageX );
625
+ const y = 0.5 * ( pointers[ 0 ].pageY + pointers[ 1 ].pageY );
626
+ rotateStart.set( x, y );
627
+
628
+ }
629
+
630
+ }
631
+
632
+ function handleTouchStartPan() {
633
+
634
+ if ( pointers.length === 1 ) {
635
+
636
+ panStart.set( pointers[ 0 ].pageX, pointers[ 0 ].pageY );
637
+
638
+ } else {
639
+
640
+ const x = 0.5 * ( pointers[ 0 ].pageX + pointers[ 1 ].pageX );
641
+ const y = 0.5 * ( pointers[ 0 ].pageY + pointers[ 1 ].pageY );
642
+ panStart.set( x, y );
643
+
644
+ }
645
+
646
+ }
647
+
648
+ function handleTouchStartDolly() {
649
+
650
+ const dx = pointers[ 0 ].pageX - pointers[ 1 ].pageX;
651
+ const dy = pointers[ 0 ].pageY - pointers[ 1 ].pageY;
652
+ const distance = Math.sqrt( dx * dx + dy * dy );
653
+ dollyStart.set( 0, distance );
654
+
655
+ }
656
+
657
+ function handleTouchStartDollyPan() {
658
+
659
+ if ( scope.enableZoom ) handleTouchStartDolly();
660
+ if ( scope.enablePan ) handleTouchStartPan();
661
+
662
+ }
663
+
664
+ function handleTouchStartDollyRotate() {
665
+
666
+ if ( scope.enableZoom ) handleTouchStartDolly();
667
+ if ( scope.enableRotate ) handleTouchStartRotate();
668
+
669
+ }
670
+
671
+ function handleTouchMoveRotate( event ) {
672
+
673
+ if ( pointers.length == 1 ) {
674
+
675
+ rotateEnd.set( event.pageX, event.pageY );
676
+
677
+ } else {
678
+
679
+ const position = getSecondPointerPosition( event );
680
+ const x = 0.5 * ( event.pageX + position.x );
681
+ const y = 0.5 * ( event.pageY + position.y );
682
+ rotateEnd.set( x, y );
683
+
684
+ }
685
+
686
+ rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
687
+ const element = scope.domElement;
688
+ rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
689
+
690
+ rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
691
+ rotateStart.copy( rotateEnd );
692
+
693
+ }
694
+
695
+ function handleTouchMovePan( event ) {
696
+
697
+ if ( pointers.length === 1 ) {
698
+
699
+ panEnd.set( event.pageX, event.pageY );
700
+
701
+ } else {
702
+
703
+ const position = getSecondPointerPosition( event );
704
+ const x = 0.5 * ( event.pageX + position.x );
705
+ const y = 0.5 * ( event.pageY + position.y );
706
+ panEnd.set( x, y );
707
+
708
+ }
709
+
710
+ panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
711
+ pan( panDelta.x, panDelta.y );
712
+ panStart.copy( panEnd );
713
+
714
+ }
715
+
716
+ function handleTouchMoveDolly( event ) {
717
+
718
+ const position = getSecondPointerPosition( event );
719
+ const dx = event.pageX - position.x;
720
+ const dy = event.pageY - position.y;
721
+ const distance = Math.sqrt( dx * dx + dy * dy );
722
+ dollyEnd.set( 0, distance );
723
+ dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
724
+ dollyOut( dollyDelta.y );
725
+ dollyStart.copy( dollyEnd );
726
+
727
+ }
728
+
729
+ function handleTouchMoveDollyPan( event ) {
730
+
731
+ if ( scope.enableZoom ) handleTouchMoveDolly( event );
732
+ if ( scope.enablePan ) handleTouchMovePan( event );
733
+
734
+ }
735
+
736
+ function handleTouchMoveDollyRotate( event ) {
737
+
738
+ if ( scope.enableZoom ) handleTouchMoveDolly( event );
739
+ if ( scope.enableRotate ) handleTouchMoveRotate( event );
740
+
741
+ }
742
+
743
+ //
744
+ // event handlers - FSM: listen for events and reset state
745
+ //
746
+
747
+ function onPointerDown( event ) {
748
+
749
+ if ( scope.enabled === false ) return;
750
+ if ( pointers.length === 0 ) {
751
+
752
+ scope.domElement.setPointerCapture( event.pointerId );
753
+ scope.domElement.addEventListener( 'pointermove', onPointerMove );
754
+ scope.domElement.addEventListener( 'pointerup', onPointerUp );
755
+
756
+ }
757
+
758
+ //
759
+
760
+ addPointer( event );
761
+ if ( event.pointerType === 'touch' ) {
762
+
763
+ onTouchStart( event );
764
+
765
+ } else {
766
+
767
+ onMouseDown( event );
768
+
769
+ }
770
+
771
+ }
772
+
773
+ function onPointerMove( event ) {
774
+
775
+ if ( scope.enabled === false ) return;
776
+ if ( event.pointerType === 'touch' ) {
777
+
778
+ onTouchMove( event );
779
+
780
+ } else {
781
+
782
+ onMouseMove( event );
783
+
784
+ }
785
+
786
+ }
787
+
788
+ function onPointerUp( event ) {
789
+
790
+ removePointer( event );
791
+ if ( pointers.length === 0 ) {
792
+
793
+ scope.domElement.releasePointerCapture( event.pointerId );
794
+ scope.domElement.removeEventListener( 'pointermove', onPointerMove );
795
+ scope.domElement.removeEventListener( 'pointerup', onPointerUp );
796
+
797
+ }
798
+
799
+ scope.dispatchEvent( _endEvent );
800
+ state = STATE.NONE;
801
+
802
+ }
803
+
804
+ function onPointerCancel( event ) {
805
+
806
+ removePointer( event );
807
+
808
+ }
809
+
810
+ function onMouseDown( event ) {
811
+
812
+ let mouseAction;
813
+ switch ( event.button ) {
814
+
815
+ case 0:
816
+ mouseAction = scope.mouseButtons.LEFT;
817
+ break;
818
+ case 1:
819
+ mouseAction = scope.mouseButtons.MIDDLE;
820
+ break;
821
+ case 2:
822
+ mouseAction = scope.mouseButtons.RIGHT;
823
+ break;
824
+ default:
825
+ mouseAction = - 1;
826
+
827
+ }
828
+
829
+ switch ( mouseAction ) {
830
+
831
+ case THREE.MOUSE.DOLLY:
832
+ if ( scope.enableZoom === false ) return;
833
+ handleMouseDownDolly( event );
834
+ state = STATE.DOLLY;
835
+ break;
836
+ case THREE.MOUSE.ROTATE:
837
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
838
+
839
+ if ( scope.enablePan === false ) return;
840
+ handleMouseDownPan( event );
841
+ state = STATE.PAN;
842
+
843
+ } else {
844
+
845
+ if ( scope.enableRotate === false ) return;
846
+ handleMouseDownRotate( event );
847
+ state = STATE.ROTATE;
848
+
849
+ }
850
+
851
+ break;
852
+ case THREE.MOUSE.PAN:
853
+ if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
854
+
855
+ if ( scope.enableRotate === false ) return;
856
+ handleMouseDownRotate( event );
857
+ state = STATE.ROTATE;
858
+
859
+ } else {
860
+
861
+ if ( scope.enablePan === false ) return;
862
+ handleMouseDownPan( event );
863
+ state = STATE.PAN;
864
+
865
+ }
866
+
867
+ break;
868
+ default:
869
+ state = STATE.NONE;
870
+
871
+ }
872
+
873
+ if ( state !== STATE.NONE ) {
874
+
875
+ scope.dispatchEvent( _startEvent );
876
+
877
+ }
878
+
879
+ }
880
+
881
+ function onMouseMove( event ) {
882
+
883
+ switch ( state ) {
884
+
885
+ case STATE.ROTATE:
886
+ if ( scope.enableRotate === false ) return;
887
+ handleMouseMoveRotate( event );
888
+ break;
889
+ case STATE.DOLLY:
890
+ if ( scope.enableZoom === false ) return;
891
+ handleMouseMoveDolly( event );
892
+ break;
893
+ case STATE.PAN:
894
+ if ( scope.enablePan === false ) return;
895
+ handleMouseMovePan( event );
896
+ break;
897
+
898
+ }
899
+
900
+ }
901
+
902
+ function onMouseWheel( event ) {
903
+
904
+ if ( scope.enabled === false || scope.enableZoom === false || state !== STATE.NONE ) return;
905
+ event.preventDefault();
906
+ scope.dispatchEvent( _startEvent );
907
+ handleMouseWheel( event );
908
+ scope.dispatchEvent( _endEvent );
909
+
910
+ }
911
+
912
+ function onKeyDown( event ) {
913
+
914
+ if ( scope.enabled === false || scope.enablePan === false ) return;
915
+ handleKeyDown( event );
916
+
917
+ }
918
+
919
+ function onTouchStart( event ) {
920
+
921
+ trackPointer( event );
922
+ switch ( pointers.length ) {
923
+
924
+ case 1:
925
+ switch ( scope.touches.ONE ) {
926
+
927
+ case THREE.TOUCH.ROTATE:
928
+ if ( scope.enableRotate === false ) return;
929
+ handleTouchStartRotate();
930
+ state = STATE.TOUCH_ROTATE;
931
+ break;
932
+ case THREE.TOUCH.PAN:
933
+ if ( scope.enablePan === false ) return;
934
+ handleTouchStartPan();
935
+ state = STATE.TOUCH_PAN;
936
+ break;
937
+ default:
938
+ state = STATE.NONE;
939
+
940
+ }
941
+
942
+ break;
943
+ case 2:
944
+ switch ( scope.touches.TWO ) {
945
+
946
+ case THREE.TOUCH.DOLLY_PAN:
947
+ if ( scope.enableZoom === false && scope.enablePan === false ) return;
948
+ handleTouchStartDollyPan();
949
+ state = STATE.TOUCH_DOLLY_PAN;
950
+ break;
951
+ case THREE.TOUCH.DOLLY_ROTATE:
952
+ if ( scope.enableZoom === false && scope.enableRotate === false ) return;
953
+ handleTouchStartDollyRotate();
954
+ state = STATE.TOUCH_DOLLY_ROTATE;
955
+ break;
956
+ default:
957
+ state = STATE.NONE;
958
+
959
+ }
960
+
961
+ break;
962
+ default:
963
+ state = STATE.NONE;
964
+
965
+ }
966
+
967
+ if ( state !== STATE.NONE ) {
968
+
969
+ scope.dispatchEvent( _startEvent );
970
+
971
+ }
972
+
973
+ }
974
+
975
+ function onTouchMove( event ) {
976
+
977
+ trackPointer( event );
978
+ switch ( state ) {
979
+
980
+ case STATE.TOUCH_ROTATE:
981
+ if ( scope.enableRotate === false ) return;
982
+ handleTouchMoveRotate( event );
983
+ scope.update();
984
+ break;
985
+ case STATE.TOUCH_PAN:
986
+ if ( scope.enablePan === false ) return;
987
+ handleTouchMovePan( event );
988
+ scope.update();
989
+ break;
990
+ case STATE.TOUCH_DOLLY_PAN:
991
+ if ( scope.enableZoom === false && scope.enablePan === false ) return;
992
+ handleTouchMoveDollyPan( event );
993
+ scope.update();
994
+ break;
995
+ case STATE.TOUCH_DOLLY_ROTATE:
996
+ if ( scope.enableZoom === false && scope.enableRotate === false ) return;
997
+ handleTouchMoveDollyRotate( event );
998
+ scope.update();
999
+ break;
1000
+ default:
1001
+ state = STATE.NONE;
1002
+
1003
+ }
1004
+
1005
+ }
1006
+
1007
+ function onContextMenu( event ) {
1008
+
1009
+ if ( scope.enabled === false ) return;
1010
+ event.preventDefault();
1011
+
1012
+ }
1013
+
1014
+ function addPointer( event ) {
1015
+
1016
+ pointers.push( event );
1017
+
1018
+ }
1019
+
1020
+ function removePointer( event ) {
1021
+
1022
+ delete pointerPositions[ event.pointerId ];
1023
+ for ( let i = 0; i < pointers.length; i ++ ) {
1024
+
1025
+ if ( pointers[ i ].pointerId == event.pointerId ) {
1026
+
1027
+ pointers.splice( i, 1 );
1028
+ return;
1029
+
1030
+ }
1031
+
1032
+ }
1033
+
1034
+ }
1035
+
1036
+ function trackPointer( event ) {
1037
+
1038
+ let position = pointerPositions[ event.pointerId ];
1039
+ if ( position === undefined ) {
1040
+
1041
+ position = new THREE.Vector2();
1042
+ pointerPositions[ event.pointerId ] = position;
1043
+
1044
+ }
1045
+
1046
+ position.set( event.pageX, event.pageY );
1047
+
1048
+ }
1049
+
1050
+ function getSecondPointerPosition( event ) {
1051
+
1052
+ const pointer = event.pointerId === pointers[ 0 ].pointerId ? pointers[ 1 ] : pointers[ 0 ];
1053
+ return pointerPositions[ pointer.pointerId ];
1054
+
1055
+ }
1056
+
1057
+ //
1058
+
1059
+ scope.domElement.addEventListener( 'contextmenu', onContextMenu );
1060
+ scope.domElement.addEventListener( 'pointerdown', onPointerDown );
1061
+ scope.domElement.addEventListener( 'pointercancel', onPointerCancel );
1062
+ scope.domElement.addEventListener( 'wheel', onMouseWheel, {
1063
+ passive: false
1064
+ } );
1065
+
1066
+ // force an update at start
1067
+
1068
+ this.update();
1069
+
1070
+ }
1071
+
1072
+ }
1073
+
1074
+ // This set of controls performs orbiting, dollying (zooming), and panning.
1075
+ // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
1076
+ // This is very similar to OrbitControls, another set of touch behavior
1077
+ //
1078
+ // Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
1079
+ // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
1080
+ // Pan - left mouse, or arrow keys / touch: one-finger move
1081
+
1082
+ class MapControls extends OrbitControls {
1083
+
1084
+ constructor( object, domElement ) {
1085
+
1086
+ super( object, domElement );
1087
+ this.screenSpacePanning = false; // pan orthogonal to world-space direction camera.up
1088
+
1089
+ this.mouseButtons.LEFT = THREE.MOUSE.PAN;
1090
+ this.mouseButtons.RIGHT = THREE.MOUSE.ROTATE;
1091
+ this.touches.ONE = THREE.TOUCH.PAN;
1092
+ this.touches.TWO = THREE.TOUCH.DOLLY_ROTATE;
1093
+
1094
+ }
1095
+
1096
+ }
1097
+
1098
+ THREE.MapControls = MapControls;
1099
+ THREE.OrbitControls = OrbitControls;
1100
+
1101
+ } )();