-
Notifications
You must be signed in to change notification settings - Fork 281
/
CameraControl.js
1770 lines (1639 loc) · 58.7 KB
/
CameraControl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {Component} from '../Component.js';
import {CameraFlightAnimation} from './../camera/CameraFlightAnimation.js';
import {PanController} from "./lib/controllers/PanController.js";
import {PivotController} from "./lib/controllers/PivotController.js";
import {PickController} from "./lib/controllers/PickController.js";
import {MousePanRotateDollyHandler} from "./lib/handlers/MousePanRotateDollyHandler.js";
import {KeyboardAxisViewHandler} from "./lib/handlers/KeyboardAxisViewHandler.js";
import {MousePickHandler} from "./lib/handlers/MousePickHandler.js";
import {KeyboardPanRotateDollyHandler} from "./lib/handlers/KeyboardPanRotateDollyHandler.js";
import {CameraUpdater} from "./lib/CameraUpdater.js";
import {MouseMiscHandler} from "./lib/handlers/MouseMiscHandler.js";
import {TouchPanRotateAndDollyHandler} from "./lib/handlers/TouchPanRotateAndDollyHandler.js";
import {utils} from "../utils.js";
import {math} from "../math/math.js";
import {TouchPickHandler} from "./lib/handlers/TouchPickHandler.js";
const DEFAULT_SNAP_PICK_RADIUS = 30;
const DEFAULT_SNAP_VERTEX = true;
const DEFAULT_SNAP_EDGE = true;
/**
* @desc Controls the {@link Camera} with user input, and fires events when the user interacts with pickable {@link Entity}s.
*
* # Contents
*
* * [Overview](#overview)
* * [Examples](#examples)
* * [Orbit Mode](#orbit-mode)
* + [Following the Pointer in Orbit Mode](#--following-the-pointer-in-orbit-mode--)
* + [Showing the Pivot Position](#--showing-the-pivot-position--)
* + [Axis-Aligned Views in Orbit Mode](#--axis-aligned-views-in-orbit-mode--)
* + [View-Fitting Entitys in Orbit Mode](#--view-fitting-entitys-in-orbit-mode--)
* * [First-Person Mode](#first-person-mode)
* + [Following the Pointer in First-Person Mode](#--following-the-pointer-in-first-person-mode--)
* + [Constraining Vertical Position in First-Person Mode](#--constraining-vertical-position-in-first-person-mode--)
* + [Axis-Aligned Views in First-Person Mode](#--axis-aligned-views-in-first-person-mode--)
* + [View-Fitting Entitys in First-Person Mode](#--view-fitting-entitys-in-first-person-mode--)
* * [Plan-View Mode](#plan-view-mode)
* + [Following the Pointer in Plan-View Mode](#--following-the-pointer-in-plan-view-mode--)
* + [Axis-Aligned Views in Plan-View Mode](#--axis-aligned-views-in-plan-view-mode--)
* * [CameraControl Events](#cameracontrol-events)
* + ["hover"](#---hover---)
* + ["hoverOff"](#---hoveroff---)
* + ["hoverEnter"](#---hoverenter---)
* + ["hoverOut"](#---hoverout---)
* + ["picked"](#---picked---)
* + ["pickedSurface"](#---pickedsurface---)
* + ["pickedNothing"](#---pickednothing---)
* + ["doublePicked"](#---doublepicked---)
* + ["doublePickedSurface"](#---doublepickedsurface---)
* + ["doublePickedNothing"](#---doublepickednothing---)
* + ["rightClick"](#---rightclick---)
* * [Custom Keyboard Mappings](#custom-keyboard-mappings)
*
* <br><br>
*
* # Overview
*
* * Each {@link Viewer} has a ````CameraControl````, located at {@link Viewer#cameraControl}.
* * {@link CameraControl#navMode} selects the navigation mode:
* * ````"orbit"```` rotates the {@link Camera} position about the target.
* * ````"firstPerson"```` rotates the World about the Camera position.
* * ````"planView"```` never rotates, but still allows to pan and dolly, typically for an axis-aligned view.
* * {@link CameraControl#followPointer} makes the Camera follow the mouse or touch pointer.
* * {@link CameraControl#constrainVertical} locks the Camera to its current height when in first-person mode.
* * ````CameraControl```` fires pick events when we hover, click or tap on an {@link Entity}.
* <br><br>
*
* # Examples
*
* * [Orbit Navigation - Duplex Model](https://xeokit.github.io/xeokit-sdk/examples/index.html#CameraControl_orbit_Duplex)
* * [Orbit Navigation - Holter Tower Model](https://xeokit.github.io/xeokit-sdk/examples/index.html#CameraControl_orbit_HolterTower)
* * [First-Person Navigation - Duplex Model](https://xeokit.github.io/xeokit-sdk/examples/index.html#CameraControl_firstPerson_Duplex)
* * [First-Person Navigation - Holter Tower Model](https://xeokit.github.io/xeokit-sdk/examples/index.html#CameraControl_firstPerson_HolterTower)
* * [Plan-view Navigation - Schependomlaan Model](https://xeokit.github.io/xeokit-sdk/examples/index.html#CameraControl_planView_Schependomlaan)
* * [Custom Keyboard Mapping](https://xeokit.github.io/xeokit-sdk/examples/index.html#CameraControl_keyMap)
* <br><br>
*
* # Orbit Mode
*
* In orbit mode, ````CameraControl```` orbits the {@link Camera} about the target.
*
* To enable orbit mode:
*
* ````javascript
* const cameraControl = myViewer.cameraControl;
* cameraControl.navMode = "orbit";
* ````
*
* Then orbit by:
*
* * left-dragging the mouse,
* * tap-dragging the touch pad, and
* * pressing arrow keys, or ````Q```` and ````E```` on a QWERTY keyboard, or ````A```` and ````E```` on an AZERTY keyboard.
* <br><br>
*
* Dolly forwards and backwards by:
*
* * spinning the mouse wheel,
* * pinching on the touch pad, and
* * pressing the ````+```` and ````-```` keys, or ````W```` and ````S```` on a QWERTY keyboard, or ````Z```` and ````S```` for AZERTY.
* <br><br>
*
* Pan horizontally and vertically by:
*
* * right-dragging the mouse,
* * left-dragging the mouse with the SHIFT key down,
* * tap-dragging the touch pad with SHIFT down,
* * pressing the ````A````, ````D````, ````Z```` and ````X```` keys on a QWERTY keyboard, and
* * pressing the ````Q````, ````D````, ````W```` and ````X```` keys on an AZERTY keyboard,
* <br><br>
*
* ## Following the Pointer in Orbit Mode
*
* When {@link CameraControl#followPointer} is ````true````in orbiting mode, the mouse or touch pointer will dynamically
* indicate the target that the {@link Camera} will orbit, as well as dolly to and from.
*
* Lets ensure that we're in orbit mode, then enable the {@link Camera} to follow the pointer:
*
* ````javascript
* cameraControl.navMode = "orbit";
* cameraControl.followPointer = true;
* ````
*
* ## Smart Pivoting
*
* TODO
*
* ## Showing the Pivot Position
*
* We can configure {@link CameraControl#pivotElement} with an HTML element to indicate the current
* pivot position. The indicator will appear momentarily each time we move the {@link Camera} while in orbit mode with
* {@link CameraControl#followPointer} set ````true````.
*
* First we'll define some CSS to style our pivot indicator as a black dot with a white border:
*
* ````css
* .camera-pivot-marker {
* color: #ffffff;
* position: absolute;
* width: 25px;
* height: 25px;
* border-radius: 15px;
* border: 2px solid #ebebeb;
* background: black;
* visibility: hidden;
* box-shadow: 5px 5px 15px 1px #000000;
* z-index: 10000;
* pointer-events: none;
* }
* ````
*
* Then we'll attach our pivot indicator's HTML element to the ````CameraControl````:
*
* ````javascript
* const pivotElement = document.createRange().createContextualFragment("<div class='camera-pivot-marker'></div>").firstChild;
*
* document.body.appendChild(pivotElement);
*
* cameraControl.pivotElement = pivotElement;
* ````
*
* ## Axis-Aligned Views in Orbit Mode
*
* In orbit mode, we can use keys 1-6 to position the {@link Camera} to look at the center of the {@link Scene} from along each of the
* six World-space axis. Pressing one of these keys will fly the {@link Camera} to the corresponding axis-aligned view.
*
* ## View-Fitting Entitys in Orbit Mode
*
* When {@link CameraControl#doublePickFlyTo} is ````true````, we can left-double-click or
* double-tap (ie. "double-pick") an {@link Entity} to fit it to view. This will cause the {@link Camera}
* to fly to that Entity. Our target then becomes the center of that Entity. If we are currently pivoting,
* then our pivot position is then also set to the Entity center.
*
* Disable that behaviour by setting {@link CameraControl#doublePickFlyTo} ````false````.
*
* # First-Person Mode
*
* In first-person mode, ````CameraControl```` rotates the World about the {@link Camera} position.
*
* To enable first-person mode:
*
* ````javascript
* cameraControl.navMode = "firstPerson";
* ````
*
* Then rotate by:
*
* * left-dragging the mouse,
* * tap-dragging the touch pad,
* * pressing arrow keys, or ````Q```` and ````E```` on a QWERTY keyboard, or ````A```` and ````E```` on an AZERTY keyboard.
* <br><br>
*
* Dolly forwards and backwards by:
*
* * spinning the mouse wheel,
* * pinching on the touch pad, and
* * pressing the ````+```` and ````-```` keys, or ````W```` and ````S```` on a QWERTY keyboard, or ````Z```` and ````S```` for AZERTY.
* <br><br>
*
* Pan left, right, up and down by:
*
* * left-dragging or right-dragging the mouse, and
* * tap-dragging the touch pad with SHIFT down.
*
* Pan forwards, backwards, left, right, up and down by pressing the ````WSADZX```` keys on a QWERTY keyboard,
* or ````WSQDWX```` keys on an AZERTY keyboard.
* <br><br>
*
* ## Following the Pointer in First-Person Mode
*
* When {@link CameraControl#followPointer} is ````true```` in first-person mode, the mouse or touch pointer will dynamically
* indicate the target to which the {@link Camera} will dolly to and from. In first-person mode, however, the World will always rotate
* about the {@link Camera} position.
*
* Lets ensure that we're in first-person mode, then enable the {@link Camera} to follow the pointer:
*
* ````javascript
* cameraControl.navMode = "firstPerson";
* cameraControl.followPointer = true;
* ````
*
* When the pointer is over empty space, the target will remain the last object that the pointer was over.
*
* ## Constraining Vertical Position in First-Person Mode
*
* In first-person mode, we can lock the {@link Camera} to its current position on the vertical World axis, which is useful for walk-through navigation:
*
* ````javascript
* cameraControl.constrainVertical = true;
* ````
*
* ## Axis-Aligned Views in First-Person Mode
*
* In first-person mode we can use keys 1-6 to position the {@link Camera} to look at the center of
* the {@link Scene} from along each of the six World-space axis. Pressing one of these keys will fly the {@link Camera} to the
* corresponding axis-aligned view.
*
* ## View-Fitting Entitys in First-Person Mode
*
* As in orbit mode, when in first-person mode and {@link CameraControl#doublePickFlyTo} is ````true````, we can double-click
* or double-tap an {@link Entity} (ie. "double-picking") to fit it in view. This will cause the {@link Camera} to fly to
* that Entity. Our target then becomes the center of that Entity.
*
* Disable that behaviour by setting {@link CameraControl#doublePickFlyTo} ````false````.
*
* # Plan-View Mode
*
* In plan-view mode, ````CameraControl```` pans and rotates the {@link Camera}, without rotating it.
*
* To enable plan-view mode:
*
* ````javascript
* cameraControl.navMode = "planView";
* ````
*
* Dolly forwards and backwards by:
*
* * spinning the mouse wheel,
* * pinching on the touch pad, and
* * pressing the ````+```` and ````-```` keys.
*
* <br>
* Pan left, right, up and down by:
*
* * left-dragging or right-dragging the mouse, and
* * tap-dragging the touch pad with SHIFT down.
*
* Pan forwards, backwards, left, right, up and down by pressing the ````WSADZX```` keys on a QWERTY keyboard,
* or ````WSQDWX```` keys on an AZERTY keyboard.
* <br><br>
*
* ## Following the Pointer in Plan-View Mode
*
* When {@link CameraControl#followPointer} is ````true```` in plan-view mode, the mouse or touch pointer will dynamically
* indicate the target to which the {@link Camera} will dolly to and from. In plan-view mode, however, the {@link Camera} cannot rotate.
*
* Lets ensure that we're in plan-view mode, then enable the {@link Camera} to follow the pointer:
*
* ````javascript
* cameraControl.navMode = "planView";
* cameraControl.followPointer = true; // Default
* ````
*
* When the pointer is over empty space, the target will remain the last object that the pointer was over.
*
* ## Axis-Aligned Views in Plan-View Mode
*
* As in orbit and first-person modes, in plan-view mode we can use keys 1-6 to position the {@link Camera} to look at the center of
* the {@link Scene} from along each of the six World-space axis. Pressing one of these keys will fly the {@link Camera} to the
* corresponding axis-aligned view.
*
* # CameraControl Events
*
* ````CameraControl```` fires events as we interact with {@link Entity}s using mouse or touch input.
*
* The following examples demonstrate how to subscribe to those events.
*
* The first example shows how to save a handle to a subscription, which we can later use to unsubscribe.
*
* ## "hover"
*
* Event fired when the pointer moves while hovering over an Entity.
*
* ````javascript
* const onHover = cameraControl.on("hover", (e) => {
* const entity = e.entity; // Entity
* const canvasPos = e.canvasPos; // 2D canvas position
* });
* ````
*
* To unsubscribe from the event:
*
* ````javascript
* cameraControl.off(onHover);
* ````
*
* ## "hoverOff"
*
* Event fired when the pointer moves while hovering over empty space.
*
* ````javascript
* cameraControl.on("hoverOff", (e) => {
* const canvasPos = e.canvasPos;
* });
* ````
*
* ## "hoverEnter"
*
* Event fired when the pointer moves onto an Entity.
*
* ````javascript
* cameraControl.on("hoverEnter", (e) => {
* const entity = e.entity;
* const canvasPos = e.canvasPos;
* });
* ````
*
* ## "hoverOut"
*
* Event fired when the pointer moves off an Entity.
*
* ````javascript
* cameraControl.on("hoverOut", (e) => {
* const entity = e.entity;
* const canvasPos = e.canvasPos;
* });
* ````
*
* ## "picked"
*
* Event fired when we left-click or tap on an Entity.
*
* ````javascript
* cameraControl.on("picked", (e) => {
* const entity = e.entity;
* const canvasPos = e.canvasPos;
* });
* ````
*
* ## "pickedSurface"
*
* Event fired when we left-click or tap on the surface of an Entity.
*
* ````javascript
* cameraControl.on("picked", (e) => {
* const entity = e.entity;
* const canvasPos = e.canvasPos;
* const worldPos = e.worldPos; // 3D World-space position
* const viewPos = e.viewPos; // 3D View-space position
* const worldNormal = e.worldNormal; // 3D World-space normal vector
* });
* ````
*
* ## "pickedNothing"
*
* Event fired when we left-click or tap on empty space.
*
* ````javascript
* cameraControl.on("pickedNothing", (e) => {
* const canvasPos = e.canvasPos;
* });
* ````
*
* ## "doublePicked"
*
* Event fired wwhen we left-double-click or double-tap on an Entity.
*
* ````javascript
* cameraControl.on("doublePicked", (e) => {
* const entity = e.entity;
* const canvasPos = e.canvasPos;
* });
* ````
*
* ## "doublePickedSurface"
*
* Event fired when we left-double-click or double-tap on the surface of an Entity.
*
* ````javascript
* cameraControl.on("doublePickedSurface", (e) => {
* const entity = e.entity;
* const canvasPos = e.canvasPos;
* const worldPos = e.worldPos;
* const viewPos = e.viewPos;
* const worldNormal = e.worldNormal;
* });
* ````
*
* ## "doublePickedNothing"
*
* Event fired when we left-double-click or double-tap on empty space.
*
* ````javascript
* cameraControl.on("doublePickedNothing", (e) => {
* const canvasPos = e.canvasPos;
* });
* ````
*
* ## "rightClick"
*
* Event fired when we right-click on the canvas.
*
* ````javascript
* cameraControl.on("rightClick", (e) => {
* const event = e.event; // Mouse event
* const canvasPos = e.canvasPos;
* });
* ````
*
* ## Custom Keyboard Mappings
*
* We can customize````CameraControl```` key bindings as shown below.
*
* In this example, we'll just set the default bindings for a QWERTY keyboard.
*
* ````javascript
* const input = myViewer.scene.input;
*
* cameraControl.navMode = "orbit";
* cameraControl.followPointer = true;
*
* const keyMap = {};
*
* keyMap[cameraControl.PAN_LEFT] = [input.KEY_A];
* keyMap[cameraControl.PAN_RIGHT] = [input.KEY_D];
* keyMap[cameraControl.PAN_UP] = [input.KEY_Z];
* keyMap[cameraControl.PAN_DOWN] = [input.KEY_X];
* keyMap[cameraControl.DOLLY_FORWARDS] = [input.KEY_W, input.KEY_ADD];
* keyMap[cameraControl.DOLLY_BACKWARDS] = [input.KEY_S, input.KEY_SUBTRACT];
* keyMap[cameraControl.ROTATE_X_POS] = [input.KEY_DOWN_ARROW];
* keyMap[cameraControl.ROTATE_X_NEG] = [input.KEY_UP_ARROW];
* keyMap[cameraControl.ROTATE_Y_POS] = [input.KEY_LEFT_ARROW];
* keyMap[cameraControl.ROTATE_Y_NEG] = [input.KEY_RIGHT_ARROW];
* keyMap[cameraControl.AXIS_VIEW_RIGHT] = [input.KEY_NUM_1];
* keyMap[cameraControl.AXIS_VIEW_BACK] = [input.KEY_NUM_2];
* keyMap[cameraControl.AXIS_VIEW_LEFT] = [input.KEY_NUM_3];
* keyMap[cameraControl.AXIS_VIEW_FRONT] = [input.KEY_NUM_4];
* keyMap[cameraControl.AXIS_VIEW_TOP] = [input.KEY_NUM_5];
* keyMap[cameraControl.AXIS_VIEW_BOTTOM] = [input.KEY_NUM_6];
*
* cameraControl.keyMap = keyMap;
* ````
*
* We can also just configure default bindings for a specified keyboard layout, like this:
*
* ````javascript
* cameraControl.keyMap = "qwerty";
* ````
*
* Then, ````CameraControl```` will internally set {@link CameraControl#keyMap} to the default key map for the QWERTY
* layout (which is the same set of mappings we set in the previous example). In other words, if we subsequently
* read {@link CameraControl#keyMap}, it will now be a key map, instead of the "qwerty" string value we set it to.
*
* Supported layouts are, so far:
*
* * ````"qwerty"````
* * ````"azerty"````
*/
class CameraControl extends Component {
/**
* @private
* @constructor
*/
constructor(owner, cfg = {}) {
super(owner, cfg);
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.PAN_LEFT = 0;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.PAN_RIGHT = 1;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.PAN_UP = 2;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.PAN_DOWN = 3;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.PAN_FORWARDS = 4;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.PAN_BACKWARDS = 5;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.ROTATE_X_POS = 6;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.ROTATE_X_NEG = 7;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.ROTATE_Y_POS = 8;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.ROTATE_Y_NEG = 9;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.DOLLY_FORWARDS = 10;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.DOLLY_BACKWARDS = 11;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.AXIS_VIEW_RIGHT = 12;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.AXIS_VIEW_BACK = 13;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.AXIS_VIEW_LEFT = 14;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.AXIS_VIEW_FRONT = 15;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.AXIS_VIEW_TOP = 16;
/**
* Identifies the XX action.
* @final
* @type {Number}
*/
this.AXIS_VIEW_BOTTOM = 17;
this._keyMap = {}; // Maps key codes to the above actions
this.scene.canvas.canvas.oncontextmenu = (e) => {
e.preventDefault();
};
// User-settable CameraControl configurations
this._configs = {
// Private
longTapTimeout: 600, // Millisecs
longTapRadius: 5, // Pixels
// General
active: true,
keyboardLayout: "qwerty",
navMode: "orbit",
planView: false,
firstPerson: false,
followPointer: true,
doublePickFlyTo: true,
panRightClick: true,
showPivot: false,
pointerEnabled: true,
constrainVertical: false,
smartPivot: false,
doubleClickTimeFrame: 250,
snapToVertex: DEFAULT_SNAP_VERTEX,
snapToEdge: DEFAULT_SNAP_EDGE,
snapRadius: DEFAULT_SNAP_PICK_RADIUS,
keyboardEnabledOnlyIfMouseover: true,
// Rotation
dragRotationRate: 360.0,
keyboardRotationRate: 90.0,
rotationInertia: 0.0,
// Panning
keyboardPanRate: 1.0,
touchPanRate: 1.0,
panInertia: 0.5,
// Dollying
keyboardDollyRate: 10,
mouseWheelDollyRate: 100,
touchDollyRate: 0.2,
dollyInertia: 0,
dollyProximityThreshold: 30.0,
dollyMinSpeed: 0.04
};
// Current runtime state of the CameraControl
this._states = {
pointerCanvasPos: math.vec2(),
mouseover: false,
followPointerDirty: true,
mouseDownClientX: 0,
mouseDownClientY: 0,
mouseDownCursorX: 0,
mouseDownCursorY: 0,
touchStartTime: null,
activeTouches: [],
tapStartPos: math.vec2(),
tapStartTime: -1,
lastTapTime: -1,
longTouchTimeout: null
};
// Updates for CameraUpdater to process on next Scene "tick" event
this._updates = {
rotateDeltaX: 0,
rotateDeltaY: 0,
panDeltaX: 0,
panDeltaY: 0,
panDeltaZ: 0,
dollyDelta: 0
};
// Controllers to assist input event handlers with controlling the Camera
const scene = this.scene;
this._controllers = {
cameraControl: this,
pickController: new PickController(this, this._configs),
pivotController: new PivotController(scene, this._configs),
panController: new PanController(scene),
cameraFlight: new CameraFlightAnimation(this, {
duration: 0.5
})
};
// Input event handlers
this._handlers = [
new MouseMiscHandler(this.scene, this._controllers, this._configs, this._states, this._updates),
new TouchPanRotateAndDollyHandler(this.scene, this._controllers, this._configs, this._states, this._updates),
new MousePanRotateDollyHandler(this.scene, this._controllers, this._configs, this._states, this._updates),
new KeyboardAxisViewHandler(this.scene, this._controllers, this._configs, this._states, this._updates),
new MousePickHandler(this.scene, this._controllers, this._configs, this._states, this._updates),
new TouchPickHandler(this.scene, this._controllers, this._configs, this._states, this._updates),
new KeyboardPanRotateDollyHandler(this.scene, this._controllers, this._configs, this._states, this._updates)
];
// Applies scheduled updates to the Camera on each Scene "tick" event
this._cameraUpdater = new CameraUpdater(this.scene, this._controllers, this._configs, this._states, this._updates);
// Set initial user configurations
this.navMode = cfg.navMode;
if (cfg.planView) {
this.planView = cfg.planView;
}
this.constrainVertical = cfg.constrainVertical;
if (cfg.keyboardLayout) {
this.keyboardLayout = cfg.keyboardLayout; // Deprecated
} else {
this.keyMap = cfg.keyMap;
}
this.doublePickFlyTo = cfg.doublePickFlyTo;
this.panRightClick = cfg.panRightClick;
this.active = cfg.active;
this.followPointer = cfg.followPointer;
this.rotationInertia = cfg.rotationInertia;
this.keyboardPanRate = cfg.keyboardPanRate;
this.touchPanRate = cfg.touchPanRate;
this.keyboardRotationRate = cfg.keyboardRotationRate;
this.dragRotationRate = cfg.dragRotationRate;
this.touchDollyRate = cfg.touchDollyRate;
this.dollyInertia = cfg.dollyInertia;
this.dollyProximityThreshold = cfg.dollyProximityThreshold;
this.dollyMinSpeed = cfg.dollyMinSpeed;
this.panInertia = cfg.panInertia;
this.pointerEnabled = true;
this.keyboardDollyRate = cfg.keyboardDollyRate;
this.mouseWheelDollyRate = cfg.mouseWheelDollyRate;
}
/**
* Sets custom mappings of keys to ````CameraControl```` actions.
*
* See class docs for usage.
*
* @param {{Number:Number}|String} value Either a set of new key mappings, or a string to select a keyboard layout,
* which causes ````CameraControl```` to use the default key mappings for that layout.
*/
set keyMap(value) {
value = value || "qwerty";
if (utils.isString(value)) {
const input = this.scene.input;
const keyMap = {};
switch (value) {
default:
this.error("Unsupported value for 'keyMap': " + value + " defaulting to 'qwerty'");
// Intentional fall-through to "qwerty"
case "qwerty":
keyMap[this.PAN_LEFT] = [input.KEY_A];
keyMap[this.PAN_RIGHT] = [input.KEY_D];
keyMap[this.PAN_UP] = [input.KEY_Z];
keyMap[this.PAN_DOWN] = [input.KEY_X];
keyMap[this.PAN_BACKWARDS] = [];
keyMap[this.PAN_FORWARDS] = [];
keyMap[this.DOLLY_FORWARDS] = [input.KEY_W, input.KEY_ADD];
keyMap[this.DOLLY_BACKWARDS] = [input.KEY_S, input.KEY_SUBTRACT];
keyMap[this.ROTATE_X_POS] = [input.KEY_DOWN_ARROW];
keyMap[this.ROTATE_X_NEG] = [input.KEY_UP_ARROW];
keyMap[this.ROTATE_Y_POS] = [input.KEY_Q, input.KEY_LEFT_ARROW];
keyMap[this.ROTATE_Y_NEG] = [input.KEY_E, input.KEY_RIGHT_ARROW];
keyMap[this.AXIS_VIEW_RIGHT] = [input.KEY_NUM_1];
keyMap[this.AXIS_VIEW_BACK] = [input.KEY_NUM_2];
keyMap[this.AXIS_VIEW_LEFT] = [input.KEY_NUM_3];
keyMap[this.AXIS_VIEW_FRONT] = [input.KEY_NUM_4];
keyMap[this.AXIS_VIEW_TOP] = [input.KEY_NUM_5];
keyMap[this.AXIS_VIEW_BOTTOM] = [input.KEY_NUM_6];
break;
case "azerty":
keyMap[this.PAN_LEFT] = [input.KEY_Q];
keyMap[this.PAN_RIGHT] = [input.KEY_D];
keyMap[this.PAN_UP] = [input.KEY_W];
keyMap[this.PAN_DOWN] = [input.KEY_X];
keyMap[this.PAN_BACKWARDS] = [];
keyMap[this.PAN_FORWARDS] = [];
keyMap[this.DOLLY_FORWARDS] = [input.KEY_Z, input.KEY_ADD];
keyMap[this.DOLLY_BACKWARDS] = [input.KEY_S, input.KEY_SUBTRACT];
keyMap[this.ROTATE_X_POS] = [input.KEY_DOWN_ARROW];
keyMap[this.ROTATE_X_NEG] = [input.KEY_UP_ARROW];
keyMap[this.ROTATE_Y_POS] = [input.KEY_A, input.KEY_LEFT_ARROW];
keyMap[this.ROTATE_Y_NEG] = [input.KEY_E, input.KEY_RIGHT_ARROW];
keyMap[this.AXIS_VIEW_RIGHT] = [input.KEY_NUM_1];
keyMap[this.AXIS_VIEW_BACK] = [input.KEY_NUM_2];
keyMap[this.AXIS_VIEW_LEFT] = [input.KEY_NUM_3];
keyMap[this.AXIS_VIEW_FRONT] = [input.KEY_NUM_4];
keyMap[this.AXIS_VIEW_TOP] = [input.KEY_NUM_5];
keyMap[this.AXIS_VIEW_BOTTOM] = [input.KEY_NUM_6];
break;
}
this._keyMap = keyMap;
} else {
const keyMap = value;
this._keyMap = keyMap;
}
}
/**
* Gets custom mappings of keys to {@link CameraControl} actions.
*
* @returns {{Number:Number}} Current key mappings.
*/
get keyMap() {
return this._keyMap;
}
/**
* Returns true if any keys configured for the given action are down.
* @param action
* @param keyDownMap
* @private
*/
_isKeyDownForAction(action, keyDownMap) {
const keys = this._keyMap[action];
if (!keys) {
return false;
}
if (!keyDownMap) {
keyDownMap = this.scene.input.keyDown;
}
for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
if (keyDownMap[key]) {
return true;
}
}
return false;
}
/**
* Sets the HTMl element to represent the pivot point when {@link CameraControl#followPointer} is true.
*
* See class comments for an example.
*
* @param {HTMLElement} element HTML element representing the pivot point.
*/
set pivotElement(element) {
this._controllers.pivotController.setPivotElement(element);
}
/**
* Sets if this ````CameraControl```` is active or not.
*
* When inactive, the ````CameraControl```` will not react to input.
*
* Default is ````true````.
*
* @param {Boolean} value Set ````true```` to activate this ````CameraControl````.
*/
set active(value) {
value = value !== false;
this._configs.active = value;
this._handlers[1]._active = value;
this._handlers[5]._active = value;
}
/**
* Gets if this ````CameraControl```` is active or not.
*
* When inactive, the ````CameraControl```` will not react to input.
*
* Default is ````true````.
*
* @returns {Boolean} Returns ````true```` if this ````CameraControl```` is active.
*/
get active() {
return this._configs.active;
}
/**
* Sets whether the pointer snap to vertex.
*
* @param {boolean} snapToVertex
*/
set snapToVertex(snapToVertex) {
this._configs.snapToVertex = !!snapToVertex;
}
/**
* Gets whether the pointer snap to vertex.
*
* @returns {boolean}
*/
get snapToVertex() {
return this._configs.snapToVertex;
}
/**
* Sets whether the pointer snap to edge.
*
* @param {boolean} snapToEdge
*/
set snapToEdge(snapToEdge) {
this._configs.snapToEdge = !!snapToEdge;
}
/**
* Gets whether the pointer snap to edge.
*
* @returns {boolean}
*/
get snapToEdge() {
return this._configs.snapToEdge;
}
/**
* Sets the current snap radius for "hoverSnapOrSurface" events, to specify whether the radius
* within which the pointer snaps to the nearest vertex or the nearest edge.
*
* Default value is 30 pixels.
*
* @param {Number} snapRadius The snap radius.
*/
set snapRadius(snapRadius) {
snapRadius = snapRadius || DEFAULT_SNAP_PICK_RADIUS;
this._configs.snapRadius = snapRadius;
}
/**
* Gets the current snap radius.
*
* @returns {Number} The snap radius.
*/
get snapRadius() {
return this._configs.snapRadius;
}
/**
* If `true`, the keyboard shortcuts are enabled ONLY if the mouse is over the canvas.
*
* @param {boolean} value
*/
set keyboardEnabledOnlyIfMouseover(value) {
this._configs.keyboardEnabledOnlyIfMouseover = !!value;
}
/**
* Gets whether the keyboard shortcuts are enabled ONLY if the mouse is over the canvas or ALWAYS.
*
* @returns {boolean}
*/
get keyboardEnabledOnlyIfMouseover() {
return this._configs.keyboardEnabledOnlyIfMouseover;
}
/**
* Sets the current navigation mode.
*
* Accepted values are:
*
* * "orbit" - rotation orbits about the current target or pivot point,
* * "firstPerson" - rotation is about the current eye position,
* * "planView" - rotation is disabled.
*
* See class comments for more info.
*
* @param {String} navMode The navigation mode: "orbit", "firstPerson" or "planView".
*/
set navMode(navMode) {
navMode = navMode || "orbit";
if (navMode !== "firstPerson" && navMode !== "orbit" && navMode !== "planView") {
this.error("Unsupported value for navMode: " + navMode + " - supported values are 'orbit', 'firstPerson' and 'planView' - defaulting to 'orbit'");
navMode = "orbit";