Skip to content

Commit eac55ef

Browse files
committed
Core Cesium function for animated rotation around axis
May be used by application developers to implement: - 2D/3D transition (tilt); - orientation transition (heading).
1 parent 89d1abc commit eac55ef

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

externs/olcsx.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,15 @@ olcsx.core;
2525
* @api
2626
*/
2727
olcsx.core.OlFeatureToCesiumContext;
28+
29+
30+
/**
31+
* Options for rotate around axis core function.
32+
* @typedef {{
33+
* duration: (number|undefined),
34+
* easing: (function(number):number|undefined),
35+
* callback: (function()|undefined)
36+
* }}
37+
* @api
38+
*/
39+
olcsx.core.RotateAroundAxisOption;

src/core.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,79 @@ goog.require('olcs.core.OlLayerPrimitive');
4343
};
4444

4545

46+
/**
47+
* @param {!Cesium.Camera} camera
48+
* @param {number} angle
49+
* @param {!Cesium.Cartesian3} axis
50+
* @param {!Cesium.Matrix4} transform
51+
* @param {olcsx.core.RotateAroundAxisOption=} opt_options
52+
* @api
53+
*/
54+
olcs.core.rotateAroundAxis = function(camera, angle, axis, transform,
55+
opt_options) {
56+
var clamp = Cesium.Math.clamp;
57+
var defaultValue = Cesium.defaultValue;
58+
59+
var options = opt_options || {};
60+
var duration = defaultValue(options.duration, 500); // ms
61+
var easing = defaultValue(options.easing, ol.easing.linear);
62+
var callback = options.callback;
63+
64+
var start = goog.now();
65+
var lastProgress = 0;
66+
var oldTransform = new Cesium.Matrix4();
67+
68+
var animation = new goog.async.AnimationDelay(function(millis) {
69+
var progress = easing(clamp((millis - start) / duration, 0, 1));
70+
goog.asserts.assert(progress > lastProgress);
71+
72+
camera.transform.clone(oldTransform);
73+
var stepAngle = (progress - lastProgress) * angle;
74+
lastProgress = progress;
75+
camera.setTransform(transform);
76+
camera.rotate(axis, stepAngle);
77+
camera.setTransform(oldTransform);
78+
79+
if (progress < 1) {
80+
animation.start();
81+
} else if (callback) {
82+
callback();
83+
}
84+
});
85+
animation.start();
86+
};
87+
88+
89+
/**
90+
* @param {!Cesium.Scene} scene
91+
* @param {number} heading
92+
* @param {!Cesium.Cartesian3} bottomCenter
93+
* @api
94+
*/
95+
olcs.core.setHeadingUsingBottomCenter = function(scene, heading,
96+
bottomCenter) {
97+
var camera = scene.camera;
98+
// Compute the camera position to zenith quaternion
99+
var angleToZenith = olcs.core.computeAngleToZenith(scene, bottomCenter);
100+
var axis = camera.right;
101+
var quaternion = Cesium.Quaternion.fromAxisAngle(axis, angleToZenith);
102+
var rotation = Cesium.Matrix3.fromQuaternion(quaternion);
103+
104+
// Get the zenith point from the rotation of the position vector
105+
var vector = new Cesium.Cartesian3();
106+
Cesium.Cartesian3.subtract(camera.position, bottomCenter, vector);
107+
var zenith = new Cesium.Cartesian3();
108+
Cesium.Matrix3.multiplyByVector(rotation, vector, zenith);
109+
Cesium.Cartesian3.add(zenith, bottomCenter, zenith);
110+
111+
// Actually rotate around the zenith normal
112+
var transform = Cesium.Matrix4.fromTranslation(zenith);
113+
var rotateAroundAxis = olcs.core.rotateAroundAxis;
114+
rotateAroundAxis(camera, heading, zenith, transform);
115+
};
116+
117+
118+
46119
/**
47120
* Get 3D positiion of the point at the bottom-center of the screen.
48121
* @param {!Cesium.Scene} scene

0 commit comments

Comments
 (0)