-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathCameraFlightPath.js
365 lines (293 loc) · 12.6 KB
/
CameraFlightPath.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
/*global define*/
define([
'../Core/Cartesian2',
'../Core/Cartesian3',
'../Core/Cartographic',
'../Core/defaultValue',
'../Core/defined',
'../Core/DeveloperError',
'../Core/EasingFunction',
'../Core/Math',
'./PerspectiveFrustum',
'./PerspectiveOffCenterFrustum',
'./SceneMode'
], function(
Cartesian2,
Cartesian3,
Cartographic,
defaultValue,
defined,
DeveloperError,
EasingFunction,
CesiumMath,
PerspectiveFrustum,
PerspectiveOffCenterFrustum,
SceneMode) {
"use strict";
/**
* Creates tweens for camera flights.
* <br /><br />
* Mouse interaction is disabled during flights.
*
* @private
*/
var CameraFlightPath = {
};
function getAltitude(frustum, dx, dy) {
var near;
var top;
var right;
if (frustum instanceof PerspectiveFrustum) {
var tanTheta = Math.tan(0.5 * frustum.fovy);
near = frustum.near;
top = frustum.near * tanTheta;
right = frustum.aspectRatio * top;
return Math.max(dx * near / right, dy * near / top);
} else if (frustum instanceof PerspectiveOffCenterFrustum) {
near = frustum.near;
top = frustum.top;
right = frustum.right;
return Math.max(dx * near / right, dy * near / top);
}
return Math.max(dx, dy);
}
var scratchCart = new Cartesian3();
var scratchCart2 = new Cartesian3();
function createHeightFunction(camera, destination, startHeight, endHeight, optionAltitude) {
var altitude = optionAltitude;
var maxHeight;
if (!defined(optionAltitude)) {
var start = camera.position;
var end = destination;
var up = camera.up;
var right = camera.right;
var frustum = camera.frustum;
var diff = Cartesian3.subtract(start, end, scratchCart);
var verticalDistance = Cartesian3.magnitude(Cartesian3.multiplyByScalar(up, Cartesian3.dot(diff, up), scratchCart2));
var horizontalDistance = Cartesian3.magnitude(Cartesian3.multiplyByScalar(right, Cartesian3.dot(diff, right), scratchCart2));
maxHeight = Math.max(startHeight, endHeight);
altitude = Math.min(getAltitude(frustum, verticalDistance, horizontalDistance) * 0.20, 1000000000.0);
}
if (defined(optionAltitude) || maxHeight < altitude) {
var power = 8.0;
var factor = 1000000.0;
var s = -Math.pow((altitude - startHeight) * factor, 1.0 / power);
var e = Math.pow((altitude - endHeight) * factor, 1.0 / power);
return function(t) {
var x = t * (e - s) + s;
return -Math.pow(x, power) / factor + altitude;
};
}
return function(t) {
return CesiumMath.lerp(startHeight, endHeight, t);
};
}
function adjustAngleForLERP(startAngle, endAngle) {
if (CesiumMath.equalsEpsilon(startAngle, CesiumMath.TWO_PI, CesiumMath.EPSILON11)) {
startAngle = 0.0;
}
if (endAngle > startAngle + Math.PI) {
startAngle += CesiumMath.TWO_PI;
} else if (endAngle < startAngle - Math.PI) {
startAngle -= CesiumMath.TWO_PI;
}
return startAngle;
}
var scratchStart = new Cartesian3();
function createUpdateCV(scene, duration, destination, heading, pitch, roll, optionAltitude) {
var camera = scene.camera;
var start = Cartesian3.clone(camera.position, scratchStart);
var startPitch = camera.pitch;
var startHeading = adjustAngleForLERP(camera.heading, heading);
var startRoll = adjustAngleForLERP(camera.roll, roll);
var heightFunction = createHeightFunction(camera, destination, start.z, destination.z, optionAltitude);
var update = function(value) {
var time = value.time / duration;
camera.setView({
heading : CesiumMath.lerp(startHeading, heading, time),
pitch : CesiumMath.lerp(startPitch, pitch, time),
roll : CesiumMath.lerp(startRoll, roll, time)
});
Cartesian2.lerp(start, destination, time, camera.position);
camera.position.z = heightFunction(time);
};
return update;
}
var scratchStartCart = new Cartographic();
var scratchEndCart = new Cartographic();
var scratchCurrentPositionCart = new Cartographic();
function createUpdate3D(scene, duration, destination, heading, pitch, roll, optionAltitude) {
var camera = scene.camera;
var projection = scene.mapProjection;
var ellipsoid = projection.ellipsoid;
var startCart = Cartographic.clone(camera.positionCartographic, scratchStartCart);
var startPitch = camera.pitch;
var startHeading = adjustAngleForLERP(camera.heading, heading);
var startRoll = adjustAngleForLERP(camera.roll, roll);
var destCart = ellipsoid.cartesianToCartographic(destination, scratchEndCart);
if (destCart.height <= 0.0) {
destCart.height = startCart.height;
}
startCart.longitude = CesiumMath.zeroToTwoPi(startCart.longitude);
destCart.longitude = CesiumMath.zeroToTwoPi(destCart.longitude);
var diff = startCart.longitude - destCart.longitude;
if (diff < -CesiumMath.PI) {
startCart.longitude += CesiumMath.TWO_PI;
} else if (diff > CesiumMath.PI) {
destCart.longitude += CesiumMath.TWO_PI;
}
var heightFunction = createHeightFunction(camera, destination, startCart.height, destCart.height, optionAltitude);
var update = function(value) {
var time = value.time / duration;
var position = scratchCurrentPositionCart;
position.longitude = CesiumMath.lerp(startCart.longitude, destCart.longitude, time);
position.latitude = CesiumMath.lerp(startCart.latitude, destCart.latitude, time);
position.height = heightFunction(time);
camera.setView({
positionCartographic : position,
heading : CesiumMath.lerp(startHeading, heading, time),
pitch : CesiumMath.lerp(startPitch, pitch, time),
roll : CesiumMath.lerp(startRoll, roll, time)
});
};
return update;
}
function createUpdate2D(scene, duration, destination, heading, pitch, roll, optionAltitude) {
var camera = scene.camera;
var start = Cartesian3.clone(camera.position, scratchStart);
var startPitch = camera.pitch;
var startHeading = adjustAngleForLERP(camera.heading, heading);
var startRoll = adjustAngleForLERP(camera.roll, roll);
var startHeight = camera.frustum.right - camera.frustum.left;
var heightFunction = createHeightFunction(camera, destination, startHeight, destination.z, optionAltitude);
var update = function(value) {
var time = value.time / duration;
camera.setView({
heading : CesiumMath.lerp(startHeading, heading, time),
pitch : CesiumMath.lerp(startPitch, pitch, time),
roll : CesiumMath.lerp(startRoll, roll, time)
});
Cartesian2.lerp(start, destination, time, camera.position);
var zoom = heightFunction(time);
var frustum = camera.frustum;
var ratio = frustum.top / frustum.right;
var incrementAmount = (zoom - (frustum.right - frustum.left)) * 0.5;
frustum.right += incrementAmount;
frustum.left -= incrementAmount;
frustum.top = ratio * frustum.right;
frustum.bottom = -frustum.top;
};
return update;
}
var dirScratch = new Cartesian3();
var rightScratch = new Cartesian3();
var upScratch = new Cartesian3();
var scratchCartographic = new Cartographic();
var scratchDestination = new Cartesian3();
function emptyFlight(complete, cancel) {
return {
startObject : {},
stopObject : {},
duration : 0.0,
complete : complete,
cancel : cancel
};
}
function wrapCallback(controller, cb) {
var wrapped = function() {
if (typeof cb === 'function') {
cb();
}
controller.enableInputs = true;
};
return wrapped;
}
CameraFlightPath.createTween = function(scene, options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
var destination = options.destination;
//>>includeStart('debug', pragmas.debug);
if (!defined(scene)) {
throw new DeveloperError('scene is required.');
}
if (!defined(destination)) {
throw new DeveloperError('destination is required.');
}
//>>includeEnd('debug');
var projection = scene.mapProjection;
var ellipsoid = projection.ellipsoid;
var maximumHeight = options.maximumHeight;
var easingFunction = options.easingFunction;
if (scene.mode === SceneMode.MORPHING) {
return emptyFlight();
}
var convert = defaultValue(options.convert, true);
if (convert && scene.mode !== SceneMode.SCENE3D) {
ellipsoid.cartesianToCartographic(destination, scratchCartographic);
destination = projection.project(scratchCartographic, scratchDestination);
}
var camera = scene.camera;
var transform = options.endTransform;
if (defined(transform)) {
camera._setTransform(transform);
}
var duration = options.duration;
if (!defined(duration)) {
duration = Math.ceil(Cartesian3.distance(camera.position, destination) / 1000000.0) + 3.0;
duration = Math.min(duration, 10.0);
}
var mode = scene.mode;
var heading = defaultValue(options.heading, 0.0);
var pitch = scene.mode !== SceneMode.SCENE2D ? defaultValue(options.pitch, -CesiumMath.PI_OVER_TWO) : -CesiumMath.PI_OVER_TWO;
var roll = defaultValue(options.roll, 0.0);
var controller = scene.screenSpaceCameraController;
controller.enableInputs = false;
var complete = wrapCallback(controller, options.complete);
var cancel = wrapCallback(controller, options.cancel);
var frustum = camera.frustum;
var empty = scene.mode === SceneMode.SCENE2D;
empty = empty && Cartesian2.equalsEpsilon(camera.position, destination, CesiumMath.EPSILON6);
empty = empty && CesiumMath.equalsEpsilon(Math.max(frustum.right - frustum.left, frustum.top - frustum.bottom), destination.z, CesiumMath.EPSILON6);
empty = empty || (scene.mode !== SceneMode.SCENE2D && Cartesian3.equalsEpsilon(destination, camera.position, CesiumMath.EPSILON6));
if (empty) {
return emptyFlight(complete, cancel);
}
var updateFunctions = new Array(4);
updateFunctions[SceneMode.SCENE2D] = createUpdate2D;
updateFunctions[SceneMode.SCENE3D] = createUpdate3D;
updateFunctions[SceneMode.COLUMBUS_VIEW] = createUpdateCV;
if (duration <= 0.0) {
var newOnComplete = function() {
var update = updateFunctions[mode](scene, 1.0, destination, heading, pitch, roll, maximumHeight);
update({ time: 1.0 });
if (typeof complete === 'function') {
complete();
}
};
return emptyFlight(newOnComplete, cancel);
}
var update = updateFunctions[mode](scene, duration, destination, heading, pitch, roll, maximumHeight);
if (!defined(easingFunction)) {
var startHeight = camera.positionCartographic.height;
var endHeight = mode === SceneMode.SCENE3D ? ellipsoid.cartesianToCartographic(destination).height : destination.z;
if (startHeight > endHeight && startHeight > 11500.0) {
easingFunction = EasingFunction.CUBIC_OUT;
} else {
easingFunction = EasingFunction.QUINTIC_IN_OUT;
}
}
return {
duration : duration,
easingFunction : easingFunction,
startObject : {
time : 0.0
},
stopObject : {
time : duration
},
update : update,
complete : complete,
cancel: cancel
};
};
return CameraFlightPath;
});