-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathCamera.js
3933 lines (3487 loc) · 119 KB
/
Camera.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 BoundingSphere from "../Core/BoundingSphere.js";
import Cartesian2 from "../Core/Cartesian2.js";
import Cartesian3 from "../Core/Cartesian3.js";
import Cartesian4 from "../Core/Cartesian4.js";
import Cartographic from "../Core/Cartographic.js";
import defaultValue from "../Core/defaultValue.js";
import defined from "../Core/defined.js";
import DeveloperError from "../Core/DeveloperError.js";
import EasingFunction from "../Core/EasingFunction.js";
import Ellipsoid from "../Core/Ellipsoid.js";
import EllipsoidGeodesic from "../Core/EllipsoidGeodesic.js";
import Event from "../Core/Event.js";
import getTimestamp from "../Core/getTimestamp.js";
import HeadingPitchRange from "../Core/HeadingPitchRange.js";
import HeadingPitchRoll from "../Core/HeadingPitchRoll.js";
import Intersect from "../Core/Intersect.js";
import IntersectionTests from "../Core/IntersectionTests.js";
import CesiumMath from "../Core/Math.js";
import Matrix3 from "../Core/Matrix3.js";
import Matrix4 from "../Core/Matrix4.js";
import OrthographicFrustum from "../Core/OrthographicFrustum.js";
import OrthographicOffCenterFrustum from "../Core/OrthographicOffCenterFrustum.js";
import PerspectiveFrustum from "../Core/PerspectiveFrustum.js";
import Quaternion from "../Core/Quaternion.js";
import Ray from "../Core/Ray.js";
import Rectangle from "../Core/Rectangle.js";
import Transforms from "../Core/Transforms.js";
import CameraFlightPath from "./CameraFlightPath.js";
import MapMode2D from "./MapMode2D.js";
import SceneMode from "./SceneMode.js";
/**
* The camera is defined by a position, orientation, and view frustum.
* <br /><br />
* The orientation forms an orthonormal basis with a view, up and right = view x up unit vectors.
* <br /><br />
* The viewing frustum is defined by 6 planes.
* Each plane is represented by a {@link Cartesian4} object, where the x, y, and z components
* define the unit vector normal to the plane, and the w component is the distance of the
* plane from the origin/camera position.
*
* @alias Camera
*
* @constructor
*
* @param {Scene} scene The scene.
*
* @demo {@link https://sandcastle.cesium.com/index.html?src=Camera.html|Cesium Sandcastle Camera Demo}
* @demo {@link https://sandcastle.cesium.com/index.html?src=Camera%20Tutorial.html|Cesium Sandcastle Camera Tutorial Example}
* @demo {@link https://cesium.com/learn/cesiumjs-learn/cesiumjs-camera|Camera Tutorial}
*
* @example
* // Create a camera looking down the negative z-axis, positioned at the origin,
* // with a field of view of 60 degrees, and 1:1 aspect ratio.
* const camera = new Cesium.Camera(scene);
* camera.position = new Cesium.Cartesian3();
* camera.direction = Cesium.Cartesian3.negate(Cesium.Cartesian3.UNIT_Z, new Cesium.Cartesian3());
* camera.up = Cesium.Cartesian3.clone(Cesium.Cartesian3.UNIT_Y);
* camera.frustum.fov = Cesium.Math.PI_OVER_THREE;
* camera.frustum.near = 1.0;
* camera.frustum.far = 2.0;
*/
function Camera(scene) {
//>>includeStart('debug', pragmas.debug);
if (!defined(scene)) {
throw new DeveloperError("scene is required.");
}
//>>includeEnd('debug');
this._scene = scene;
this._transform = Matrix4.clone(Matrix4.IDENTITY);
this._invTransform = Matrix4.clone(Matrix4.IDENTITY);
this._actualTransform = Matrix4.clone(Matrix4.IDENTITY);
this._actualInvTransform = Matrix4.clone(Matrix4.IDENTITY);
this._transformChanged = false;
/**
* The position of the camera.
*
* @type {Cartesian3}
*/
this.position = new Cartesian3();
this._position = new Cartesian3();
this._positionWC = new Cartesian3();
this._positionCartographic = new Cartographic();
this._oldPositionWC = undefined;
/**
* The position delta magnitude.
*
* @private
*/
this.positionWCDeltaMagnitude = 0.0;
/**
* The position delta magnitude last frame.
*
* @private
*/
this.positionWCDeltaMagnitudeLastFrame = 0.0;
/**
* How long in seconds since the camera has stopped moving
*
* @private
*/
this.timeSinceMoved = 0.0;
this._lastMovedTimestamp = 0.0;
/**
* The view direction of the camera.
*
* @type {Cartesian3}
*/
this.direction = new Cartesian3();
this._direction = new Cartesian3();
this._directionWC = new Cartesian3();
/**
* The up direction of the camera.
*
* @type {Cartesian3}
*/
this.up = new Cartesian3();
this._up = new Cartesian3();
this._upWC = new Cartesian3();
/**
* The right direction of the camera.
*
* @type {Cartesian3}
*/
this.right = new Cartesian3();
this._right = new Cartesian3();
this._rightWC = new Cartesian3();
/**
* The region of space in view.
*
* @type {PerspectiveFrustum|PerspectiveOffCenterFrustum|OrthographicFrustum}
* @default PerspectiveFrustum()
*
* @see PerspectiveFrustum
* @see PerspectiveOffCenterFrustum
* @see OrthographicFrustum
*/
this.frustum = new PerspectiveFrustum();
this.frustum.aspectRatio =
scene.drawingBufferWidth / scene.drawingBufferHeight;
this.frustum.fov = CesiumMath.toRadians(60.0);
/**
* The default amount to move the camera when an argument is not
* provided to the move methods.
* @type {Number}
* @default 100000.0;
*/
this.defaultMoveAmount = 100000.0;
/**
* The default amount to rotate the camera when an argument is not
* provided to the look methods.
* @type {Number}
* @default Math.PI / 60.0
*/
this.defaultLookAmount = Math.PI / 60.0;
/**
* The default amount to rotate the camera when an argument is not
* provided to the rotate methods.
* @type {Number}
* @default Math.PI / 3600.0
*/
this.defaultRotateAmount = Math.PI / 3600.0;
/**
* The default amount to move the camera when an argument is not
* provided to the zoom methods.
* @type {Number}
* @default 100000.0;
*/
this.defaultZoomAmount = 100000.0;
/**
* If set, the camera will not be able to rotate past this axis in either direction.
* @type {Cartesian3}
* @default undefined
*/
this.constrainedAxis = undefined;
/**
* The factor multiplied by the the map size used to determine where to clamp the camera position
* when zooming out from the surface. The default is 1.5. Only valid for 2D and the map is rotatable.
* @type {Number}
* @default 1.5
*/
this.maximumZoomFactor = 1.5;
this._moveStart = new Event();
this._moveEnd = new Event();
this._changed = new Event();
this._changedPosition = undefined;
this._changedDirection = undefined;
this._changedFrustum = undefined;
this._changedHeading = undefined;
/**
* The amount the camera has to change before the <code>changed</code> event is raised. The value is a percentage in the [0, 1] range.
* @type {number}
* @default 0.5
*/
this.percentageChanged = 0.5;
this._viewMatrix = new Matrix4();
this._invViewMatrix = new Matrix4();
updateViewMatrix(this);
this._mode = SceneMode.SCENE3D;
this._modeChanged = true;
const projection = scene.mapProjection;
this._projection = projection;
this._maxCoord = projection.project(
new Cartographic(Math.PI, CesiumMath.PI_OVER_TWO)
);
this._max2Dfrustum = undefined;
// set default view
rectangleCameraPosition3D(
this,
Camera.DEFAULT_VIEW_RECTANGLE,
this.position,
true
);
let mag = Cartesian3.magnitude(this.position);
mag += mag * Camera.DEFAULT_VIEW_FACTOR;
Cartesian3.normalize(this.position, this.position);
Cartesian3.multiplyByScalar(this.position, mag, this.position);
}
/**
* @private
*/
Camera.TRANSFORM_2D = new Matrix4(
0.0,
0.0,
1.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
0.0,
1.0
);
/**
* @private
*/
Camera.TRANSFORM_2D_INVERSE = Matrix4.inverseTransformation(
Camera.TRANSFORM_2D,
new Matrix4()
);
/**
* The default rectangle the camera will view on creation.
* @type Rectangle
*/
Camera.DEFAULT_VIEW_RECTANGLE = Rectangle.fromDegrees(
-95.0,
-20.0,
-70.0,
90.0
);
/**
* A scalar to multiply to the camera position and add it back after setting the camera to view the rectangle.
* A value of zero means the camera will view the entire {@link Camera#DEFAULT_VIEW_RECTANGLE}, a value greater than zero
* will move it further away from the extent, and a value less than zero will move it close to the extent.
* @type Number
*/
Camera.DEFAULT_VIEW_FACTOR = 0.5;
/**
* The default heading/pitch/range that is used when the camera flies to a location that contains a bounding sphere.
* @type HeadingPitchRange
*/
Camera.DEFAULT_OFFSET = new HeadingPitchRange(
0.0,
-CesiumMath.PI_OVER_FOUR,
0.0
);
function updateViewMatrix(camera) {
Matrix4.computeView(
camera._position,
camera._direction,
camera._up,
camera._right,
camera._viewMatrix
);
Matrix4.multiply(
camera._viewMatrix,
camera._actualInvTransform,
camera._viewMatrix
);
Matrix4.inverseTransformation(camera._viewMatrix, camera._invViewMatrix);
}
function updateCameraDeltas(camera) {
if (!defined(camera._oldPositionWC)) {
camera._oldPositionWC = Cartesian3.clone(
camera.positionWC,
camera._oldPositionWC
);
} else {
camera.positionWCDeltaMagnitudeLastFrame = camera.positionWCDeltaMagnitude;
const delta = Cartesian3.subtract(
camera.positionWC,
camera._oldPositionWC,
camera._oldPositionWC
);
camera.positionWCDeltaMagnitude = Cartesian3.magnitude(delta);
camera._oldPositionWC = Cartesian3.clone(
camera.positionWC,
camera._oldPositionWC
);
// Update move timers
if (camera.positionWCDeltaMagnitude > 0.0) {
camera.timeSinceMoved = 0.0;
camera._lastMovedTimestamp = getTimestamp();
} else {
camera.timeSinceMoved =
Math.max(getTimestamp() - camera._lastMovedTimestamp, 0.0) / 1000.0;
}
}
}
/**
* Checks if there's a camera flight with preload for this camera.
*
* @returns {Boolean} Whether or not this camera has a current flight with a valid preloadFlightCamera in scene.
*
* @private
*
*/
Camera.prototype.canPreloadFlight = function () {
return defined(this._currentFlight) && this._mode !== SceneMode.SCENE2D;
};
Camera.prototype._updateCameraChanged = function () {
const camera = this;
updateCameraDeltas(camera);
if (camera._changed.numberOfListeners === 0) {
return;
}
const percentageChanged = camera.percentageChanged;
const currentHeading = camera.heading;
if (!defined(camera._changedHeading)) {
camera._changedHeading = currentHeading;
}
let delta =
Math.abs(camera._changedHeading - currentHeading) % CesiumMath.TWO_PI;
delta = delta > CesiumMath.PI ? CesiumMath.TWO_PI - delta : delta;
// Since delta is computed as the shortest distance between two angles
// the percentage is relative to the half circle.
const headingChangedPercentage = delta / Math.PI;
if (headingChangedPercentage > percentageChanged) {
camera._changed.raiseEvent(headingChangedPercentage);
camera._changedHeading = currentHeading;
}
if (camera._mode === SceneMode.SCENE2D) {
if (!defined(camera._changedFrustum)) {
camera._changedPosition = Cartesian3.clone(
camera.position,
camera._changedPosition
);
camera._changedFrustum = camera.frustum.clone();
return;
}
const position = camera.position;
const lastPosition = camera._changedPosition;
const frustum = camera.frustum;
const lastFrustum = camera._changedFrustum;
const x0 = position.x + frustum.left;
const x1 = position.x + frustum.right;
const x2 = lastPosition.x + lastFrustum.left;
const x3 = lastPosition.x + lastFrustum.right;
const y0 = position.y + frustum.bottom;
const y1 = position.y + frustum.top;
const y2 = lastPosition.y + lastFrustum.bottom;
const y3 = lastPosition.y + lastFrustum.top;
const leftX = Math.max(x0, x2);
const rightX = Math.min(x1, x3);
const bottomY = Math.max(y0, y2);
const topY = Math.min(y1, y3);
let areaPercentage;
if (leftX >= rightX || bottomY >= y1) {
areaPercentage = 1.0;
} else {
let areaRef = lastFrustum;
if (x0 < x2 && x1 > x3 && y0 < y2 && y1 > y3) {
areaRef = frustum;
}
areaPercentage =
1.0 -
((rightX - leftX) * (topY - bottomY)) /
((areaRef.right - areaRef.left) * (areaRef.top - areaRef.bottom));
}
if (areaPercentage > percentageChanged) {
camera._changed.raiseEvent(areaPercentage);
camera._changedPosition = Cartesian3.clone(
camera.position,
camera._changedPosition
);
camera._changedFrustum = camera.frustum.clone(camera._changedFrustum);
}
return;
}
if (!defined(camera._changedDirection)) {
camera._changedPosition = Cartesian3.clone(
camera.positionWC,
camera._changedPosition
);
camera._changedDirection = Cartesian3.clone(
camera.directionWC,
camera._changedDirection
);
return;
}
const dirAngle = CesiumMath.acosClamped(
Cartesian3.dot(camera.directionWC, camera._changedDirection)
);
let dirPercentage;
if (defined(camera.frustum.fovy)) {
dirPercentage = dirAngle / (camera.frustum.fovy * 0.5);
} else {
dirPercentage = dirAngle;
}
const distance = Cartesian3.distance(
camera.positionWC,
camera._changedPosition
);
const heightPercentage = distance / camera.positionCartographic.height;
if (
dirPercentage > percentageChanged ||
heightPercentage > percentageChanged
) {
camera._changed.raiseEvent(Math.max(dirPercentage, heightPercentage));
camera._changedPosition = Cartesian3.clone(
camera.positionWC,
camera._changedPosition
);
camera._changedDirection = Cartesian3.clone(
camera.directionWC,
camera._changedDirection
);
}
};
function convertTransformForColumbusView(camera) {
Transforms.basisTo2D(
camera._projection,
camera._transform,
camera._actualTransform
);
}
const scratchCartographic = new Cartographic();
const scratchCartesian3Projection = new Cartesian3();
const scratchCartesian3 = new Cartesian3();
const scratchCartesian4Origin = new Cartesian4();
const scratchCartesian4NewOrigin = new Cartesian4();
const scratchCartesian4NewXAxis = new Cartesian4();
const scratchCartesian4NewYAxis = new Cartesian4();
const scratchCartesian4NewZAxis = new Cartesian4();
function convertTransformFor2D(camera) {
const projection = camera._projection;
const ellipsoid = projection.ellipsoid;
const origin = Matrix4.getColumn(
camera._transform,
3,
scratchCartesian4Origin
);
const cartographic = ellipsoid.cartesianToCartographic(
origin,
scratchCartographic
);
const projectedPosition = projection.project(
cartographic,
scratchCartesian3Projection
);
const newOrigin = scratchCartesian4NewOrigin;
newOrigin.x = projectedPosition.z;
newOrigin.y = projectedPosition.x;
newOrigin.z = projectedPosition.y;
newOrigin.w = 1.0;
const newZAxis = Cartesian4.clone(
Cartesian4.UNIT_X,
scratchCartesian4NewZAxis
);
const xAxis = Cartesian4.add(
Matrix4.getColumn(camera._transform, 0, scratchCartesian3),
origin,
scratchCartesian3
);
ellipsoid.cartesianToCartographic(xAxis, cartographic);
projection.project(cartographic, projectedPosition);
const newXAxis = scratchCartesian4NewXAxis;
newXAxis.x = projectedPosition.z;
newXAxis.y = projectedPosition.x;
newXAxis.z = projectedPosition.y;
newXAxis.w = 0.0;
Cartesian3.subtract(newXAxis, newOrigin, newXAxis);
newXAxis.x = 0.0;
const newYAxis = scratchCartesian4NewYAxis;
if (Cartesian3.magnitudeSquared(newXAxis) > CesiumMath.EPSILON10) {
Cartesian3.cross(newZAxis, newXAxis, newYAxis);
} else {
const yAxis = Cartesian4.add(
Matrix4.getColumn(camera._transform, 1, scratchCartesian3),
origin,
scratchCartesian3
);
ellipsoid.cartesianToCartographic(yAxis, cartographic);
projection.project(cartographic, projectedPosition);
newYAxis.x = projectedPosition.z;
newYAxis.y = projectedPosition.x;
newYAxis.z = projectedPosition.y;
newYAxis.w = 0.0;
Cartesian3.subtract(newYAxis, newOrigin, newYAxis);
newYAxis.x = 0.0;
if (Cartesian3.magnitudeSquared(newYAxis) < CesiumMath.EPSILON10) {
Cartesian4.clone(Cartesian4.UNIT_Y, newXAxis);
Cartesian4.clone(Cartesian4.UNIT_Z, newYAxis);
}
}
Cartesian3.cross(newYAxis, newZAxis, newXAxis);
Cartesian3.normalize(newXAxis, newXAxis);
Cartesian3.cross(newZAxis, newXAxis, newYAxis);
Cartesian3.normalize(newYAxis, newYAxis);
Matrix4.setColumn(
camera._actualTransform,
0,
newXAxis,
camera._actualTransform
);
Matrix4.setColumn(
camera._actualTransform,
1,
newYAxis,
camera._actualTransform
);
Matrix4.setColumn(
camera._actualTransform,
2,
newZAxis,
camera._actualTransform
);
Matrix4.setColumn(
camera._actualTransform,
3,
newOrigin,
camera._actualTransform
);
}
const scratchCartesian = new Cartesian3();
function updateMembers(camera) {
const mode = camera._mode;
let heightChanged = false;
let height = 0.0;
if (mode === SceneMode.SCENE2D) {
height = camera.frustum.right - camera.frustum.left;
heightChanged = height !== camera._positionCartographic.height;
}
let position = camera._position;
const positionChanged =
!Cartesian3.equals(position, camera.position) || heightChanged;
if (positionChanged) {
position = Cartesian3.clone(camera.position, camera._position);
}
let direction = camera._direction;
const directionChanged = !Cartesian3.equals(direction, camera.direction);
if (directionChanged) {
Cartesian3.normalize(camera.direction, camera.direction);
direction = Cartesian3.clone(camera.direction, camera._direction);
}
let up = camera._up;
const upChanged = !Cartesian3.equals(up, camera.up);
if (upChanged) {
Cartesian3.normalize(camera.up, camera.up);
up = Cartesian3.clone(camera.up, camera._up);
}
let right = camera._right;
const rightChanged = !Cartesian3.equals(right, camera.right);
if (rightChanged) {
Cartesian3.normalize(camera.right, camera.right);
right = Cartesian3.clone(camera.right, camera._right);
}
const transformChanged = camera._transformChanged || camera._modeChanged;
camera._transformChanged = false;
if (transformChanged) {
Matrix4.inverseTransformation(camera._transform, camera._invTransform);
if (
camera._mode === SceneMode.COLUMBUS_VIEW ||
camera._mode === SceneMode.SCENE2D
) {
if (Matrix4.equals(Matrix4.IDENTITY, camera._transform)) {
Matrix4.clone(Camera.TRANSFORM_2D, camera._actualTransform);
} else if (camera._mode === SceneMode.COLUMBUS_VIEW) {
convertTransformForColumbusView(camera);
} else {
convertTransformFor2D(camera);
}
} else {
Matrix4.clone(camera._transform, camera._actualTransform);
}
Matrix4.inverseTransformation(
camera._actualTransform,
camera._actualInvTransform
);
camera._modeChanged = false;
}
const transform = camera._actualTransform;
if (positionChanged || transformChanged) {
camera._positionWC = Matrix4.multiplyByPoint(
transform,
position,
camera._positionWC
);
// Compute the Cartographic position of the camera.
if (mode === SceneMode.SCENE3D || mode === SceneMode.MORPHING) {
camera._positionCartographic = camera._projection.ellipsoid.cartesianToCartographic(
camera._positionWC,
camera._positionCartographic
);
} else {
// The camera position is expressed in the 2D coordinate system where the Y axis is to the East,
// the Z axis is to the North, and the X axis is out of the map. Express them instead in the ENU axes where
// X is to the East, Y is to the North, and Z is out of the local horizontal plane.
const positionENU = scratchCartesian;
positionENU.x = camera._positionWC.y;
positionENU.y = camera._positionWC.z;
positionENU.z = camera._positionWC.x;
// In 2D, the camera height is always 12.7 million meters.
// The apparent height is equal to half the frustum width.
if (mode === SceneMode.SCENE2D) {
positionENU.z = height;
}
camera._projection.unproject(positionENU, camera._positionCartographic);
}
}
if (directionChanged || upChanged || rightChanged) {
const det = Cartesian3.dot(
direction,
Cartesian3.cross(up, right, scratchCartesian)
);
if (Math.abs(1.0 - det) > CesiumMath.EPSILON2) {
//orthonormalize axes
const invUpMag = 1.0 / Cartesian3.magnitudeSquared(up);
const scalar = Cartesian3.dot(up, direction) * invUpMag;
const w0 = Cartesian3.multiplyByScalar(
direction,
scalar,
scratchCartesian
);
up = Cartesian3.normalize(
Cartesian3.subtract(up, w0, camera._up),
camera._up
);
Cartesian3.clone(up, camera.up);
right = Cartesian3.cross(direction, up, camera._right);
Cartesian3.clone(right, camera.right);
}
}
if (directionChanged || transformChanged) {
camera._directionWC = Matrix4.multiplyByPointAsVector(
transform,
direction,
camera._directionWC
);
Cartesian3.normalize(camera._directionWC, camera._directionWC);
}
if (upChanged || transformChanged) {
camera._upWC = Matrix4.multiplyByPointAsVector(transform, up, camera._upWC);
Cartesian3.normalize(camera._upWC, camera._upWC);
}
if (rightChanged || transformChanged) {
camera._rightWC = Matrix4.multiplyByPointAsVector(
transform,
right,
camera._rightWC
);
Cartesian3.normalize(camera._rightWC, camera._rightWC);
}
if (
positionChanged ||
directionChanged ||
upChanged ||
rightChanged ||
transformChanged
) {
updateViewMatrix(camera);
}
}
function getHeading(direction, up) {
let heading;
if (
!CesiumMath.equalsEpsilon(Math.abs(direction.z), 1.0, CesiumMath.EPSILON3)
) {
heading = Math.atan2(direction.y, direction.x) - CesiumMath.PI_OVER_TWO;
} else {
heading = Math.atan2(up.y, up.x) - CesiumMath.PI_OVER_TWO;
}
return CesiumMath.TWO_PI - CesiumMath.zeroToTwoPi(heading);
}
function getPitch(direction) {
return CesiumMath.PI_OVER_TWO - CesiumMath.acosClamped(direction.z);
}
function getRoll(direction, up, right) {
let roll = 0.0;
if (
!CesiumMath.equalsEpsilon(Math.abs(direction.z), 1.0, CesiumMath.EPSILON3)
) {
roll = Math.atan2(-right.z, up.z);
roll = CesiumMath.zeroToTwoPi(roll + CesiumMath.TWO_PI);
}
return roll;
}
const scratchHPRMatrix1 = new Matrix4();
const scratchHPRMatrix2 = new Matrix4();
Object.defineProperties(Camera.prototype, {
/**
* Gets the camera's reference frame. The inverse of this transformation is appended to the view matrix.
* @memberof Camera.prototype
*
* @type {Matrix4}
* @readonly
*
* @default {@link Matrix4.IDENTITY}
*/
transform: {
get: function () {
return this._transform;
},
},
/**
* Gets the inverse camera transform.
* @memberof Camera.prototype
*
* @type {Matrix4}
* @readonly
*
* @default {@link Matrix4.IDENTITY}
*/
inverseTransform: {
get: function () {
updateMembers(this);
return this._invTransform;
},
},
/**
* Gets the view matrix.
* @memberof Camera.prototype
*
* @type {Matrix4}
* @readonly
*
* @see Camera#inverseViewMatrix
*/
viewMatrix: {
get: function () {
updateMembers(this);
return this._viewMatrix;
},
},
/**
* Gets the inverse view matrix.
* @memberof Camera.prototype
*
* @type {Matrix4}
* @readonly
*
* @see Camera#viewMatrix
*/
inverseViewMatrix: {
get: function () {
updateMembers(this);
return this._invViewMatrix;
},
},
/**
* Gets the {@link Cartographic} position of the camera, with longitude and latitude
* expressed in radians and height in meters. In 2D and Columbus View, it is possible
* for the returned longitude and latitude to be outside the range of valid longitudes
* and latitudes when the camera is outside the map.
* @memberof Camera.prototype
*
* @type {Cartographic}
* @readonly
*/
positionCartographic: {
get: function () {
updateMembers(this);
return this._positionCartographic;
},
},
/**
* Gets the position of the camera in world coordinates.
* @memberof Camera.prototype
*
* @type {Cartesian3}
* @readonly
*/
positionWC: {
get: function () {
updateMembers(this);
return this._positionWC;
},
},
/**
* Gets the view direction of the camera in world coordinates.
* @memberof Camera.prototype
*
* @type {Cartesian3}
* @readonly
*/
directionWC: {
get: function () {
updateMembers(this);
return this._directionWC;
},
},
/**
* Gets the up direction of the camera in world coordinates.
* @memberof Camera.prototype
*
* @type {Cartesian3}
* @readonly
*/
upWC: {
get: function () {
updateMembers(this);
return this._upWC;
},
},
/**
* Gets the right direction of the camera in world coordinates.
* @memberof Camera.prototype
*
* @type {Cartesian3}
* @readonly
*/
rightWC: {
get: function () {
updateMembers(this);
return this._rightWC;
},
},
/**
* Gets the camera heading in radians.
* @memberof Camera.prototype
*
* @type {Number}
* @readonly
*/
heading: {
get: function () {
if (this._mode !== SceneMode.MORPHING) {
const ellipsoid = this._projection.ellipsoid;
const oldTransform = Matrix4.clone(this._transform, scratchHPRMatrix1);
const transform = Transforms.eastNorthUpToFixedFrame(
this.positionWC,
ellipsoid,
scratchHPRMatrix2
);
this._setTransform(transform);
const heading = getHeading(this.direction, this.up);
this._setTransform(oldTransform);
return heading;
}
return undefined;
},
},
/**
* Gets the camera pitch in radians.
* @memberof Camera.prototype
*
* @type {Number}
* @readonly
*/
pitch: {
get: function () {
if (this._mode !== SceneMode.MORPHING) {
const ellipsoid = this._projection.ellipsoid;
const oldTransform = Matrix4.clone(this._transform, scratchHPRMatrix1);
const transform = Transforms.eastNorthUpToFixedFrame(
this.positionWC,
ellipsoid,
scratchHPRMatrix2
);
this._setTransform(transform);
const pitch = getPitch(this.direction);
this._setTransform(oldTransform);
return pitch;
}
return undefined;
},
},
/**
* Gets the camera roll in radians.
* @memberof Camera.prototype