-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
ScreenSpaceCameraController.js
1947 lines (1622 loc) · 81.2 KB
/
ScreenSpaceCameraController.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
/*global define*/
define([
'../Core/Cartesian2',
'../Core/Cartesian3',
'../Core/Cartesian4',
'../Core/Cartographic',
'../Core/defaultValue',
'../Core/defined',
'../Core/destroyObject',
'../Core/DeveloperError',
'../Core/Ellipsoid',
'../Core/IntersectionTests',
'../Core/isArray',
'../Core/KeyboardEventModifier',
'../Core/Math',
'../Core/Matrix3',
'../Core/Matrix4',
'../Core/Plane',
'../Core/Quaternion',
'../Core/Ray',
'../Core/Transforms',
'./CameraEventAggregator',
'./CameraEventType',
'./MapMode2D',
'./SceneMode',
'./SceneTransforms',
'./TweenCollection'
], function(
Cartesian2,
Cartesian3,
Cartesian4,
Cartographic,
defaultValue,
defined,
destroyObject,
DeveloperError,
Ellipsoid,
IntersectionTests,
isArray,
KeyboardEventModifier,
CesiumMath,
Matrix3,
Matrix4,
Plane,
Quaternion,
Ray,
Transforms,
CameraEventAggregator,
CameraEventType,
MapMode2D,
SceneMode,
SceneTransforms,
TweenCollection) {
'use strict';
/**
* Modifies the camera position and orientation based on mouse input to a canvas.
* @alias ScreenSpaceCameraController
* @constructor
*
* @param {Scene} scene The scene.
*/
function ScreenSpaceCameraController(scene) {
//>>includeStart('debug', pragmas.debug);
if (!defined(scene)) {
throw new DeveloperError('scene is required.');
}
//>>includeEnd('debug');
/**
* If true, inputs are allowed conditionally with the flags enableTranslate, enableZoom,
* enableRotate, enableTilt, and enableLook. If false, all inputs are disabled.
*
* NOTE: This setting is for temporary use cases, such as camera flights and
* drag-selection of regions (see Picking demo). It is typically set to false at the
* start of such events, and set true on completion. To keep inputs disabled
* past the end of camera flights, you must use the other booleans (enableTranslate,
* enableZoom, enableRotate, enableTilt, and enableLook).
* @type {Boolean}
* @default true
*/
this.enableInputs = true;
/**
* If true, allows the user to pan around the map. If false, the camera stays locked at the current position.
* This flag only applies in 2D and Columbus view modes.
* @type {Boolean}
* @default true
*/
this.enableTranslate = true;
/**
* If true, allows the user to zoom in and out. If false, the camera is locked to the current distance from the ellipsoid.
* @type {Boolean}
* @default true
*/
this.enableZoom = true;
/**
* If true, allows the user to rotate the camera. If false, the camera is locked to the current heading.
* This flag only applies in 2D and 3D.
* @type {Boolean}
* @default true
*/
this.enableRotate = true;
/**
* If true, allows the user to tilt the camera. If false, the camera is locked to the current heading.
* This flag only applies in 3D and Columbus view.
* @type {Boolean}
* @default true
*/
this.enableTilt = true;
/**
* If true, allows the user to use free-look. If false, the camera view direction can only be changed through translating
* or rotating. This flag only applies in 3D and Columbus view modes.
* @type {Boolean}
* @default true
*/
this.enableLook = true;
/**
* A parameter in the range <code>[0, 1)</code> used to determine how long
* the camera will continue to spin because of inertia.
* With value of zero, the camera will have no inertia.
* @type {Number}
* @default 0.9
*/
this.inertiaSpin = 0.9;
/**
* A parameter in the range <code>[0, 1)</code> used to determine how long
* the camera will continue to translate because of inertia.
* With value of zero, the camera will have no inertia.
* @type {Number}
* @default 0.9
*/
this.inertiaTranslate = 0.9;
/**
* A parameter in the range <code>[0, 1)</code> used to determine how long
* the camera will continue to zoom because of inertia.
* With value of zero, the camera will have no inertia.
* @type {Number}
* @default 0.8
*/
this.inertiaZoom = 0.8;
/**
* A parameter in the range <code>[0, 1)</code> used to limit the range
* of various user inputs to a percentage of the window width/height per animation frame.
* This helps keep the camera under control in low-frame-rate situations.
* @type {Number}
* @default 0.1
*/
this.maximumMovementRatio = 0.1;
/**
* Sets the duration, in seconds, of the bounce back animations in 2D and Columbus view.
* @type {Number}
* @default 3.0
*/
this.bounceAnimationTime = 3.0;
/**
* The minimum magnitude, in meters, of the camera position when zooming. Defaults to 1.0.
* @type {Number}
* @default 1.0
*/
this.minimumZoomDistance = 1.0;
/**
* The maximum magnitude, in meters, of the camera position when zooming. Defaults to positive infinity.
* @type {Number}
* @default {@link Number.POSITIVE_INFINITY}
*/
this.maximumZoomDistance = Number.POSITIVE_INFINITY;
/**
* The input that allows the user to pan around the map. This only applies in 2D and Columbus view modes.
* <p>
* The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
* and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
* or an array of any of the preceding.
* </p>
* @type {CameraEventType|Array|undefined}
* @default {@link CameraEventType.LEFT_DRAG}
*/
this.translateEventTypes = CameraEventType.LEFT_DRAG;
/**
* The input that allows the user to zoom in/out.
* <p>
* The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
* and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
* or an array of any of the preceding.
* </p>
* @type {CameraEventType|Array|undefined}
* @default [{@link CameraEventType.RIGHT_DRAG}, {@link CameraEventType.WHEEL}, {@link CameraEventType.PINCH}]
*/
this.zoomEventTypes = [CameraEventType.RIGHT_DRAG, CameraEventType.WHEEL, CameraEventType.PINCH];
/**
* The input that allows the user to rotate around the globe or another object. This only applies in 3D and Columbus view modes.
* <p>
* The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
* and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
* or an array of any of the preceding.
* </p>
* @type {CameraEventType|Array|undefined}
* @default {@link CameraEventType.LEFT_DRAG}
*/
this.rotateEventTypes = CameraEventType.LEFT_DRAG;
/**
* The input that allows the user to tilt in 3D and Columbus view or twist in 2D.
* <p>
* The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
* and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
* or an array of any of the preceding.
* </p>
* @type {CameraEventType|Array|undefined}
* @default [{@link CameraEventType.MIDDLE_DRAG}, {@link CameraEventType.PINCH}, {
* eventType : {@link CameraEventType.LEFT_DRAG},
* modifier : {@link KeyboardEventModifier.CTRL}
* }, {
* eventType : {@link CameraEventType.RIGHT_DRAG},
* modifier : {@link KeyboardEventModifier.CTRL}
* }]
*/
this.tiltEventTypes = [CameraEventType.MIDDLE_DRAG, CameraEventType.PINCH, {
eventType : CameraEventType.LEFT_DRAG,
modifier : KeyboardEventModifier.CTRL
}, {
eventType : CameraEventType.RIGHT_DRAG,
modifier : KeyboardEventModifier.CTRL
}];
/**
* The input that allows the user to change the direction the camera is viewing. This only applies in 3D and Columbus view modes.
* <p>
* The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
* and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
* or an array of any of the preceding.
* </p>
* @type {CameraEventType|Array|undefined}
* @default { eventType : {@link CameraEventType.LEFT_DRAG}, modifier : {@link KeyboardEventModifier.SHIFT} }
*/
this.lookEventTypes = {
eventType : CameraEventType.LEFT_DRAG,
modifier : KeyboardEventModifier.SHIFT
};
/**
* The minimum height the camera must be before picking the terrain instead of the ellipsoid.
* @type {Number}
* @default 150000.0
*/
this.minimumPickingTerrainHeight = 150000.0;
this._minimumPickingTerrainHeight = this.minimumPickingTerrainHeight;
/**
* The minimum height the camera must be before testing for collision with terrain.
* @type {Number}
* @default 10000.0
*/
this.minimumCollisionTerrainHeight = 15000.0;
this._minimumCollisionTerrainHeight = this.minimumCollisionTerrainHeight;
/**
* The minimum height the camera must be before switching from rotating a track ball to
* free look when clicks originate on the sky on in space.
* @type {Number}
* @default 7500000.0
*/
this.minimumTrackBallHeight = 7500000.0;
this._minimumTrackBallHeight = this.minimumTrackBallHeight;
/**
* Enables or disables camera collision detection with terrain.
* @type {Boolean}
* @default true
*/
this.enableCollisionDetection = true;
this._scene = scene;
this._globe = undefined;
this._ellipsoid = undefined;
this._aggregator = new CameraEventAggregator(scene.canvas);
this._lastInertiaSpinMovement = undefined;
this._lastInertiaZoomMovement = undefined;
this._lastInertiaTranslateMovement = undefined;
this._lastInertiaTiltMovement = undefined;
this._tweens = new TweenCollection();
this._tween = undefined;
this._horizontalRotationAxis = undefined;
this._tiltCenterMousePosition = new Cartesian2(-1.0, -1.0);
this._tiltCenter = new Cartesian3();
this._rotateMousePosition = new Cartesian2(-1.0, -1.0);
this._rotateStartPosition = new Cartesian3();
this._strafeStartPosition = new Cartesian3();
this._zoomMouseStart = new Cartesian2(-1.0, -1.0);
this._zoomWorldPosition = new Cartesian3();
this._useZoomWorldPosition = false;
this._tiltCVOffMap = false;
this._looking = false;
this._rotating = false;
this._strafing = false;
this._zoomingOnVector = false;
this._rotatingZoom = false;
var projection = scene.mapProjection;
this._maxCoord = projection.project(new Cartographic(Math.PI, CesiumMath.PI_OVER_TWO));
// Constants, Make any of these public?
this._zoomFactor = 5.0;
this._rotateFactor = undefined;
this._rotateRateRangeAdjustment = undefined;
this._maximumRotateRate = 1.77;
this._minimumRotateRate = 1.0 / 5000.0;
this._minimumZoomRate = 20.0;
this._maximumZoomRate = 5906376272000.0; // distance from the Sun to Pluto in meters.
}
function decay(time, coefficient) {
if (time < 0) {
return 0.0;
}
var tau = (1.0 - coefficient) * 25.0;
return Math.exp(-tau * time);
}
function sameMousePosition(movement) {
return Cartesian2.equalsEpsilon(movement.startPosition, movement.endPosition, CesiumMath.EPSILON14);
}
// If the time between mouse down and mouse up is not between
// these thresholds, the camera will not move with inertia.
// This value is probably dependent on the browser and/or the
// hardware. Should be investigated further.
var inertiaMaxClickTimeThreshold = 0.4;
function maintainInertia(aggregator, type, modifier, decayCoef, action, object, lastMovementName) {
var movementState = object[lastMovementName];
if (!defined(movementState)) {
movementState = object[lastMovementName] = {
startPosition : new Cartesian2(),
endPosition : new Cartesian2(),
motion : new Cartesian2(),
active : false
};
}
var ts = aggregator.getButtonPressTime(type, modifier);
var tr = aggregator.getButtonReleaseTime(type, modifier);
var threshold = ts && tr && ((tr.getTime() - ts.getTime()) / 1000.0);
var now = new Date();
var fromNow = tr && ((now.getTime() - tr.getTime()) / 1000.0);
if (ts && tr && threshold < inertiaMaxClickTimeThreshold) {
var d = decay(fromNow, decayCoef);
if (!movementState.active) {
var lastMovement = aggregator.getLastMovement(type, modifier);
if (!defined(lastMovement) || sameMousePosition(lastMovement)) {
return;
}
movementState.motion.x = (lastMovement.endPosition.x - lastMovement.startPosition.x) * 0.5;
movementState.motion.y = (lastMovement.endPosition.y - lastMovement.startPosition.y) * 0.5;
movementState.startPosition = Cartesian2.clone(lastMovement.startPosition, movementState.startPosition);
movementState.endPosition = Cartesian2.multiplyByScalar(movementState.motion, d, movementState.endPosition);
movementState.endPosition = Cartesian2.add(movementState.startPosition, movementState.endPosition, movementState.endPosition);
movementState.active = true;
} else {
movementState.startPosition = Cartesian2.clone(movementState.endPosition, movementState.startPosition);
movementState.endPosition = Cartesian2.multiplyByScalar(movementState.motion, d, movementState.endPosition);
movementState.endPosition = Cartesian2.add(movementState.startPosition, movementState.endPosition, movementState.endPosition);
movementState.motion = Cartesian2.clone(Cartesian2.ZERO, movementState.motion);
}
// If value from the decreasing exponential function is close to zero,
// the end coordinates may be NaN.
if (isNaN(movementState.endPosition.x) || isNaN(movementState.endPosition.y) || Cartesian2.distance(movementState.startPosition, movementState.endPosition) < 0.5) {
movementState.active = false;
return;
}
if (!aggregator.isButtonDown(type, modifier)) {
var startPosition = aggregator.getStartMousePosition(type, modifier);
action(object, startPosition, movementState);
}
} else {
movementState.active = false;
}
}
var scratchEventTypeArray = [];
function reactToInput(controller, enabled, eventTypes, action, inertiaConstant, inertiaStateName) {
if (!defined(eventTypes)) {
return;
}
var aggregator = controller._aggregator;
if (!isArray(eventTypes)) {
scratchEventTypeArray[0] = eventTypes;
eventTypes = scratchEventTypeArray;
}
var length = eventTypes.length;
for (var i = 0; i < length; ++i) {
var eventType = eventTypes[i];
var type = defined(eventType.eventType) ? eventType.eventType : eventType;
var modifier = eventType.modifier;
var movement = aggregator.isMoving(type, modifier) && aggregator.getMovement(type, modifier);
var startPosition = aggregator.getStartMousePosition(type, modifier);
if (controller.enableInputs && enabled) {
if (movement) {
action(controller, startPosition, movement);
} else if (inertiaConstant < 1.0) {
maintainInertia(aggregator, type, modifier, inertiaConstant, action, controller, inertiaStateName);
}
}
}
}
var scratchZoomPickRay = new Ray();
var scratchPickCartesian = new Cartesian3();
var scratchZoomOffset = new Cartesian2();
var scratchZoomDirection = new Cartesian3();
var scratchCenterPixel = new Cartesian2();
var scratchCenterPosition = new Cartesian3();
var scratchPositionNormal = new Cartesian3();
var scratchPickNormal = new Cartesian3();
var scratchZoomAxis = new Cartesian3();
var scratchCameraPositionNormal = new Cartesian3();
// Scratch variables used in zooming algorithm
var scratchTargetNormal = new Cartesian3();
var scratchCameraPosition = new Cartesian3();
var scratchCameraUpNormal = new Cartesian3();
var scratchCameraRightNormal = new Cartesian3();
var scratchForwardNormal = new Cartesian3();
var scratchPositionToTarget = new Cartesian3();
var scratchPositionToTargetNormal = new Cartesian3();
var scratchPan = new Cartesian3();
var scratchCenterMovement = new Cartesian3();
var scratchCenter = new Cartesian3();
var scratchCartesian = new Cartesian3();
var scratchCartesianTwo = new Cartesian3();
var scratchCartesianThree = new Cartesian3();
function handleZoom(object, startPosition, movement, zoomFactor, distanceMeasure, unitPositionDotDirection) {
var percentage = 1.0;
if (defined(unitPositionDotDirection)) {
percentage = CesiumMath.clamp(Math.abs(unitPositionDotDirection), 0.25, 1.0);
}
// distanceMeasure should be the height above the ellipsoid.
// The zoomRate slows as it approaches the surface and stops minimumZoomDistance above it.
var minHeight = object.minimumZoomDistance * percentage;
var maxHeight = object.maximumZoomDistance;
var minDistance = distanceMeasure - minHeight;
var zoomRate = zoomFactor * minDistance;
zoomRate = CesiumMath.clamp(zoomRate, object._minimumZoomRate, object._maximumZoomRate);
var diff = movement.endPosition.y - movement.startPosition.y;
var rangeWindowRatio = diff / object._scene.canvas.clientHeight;
rangeWindowRatio = Math.min(rangeWindowRatio, object.maximumMovementRatio);
var distance = zoomRate * rangeWindowRatio;
if (distance > 0.0 && Math.abs(distanceMeasure - minHeight) < 1.0) {
return;
}
if (distance < 0.0 && Math.abs(distanceMeasure - maxHeight) < 1.0) {
return;
}
if (distanceMeasure - distance < minHeight) {
distance = distanceMeasure - minHeight - 1.0;
} else if (distanceMeasure - distance > maxHeight) {
distance = distanceMeasure - maxHeight;
}
var scene = object._scene;
var camera = scene.camera;
var mode = scene.mode;
var sameStartPosition = Cartesian2.equals(startPosition, object._zoomMouseStart);
var zoomingOnVector = object._zoomingOnVector;
var rotatingZoom = object._rotatingZoom;
var pickedPosition;
if (!sameStartPosition) {
object._zoomMouseStart = Cartesian2.clone(startPosition, object._zoomMouseStart);
if (defined(object._globe)) {
pickedPosition = mode !== SceneMode.SCENE2D ? pickGlobe(object, startPosition, scratchPickCartesian) : camera.getPickRay(startPosition, scratchZoomPickRay).origin;
}
if (defined(pickedPosition)) {
object._useZoomWorldPosition = true;
object._zoomWorldPosition = Cartesian3.clone(pickedPosition, object._zoomWorldPosition);
} else {
object._useZoomWorldPosition = false;
}
zoomingOnVector = object._zoomingOnVector = false;
rotatingZoom = object._rotatingZoom = false;
}
if (!object._useZoomWorldPosition) {
camera.zoomIn(distance);
return;
}
var zoomOnVector = mode === SceneMode.COLUMBUS_VIEW;
if (camera.positionCartographic.height < 2000000) {
rotatingZoom = true;
}
if (!sameStartPosition || rotatingZoom) {
if (mode === SceneMode.SCENE2D) {
var worldPosition = object._zoomWorldPosition;
var endPosition = camera.position;
if (!Cartesian3.equals(worldPosition, endPosition) && camera.positionCartographic.height < object._maxCoord.x * 2.0) {
var savedX = camera.position.x;
var direction = Cartesian3.subtract(worldPosition, endPosition, scratchZoomDirection);
Cartesian3.normalize(direction, direction);
var d = Cartesian3.distance(worldPosition, endPosition) * distance / (camera.getMagnitude() * 0.5);
camera.move(direction, d * 0.5);
if ((camera.position.x < 0.0 && savedX > 0.0) || (camera.position.x > 0.0 && savedX < 0.0)) {
pickedPosition = camera.getPickRay(startPosition, scratchZoomPickRay).origin;
object._zoomWorldPosition = Cartesian3.clone(pickedPosition, object._zoomWorldPosition);
}
}
} else if (mode === SceneMode.SCENE3D) {
var cameraPositionNormal = Cartesian3.normalize(camera.position, scratchCameraPositionNormal);
if (camera.positionCartographic.height < 3000.0 && Math.abs(Cartesian3.dot(camera.direction, cameraPositionNormal)) < 0.6) {
zoomOnVector = true;
} else {
var canvas = scene.canvas;
var centerPixel = scratchCenterPixel;
centerPixel.x = canvas.clientWidth / 2;
centerPixel.y = canvas.clientHeight / 2;
var centerPosition = pickGlobe(object, centerPixel, scratchCenterPosition);
// If centerPosition is not defined, it means the globe does not cover the center position of screen
if (defined(centerPosition) && camera.positionCartographic.height < 1000000) {
var cameraPosition = scratchCameraPosition;
Cartesian3.clone(camera.position, cameraPosition);
var target = object._zoomWorldPosition;
var targetNormal = scratchTargetNormal;
targetNormal = Cartesian3.normalize(target, targetNormal);
if (Cartesian3.dot(targetNormal, cameraPositionNormal) < 0.0) {
return;
}
var center = scratchCenter;
var forward = scratchForwardNormal;
Cartesian3.clone(camera.direction, forward);
Cartesian3.add(cameraPosition, Cartesian3.multiplyByScalar(forward, 1000, scratchCartesian), center);
var positionToTarget = scratchPositionToTarget;
var positionToTargetNormal = scratchPositionToTargetNormal;
Cartesian3.subtract(target, cameraPosition, positionToTarget);
Cartesian3.normalize(positionToTarget, positionToTargetNormal);
var alpha = Math.acos( -Cartesian3.dot( cameraPositionNormal, positionToTargetNormal ) );
var cameraDistance = Cartesian3.magnitude( cameraPosition );
var targetDistance = Cartesian3.magnitude( target );
var remainingDistance = cameraDistance - distance;
var positionToTargetDistance = Cartesian3.magnitude(positionToTarget);
var gamma = Math.asin( CesiumMath.clamp( positionToTargetDistance / targetDistance * Math.sin(alpha), -1.0, 1.0 ) );
var delta = Math.asin( CesiumMath.clamp( remainingDistance / targetDistance * Math.sin(alpha), -1.0, 1.0 ) );
var beta = gamma - delta + alpha;
var up = scratchCameraUpNormal;
Cartesian3.normalize(cameraPosition, up);
var right = scratchCameraRightNormal;
right = Cartesian3.cross(positionToTargetNormal, up, right);
right = Cartesian3.normalize(right, right );
Cartesian3.normalize( Cartesian3.cross(up, right, scratchCartesian), forward );
// Calculate new position to move to
Cartesian3.multiplyByScalar(Cartesian3.normalize(center, scratchCartesian), (Cartesian3.magnitude(center) - distance), center);
Cartesian3.normalize(cameraPosition, cameraPosition);
Cartesian3.multiplyByScalar(cameraPosition, remainingDistance, cameraPosition);
// Pan
var pMid = scratchPan;
Cartesian3.multiplyByScalar(Cartesian3.add(
Cartesian3.multiplyByScalar(up, Math.cos(beta) - 1, scratchCartesianTwo),
Cartesian3.multiplyByScalar(forward, Math.sin(beta), scratchCartesianThree),
scratchCartesian
), remainingDistance, pMid);
Cartesian3.add(cameraPosition, pMid, cameraPosition);
Cartesian3.normalize(center, up);
Cartesian3.normalize( Cartesian3.cross(up, right, scratchCartesian), forward );
var cMid = scratchCenterMovement;
Cartesian3.multiplyByScalar(Cartesian3.add(
Cartesian3.multiplyByScalar(up, Math.cos(beta) - 1, scratchCartesianTwo),
Cartesian3.multiplyByScalar(forward, Math.sin(beta), scratchCartesianThree),
scratchCartesian
), Cartesian3.magnitude(center), cMid);
Cartesian3.add(center, cMid, center);
// Update camera
// Set new position
Cartesian3.clone(cameraPosition, camera.position);
// Set new direction
Cartesian3.normalize(Cartesian3.subtract(center, cameraPosition, scratchCartesian), camera.direction);
Cartesian3.clone(camera.direction, camera.direction);
// Set new right & up vectors
Cartesian3.cross(camera.direction, camera.up, camera.right);
Cartesian3.cross(camera.right, camera.direction, camera.up);
return;
} else if (defined(centerPosition)) {
var positionNormal = Cartesian3.normalize(centerPosition, scratchPositionNormal);
var pickedNormal = Cartesian3.normalize(object._zoomWorldPosition, scratchPickNormal);
var dotProduct = Cartesian3.dot(pickedNormal, positionNormal);
if (dotProduct > 0.0 && dotProduct < 1.0) {
var angle = CesiumMath.acosClamped(dotProduct);
var axis = Cartesian3.cross(pickedNormal, positionNormal, scratchZoomAxis);
var denom = Math.abs(angle) > CesiumMath.toRadians(20.0) ? camera.positionCartographic.height * 0.75 : camera.positionCartographic.height - distance;
var scalar = distance / denom;
camera.rotate(axis, angle * scalar);
}
} else {
zoomOnVector = true;
}
}
}
object._rotatingZoom = !zoomOnVector;
}
if ((!sameStartPosition && zoomOnVector) || zoomingOnVector) {
var ray;
var zoomMouseStart = SceneTransforms.wgs84ToWindowCoordinates(scene, object._zoomWorldPosition, scratchZoomOffset);
if (mode !== SceneMode.COLUMBUS_VIEW && Cartesian2.equals(startPosition, object._zoomMouseStart) && defined(zoomMouseStart)) {
ray = camera.getPickRay(zoomMouseStart, scratchZoomPickRay);
} else {
ray = camera.getPickRay(startPosition, scratchZoomPickRay);
}
var rayDirection = ray.direction;
if (mode === SceneMode.COLUMBUS_VIEW) {
Cartesian3.fromElements(rayDirection.y, rayDirection.z, rayDirection.x, rayDirection);
}
camera.move(rayDirection, distance);
object._zoomingOnVector = true;
} else {
camera.zoomIn(distance);
}
}
var translate2DStart = new Ray();
var translate2DEnd = new Ray();
var scratchTranslateP0 = new Cartesian3();
function translate2D(controller, startPosition, movement) {
var scene = controller._scene;
var camera = scene.camera;
var start = camera.getPickRay(movement.startPosition, translate2DStart).origin;
var end = camera.getPickRay(movement.endPosition, translate2DEnd).origin;
var direction = Cartesian3.subtract(start, end, scratchTranslateP0);
var distance = Cartesian3.magnitude(direction);
if (distance > 0.0) {
Cartesian3.normalize(direction, direction);
camera.move(direction, distance);
}
}
function zoom2D(controller, startPosition, movement) {
if (defined(movement.distance)) {
movement = movement.distance;
}
var scene = controller._scene;
var camera = scene.camera;
handleZoom(controller, startPosition, movement, controller._zoomFactor, camera.getMagnitude());
}
var twist2DStart = new Cartesian2();
var twist2DEnd = new Cartesian2();
function twist2D(controller, startPosition, movement) {
if (defined(movement.angleAndHeight)) {
singleAxisTwist2D(controller, startPosition, movement.angleAndHeight);
return;
}
var scene = controller._scene;
var camera = scene.camera;
var canvas = scene.canvas;
var width = canvas.clientWidth;
var height = canvas.clientHeight;
var start = twist2DStart;
start.x = (2.0 / width) * movement.startPosition.x - 1.0;
start.y = (2.0 / height) * (height - movement.startPosition.y) - 1.0;
start = Cartesian2.normalize(start, start);
var end = twist2DEnd;
end.x = (2.0 / width) * movement.endPosition.x - 1.0;
end.y = (2.0 / height) * (height - movement.endPosition.y) - 1.0;
end = Cartesian2.normalize(end, end);
var startTheta = CesiumMath.acosClamped(start.x);
if (start.y < 0) {
startTheta = CesiumMath.TWO_PI - startTheta;
}
var endTheta = CesiumMath.acosClamped(end.x);
if (end.y < 0) {
endTheta = CesiumMath.TWO_PI - endTheta;
}
var theta = endTheta - startTheta;
camera.twistRight(theta);
}
function singleAxisTwist2D(controller, startPosition, movement) {
var rotateRate = controller._rotateFactor * controller._rotateRateRangeAdjustment;
if (rotateRate > controller._maximumRotateRate) {
rotateRate = controller._maximumRotateRate;
}
if (rotateRate < controller._minimumRotateRate) {
rotateRate = controller._minimumRotateRate;
}
var scene = controller._scene;
var camera = scene.camera;
var canvas = scene.canvas;
var phiWindowRatio = (movement.endPosition.x - movement.startPosition.x) / canvas.clientWidth;
phiWindowRatio = Math.min(phiWindowRatio, controller.maximumMovementRatio);
var deltaPhi = rotateRate * phiWindowRatio * Math.PI * 4.0;
camera.twistRight(deltaPhi);
}
function update2D(controller) {
var rotatable2D = controller._scene.mapMode2D === MapMode2D.ROTATE;
if (!Matrix4.equals(Matrix4.IDENTITY, controller._scene.camera.transform)) {
reactToInput(controller, controller.enableZoom, controller.zoomEventTypes, zoom2D, controller.inertiaZoom, '_lastInertiaZoomMovement');
if (rotatable2D) {
reactToInput(controller, controller.enableRotate, controller.translateEventTypes, twist2D, controller.inertiaSpin, '_lastInertiaSpinMovement');
}
} else {
reactToInput(controller, controller.enableTranslate, controller.translateEventTypes, translate2D, controller.inertiaTranslate, '_lastInertiaTranslateMovement');
reactToInput(controller, controller.enableZoom, controller.zoomEventTypes, zoom2D, controller.inertiaZoom, '_lastInertiaZoomMovement');
if (rotatable2D) {
reactToInput(controller, controller.enableRotate, controller.tiltEventTypes, twist2D, controller.inertiaSpin, '_lastInertiaTiltMovement');
}
}
}
var pickGlobeScratchRay = new Ray();
var scratchDepthIntersection = new Cartesian3();
var scratchRayIntersection = new Cartesian3();
function pickGlobe(controller, mousePosition, result) {
var scene = controller._scene;
var globe = controller._globe;
var camera = scene.camera;
if (!defined(globe)) {
return undefined;
}
var depthIntersection;
if (scene.pickPositionSupported) {
depthIntersection = scene.pickPosition(mousePosition, scratchDepthIntersection);
}
var ray = camera.getPickRay(mousePosition, pickGlobeScratchRay);
var rayIntersection = globe.pick(ray, scene, scratchRayIntersection);
var pickDistance = defined(depthIntersection) ? Cartesian3.distance(depthIntersection, camera.positionWC) : Number.POSITIVE_INFINITY;
var rayDistance = defined(rayIntersection) ? Cartesian3.distance(rayIntersection, camera.positionWC) : Number.POSITIVE_INFINITY;
if (pickDistance < rayDistance) {
return Cartesian3.clone(depthIntersection, result);
}
return Cartesian3.clone(rayIntersection, result);
}
var translateCVStartRay = new Ray();
var translateCVEndRay = new Ray();
var translateCVStartPos = new Cartesian3();
var translateCVEndPos = new Cartesian3();
var translatCVDifference = new Cartesian3();
var translateCVOrigin = new Cartesian3();
var translateCVPlane = new Plane(Cartesian3.ZERO, 0.0);
var translateCVStartMouse = new Cartesian2();
var translateCVEndMouse = new Cartesian2();
function translateCV(controller, startPosition, movement) {
if (!Cartesian3.equals(startPosition, controller._translateMousePosition)) {
controller._looking = false;
}
if (!Cartesian3.equals(startPosition, controller._strafeMousePosition)) {
controller._strafing = false;
}
if (controller._looking) {
look3D(controller, startPosition, movement);
return;
}
if (controller._strafing) {
strafe(controller, startPosition, movement);
return;
}
var scene = controller._scene;
var camera = scene.camera;
var startMouse = Cartesian2.clone(movement.startPosition, translateCVStartMouse);
var endMouse = Cartesian2.clone(movement.endPosition, translateCVEndMouse);
var startRay = camera.getPickRay(startMouse, translateCVStartRay);
var origin = Cartesian3.clone(Cartesian3.ZERO, translateCVOrigin);
var normal = Cartesian3.UNIT_X;
var globePos;
if (camera.position.z < controller._minimumPickingTerrainHeight) {
globePos = pickGlobe(controller, startMouse, translateCVStartPos);
if (defined(globePos)) {
origin.x = globePos.x;
}
}
if (origin.x > camera.position.z && defined(globePos)) {
Cartesian3.clone(globePos, controller._strafeStartPosition);
controller._strafing = true;
strafe(controller, startPosition, movement);
controller._strafeMousePosition = Cartesian2.clone(startPosition, controller._strafeMousePosition);
return;
}
var plane = Plane.fromPointNormal(origin, normal, translateCVPlane);
startRay = camera.getPickRay(startMouse, translateCVStartRay);
var startPlanePos = IntersectionTests.rayPlane(startRay, plane, translateCVStartPos);
var endRay = camera.getPickRay(endMouse, translateCVEndRay);
var endPlanePos = IntersectionTests.rayPlane(endRay, plane, translateCVEndPos);
if (!defined(startPlanePos) || !defined(endPlanePos)) {
controller._looking = true;
look3D(controller, startPosition, movement);
Cartesian2.clone(startPosition, controller._translateMousePosition);
return;
}
var diff = Cartesian3.subtract(startPlanePos, endPlanePos, translatCVDifference);
var temp = diff.x;
diff.x = diff.y;
diff.y = diff.z;
diff.z = temp;
var mag = Cartesian3.magnitude(diff);
if (mag > CesiumMath.EPSILON6) {
Cartesian3.normalize(diff, diff);
camera.move(diff, mag);
}
}
var rotateCVWindowPos = new Cartesian2();
var rotateCVWindowRay = new Ray();
var rotateCVCenter = new Cartesian3();
var rotateCVVerticalCenter = new Cartesian3();
var rotateCVTransform = new Matrix4();
var rotateCVVerticalTransform = new Matrix4();
var rotateCVOrigin = new Cartesian3();
var rotateCVPlane = new Plane(Cartesian3.ZERO, 0.0);
var rotateCVCartesian3 = new Cartesian3();
var rotateCVCart = new Cartographic();
var rotateCVOldTransform = new Matrix4();
var rotateCVQuaternion = new Quaternion();
var rotateCVMatrix = new Matrix3();
function rotateCV(controller, startPosition, movement) {
if (defined(movement.angleAndHeight)) {
movement = movement.angleAndHeight;
}
if (!Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
controller._tiltCVOffMap = false;
controller._looking = false;
}
if (controller._looking) {
look3D(controller, startPosition, movement);
return;
}
var scene = controller._scene;
var camera = scene.camera;
var maxCoord = controller._maxCoord;
var onMap = Math.abs(camera.position.x) - maxCoord.x < 0 && Math.abs(camera.position.y) - maxCoord.y < 0;
if (controller._tiltCVOffMap || !onMap || camera.position.z > controller._minimumPickingTerrainHeight) {
controller._tiltCVOffMap = true;
rotateCVOnPlane(controller, startPosition, movement);
} else {
rotateCVOnTerrain(controller, startPosition, movement);
}
}
function rotateCVOnPlane(controller, startPosition, movement) {
var scene = controller._scene;
var camera = scene.camera;
var canvas = scene.canvas;
var windowPosition = rotateCVWindowPos;
windowPosition.x = canvas.clientWidth / 2;
windowPosition.y = canvas.clientHeight / 2;
var ray = camera.getPickRay(windowPosition, rotateCVWindowRay);
var normal = Cartesian3.UNIT_X;
var position = ray.origin;
var direction = ray.direction;
var scalar;
var normalDotDirection = Cartesian3.dot(normal, direction);
if (Math.abs(normalDotDirection) > CesiumMath.EPSILON6) {
scalar = -Cartesian3.dot(normal, position) / normalDotDirection;
}
if (!defined(scalar) || scalar <= 0.0) {
controller._looking = true;
look3D(controller, startPosition, movement);
Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
return;
}
var center = Cartesian3.multiplyByScalar(direction, scalar, rotateCVCenter);
Cartesian3.add(position, center, center);
var projection = scene.mapProjection;
var ellipsoid = projection.ellipsoid;
Cartesian3.fromElements(center.y, center.z, center.x, center);
var cart = projection.unproject(center, rotateCVCart);
ellipsoid.cartographicToCartesian(cart, center);
var transform = Transforms.eastNorthUpToFixedFrame(center, ellipsoid, rotateCVTransform);
var oldGlobe = controller._globe;
var oldEllipsoid = controller._ellipsoid;
controller._globe = undefined;
controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
controller._rotateFactor = 1.0;
controller._rotateRateRangeAdjustment = 1.0;
var oldTransform = Matrix4.clone(camera.transform, rotateCVOldTransform);
camera._setTransform(transform);
rotate3D(controller, startPosition, movement, Cartesian3.UNIT_Z);
camera._setTransform(oldTransform);
controller._globe = oldGlobe;
controller._ellipsoid = oldEllipsoid;
var radius = oldEllipsoid.maximumRadius;
controller._rotateFactor = 1.0 / radius;
controller._rotateRateRangeAdjustment = radius;
}
function rotateCVOnTerrain(controller, startPosition, movement) {
var scene = controller._scene;