CameraFlightPath.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. import Cartesian2 from "../Core/Cartesian2.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Cartographic from "../Core/Cartographic.js";
  4. import defaultValue from "../Core/defaultValue.js";
  5. import defined from "../Core/defined.js";
  6. import DeveloperError from "../Core/DeveloperError.js";
  7. import EasingFunction from "../Core/EasingFunction.js";
  8. import CesiumMath from "../Core/Math.js";
  9. import PerspectiveFrustum from "../Core/PerspectiveFrustum.js";
  10. import PerspectiveOffCenterFrustum from "../Core/PerspectiveOffCenterFrustum.js";
  11. import SceneMode from "./SceneMode.js";
  12. /**
  13. * Creates tweens for camera flights.
  14. * <br /><br />
  15. * Mouse interaction is disabled during flights.
  16. *
  17. * @private
  18. */
  19. var CameraFlightPath = {};
  20. function getAltitude(frustum, dx, dy) {
  21. var near;
  22. var top;
  23. var right;
  24. if (frustum instanceof PerspectiveFrustum) {
  25. var tanTheta = Math.tan(0.5 * frustum.fovy);
  26. near = frustum.near;
  27. top = frustum.near * tanTheta;
  28. right = frustum.aspectRatio * top;
  29. return Math.max((dx * near) / right, (dy * near) / top);
  30. } else if (frustum instanceof PerspectiveOffCenterFrustum) {
  31. near = frustum.near;
  32. top = frustum.top;
  33. right = frustum.right;
  34. return Math.max((dx * near) / right, (dy * near) / top);
  35. }
  36. return Math.max(dx, dy);
  37. }
  38. var scratchCart = new Cartesian3();
  39. var scratchCart2 = new Cartesian3();
  40. function createPitchFunction(
  41. startPitch,
  42. endPitch,
  43. heightFunction,
  44. pitchAdjustHeight
  45. ) {
  46. if (defined(pitchAdjustHeight) && heightFunction(0.5) > pitchAdjustHeight) {
  47. var startHeight = heightFunction(0.0);
  48. var endHeight = heightFunction(1.0);
  49. var middleHeight = heightFunction(0.5);
  50. var d1 = middleHeight - startHeight;
  51. var d2 = middleHeight - endHeight;
  52. return function (time) {
  53. var altitude = heightFunction(time);
  54. if (time <= 0.5) {
  55. var t1 = (altitude - startHeight) / d1;
  56. return CesiumMath.lerp(startPitch, -CesiumMath.PI_OVER_TWO, t1);
  57. }
  58. var t2 = (altitude - endHeight) / d2;
  59. return CesiumMath.lerp(-CesiumMath.PI_OVER_TWO, endPitch, 1 - t2);
  60. };
  61. }
  62. return function (time) {
  63. return CesiumMath.lerp(startPitch, endPitch, time);
  64. };
  65. }
  66. function createHeightFunction(
  67. camera,
  68. destination,
  69. startHeight,
  70. endHeight,
  71. optionAltitude
  72. ) {
  73. var altitude = optionAltitude;
  74. var maxHeight = Math.max(startHeight, endHeight);
  75. if (!defined(altitude)) {
  76. var start = camera.position;
  77. var end = destination;
  78. var up = camera.up;
  79. var right = camera.right;
  80. var frustum = camera.frustum;
  81. var diff = Cartesian3.subtract(start, end, scratchCart);
  82. var verticalDistance = Cartesian3.magnitude(
  83. Cartesian3.multiplyByScalar(up, Cartesian3.dot(diff, up), scratchCart2)
  84. );
  85. var horizontalDistance = Cartesian3.magnitude(
  86. Cartesian3.multiplyByScalar(
  87. right,
  88. Cartesian3.dot(diff, right),
  89. scratchCart2
  90. )
  91. );
  92. altitude = Math.min(
  93. getAltitude(frustum, verticalDistance, horizontalDistance) * 0.2,
  94. 1000000000.0
  95. );
  96. }
  97. if (maxHeight < altitude) {
  98. var power = 8.0;
  99. var factor = 1000000.0;
  100. var s = -Math.pow((altitude - startHeight) * factor, 1.0 / power);
  101. var e = Math.pow((altitude - endHeight) * factor, 1.0 / power);
  102. return function (t) {
  103. var x = t * (e - s) + s;
  104. return -Math.pow(x, power) / factor + altitude;
  105. };
  106. }
  107. return function (t) {
  108. return CesiumMath.lerp(startHeight, endHeight, t);
  109. };
  110. }
  111. function adjustAngleForLERP(startAngle, endAngle) {
  112. if (
  113. CesiumMath.equalsEpsilon(
  114. startAngle,
  115. CesiumMath.TWO_PI,
  116. CesiumMath.EPSILON11
  117. )
  118. ) {
  119. startAngle = 0.0;
  120. }
  121. if (endAngle > startAngle + Math.PI) {
  122. startAngle += CesiumMath.TWO_PI;
  123. } else if (endAngle < startAngle - Math.PI) {
  124. startAngle -= CesiumMath.TWO_PI;
  125. }
  126. return startAngle;
  127. }
  128. var scratchStart = new Cartesian3();
  129. function createUpdateCV(
  130. scene,
  131. duration,
  132. destination,
  133. heading,
  134. pitch,
  135. roll,
  136. optionAltitude
  137. ) {
  138. var camera = scene.camera;
  139. var start = Cartesian3.clone(camera.position, scratchStart);
  140. var startPitch = camera.pitch;
  141. var startHeading = adjustAngleForLERP(camera.heading, heading);
  142. var startRoll = adjustAngleForLERP(camera.roll, roll);
  143. var heightFunction = createHeightFunction(
  144. camera,
  145. destination,
  146. start.z,
  147. destination.z,
  148. optionAltitude
  149. );
  150. function update(value) {
  151. var time = value.time / duration;
  152. camera.setView({
  153. orientation: {
  154. heading: CesiumMath.lerp(startHeading, heading, time),
  155. pitch: CesiumMath.lerp(startPitch, pitch, time),
  156. roll: CesiumMath.lerp(startRoll, roll, time),
  157. },
  158. });
  159. Cartesian2.lerp(start, destination, time, camera.position);
  160. camera.position.z = heightFunction(time);
  161. }
  162. return update;
  163. }
  164. function useLongestFlight(startCart, destCart) {
  165. if (startCart.longitude < destCart.longitude) {
  166. startCart.longitude += CesiumMath.TWO_PI;
  167. } else {
  168. destCart.longitude += CesiumMath.TWO_PI;
  169. }
  170. }
  171. function useShortestFlight(startCart, destCart) {
  172. var diff = startCart.longitude - destCart.longitude;
  173. if (diff < -CesiumMath.PI) {
  174. startCart.longitude += CesiumMath.TWO_PI;
  175. } else if (diff > CesiumMath.PI) {
  176. destCart.longitude += CesiumMath.TWO_PI;
  177. }
  178. }
  179. var scratchStartCart = new Cartographic();
  180. var scratchEndCart = new Cartographic();
  181. function createUpdate3D(
  182. scene,
  183. duration,
  184. destination,
  185. heading,
  186. pitch,
  187. roll,
  188. optionAltitude,
  189. optionFlyOverLongitude,
  190. optionFlyOverLongitudeWeight,
  191. optionPitchAdjustHeight
  192. ) {
  193. var camera = scene.camera;
  194. var projection = scene.mapProjection;
  195. var ellipsoid = projection.ellipsoid;
  196. var startCart = Cartographic.clone(
  197. camera.positionCartographic,
  198. scratchStartCart
  199. );
  200. var startPitch = camera.pitch;
  201. var startHeading = adjustAngleForLERP(camera.heading, heading);
  202. var startRoll = adjustAngleForLERP(camera.roll, roll);
  203. var destCart = ellipsoid.cartesianToCartographic(destination, scratchEndCart);
  204. startCart.longitude = CesiumMath.zeroToTwoPi(startCart.longitude);
  205. destCart.longitude = CesiumMath.zeroToTwoPi(destCart.longitude);
  206. var useLongFlight = false;
  207. if (defined(optionFlyOverLongitude)) {
  208. var hitLon = CesiumMath.zeroToTwoPi(optionFlyOverLongitude);
  209. var lonMin = Math.min(startCart.longitude, destCart.longitude);
  210. var lonMax = Math.max(startCart.longitude, destCart.longitude);
  211. var hitInside = hitLon >= lonMin && hitLon <= lonMax;
  212. if (defined(optionFlyOverLongitudeWeight)) {
  213. // Distance inside (0...2Pi)
  214. var din = Math.abs(startCart.longitude - destCart.longitude);
  215. // Distance outside (0...2Pi)
  216. var dot = CesiumMath.TWO_PI - din;
  217. var hitDistance = hitInside ? din : dot;
  218. var offDistance = hitInside ? dot : din;
  219. if (
  220. hitDistance < offDistance * optionFlyOverLongitudeWeight &&
  221. !hitInside
  222. ) {
  223. useLongFlight = true;
  224. }
  225. } else if (!hitInside) {
  226. useLongFlight = true;
  227. }
  228. }
  229. if (useLongFlight) {
  230. useLongestFlight(startCart, destCart);
  231. } else {
  232. useShortestFlight(startCart, destCart);
  233. }
  234. var heightFunction = createHeightFunction(
  235. camera,
  236. destination,
  237. startCart.height,
  238. destCart.height,
  239. optionAltitude
  240. );
  241. var pitchFunction = createPitchFunction(
  242. startPitch,
  243. pitch,
  244. heightFunction,
  245. optionPitchAdjustHeight
  246. );
  247. // Isolate scope for update function.
  248. // to have local copies of vars used in lerp
  249. // Othervise, if you call nex
  250. // createUpdate3D (createAnimationTween)
  251. // before you played animation, variables will be overwriten.
  252. function isolateUpdateFunction() {
  253. var startLongitude = startCart.longitude;
  254. var destLongitude = destCart.longitude;
  255. var startLatitude = startCart.latitude;
  256. var destLatitude = destCart.latitude;
  257. return function update(value) {
  258. var time = value.time / duration;
  259. var position = Cartesian3.fromRadians(
  260. CesiumMath.lerp(startLongitude, destLongitude, time),
  261. CesiumMath.lerp(startLatitude, destLatitude, time),
  262. heightFunction(time)
  263. );
  264. camera.setView({
  265. destination: position,
  266. orientation: {
  267. heading: CesiumMath.lerp(startHeading, heading, time),
  268. pitch: pitchFunction(time),
  269. roll: CesiumMath.lerp(startRoll, roll, time),
  270. },
  271. });
  272. };
  273. }
  274. return isolateUpdateFunction();
  275. }
  276. function createUpdate2D(
  277. scene,
  278. duration,
  279. destination,
  280. heading,
  281. pitch,
  282. roll,
  283. optionAltitude
  284. ) {
  285. var camera = scene.camera;
  286. var start = Cartesian3.clone(camera.position, scratchStart);
  287. var startHeading = adjustAngleForLERP(camera.heading, heading);
  288. var startHeight = camera.frustum.right - camera.frustum.left;
  289. var heightFunction = createHeightFunction(
  290. camera,
  291. destination,
  292. startHeight,
  293. destination.z,
  294. optionAltitude
  295. );
  296. function update(value) {
  297. var time = value.time / duration;
  298. camera.setView({
  299. orientation: {
  300. heading: CesiumMath.lerp(startHeading, heading, time),
  301. },
  302. });
  303. Cartesian2.lerp(start, destination, time, camera.position);
  304. var zoom = heightFunction(time);
  305. var frustum = camera.frustum;
  306. var ratio = frustum.top / frustum.right;
  307. var incrementAmount = (zoom - (frustum.right - frustum.left)) * 0.5;
  308. frustum.right += incrementAmount;
  309. frustum.left -= incrementAmount;
  310. frustum.top = ratio * frustum.right;
  311. frustum.bottom = -frustum.top;
  312. }
  313. return update;
  314. }
  315. var scratchCartographic = new Cartographic();
  316. var scratchDestination = new Cartesian3();
  317. function emptyFlight(complete, cancel) {
  318. return {
  319. startObject: {},
  320. stopObject: {},
  321. duration: 0.0,
  322. complete: complete,
  323. cancel: cancel,
  324. };
  325. }
  326. function wrapCallback(controller, cb) {
  327. function wrapped() {
  328. if (typeof cb === "function") {
  329. cb();
  330. }
  331. controller.enableInputs = true;
  332. }
  333. return wrapped;
  334. }
  335. CameraFlightPath.createTween = function (scene, options) {
  336. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  337. var destination = options.destination;
  338. //>>includeStart('debug', pragmas.debug);
  339. if (!defined(scene)) {
  340. throw new DeveloperError("scene is required.");
  341. }
  342. if (!defined(destination)) {
  343. throw new DeveloperError("destination is required.");
  344. }
  345. //>>includeEnd('debug');
  346. var mode = scene.mode;
  347. if (mode === SceneMode.MORPHING) {
  348. return emptyFlight();
  349. }
  350. var convert = defaultValue(options.convert, true);
  351. var projection = scene.mapProjection;
  352. var ellipsoid = projection.ellipsoid;
  353. var maximumHeight = options.maximumHeight;
  354. var flyOverLongitude = options.flyOverLongitude;
  355. var flyOverLongitudeWeight = options.flyOverLongitudeWeight;
  356. var pitchAdjustHeight = options.pitchAdjustHeight;
  357. var easingFunction = options.easingFunction;
  358. if (convert && mode !== SceneMode.SCENE3D) {
  359. ellipsoid.cartesianToCartographic(destination, scratchCartographic);
  360. destination = projection.project(scratchCartographic, scratchDestination);
  361. }
  362. var camera = scene.camera;
  363. var transform = options.endTransform;
  364. if (defined(transform)) {
  365. camera._setTransform(transform);
  366. }
  367. var duration = options.duration;
  368. if (!defined(duration)) {
  369. duration =
  370. Math.ceil(Cartesian3.distance(camera.position, destination) / 1000000.0) +
  371. 2.0;
  372. duration = Math.min(duration, 3.0);
  373. }
  374. var heading = defaultValue(options.heading, 0.0);
  375. var pitch = defaultValue(options.pitch, -CesiumMath.PI_OVER_TWO);
  376. var roll = defaultValue(options.roll, 0.0);
  377. var controller = scene.screenSpaceCameraController;
  378. controller.enableInputs = false;
  379. var complete = wrapCallback(controller, options.complete);
  380. var cancel = wrapCallback(controller, options.cancel);
  381. var frustum = camera.frustum;
  382. var empty = scene.mode === SceneMode.SCENE2D;
  383. empty =
  384. empty &&
  385. Cartesian2.equalsEpsilon(camera.position, destination, CesiumMath.EPSILON6);
  386. empty =
  387. empty &&
  388. CesiumMath.equalsEpsilon(
  389. Math.max(frustum.right - frustum.left, frustum.top - frustum.bottom),
  390. destination.z,
  391. CesiumMath.EPSILON6
  392. );
  393. empty =
  394. empty ||
  395. (scene.mode !== SceneMode.SCENE2D &&
  396. Cartesian3.equalsEpsilon(
  397. destination,
  398. camera.position,
  399. CesiumMath.EPSILON10
  400. ));
  401. empty =
  402. empty &&
  403. CesiumMath.equalsEpsilon(
  404. CesiumMath.negativePiToPi(heading),
  405. CesiumMath.negativePiToPi(camera.heading),
  406. CesiumMath.EPSILON10
  407. ) &&
  408. CesiumMath.equalsEpsilon(
  409. CesiumMath.negativePiToPi(pitch),
  410. CesiumMath.negativePiToPi(camera.pitch),
  411. CesiumMath.EPSILON10
  412. ) &&
  413. CesiumMath.equalsEpsilon(
  414. CesiumMath.negativePiToPi(roll),
  415. CesiumMath.negativePiToPi(camera.roll),
  416. CesiumMath.EPSILON10
  417. );
  418. if (empty) {
  419. return emptyFlight(complete, cancel);
  420. }
  421. var updateFunctions = new Array(4);
  422. updateFunctions[SceneMode.SCENE2D] = createUpdate2D;
  423. updateFunctions[SceneMode.SCENE3D] = createUpdate3D;
  424. updateFunctions[SceneMode.COLUMBUS_VIEW] = createUpdateCV;
  425. if (duration <= 0.0) {
  426. var newOnComplete = function () {
  427. var update = updateFunctions[mode](
  428. scene,
  429. 1.0,
  430. destination,
  431. heading,
  432. pitch,
  433. roll,
  434. maximumHeight,
  435. flyOverLongitude,
  436. flyOverLongitudeWeight,
  437. pitchAdjustHeight
  438. );
  439. update({ time: 1.0 });
  440. if (typeof complete === "function") {
  441. complete();
  442. }
  443. };
  444. return emptyFlight(newOnComplete, cancel);
  445. }
  446. var update = updateFunctions[mode](
  447. scene,
  448. duration,
  449. destination,
  450. heading,
  451. pitch,
  452. roll,
  453. maximumHeight,
  454. flyOverLongitude,
  455. flyOverLongitudeWeight,
  456. pitchAdjustHeight
  457. );
  458. if (!defined(easingFunction)) {
  459. var startHeight = camera.positionCartographic.height;
  460. var endHeight =
  461. mode === SceneMode.SCENE3D
  462. ? ellipsoid.cartesianToCartographic(destination).height
  463. : destination.z;
  464. if (startHeight > endHeight && startHeight > 11500.0) {
  465. easingFunction = EasingFunction.CUBIC_OUT;
  466. } else {
  467. easingFunction = EasingFunction.QUINTIC_IN_OUT;
  468. }
  469. }
  470. return {
  471. duration: duration,
  472. easingFunction: easingFunction,
  473. startObject: {
  474. time: 0.0,
  475. },
  476. stopObject: {
  477. time: duration,
  478. },
  479. update: update,
  480. complete: complete,
  481. cancel: cancel,
  482. };
  483. };
  484. export default CameraFlightPath;