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