CameraFlightPath.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. };
  21. function getAltitude(frustum, dx, dy) {
  22. var near;
  23. var top;
  24. var right;
  25. if (frustum instanceof PerspectiveFrustum) {
  26. var tanTheta = Math.tan(0.5 * frustum.fovy);
  27. near = frustum.near;
  28. top = frustum.near * tanTheta;
  29. right = frustum.aspectRatio * top;
  30. return Math.max(dx * near / right, dy * near / top);
  31. } else if (frustum instanceof PerspectiveOffCenterFrustum) {
  32. near = frustum.near;
  33. top = frustum.top;
  34. right = frustum.right;
  35. return Math.max(dx * near / right, dy * near / top);
  36. }
  37. return Math.max(dx, dy);
  38. }
  39. var scratchCart = new Cartesian3();
  40. var scratchCart2 = new Cartesian3();
  41. function createPitchFunction(startPitch, endPitch, heightFunction, pitchAdjustHeight) {
  42. if (defined(pitchAdjustHeight) && heightFunction(0.5) > pitchAdjustHeight) {
  43. var startHeight = heightFunction(0.0);
  44. var endHeight = heightFunction(1.0);
  45. var middleHeight = heightFunction(0.5);
  46. var d1 = middleHeight - startHeight;
  47. var d2 = middleHeight - endHeight;
  48. return function(time) {
  49. var altitude = heightFunction(time);
  50. if (time <= 0.5) {
  51. var t1 = (altitude - startHeight) / d1;
  52. return CesiumMath.lerp(startPitch, -CesiumMath.PI_OVER_TWO, t1);
  53. }
  54. var t2 = (altitude - endHeight) / d2;
  55. return CesiumMath.lerp(-CesiumMath.PI_OVER_TWO, endPitch, 1 - t2);
  56. };
  57. }
  58. return function(time) {
  59. return CesiumMath.lerp(startPitch, endPitch, time);
  60. };
  61. }
  62. function createHeightFunction(camera, destination, startHeight, endHeight, optionAltitude) {
  63. var altitude = optionAltitude;
  64. var maxHeight = Math.max(startHeight, endHeight);
  65. if (!defined(altitude)) {
  66. var start = camera.position;
  67. var end = destination;
  68. var up = camera.up;
  69. var right = camera.right;
  70. var frustum = camera.frustum;
  71. var diff = Cartesian3.subtract(start, end, scratchCart);
  72. var verticalDistance = Cartesian3.magnitude(Cartesian3.multiplyByScalar(up, Cartesian3.dot(diff, up), scratchCart2));
  73. var horizontalDistance = Cartesian3.magnitude(Cartesian3.multiplyByScalar(right, Cartesian3.dot(diff, right), scratchCart2));
  74. altitude = Math.min(getAltitude(frustum, verticalDistance, horizontalDistance) * 0.20, 1000000000.0);
  75. }
  76. if (maxHeight < altitude) {
  77. var power = 8.0;
  78. var factor = 1000000.0;
  79. var s = -Math.pow((altitude - startHeight) * factor, 1.0 / power);
  80. var e = Math.pow((altitude - endHeight) * factor, 1.0 / power);
  81. return function(t) {
  82. var x = t * (e - s) + s;
  83. return -Math.pow(x, power) / factor + altitude;
  84. };
  85. }
  86. return function(t) {
  87. return CesiumMath.lerp(startHeight, endHeight, t);
  88. };
  89. }
  90. function adjustAngleForLERP(startAngle, endAngle) {
  91. if (CesiumMath.equalsEpsilon(startAngle, CesiumMath.TWO_PI, CesiumMath.EPSILON11)) {
  92. startAngle = 0.0;
  93. }
  94. if (endAngle > startAngle + Math.PI) {
  95. startAngle += CesiumMath.TWO_PI;
  96. } else if (endAngle < startAngle - Math.PI) {
  97. startAngle -= CesiumMath.TWO_PI;
  98. }
  99. return startAngle;
  100. }
  101. var scratchStart = new Cartesian3();
  102. function createUpdateCV(scene, duration, destination, heading, pitch, roll, optionAltitude) {
  103. var camera = scene.camera;
  104. var start = Cartesian3.clone(camera.position, scratchStart);
  105. var startPitch = camera.pitch;
  106. var startHeading = adjustAngleForLERP(camera.heading, heading);
  107. var startRoll = adjustAngleForLERP(camera.roll, roll);
  108. var heightFunction = createHeightFunction(camera, destination, start.z, destination.z, optionAltitude);
  109. function update(value) {
  110. var time = value.time / duration;
  111. camera.setView({
  112. orientation: {
  113. heading : CesiumMath.lerp(startHeading, heading, time),
  114. pitch : CesiumMath.lerp(startPitch, pitch, time),
  115. roll : CesiumMath.lerp(startRoll, roll, time)
  116. }
  117. });
  118. Cartesian2.lerp(start, destination, time, camera.position);
  119. camera.position.z = heightFunction(time);
  120. }
  121. return update;
  122. }
  123. function useLongestFlight(startCart, destCart) {
  124. if (startCart.longitude < destCart.longitude) {
  125. startCart.longitude += CesiumMath.TWO_PI;
  126. } else {
  127. destCart.longitude += CesiumMath.TWO_PI;
  128. }
  129. }
  130. function useShortestFlight(startCart, destCart) {
  131. var diff = startCart.longitude - destCart.longitude;
  132. if (diff < -CesiumMath.PI) {
  133. startCart.longitude += CesiumMath.TWO_PI;
  134. } else if (diff > CesiumMath.PI) {
  135. destCart.longitude += CesiumMath.TWO_PI;
  136. }
  137. }
  138. var scratchStartCart = new Cartographic();
  139. var scratchEndCart = new Cartographic();
  140. function createUpdate3D(scene, duration, destination, heading, pitch, roll, optionAltitude, optionFlyOverLongitude, optionFlyOverLongitudeWeight, optionPitchAdjustHeight) {
  141. var camera = scene.camera;
  142. var projection = scene.mapProjection;
  143. var ellipsoid = projection.ellipsoid;
  144. var startCart = Cartographic.clone(camera.positionCartographic, scratchStartCart);
  145. var startPitch = camera.pitch;
  146. var startHeading = adjustAngleForLERP(camera.heading, heading);
  147. var startRoll = adjustAngleForLERP(camera.roll, roll);
  148. var destCart = ellipsoid.cartesianToCartographic(destination, scratchEndCart);
  149. startCart.longitude = CesiumMath.zeroToTwoPi(startCart.longitude);
  150. destCart.longitude = CesiumMath.zeroToTwoPi(destCart.longitude);
  151. var useLongFlight = false;
  152. if (defined(optionFlyOverLongitude)) {
  153. var hitLon = CesiumMath.zeroToTwoPi(optionFlyOverLongitude);
  154. var lonMin = Math.min(startCart.longitude, destCart.longitude);
  155. var lonMax = Math.max(startCart.longitude, destCart.longitude);
  156. var hitInside = (hitLon >= lonMin && hitLon <= lonMax);
  157. if (defined(optionFlyOverLongitudeWeight)) {
  158. // Distance inside (0...2Pi)
  159. var din = Math.abs(startCart.longitude - destCart.longitude);
  160. // Distance outside (0...2Pi)
  161. var dot = CesiumMath.TWO_PI - din;
  162. var hitDistance = hitInside ? din : dot;
  163. var offDistance = hitInside ? dot : din;
  164. if (hitDistance < offDistance * optionFlyOverLongitudeWeight && !hitInside) {
  165. useLongFlight = true;
  166. }
  167. } else if (!hitInside) {
  168. useLongFlight = true;
  169. }
  170. }
  171. if (useLongFlight) {
  172. useLongestFlight(startCart, destCart);
  173. } else {
  174. useShortestFlight(startCart, destCart);
  175. }
  176. var heightFunction = createHeightFunction(camera, destination, startCart.height, destCart.height, optionAltitude);
  177. var pitchFunction = createPitchFunction(startPitch, pitch, heightFunction, optionPitchAdjustHeight);
  178. // Isolate scope for update function.
  179. // to have local copies of vars used in lerp
  180. // Othervise, if you call nex
  181. // createUpdate3D (createAnimationTween)
  182. // before you played animation, variables will be overwriten.
  183. function isolateUpdateFunction() {
  184. var startLongitude = startCart.longitude;
  185. var destLongitude = destCart.longitude;
  186. var startLatitude = startCart.latitude;
  187. var destLatitude = destCart.latitude;
  188. return function update(value) {
  189. var time = value.time / duration;
  190. var position = Cartesian3.fromRadians(
  191. CesiumMath.lerp(startLongitude, destLongitude, time),
  192. CesiumMath.lerp(startLatitude, destLatitude, time),
  193. heightFunction(time)
  194. );
  195. camera.setView({
  196. destination : position,
  197. orientation: {
  198. heading : CesiumMath.lerp(startHeading, heading, time),
  199. pitch : pitchFunction(time),
  200. roll : CesiumMath.lerp(startRoll, roll, time)
  201. }
  202. });
  203. };
  204. }
  205. return isolateUpdateFunction();
  206. }
  207. function createUpdate2D(scene, duration, destination, heading, pitch, roll, optionAltitude) {
  208. var camera = scene.camera;
  209. var start = Cartesian3.clone(camera.position, scratchStart);
  210. var startHeading = adjustAngleForLERP(camera.heading, heading);
  211. var startHeight = camera.frustum.right - camera.frustum.left;
  212. var heightFunction = createHeightFunction(camera, destination, startHeight, destination.z, optionAltitude);
  213. function update(value) {
  214. var time = value.time / duration;
  215. camera.setView({
  216. orientation: {
  217. heading : CesiumMath.lerp(startHeading, heading, time)
  218. }
  219. });
  220. Cartesian2.lerp(start, destination, time, camera.position);
  221. var zoom = heightFunction(time);
  222. var frustum = camera.frustum;
  223. var ratio = frustum.top / frustum.right;
  224. var incrementAmount = (zoom - (frustum.right - frustum.left)) * 0.5;
  225. frustum.right += incrementAmount;
  226. frustum.left -= incrementAmount;
  227. frustum.top = ratio * frustum.right;
  228. frustum.bottom = -frustum.top;
  229. }
  230. return update;
  231. }
  232. var scratchCartographic = new Cartographic();
  233. var scratchDestination = new Cartesian3();
  234. function emptyFlight(complete, cancel) {
  235. return {
  236. startObject : {},
  237. stopObject : {},
  238. duration : 0.0,
  239. complete : complete,
  240. cancel : cancel
  241. };
  242. }
  243. function wrapCallback(controller, cb) {
  244. function wrapped() {
  245. if (typeof cb === 'function') {
  246. cb();
  247. }
  248. controller.enableInputs = true;
  249. }
  250. return wrapped;
  251. }
  252. CameraFlightPath.createTween = function(scene, options) {
  253. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  254. var destination = options.destination;
  255. //>>includeStart('debug', pragmas.debug);
  256. if (!defined(scene)) {
  257. throw new DeveloperError('scene is required.');
  258. }
  259. if (!defined(destination)) {
  260. throw new DeveloperError('destination is required.');
  261. }
  262. //>>includeEnd('debug');
  263. var mode = scene.mode;
  264. if (mode === SceneMode.MORPHING) {
  265. return emptyFlight();
  266. }
  267. var convert = defaultValue(options.convert, true);
  268. var projection = scene.mapProjection;
  269. var ellipsoid = projection.ellipsoid;
  270. var maximumHeight = options.maximumHeight;
  271. var flyOverLongitude = options.flyOverLongitude;
  272. var flyOverLongitudeWeight = options.flyOverLongitudeWeight;
  273. var pitchAdjustHeight = options.pitchAdjustHeight;
  274. var easingFunction = options.easingFunction;
  275. if (convert && mode !== SceneMode.SCENE3D) {
  276. ellipsoid.cartesianToCartographic(destination, scratchCartographic);
  277. destination = projection.project(scratchCartographic, scratchDestination);
  278. }
  279. var camera = scene.camera;
  280. var transform = options.endTransform;
  281. if (defined(transform)) {
  282. camera._setTransform(transform);
  283. }
  284. var duration = options.duration;
  285. if (!defined(duration)) {
  286. duration = Math.ceil(Cartesian3.distance(camera.position, destination) / 1000000.0) + 2.0;
  287. duration = Math.min(duration, 3.0);
  288. }
  289. var heading = defaultValue(options.heading, 0.0);
  290. var pitch = defaultValue(options.pitch, -CesiumMath.PI_OVER_TWO);
  291. var roll = defaultValue(options.roll, 0.0);
  292. var controller = scene.screenSpaceCameraController;
  293. controller.enableInputs = false;
  294. var complete = wrapCallback(controller, options.complete);
  295. var cancel = wrapCallback(controller, options.cancel);
  296. var frustum = camera.frustum;
  297. var empty = scene.mode === SceneMode.SCENE2D;
  298. empty = empty && Cartesian2.equalsEpsilon(camera.position, destination, CesiumMath.EPSILON6);
  299. empty = empty && CesiumMath.equalsEpsilon(Math.max(frustum.right - frustum.left, frustum.top - frustum.bottom), destination.z, CesiumMath.EPSILON6);
  300. empty = empty || (scene.mode !== SceneMode.SCENE2D &&
  301. Cartesian3.equalsEpsilon(destination, camera.position, CesiumMath.EPSILON10));
  302. empty = empty &&
  303. CesiumMath.equalsEpsilon(CesiumMath.negativePiToPi(heading), CesiumMath.negativePiToPi(camera.heading), CesiumMath.EPSILON10) &&
  304. CesiumMath.equalsEpsilon(CesiumMath.negativePiToPi(pitch), CesiumMath.negativePiToPi(camera.pitch), CesiumMath.EPSILON10) &&
  305. CesiumMath.equalsEpsilon(CesiumMath.negativePiToPi(roll), CesiumMath.negativePiToPi(camera.roll), CesiumMath.EPSILON10);
  306. if (empty) {
  307. return emptyFlight(complete, cancel);
  308. }
  309. var updateFunctions = new Array(4);
  310. updateFunctions[SceneMode.SCENE2D] = createUpdate2D;
  311. updateFunctions[SceneMode.SCENE3D] = createUpdate3D;
  312. updateFunctions[SceneMode.COLUMBUS_VIEW] = createUpdateCV;
  313. if (duration <= 0.0) {
  314. var newOnComplete = function() {
  315. var update = updateFunctions[mode](scene, 1.0, destination, heading, pitch, roll, maximumHeight, flyOverLongitude, flyOverLongitudeWeight, pitchAdjustHeight);
  316. update({ time: 1.0 });
  317. if (typeof complete === 'function') {
  318. complete();
  319. }
  320. };
  321. return emptyFlight(newOnComplete, cancel);
  322. }
  323. var update = updateFunctions[mode](scene, duration, destination, heading, pitch, roll, maximumHeight, flyOverLongitude, flyOverLongitudeWeight, pitchAdjustHeight);
  324. if (!defined(easingFunction)) {
  325. var startHeight = camera.positionCartographic.height;
  326. var endHeight = mode === SceneMode.SCENE3D ? ellipsoid.cartesianToCartographic(destination).height : destination.z;
  327. if (startHeight > endHeight && startHeight > 11500.0) {
  328. easingFunction = EasingFunction.CUBIC_OUT;
  329. } else {
  330. easingFunction = EasingFunction.QUINTIC_IN_OUT;
  331. }
  332. }
  333. return {
  334. duration : duration,
  335. easingFunction : easingFunction,
  336. startObject : {
  337. time : 0.0
  338. },
  339. stopObject : {
  340. time : duration
  341. },
  342. update : update,
  343. complete : complete,
  344. cancel: cancel
  345. };
  346. };
  347. export default CameraFlightPath;