CameraFlightPath.js 15 KB

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