ScreenSpaceCameraController.js 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  1. import Cartesian2 from '../Core/Cartesian2.js';
  2. import Cartesian3 from '../Core/Cartesian3.js';
  3. import Cartesian4 from '../Core/Cartesian4.js';
  4. import Cartographic from '../Core/Cartographic.js';
  5. import defaultValue from '../Core/defaultValue.js';
  6. import defined from '../Core/defined.js';
  7. import destroyObject from '../Core/destroyObject.js';
  8. import DeveloperError from '../Core/DeveloperError.js';
  9. import Ellipsoid from '../Core/Ellipsoid.js';
  10. import HeadingPitchRoll from '../Core/HeadingPitchRoll.js';
  11. import IntersectionTests from '../Core/IntersectionTests.js';
  12. import isArray from '../Core/isArray.js';
  13. import KeyboardEventModifier from '../Core/KeyboardEventModifier.js';
  14. import CesiumMath from '../Core/Math.js';
  15. import Matrix3 from '../Core/Matrix3.js';
  16. import Matrix4 from '../Core/Matrix4.js';
  17. import OrthographicFrustum from '../Core/OrthographicFrustum.js';
  18. import Plane from '../Core/Plane.js';
  19. import Quaternion from '../Core/Quaternion.js';
  20. import Ray from '../Core/Ray.js';
  21. import Transforms from '../Core/Transforms.js';
  22. import CameraEventAggregator from './CameraEventAggregator.js';
  23. import CameraEventType from './CameraEventType.js';
  24. import MapMode2D from './MapMode2D.js';
  25. import SceneMode from './SceneMode.js';
  26. import SceneTransforms from './SceneTransforms.js';
  27. import TweenCollection from './TweenCollection.js';
  28. /**
  29. * Modifies the camera position and orientation based on mouse input to a canvas.
  30. * @alias ScreenSpaceCameraController
  31. * @constructor
  32. *
  33. * @param {Scene} scene The scene.
  34. */
  35. function ScreenSpaceCameraController(scene) {
  36. //>>includeStart('debug', pragmas.debug);
  37. if (!defined(scene)) {
  38. throw new DeveloperError('scene is required.');
  39. }
  40. //>>includeEnd('debug');
  41. /**
  42. * If true, inputs are allowed conditionally with the flags enableTranslate, enableZoom,
  43. * enableRotate, enableTilt, and enableLook. If false, all inputs are disabled.
  44. *
  45. * NOTE: This setting is for temporary use cases, such as camera flights and
  46. * drag-selection of regions (see Picking demo). It is typically set to false at the
  47. * start of such events, and set true on completion. To keep inputs disabled
  48. * past the end of camera flights, you must use the other booleans (enableTranslate,
  49. * enableZoom, enableRotate, enableTilt, and enableLook).
  50. * @type {Boolean}
  51. * @default true
  52. */
  53. this.enableInputs = true;
  54. /**
  55. * If true, allows the user to pan around the map. If false, the camera stays locked at the current position.
  56. * This flag only applies in 2D and Columbus view modes.
  57. * @type {Boolean}
  58. * @default true
  59. */
  60. this.enableTranslate = true;
  61. /**
  62. * If true, allows the user to zoom in and out. If false, the camera is locked to the current distance from the ellipsoid.
  63. * @type {Boolean}
  64. * @default true
  65. */
  66. this.enableZoom = true;
  67. /**
  68. * If true, allows the user to rotate the world which translates the user's position.
  69. * This flag only applies in 2D and 3D.
  70. * @type {Boolean}
  71. * @default true
  72. */
  73. this.enableRotate = true;
  74. /**
  75. * If true, allows the user to tilt the camera. If false, the camera is locked to the current heading.
  76. * This flag only applies in 3D and Columbus view.
  77. * @type {Boolean}
  78. * @default true
  79. */
  80. this.enableTilt = true;
  81. /**
  82. * If true, allows the user to use free-look. If false, the camera view direction can only be changed through translating
  83. * or rotating. This flag only applies in 3D and Columbus view modes.
  84. * @type {Boolean}
  85. * @default true
  86. */
  87. this.enableLook = true;
  88. /**
  89. * A parameter in the range <code>[0, 1)</code> used to determine how long
  90. * the camera will continue to spin because of inertia.
  91. * With value of zero, the camera will have no inertia.
  92. * @type {Number}
  93. * @default 0.9
  94. */
  95. this.inertiaSpin = 0.9;
  96. /**
  97. * A parameter in the range <code>[0, 1)</code> used to determine how long
  98. * the camera will continue to translate because of inertia.
  99. * With value of zero, the camera will have no inertia.
  100. * @type {Number}
  101. * @default 0.9
  102. */
  103. this.inertiaTranslate = 0.9;
  104. /**
  105. * A parameter in the range <code>[0, 1)</code> used to determine how long
  106. * the camera will continue to zoom because of inertia.
  107. * With value of zero, the camera will have no inertia.
  108. * @type {Number}
  109. * @default 0.8
  110. */
  111. this.inertiaZoom = 0.8;
  112. /**
  113. * A parameter in the range <code>[0, 1)</code> used to limit the range
  114. * of various user inputs to a percentage of the window width/height per animation frame.
  115. * This helps keep the camera under control in low-frame-rate situations.
  116. * @type {Number}
  117. * @default 0.1
  118. */
  119. this.maximumMovementRatio = 0.1;
  120. /**
  121. * Sets the duration, in seconds, of the bounce back animations in 2D and Columbus view.
  122. * @type {Number}
  123. * @default 3.0
  124. */
  125. this.bounceAnimationTime = 3.0;
  126. /**
  127. * The minimum magnitude, in meters, of the camera position when zooming. Defaults to 1.0.
  128. * @type {Number}
  129. * @default 1.0
  130. */
  131. this.minimumZoomDistance = 1.0;
  132. /**
  133. * The maximum magnitude, in meters, of the camera position when zooming. Defaults to positive infinity.
  134. * @type {Number}
  135. * @default {@link Number.POSITIVE_INFINITY}
  136. */
  137. this.maximumZoomDistance = Number.POSITIVE_INFINITY;
  138. /**
  139. * The input that allows the user to pan around the map. This only applies in 2D and Columbus view modes.
  140. * <p>
  141. * The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
  142. * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
  143. * or an array of any of the preceding.
  144. * </p>
  145. * @type {CameraEventType|Array|undefined}
  146. * @default {@link CameraEventType.LEFT_DRAG}
  147. */
  148. this.translateEventTypes = CameraEventType.LEFT_DRAG;
  149. /**
  150. * The input that allows the user to zoom in/out.
  151. * <p>
  152. * The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
  153. * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
  154. * or an array of any of the preceding.
  155. * </p>
  156. * @type {CameraEventType|Array|undefined}
  157. * @default [{@link CameraEventType.RIGHT_DRAG}, {@link CameraEventType.WHEEL}, {@link CameraEventType.PINCH}]
  158. */
  159. this.zoomEventTypes = [CameraEventType.RIGHT_DRAG, CameraEventType.WHEEL, CameraEventType.PINCH];
  160. /**
  161. * The input that allows the user to rotate around the globe or another object. This only applies in 3D and Columbus view modes.
  162. * <p>
  163. * The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
  164. * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
  165. * or an array of any of the preceding.
  166. * </p>
  167. * @type {CameraEventType|Array|undefined}
  168. * @default {@link CameraEventType.LEFT_DRAG}
  169. */
  170. this.rotateEventTypes = CameraEventType.LEFT_DRAG;
  171. /**
  172. * The input that allows the user to tilt in 3D and Columbus view or twist in 2D.
  173. * <p>
  174. * The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
  175. * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
  176. * or an array of any of the preceding.
  177. * </p>
  178. * @type {CameraEventType|Array|undefined}
  179. * @default [{@link CameraEventType.MIDDLE_DRAG}, {@link CameraEventType.PINCH}, {
  180. * eventType : {@link CameraEventType.LEFT_DRAG},
  181. * modifier : {@link KeyboardEventModifier.CTRL}
  182. * }, {
  183. * eventType : {@link CameraEventType.RIGHT_DRAG},
  184. * modifier : {@link KeyboardEventModifier.CTRL}
  185. * }]
  186. */
  187. this.tiltEventTypes = [CameraEventType.MIDDLE_DRAG, CameraEventType.PINCH, {
  188. eventType : CameraEventType.LEFT_DRAG,
  189. modifier : KeyboardEventModifier.CTRL
  190. }, {
  191. eventType : CameraEventType.RIGHT_DRAG,
  192. modifier : KeyboardEventModifier.CTRL
  193. }];
  194. /**
  195. * The input that allows the user to change the direction the camera is viewing. This only applies in 3D and Columbus view modes.
  196. * <p>
  197. * The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
  198. * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
  199. * or an array of any of the preceding.
  200. * </p>
  201. * @type {CameraEventType|Array|undefined}
  202. * @default { eventType : {@link CameraEventType.LEFT_DRAG}, modifier : {@link KeyboardEventModifier.SHIFT} }
  203. */
  204. this.lookEventTypes = {
  205. eventType : CameraEventType.LEFT_DRAG,
  206. modifier : KeyboardEventModifier.SHIFT
  207. };
  208. /**
  209. * The minimum height the camera must be before picking the terrain instead of the ellipsoid.
  210. * @type {Number}
  211. * @default 150000.0
  212. */
  213. this.minimumPickingTerrainHeight = 150000.0;
  214. this._minimumPickingTerrainHeight = this.minimumPickingTerrainHeight;
  215. /**
  216. * The minimum height the camera must be before testing for collision with terrain.
  217. * @type {Number}
  218. * @default 15000.0
  219. */
  220. this.minimumCollisionTerrainHeight = 15000.0;
  221. this._minimumCollisionTerrainHeight = this.minimumCollisionTerrainHeight;
  222. /**
  223. * The minimum height the camera must be before switching from rotating a track ball to
  224. * free look when clicks originate on the sky on in space.
  225. * @type {Number}
  226. * @default 7500000.0
  227. */
  228. this.minimumTrackBallHeight = 7500000.0;
  229. this._minimumTrackBallHeight = this.minimumTrackBallHeight;
  230. /**
  231. * Enables or disables camera collision detection with terrain.
  232. * @type {Boolean}
  233. * @default true
  234. */
  235. this.enableCollisionDetection = true;
  236. this._scene = scene;
  237. this._globe = undefined;
  238. this._ellipsoid = undefined;
  239. this._aggregator = new CameraEventAggregator(scene.canvas);
  240. this._lastInertiaSpinMovement = undefined;
  241. this._lastInertiaZoomMovement = undefined;
  242. this._lastInertiaTranslateMovement = undefined;
  243. this._lastInertiaTiltMovement = undefined;
  244. this._tweens = new TweenCollection();
  245. this._tween = undefined;
  246. this._horizontalRotationAxis = undefined;
  247. this._tiltCenterMousePosition = new Cartesian2(-1.0, -1.0);
  248. this._tiltCenter = new Cartesian3();
  249. this._rotateMousePosition = new Cartesian2(-1.0, -1.0);
  250. this._rotateStartPosition = new Cartesian3();
  251. this._strafeStartPosition = new Cartesian3();
  252. this._zoomMouseStart = new Cartesian2(-1.0, -1.0);
  253. this._zoomWorldPosition = new Cartesian3();
  254. this._useZoomWorldPosition = false;
  255. this._tiltCVOffMap = false;
  256. this._looking = false;
  257. this._rotating = false;
  258. this._strafing = false;
  259. this._zoomingOnVector = false;
  260. this._rotatingZoom = false;
  261. var projection = scene.mapProjection;
  262. this._maxCoord = projection.project(new Cartographic(Math.PI, CesiumMath.PI_OVER_TWO));
  263. // Constants, Make any of these public?
  264. this._zoomFactor = 5.0;
  265. this._rotateFactor = undefined;
  266. this._rotateRateRangeAdjustment = undefined;
  267. this._maximumRotateRate = 1.77;
  268. this._minimumRotateRate = 1.0 / 5000.0;
  269. this._minimumZoomRate = 20.0;
  270. this._maximumZoomRate = 5906376272000.0; // distance from the Sun to Pluto in meters.
  271. }
  272. function decay(time, coefficient) {
  273. if (time < 0) {
  274. return 0.0;
  275. }
  276. var tau = (1.0 - coefficient) * 25.0;
  277. return Math.exp(-tau * time);
  278. }
  279. function sameMousePosition(movement) {
  280. return Cartesian2.equalsEpsilon(movement.startPosition, movement.endPosition, CesiumMath.EPSILON14);
  281. }
  282. // If the time between mouse down and mouse up is not between
  283. // these thresholds, the camera will not move with inertia.
  284. // This value is probably dependent on the browser and/or the
  285. // hardware. Should be investigated further.
  286. var inertiaMaxClickTimeThreshold = 0.4;
  287. function maintainInertia(aggregator, type, modifier, decayCoef, action, object, lastMovementName) {
  288. var movementState = object[lastMovementName];
  289. if (!defined(movementState)) {
  290. movementState = object[lastMovementName] = {
  291. startPosition : new Cartesian2(),
  292. endPosition : new Cartesian2(),
  293. motion : new Cartesian2(),
  294. active : false
  295. };
  296. }
  297. var ts = aggregator.getButtonPressTime(type, modifier);
  298. var tr = aggregator.getButtonReleaseTime(type, modifier);
  299. var threshold = ts && tr && ((tr.getTime() - ts.getTime()) / 1000.0);
  300. var now = new Date();
  301. var fromNow = tr && ((now.getTime() - tr.getTime()) / 1000.0);
  302. if (ts && tr && threshold < inertiaMaxClickTimeThreshold) {
  303. var d = decay(fromNow, decayCoef);
  304. if (!movementState.active) {
  305. var lastMovement = aggregator.getLastMovement(type, modifier);
  306. if (!defined(lastMovement) || sameMousePosition(lastMovement)) {
  307. return;
  308. }
  309. movementState.motion.x = (lastMovement.endPosition.x - lastMovement.startPosition.x) * 0.5;
  310. movementState.motion.y = (lastMovement.endPosition.y - lastMovement.startPosition.y) * 0.5;
  311. movementState.startPosition = Cartesian2.clone(lastMovement.startPosition, movementState.startPosition);
  312. movementState.endPosition = Cartesian2.multiplyByScalar(movementState.motion, d, movementState.endPosition);
  313. movementState.endPosition = Cartesian2.add(movementState.startPosition, movementState.endPosition, movementState.endPosition);
  314. movementState.active = true;
  315. } else {
  316. movementState.startPosition = Cartesian2.clone(movementState.endPosition, movementState.startPosition);
  317. movementState.endPosition = Cartesian2.multiplyByScalar(movementState.motion, d, movementState.endPosition);
  318. movementState.endPosition = Cartesian2.add(movementState.startPosition, movementState.endPosition, movementState.endPosition);
  319. movementState.motion = Cartesian2.clone(Cartesian2.ZERO, movementState.motion);
  320. }
  321. // If value from the decreasing exponential function is close to zero,
  322. // the end coordinates may be NaN.
  323. if (isNaN(movementState.endPosition.x) || isNaN(movementState.endPosition.y) || Cartesian2.distance(movementState.startPosition, movementState.endPosition) < 0.5) {
  324. movementState.active = false;
  325. return;
  326. }
  327. if (!aggregator.isButtonDown(type, modifier)) {
  328. var startPosition = aggregator.getStartMousePosition(type, modifier);
  329. action(object, startPosition, movementState);
  330. }
  331. } else {
  332. movementState.active = false;
  333. }
  334. }
  335. var scratchEventTypeArray = [];
  336. function reactToInput(controller, enabled, eventTypes, action, inertiaConstant, inertiaStateName) {
  337. if (!defined(eventTypes)) {
  338. return;
  339. }
  340. var aggregator = controller._aggregator;
  341. if (!isArray(eventTypes)) {
  342. scratchEventTypeArray[0] = eventTypes;
  343. eventTypes = scratchEventTypeArray;
  344. }
  345. var length = eventTypes.length;
  346. for (var i = 0; i < length; ++i) {
  347. var eventType = eventTypes[i];
  348. var type = defined(eventType.eventType) ? eventType.eventType : eventType;
  349. var modifier = eventType.modifier;
  350. var movement = aggregator.isMoving(type, modifier) && aggregator.getMovement(type, modifier);
  351. var startPosition = aggregator.getStartMousePosition(type, modifier);
  352. if (controller.enableInputs && enabled) {
  353. if (movement) {
  354. action(controller, startPosition, movement);
  355. } else if (inertiaConstant < 1.0) {
  356. maintainInertia(aggregator, type, modifier, inertiaConstant, action, controller, inertiaStateName);
  357. }
  358. }
  359. }
  360. }
  361. var scratchZoomPickRay = new Ray();
  362. var scratchPickCartesian = new Cartesian3();
  363. var scratchZoomOffset = new Cartesian2();
  364. var scratchZoomDirection = new Cartesian3();
  365. var scratchCenterPixel = new Cartesian2();
  366. var scratchCenterPosition = new Cartesian3();
  367. var scratchPositionNormal = new Cartesian3();
  368. var scratchPickNormal = new Cartesian3();
  369. var scratchZoomAxis = new Cartesian3();
  370. var scratchCameraPositionNormal = new Cartesian3();
  371. // Scratch variables used in zooming algorithm
  372. var scratchTargetNormal = new Cartesian3();
  373. var scratchCameraPosition = new Cartesian3();
  374. var scratchCameraUpNormal = new Cartesian3();
  375. var scratchCameraRightNormal = new Cartesian3();
  376. var scratchForwardNormal = new Cartesian3();
  377. var scratchPositionToTarget = new Cartesian3();
  378. var scratchPositionToTargetNormal = new Cartesian3();
  379. var scratchPan = new Cartesian3();
  380. var scratchCenterMovement = new Cartesian3();
  381. var scratchCenter = new Cartesian3();
  382. var scratchCartesian = new Cartesian3();
  383. var scratchCartesianTwo = new Cartesian3();
  384. var scratchCartesianThree = new Cartesian3();
  385. var scratchZoomViewOptions = {
  386. orientation: new HeadingPitchRoll()
  387. };
  388. function handleZoom(object, startPosition, movement, zoomFactor, distanceMeasure, unitPositionDotDirection) {
  389. var percentage = 1.0;
  390. if (defined(unitPositionDotDirection)) {
  391. percentage = CesiumMath.clamp(Math.abs(unitPositionDotDirection), 0.25, 1.0);
  392. }
  393. // distanceMeasure should be the height above the ellipsoid.
  394. // The zoomRate slows as it approaches the surface and stops minimumZoomDistance above it.
  395. var minHeight = object.minimumZoomDistance * percentage;
  396. var maxHeight = object.maximumZoomDistance;
  397. var minDistance = distanceMeasure - minHeight;
  398. var zoomRate = zoomFactor * minDistance;
  399. zoomRate = CesiumMath.clamp(zoomRate, object._minimumZoomRate, object._maximumZoomRate);
  400. var diff = movement.endPosition.y - movement.startPosition.y;
  401. var rangeWindowRatio = diff / object._scene.canvas.clientHeight;
  402. rangeWindowRatio = Math.min(rangeWindowRatio, object.maximumMovementRatio);
  403. var distance = zoomRate * rangeWindowRatio;
  404. if (distance > 0.0 && Math.abs(distanceMeasure - minHeight) < 1.0) {
  405. return;
  406. }
  407. if (distance < 0.0 && Math.abs(distanceMeasure - maxHeight) < 1.0) {
  408. return;
  409. }
  410. if (distanceMeasure - distance < minHeight) {
  411. distance = distanceMeasure - minHeight - 1.0;
  412. } else if (distanceMeasure - distance > maxHeight) {
  413. distance = distanceMeasure - maxHeight;
  414. }
  415. var scene = object._scene;
  416. var camera = scene.camera;
  417. var mode = scene.mode;
  418. var orientation = scratchZoomViewOptions.orientation;
  419. orientation.heading = camera.heading;
  420. orientation.pitch = camera.pitch;
  421. orientation.roll = camera.roll;
  422. if (camera.frustum instanceof OrthographicFrustum) {
  423. if (Math.abs(distance) > 0.0) {
  424. camera.zoomIn(distance);
  425. camera._adjustOrthographicFrustum();
  426. }
  427. return;
  428. }
  429. var sameStartPosition = Cartesian2.equals(startPosition, object._zoomMouseStart);
  430. var zoomingOnVector = object._zoomingOnVector;
  431. var rotatingZoom = object._rotatingZoom;
  432. var pickedPosition;
  433. if (!sameStartPosition) {
  434. object._zoomMouseStart = Cartesian2.clone(startPosition, object._zoomMouseStart);
  435. if (defined(object._globe)) {
  436. if (mode === SceneMode.SCENE2D) {
  437. pickedPosition = camera.getPickRay(startPosition, scratchZoomPickRay).origin;
  438. pickedPosition = Cartesian3.fromElements(pickedPosition.y, pickedPosition.z, pickedPosition.x);
  439. } else {
  440. pickedPosition = pickGlobe(object, startPosition, scratchPickCartesian);
  441. }
  442. }
  443. if (defined(pickedPosition)) {
  444. object._useZoomWorldPosition = true;
  445. object._zoomWorldPosition = Cartesian3.clone(pickedPosition, object._zoomWorldPosition);
  446. } else {
  447. object._useZoomWorldPosition = false;
  448. }
  449. zoomingOnVector = object._zoomingOnVector = false;
  450. rotatingZoom = object._rotatingZoom = false;
  451. }
  452. if (!object._useZoomWorldPosition) {
  453. camera.zoomIn(distance);
  454. return;
  455. }
  456. var zoomOnVector = mode === SceneMode.COLUMBUS_VIEW;
  457. if (camera.positionCartographic.height < 2000000) {
  458. rotatingZoom = true;
  459. }
  460. if (!sameStartPosition || rotatingZoom) {
  461. if (mode === SceneMode.SCENE2D) {
  462. var worldPosition = object._zoomWorldPosition;
  463. var endPosition = camera.position;
  464. if (!Cartesian3.equals(worldPosition, endPosition) && camera.positionCartographic.height < object._maxCoord.x * 2.0) {
  465. var savedX = camera.position.x;
  466. var direction = Cartesian3.subtract(worldPosition, endPosition, scratchZoomDirection);
  467. Cartesian3.normalize(direction, direction);
  468. var d = Cartesian3.distance(worldPosition, endPosition) * distance / (camera.getMagnitude() * 0.5);
  469. camera.move(direction, d * 0.5);
  470. if ((camera.position.x < 0.0 && savedX > 0.0) || (camera.position.x > 0.0 && savedX < 0.0)) {
  471. pickedPosition = camera.getPickRay(startPosition, scratchZoomPickRay).origin;
  472. pickedPosition = Cartesian3.fromElements(pickedPosition.y, pickedPosition.z, pickedPosition.x);
  473. object._zoomWorldPosition = Cartesian3.clone(pickedPosition, object._zoomWorldPosition);
  474. }
  475. }
  476. } else if (mode === SceneMode.SCENE3D) {
  477. var cameraPositionNormal = Cartesian3.normalize(camera.position, scratchCameraPositionNormal);
  478. if (camera.positionCartographic.height < 3000.0 && Math.abs(Cartesian3.dot(camera.direction, cameraPositionNormal)) < 0.6) {
  479. zoomOnVector = true;
  480. } else {
  481. var canvas = scene.canvas;
  482. var centerPixel = scratchCenterPixel;
  483. centerPixel.x = canvas.clientWidth / 2;
  484. centerPixel.y = canvas.clientHeight / 2;
  485. var centerPosition = pickGlobe(object, centerPixel, scratchCenterPosition);
  486. // If centerPosition is not defined, it means the globe does not cover the center position of screen
  487. if (defined(centerPosition) && camera.positionCartographic.height < 1000000) {
  488. var cameraPosition = scratchCameraPosition;
  489. Cartesian3.clone(camera.position, cameraPosition);
  490. var target = object._zoomWorldPosition;
  491. var targetNormal = scratchTargetNormal;
  492. targetNormal = Cartesian3.normalize(target, targetNormal);
  493. if (Cartesian3.dot(targetNormal, cameraPositionNormal) < 0.0) {
  494. return;
  495. }
  496. var center = scratchCenter;
  497. var forward = scratchForwardNormal;
  498. Cartesian3.clone(camera.direction, forward);
  499. Cartesian3.add(cameraPosition, Cartesian3.multiplyByScalar(forward, 1000, scratchCartesian), center);
  500. var positionToTarget = scratchPositionToTarget;
  501. var positionToTargetNormal = scratchPositionToTargetNormal;
  502. Cartesian3.subtract(target, cameraPosition, positionToTarget);
  503. Cartesian3.normalize(positionToTarget, positionToTargetNormal);
  504. var alphaDot = Cartesian3.dot(cameraPositionNormal, positionToTargetNormal);
  505. if (alphaDot >= 0.0) {
  506. // We zoomed past the target, and this zoom is not valid anymore.
  507. // This line causes the next zoom movement to pick a new starting point.
  508. object._zoomMouseStart.x = -1;
  509. return;
  510. }
  511. var alpha = Math.acos(-alphaDot);
  512. var cameraDistance = Cartesian3.magnitude( cameraPosition );
  513. var targetDistance = Cartesian3.magnitude( target );
  514. var remainingDistance = cameraDistance - distance;
  515. var positionToTargetDistance = Cartesian3.magnitude(positionToTarget);
  516. var gamma = Math.asin( CesiumMath.clamp( positionToTargetDistance / targetDistance * Math.sin(alpha), -1.0, 1.0 ) );
  517. var delta = Math.asin( CesiumMath.clamp( remainingDistance / targetDistance * Math.sin(alpha), -1.0, 1.0 ) );
  518. var beta = gamma - delta + alpha;
  519. var up = scratchCameraUpNormal;
  520. Cartesian3.normalize(cameraPosition, up);
  521. var right = scratchCameraRightNormal;
  522. right = Cartesian3.cross(positionToTargetNormal, up, right);
  523. right = Cartesian3.normalize(right, right );
  524. Cartesian3.normalize( Cartesian3.cross(up, right, scratchCartesian), forward );
  525. // Calculate new position to move to
  526. Cartesian3.multiplyByScalar(Cartesian3.normalize(center, scratchCartesian), (Cartesian3.magnitude(center) - distance), center);
  527. Cartesian3.normalize(cameraPosition, cameraPosition);
  528. Cartesian3.multiplyByScalar(cameraPosition, remainingDistance, cameraPosition);
  529. // Pan
  530. var pMid = scratchPan;
  531. Cartesian3.multiplyByScalar(Cartesian3.add(
  532. Cartesian3.multiplyByScalar(up, Math.cos(beta) - 1, scratchCartesianTwo),
  533. Cartesian3.multiplyByScalar(forward, Math.sin(beta), scratchCartesianThree),
  534. scratchCartesian
  535. ), remainingDistance, pMid);
  536. Cartesian3.add(cameraPosition, pMid, cameraPosition);
  537. Cartesian3.normalize(center, up);
  538. Cartesian3.normalize( Cartesian3.cross(up, right, scratchCartesian), forward );
  539. var cMid = scratchCenterMovement;
  540. Cartesian3.multiplyByScalar(Cartesian3.add(
  541. Cartesian3.multiplyByScalar(up, Math.cos(beta) - 1, scratchCartesianTwo),
  542. Cartesian3.multiplyByScalar(forward, Math.sin(beta), scratchCartesianThree),
  543. scratchCartesian
  544. ), Cartesian3.magnitude(center), cMid);
  545. Cartesian3.add(center, cMid, center);
  546. // Update camera
  547. // Set new position
  548. Cartesian3.clone(cameraPosition, camera.position);
  549. // Set new direction
  550. Cartesian3.normalize(Cartesian3.subtract(center, cameraPosition, scratchCartesian), camera.direction);
  551. Cartesian3.clone(camera.direction, camera.direction);
  552. // Set new right & up vectors
  553. Cartesian3.cross(camera.direction, camera.up, camera.right);
  554. Cartesian3.cross(camera.right, camera.direction, camera.up);
  555. camera.setView(scratchZoomViewOptions);
  556. return;
  557. }
  558. if (defined(centerPosition)) {
  559. var positionNormal = Cartesian3.normalize(centerPosition, scratchPositionNormal);
  560. var pickedNormal = Cartesian3.normalize(object._zoomWorldPosition, scratchPickNormal);
  561. var dotProduct = Cartesian3.dot(pickedNormal, positionNormal);
  562. if (dotProduct > 0.0 && dotProduct < 1.0) {
  563. var angle = CesiumMath.acosClamped(dotProduct);
  564. var axis = Cartesian3.cross(pickedNormal, positionNormal, scratchZoomAxis);
  565. var denom = Math.abs(angle) > CesiumMath.toRadians(20.0) ? camera.positionCartographic.height * 0.75 : camera.positionCartographic.height - distance;
  566. var scalar = distance / denom;
  567. camera.rotate(axis, angle * scalar);
  568. }
  569. } else {
  570. zoomOnVector = true;
  571. }
  572. }
  573. }
  574. object._rotatingZoom = !zoomOnVector;
  575. }
  576. if ((!sameStartPosition && zoomOnVector) || zoomingOnVector) {
  577. var ray;
  578. var zoomMouseStart = SceneTransforms.wgs84ToWindowCoordinates(scene, object._zoomWorldPosition, scratchZoomOffset);
  579. if (mode !== SceneMode.COLUMBUS_VIEW && Cartesian2.equals(startPosition, object._zoomMouseStart) && defined(zoomMouseStart)) {
  580. ray = camera.getPickRay(zoomMouseStart, scratchZoomPickRay);
  581. } else {
  582. ray = camera.getPickRay(startPosition, scratchZoomPickRay);
  583. }
  584. var rayDirection = ray.direction;
  585. if (mode === SceneMode.COLUMBUS_VIEW || mode === SceneMode.SCENE2D) {
  586. Cartesian3.fromElements(rayDirection.y, rayDirection.z, rayDirection.x, rayDirection);
  587. }
  588. camera.move(rayDirection, distance);
  589. object._zoomingOnVector = true;
  590. } else {
  591. camera.zoomIn(distance);
  592. }
  593. camera.setView(scratchZoomViewOptions);
  594. }
  595. var translate2DStart = new Ray();
  596. var translate2DEnd = new Ray();
  597. var scratchTranslateP0 = new Cartesian3();
  598. function translate2D(controller, startPosition, movement) {
  599. var scene = controller._scene;
  600. var camera = scene.camera;
  601. var start = camera.getPickRay(movement.startPosition, translate2DStart).origin;
  602. var end = camera.getPickRay(movement.endPosition, translate2DEnd).origin;
  603. start = Cartesian3.fromElements(start.y, start.z, start.x, start);
  604. end = Cartesian3.fromElements(end.y, end.z, end.x, end);
  605. var direction = Cartesian3.subtract(start, end, scratchTranslateP0);
  606. var distance = Cartesian3.magnitude(direction);
  607. if (distance > 0.0) {
  608. Cartesian3.normalize(direction, direction);
  609. camera.move(direction, distance);
  610. }
  611. }
  612. function zoom2D(controller, startPosition, movement) {
  613. if (defined(movement.distance)) {
  614. movement = movement.distance;
  615. }
  616. var scene = controller._scene;
  617. var camera = scene.camera;
  618. handleZoom(controller, startPosition, movement, controller._zoomFactor, camera.getMagnitude());
  619. }
  620. var twist2DStart = new Cartesian2();
  621. var twist2DEnd = new Cartesian2();
  622. function twist2D(controller, startPosition, movement) {
  623. if (defined(movement.angleAndHeight)) {
  624. singleAxisTwist2D(controller, startPosition, movement.angleAndHeight);
  625. return;
  626. }
  627. var scene = controller._scene;
  628. var camera = scene.camera;
  629. var canvas = scene.canvas;
  630. var width = canvas.clientWidth;
  631. var height = canvas.clientHeight;
  632. var start = twist2DStart;
  633. start.x = (2.0 / width) * movement.startPosition.x - 1.0;
  634. start.y = (2.0 / height) * (height - movement.startPosition.y) - 1.0;
  635. start = Cartesian2.normalize(start, start);
  636. var end = twist2DEnd;
  637. end.x = (2.0 / width) * movement.endPosition.x - 1.0;
  638. end.y = (2.0 / height) * (height - movement.endPosition.y) - 1.0;
  639. end = Cartesian2.normalize(end, end);
  640. var startTheta = CesiumMath.acosClamped(start.x);
  641. if (start.y < 0) {
  642. startTheta = CesiumMath.TWO_PI - startTheta;
  643. }
  644. var endTheta = CesiumMath.acosClamped(end.x);
  645. if (end.y < 0) {
  646. endTheta = CesiumMath.TWO_PI - endTheta;
  647. }
  648. var theta = endTheta - startTheta;
  649. camera.twistRight(theta);
  650. }
  651. function singleAxisTwist2D(controller, startPosition, movement) {
  652. var rotateRate = controller._rotateFactor * controller._rotateRateRangeAdjustment;
  653. if (rotateRate > controller._maximumRotateRate) {
  654. rotateRate = controller._maximumRotateRate;
  655. }
  656. if (rotateRate < controller._minimumRotateRate) {
  657. rotateRate = controller._minimumRotateRate;
  658. }
  659. var scene = controller._scene;
  660. var camera = scene.camera;
  661. var canvas = scene.canvas;
  662. var phiWindowRatio = (movement.endPosition.x - movement.startPosition.x) / canvas.clientWidth;
  663. phiWindowRatio = Math.min(phiWindowRatio, controller.maximumMovementRatio);
  664. var deltaPhi = rotateRate * phiWindowRatio * Math.PI * 4.0;
  665. camera.twistRight(deltaPhi);
  666. }
  667. function update2D(controller) {
  668. var rotatable2D = controller._scene.mapMode2D === MapMode2D.ROTATE;
  669. if (!Matrix4.equals(Matrix4.IDENTITY, controller._scene.camera.transform)) {
  670. reactToInput(controller, controller.enableZoom, controller.zoomEventTypes, zoom2D, controller.inertiaZoom, '_lastInertiaZoomMovement');
  671. if (rotatable2D) {
  672. reactToInput(controller, controller.enableRotate, controller.translateEventTypes, twist2D, controller.inertiaSpin, '_lastInertiaSpinMovement');
  673. }
  674. } else {
  675. reactToInput(controller, controller.enableTranslate, controller.translateEventTypes, translate2D, controller.inertiaTranslate, '_lastInertiaTranslateMovement');
  676. reactToInput(controller, controller.enableZoom, controller.zoomEventTypes, zoom2D, controller.inertiaZoom, '_lastInertiaZoomMovement');
  677. if (rotatable2D) {
  678. reactToInput(controller, controller.enableRotate, controller.tiltEventTypes, twist2D, controller.inertiaSpin, '_lastInertiaTiltMovement');
  679. }
  680. }
  681. }
  682. var pickGlobeScratchRay = new Ray();
  683. var scratchDepthIntersection = new Cartesian3();
  684. var scratchRayIntersection = new Cartesian3();
  685. function pickGlobe(controller, mousePosition, result) {
  686. var scene = controller._scene;
  687. var globe = controller._globe;
  688. var camera = scene.camera;
  689. if (!defined(globe)) {
  690. return undefined;
  691. }
  692. var depthIntersection;
  693. if (scene.pickPositionSupported) {
  694. depthIntersection = scene.pickPositionWorldCoordinates(mousePosition, scratchDepthIntersection);
  695. }
  696. var ray = camera.getPickRay(mousePosition, pickGlobeScratchRay);
  697. var rayIntersection = globe.pickWorldCoordinates(ray, scene, scratchRayIntersection);
  698. var pickDistance = defined(depthIntersection) ? Cartesian3.distance(depthIntersection, camera.positionWC) : Number.POSITIVE_INFINITY;
  699. var rayDistance = defined(rayIntersection) ? Cartesian3.distance(rayIntersection, camera.positionWC) : Number.POSITIVE_INFINITY;
  700. if (pickDistance < rayDistance) {
  701. return Cartesian3.clone(depthIntersection, result);
  702. }
  703. return Cartesian3.clone(rayIntersection, result);
  704. }
  705. var translateCVStartRay = new Ray();
  706. var translateCVEndRay = new Ray();
  707. var translateCVStartPos = new Cartesian3();
  708. var translateCVEndPos = new Cartesian3();
  709. var translatCVDifference = new Cartesian3();
  710. var translateCVOrigin = new Cartesian3();
  711. var translateCVPlane = new Plane(Cartesian3.UNIT_X, 0.0);
  712. var translateCVStartMouse = new Cartesian2();
  713. var translateCVEndMouse = new Cartesian2();
  714. function translateCV(controller, startPosition, movement) {
  715. if (!Cartesian3.equals(startPosition, controller._translateMousePosition)) {
  716. controller._looking = false;
  717. }
  718. if (!Cartesian3.equals(startPosition, controller._strafeMousePosition)) {
  719. controller._strafing = false;
  720. }
  721. if (controller._looking) {
  722. look3D(controller, startPosition, movement);
  723. return;
  724. }
  725. if (controller._strafing) {
  726. strafe(controller, startPosition, movement);
  727. return;
  728. }
  729. var scene = controller._scene;
  730. var camera = scene.camera;
  731. var startMouse = Cartesian2.clone(movement.startPosition, translateCVStartMouse);
  732. var endMouse = Cartesian2.clone(movement.endPosition, translateCVEndMouse);
  733. var startRay = camera.getPickRay(startMouse, translateCVStartRay);
  734. var origin = Cartesian3.clone(Cartesian3.ZERO, translateCVOrigin);
  735. var normal = Cartesian3.UNIT_X;
  736. var globePos;
  737. if (camera.position.z < controller._minimumPickingTerrainHeight) {
  738. globePos = pickGlobe(controller, startMouse, translateCVStartPos);
  739. if (defined(globePos)) {
  740. origin.x = globePos.x;
  741. }
  742. }
  743. if (origin.x > camera.position.z && defined(globePos)) {
  744. Cartesian3.clone(globePos, controller._strafeStartPosition);
  745. controller._strafing = true;
  746. strafe(controller, startPosition, movement);
  747. controller._strafeMousePosition = Cartesian2.clone(startPosition, controller._strafeMousePosition);
  748. return;
  749. }
  750. var plane = Plane.fromPointNormal(origin, normal, translateCVPlane);
  751. startRay = camera.getPickRay(startMouse, translateCVStartRay);
  752. var startPlanePos = IntersectionTests.rayPlane(startRay, plane, translateCVStartPos);
  753. var endRay = camera.getPickRay(endMouse, translateCVEndRay);
  754. var endPlanePos = IntersectionTests.rayPlane(endRay, plane, translateCVEndPos);
  755. if (!defined(startPlanePos) || !defined(endPlanePos)) {
  756. controller._looking = true;
  757. look3D(controller, startPosition, movement);
  758. Cartesian2.clone(startPosition, controller._translateMousePosition);
  759. return;
  760. }
  761. var diff = Cartesian3.subtract(startPlanePos, endPlanePos, translatCVDifference);
  762. var temp = diff.x;
  763. diff.x = diff.y;
  764. diff.y = diff.z;
  765. diff.z = temp;
  766. var mag = Cartesian3.magnitude(diff);
  767. if (mag > CesiumMath.EPSILON6) {
  768. Cartesian3.normalize(diff, diff);
  769. camera.move(diff, mag);
  770. }
  771. }
  772. var rotateCVWindowPos = new Cartesian2();
  773. var rotateCVWindowRay = new Ray();
  774. var rotateCVCenter = new Cartesian3();
  775. var rotateCVVerticalCenter = new Cartesian3();
  776. var rotateCVTransform = new Matrix4();
  777. var rotateCVVerticalTransform = new Matrix4();
  778. var rotateCVOrigin = new Cartesian3();
  779. var rotateCVPlane = new Plane(Cartesian3.UNIT_X, 0.0);
  780. var rotateCVCartesian3 = new Cartesian3();
  781. var rotateCVCart = new Cartographic();
  782. var rotateCVOldTransform = new Matrix4();
  783. var rotateCVQuaternion = new Quaternion();
  784. var rotateCVMatrix = new Matrix3();
  785. var tilt3DCartesian3 = new Cartesian3();
  786. function rotateCV(controller, startPosition, movement) {
  787. if (defined(movement.angleAndHeight)) {
  788. movement = movement.angleAndHeight;
  789. }
  790. if (!Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
  791. controller._tiltCVOffMap = false;
  792. controller._looking = false;
  793. }
  794. if (controller._looking) {
  795. look3D(controller, startPosition, movement);
  796. return;
  797. }
  798. var scene = controller._scene;
  799. var camera = scene.camera;
  800. var maxCoord = controller._maxCoord;
  801. var onMap = Math.abs(camera.position.x) - maxCoord.x < 0 && Math.abs(camera.position.y) - maxCoord.y < 0;
  802. if (controller._tiltCVOffMap || !onMap || camera.position.z > controller._minimumPickingTerrainHeight) {
  803. controller._tiltCVOffMap = true;
  804. rotateCVOnPlane(controller, startPosition, movement);
  805. } else {
  806. rotateCVOnTerrain(controller, startPosition, movement);
  807. }
  808. }
  809. function rotateCVOnPlane(controller, startPosition, movement) {
  810. var scene = controller._scene;
  811. var camera = scene.camera;
  812. var canvas = scene.canvas;
  813. var windowPosition = rotateCVWindowPos;
  814. windowPosition.x = canvas.clientWidth / 2;
  815. windowPosition.y = canvas.clientHeight / 2;
  816. var ray = camera.getPickRay(windowPosition, rotateCVWindowRay);
  817. var normal = Cartesian3.UNIT_X;
  818. var position = ray.origin;
  819. var direction = ray.direction;
  820. var scalar;
  821. var normalDotDirection = Cartesian3.dot(normal, direction);
  822. if (Math.abs(normalDotDirection) > CesiumMath.EPSILON6) {
  823. scalar = -Cartesian3.dot(normal, position) / normalDotDirection;
  824. }
  825. if (!defined(scalar) || scalar <= 0.0) {
  826. controller._looking = true;
  827. look3D(controller, startPosition, movement);
  828. Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
  829. return;
  830. }
  831. var center = Cartesian3.multiplyByScalar(direction, scalar, rotateCVCenter);
  832. Cartesian3.add(position, center, center);
  833. var projection = scene.mapProjection;
  834. var ellipsoid = projection.ellipsoid;
  835. Cartesian3.fromElements(center.y, center.z, center.x, center);
  836. var cart = projection.unproject(center, rotateCVCart);
  837. ellipsoid.cartographicToCartesian(cart, center);
  838. var transform = Transforms.eastNorthUpToFixedFrame(center, ellipsoid, rotateCVTransform);
  839. var oldGlobe = controller._globe;
  840. var oldEllipsoid = controller._ellipsoid;
  841. controller._globe = undefined;
  842. controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
  843. controller._rotateFactor = 1.0;
  844. controller._rotateRateRangeAdjustment = 1.0;
  845. var oldTransform = Matrix4.clone(camera.transform, rotateCVOldTransform);
  846. camera._setTransform(transform);
  847. rotate3D(controller, startPosition, movement, Cartesian3.UNIT_Z);
  848. camera._setTransform(oldTransform);
  849. controller._globe = oldGlobe;
  850. controller._ellipsoid = oldEllipsoid;
  851. var radius = oldEllipsoid.maximumRadius;
  852. controller._rotateFactor = 1.0 / radius;
  853. controller._rotateRateRangeAdjustment = radius;
  854. }
  855. function rotateCVOnTerrain(controller, startPosition, movement) {
  856. var scene = controller._scene;
  857. var camera = scene.camera;
  858. var center;
  859. var ray;
  860. var normal = Cartesian3.UNIT_X;
  861. if (Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
  862. center = Cartesian3.clone(controller._tiltCenter, rotateCVCenter);
  863. } else {
  864. if (camera.position.z < controller._minimumPickingTerrainHeight) {
  865. center = pickGlobe(controller, startPosition, rotateCVCenter);
  866. }
  867. if (!defined(center)) {
  868. ray = camera.getPickRay(startPosition, rotateCVWindowRay);
  869. var position = ray.origin;
  870. var direction = ray.direction;
  871. var scalar;
  872. var normalDotDirection = Cartesian3.dot(normal, direction);
  873. if (Math.abs(normalDotDirection) > CesiumMath.EPSILON6) {
  874. scalar = -Cartesian3.dot(normal, position) / normalDotDirection;
  875. }
  876. if (!defined(scalar) || scalar <= 0.0) {
  877. controller._looking = true;
  878. look3D(controller, startPosition, movement);
  879. Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
  880. return;
  881. }
  882. center = Cartesian3.multiplyByScalar(direction, scalar, rotateCVCenter);
  883. Cartesian3.add(position, center, center);
  884. }
  885. Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
  886. Cartesian3.clone(center, controller._tiltCenter);
  887. }
  888. var canvas = scene.canvas;
  889. var windowPosition = rotateCVWindowPos;
  890. windowPosition.x = canvas.clientWidth / 2;
  891. windowPosition.y = controller._tiltCenterMousePosition.y;
  892. ray = camera.getPickRay(windowPosition, rotateCVWindowRay);
  893. var origin = Cartesian3.clone(Cartesian3.ZERO, rotateCVOrigin);
  894. origin.x = center.x;
  895. var plane = Plane.fromPointNormal(origin, normal, rotateCVPlane);
  896. var verticalCenter = IntersectionTests.rayPlane(ray, plane, rotateCVVerticalCenter);
  897. var projection = camera._projection;
  898. var ellipsoid = projection.ellipsoid;
  899. Cartesian3.fromElements(center.y, center.z, center.x, center);
  900. var cart = projection.unproject(center, rotateCVCart);
  901. ellipsoid.cartographicToCartesian(cart, center);
  902. var transform = Transforms.eastNorthUpToFixedFrame(center, ellipsoid, rotateCVTransform);
  903. var verticalTransform;
  904. if (defined(verticalCenter)) {
  905. Cartesian3.fromElements(verticalCenter.y, verticalCenter.z, verticalCenter.x, verticalCenter);
  906. cart = projection.unproject(verticalCenter, rotateCVCart);
  907. ellipsoid.cartographicToCartesian(cart, verticalCenter);
  908. verticalTransform = Transforms.eastNorthUpToFixedFrame(verticalCenter, ellipsoid, rotateCVVerticalTransform);
  909. } else {
  910. verticalTransform = transform;
  911. }
  912. var oldGlobe = controller._globe;
  913. var oldEllipsoid = controller._ellipsoid;
  914. controller._globe = undefined;
  915. controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
  916. controller._rotateFactor = 1.0;
  917. controller._rotateRateRangeAdjustment = 1.0;
  918. var constrainedAxis = Cartesian3.UNIT_Z;
  919. var oldTransform = Matrix4.clone(camera.transform, rotateCVOldTransform);
  920. camera._setTransform(transform);
  921. var tangent = Cartesian3.cross(Cartesian3.UNIT_Z, Cartesian3.normalize(camera.position, rotateCVCartesian3), rotateCVCartesian3);
  922. var dot = Cartesian3.dot(camera.right, tangent);
  923. rotate3D(controller, startPosition, movement, constrainedAxis, false, true);
  924. camera._setTransform(verticalTransform);
  925. if (dot < 0.0) {
  926. if (movement.startPosition.y > movement.endPosition.y) {
  927. constrainedAxis = undefined;
  928. }
  929. var oldConstrainedAxis = camera.constrainedAxis;
  930. camera.constrainedAxis = undefined;
  931. rotate3D(controller, startPosition, movement, constrainedAxis, true, false);
  932. camera.constrainedAxis = oldConstrainedAxis;
  933. } else {
  934. rotate3D(controller, startPosition, movement, constrainedAxis, true, false);
  935. }
  936. if (defined(camera.constrainedAxis)) {
  937. var right = Cartesian3.cross(camera.direction, camera.constrainedAxis, tilt3DCartesian3);
  938. if (!Cartesian3.equalsEpsilon(right, Cartesian3.ZERO, CesiumMath.EPSILON6)) {
  939. if (Cartesian3.dot(right, camera.right) < 0.0) {
  940. Cartesian3.negate(right, right);
  941. }
  942. Cartesian3.cross(right, camera.direction, camera.up);
  943. Cartesian3.cross(camera.direction, camera.up, camera.right);
  944. Cartesian3.normalize(camera.up, camera.up);
  945. Cartesian3.normalize(camera.right, camera.right);
  946. }
  947. }
  948. camera._setTransform(oldTransform);
  949. controller._globe = oldGlobe;
  950. controller._ellipsoid = oldEllipsoid;
  951. var radius = oldEllipsoid.maximumRadius;
  952. controller._rotateFactor = 1.0 / radius;
  953. controller._rotateRateRangeAdjustment = radius;
  954. var originalPosition = Cartesian3.clone(camera.positionWC, rotateCVCartesian3);
  955. camera._adjustHeightForTerrain();
  956. if (!Cartesian3.equals(camera.positionWC, originalPosition)) {
  957. camera._setTransform(verticalTransform);
  958. camera.worldToCameraCoordinatesPoint(originalPosition, originalPosition);
  959. var magSqrd = Cartesian3.magnitudeSquared(originalPosition);
  960. if (Cartesian3.magnitudeSquared(camera.position) > magSqrd) {
  961. Cartesian3.normalize(camera.position, camera.position);
  962. Cartesian3.multiplyByScalar(camera.position, Math.sqrt(magSqrd), camera.position);
  963. }
  964. var angle = Cartesian3.angleBetween(originalPosition, camera.position);
  965. var axis = Cartesian3.cross(originalPosition, camera.position, originalPosition);
  966. Cartesian3.normalize(axis, axis);
  967. var quaternion = Quaternion.fromAxisAngle(axis, angle, rotateCVQuaternion);
  968. var rotation = Matrix3.fromQuaternion(quaternion, rotateCVMatrix);
  969. Matrix3.multiplyByVector(rotation, camera.direction, camera.direction);
  970. Matrix3.multiplyByVector(rotation, camera.up, camera.up);
  971. Cartesian3.cross(camera.direction, camera.up, camera.right);
  972. Cartesian3.cross(camera.right, camera.direction, camera.up);
  973. camera._setTransform(oldTransform);
  974. }
  975. }
  976. var zoomCVWindowPos = new Cartesian2();
  977. var zoomCVWindowRay = new Ray();
  978. var zoomCVIntersection = new Cartesian3();
  979. function zoomCV(controller, startPosition, movement) {
  980. if (defined(movement.distance)) {
  981. movement = movement.distance;
  982. }
  983. var scene = controller._scene;
  984. var camera = scene.camera;
  985. var canvas = scene.canvas;
  986. var windowPosition = zoomCVWindowPos;
  987. windowPosition.x = canvas.clientWidth / 2;
  988. windowPosition.y = canvas.clientHeight / 2;
  989. var ray = camera.getPickRay(windowPosition, zoomCVWindowRay);
  990. var intersection;
  991. if (camera.position.z < controller._minimumPickingTerrainHeight) {
  992. intersection = pickGlobe(controller, windowPosition, zoomCVIntersection);
  993. }
  994. var distance;
  995. if (defined(intersection)) {
  996. distance = Cartesian3.distance(ray.origin, intersection);
  997. } else {
  998. var normal = Cartesian3.UNIT_X;
  999. var position = ray.origin;
  1000. var direction = ray.direction;
  1001. distance = -Cartesian3.dot(normal, position) / Cartesian3.dot(normal, direction);
  1002. }
  1003. handleZoom(controller, startPosition, movement, controller._zoomFactor, distance);
  1004. }
  1005. function updateCV(controller) {
  1006. var scene = controller._scene;
  1007. var camera = scene.camera;
  1008. if (!Matrix4.equals(Matrix4.IDENTITY, camera.transform)) {
  1009. reactToInput(controller, controller.enableRotate, controller.rotateEventTypes, rotate3D, controller.inertiaSpin, '_lastInertiaSpinMovement');
  1010. reactToInput(controller, controller.enableZoom, controller.zoomEventTypes, zoom3D, controller.inertiaZoom, '_lastInertiaZoomMovement');
  1011. } else {
  1012. var tweens = controller._tweens;
  1013. if (controller._aggregator.anyButtonDown) {
  1014. tweens.removeAll();
  1015. }
  1016. reactToInput(controller, controller.enableTilt, controller.tiltEventTypes, rotateCV, controller.inertiaSpin, '_lastInertiaTiltMovement');
  1017. reactToInput(controller, controller.enableTranslate, controller.translateEventTypes, translateCV, controller.inertiaTranslate, '_lastInertiaTranslateMovement');
  1018. reactToInput(controller, controller.enableZoom, controller.zoomEventTypes, zoomCV, controller.inertiaZoom, '_lastInertiaZoomMovement');
  1019. reactToInput(controller, controller.enableLook, controller.lookEventTypes, look3D);
  1020. if (!controller._aggregator.anyButtonDown &&
  1021. (!defined(controller._lastInertiaZoomMovement) || !controller._lastInertiaZoomMovement.active) &&
  1022. (!defined(controller._lastInertiaTranslateMovement) || !controller._lastInertiaTranslateMovement.active) &&
  1023. !tweens.contains(controller._tween)) {
  1024. var tween = camera.createCorrectPositionTween(controller.bounceAnimationTime);
  1025. if (defined(tween)) {
  1026. controller._tween = tweens.add(tween);
  1027. }
  1028. }
  1029. tweens.update();
  1030. }
  1031. }
  1032. var scratchStrafeRay = new Ray();
  1033. var scratchStrafePlane = new Plane(Cartesian3.UNIT_X, 0.0);
  1034. var scratchStrafeIntersection = new Cartesian3();
  1035. var scratchStrafeDirection = new Cartesian3();
  1036. var scratchMousePos = new Cartesian3();
  1037. function strafe(controller, startPosition, movement) {
  1038. var scene = controller._scene;
  1039. var camera = scene.camera;
  1040. var mouseStartPosition = pickGlobe(controller, movement.startPosition, scratchMousePos);
  1041. if (!defined(mouseStartPosition)) {
  1042. return;
  1043. }
  1044. var mousePosition = movement.endPosition;
  1045. var ray = camera.getPickRay(mousePosition, scratchStrafeRay);
  1046. var direction = Cartesian3.clone(camera.direction, scratchStrafeDirection);
  1047. if (scene.mode === SceneMode.COLUMBUS_VIEW) {
  1048. Cartesian3.fromElements(direction.z, direction.x, direction.y, direction);
  1049. }
  1050. var plane = Plane.fromPointNormal(mouseStartPosition, direction, scratchStrafePlane);
  1051. var intersection = IntersectionTests.rayPlane(ray, plane, scratchStrafeIntersection);
  1052. if (!defined(intersection)) {
  1053. return;
  1054. }
  1055. direction = Cartesian3.subtract(mouseStartPosition, intersection, direction);
  1056. if (scene.mode === SceneMode.COLUMBUS_VIEW) {
  1057. Cartesian3.fromElements(direction.y, direction.z, direction.x, direction);
  1058. }
  1059. Cartesian3.add(camera.position, direction, camera.position);
  1060. }
  1061. var spin3DPick = new Cartesian3();
  1062. var scratchCartographic = new Cartographic();
  1063. var scratchRadii = new Cartesian3();
  1064. var scratchEllipsoid = new Ellipsoid();
  1065. var scratchLookUp = new Cartesian3();
  1066. function spin3D(controller, startPosition, movement) {
  1067. var scene = controller._scene;
  1068. var camera = scene.camera;
  1069. if (!Matrix4.equals(camera.transform, Matrix4.IDENTITY)) {
  1070. rotate3D(controller, startPosition, movement);
  1071. return;
  1072. }
  1073. var magnitude;
  1074. var radii;
  1075. var ellipsoid;
  1076. var up = controller._ellipsoid.geodeticSurfaceNormal(camera.position, scratchLookUp);
  1077. var height = controller._ellipsoid.cartesianToCartographic(camera.positionWC, scratchCartographic).height;
  1078. var globe = controller._globe;
  1079. var mousePos;
  1080. var tangentPick = false;
  1081. if (defined(globe) && height < controller._minimumPickingTerrainHeight) {
  1082. mousePos = pickGlobe(controller, movement.startPosition, scratchMousePos);
  1083. if (defined(mousePos)) {
  1084. var ray = camera.getPickRay(movement.startPosition, pickGlobeScratchRay);
  1085. var normal = controller._ellipsoid.geodeticSurfaceNormal(mousePos);
  1086. tangentPick = Math.abs(Cartesian3.dot(ray.direction, normal)) < 0.05;
  1087. if (tangentPick && !controller._looking) {
  1088. controller._rotating = false;
  1089. controller._strafing = true;
  1090. }
  1091. }
  1092. }
  1093. if (Cartesian2.equals(startPosition, controller._rotateMousePosition)) {
  1094. if (controller._looking) {
  1095. look3D(controller, startPosition, movement, up);
  1096. } else if (controller._rotating) {
  1097. rotate3D(controller, startPosition, movement);
  1098. } else if (controller._strafing) {
  1099. Cartesian3.clone(mousePos, controller._strafeStartPosition);
  1100. strafe(controller, startPosition, movement);
  1101. } else {
  1102. magnitude = Cartesian3.magnitude(controller._rotateStartPosition);
  1103. radii = scratchRadii;
  1104. radii.x = radii.y = radii.z = magnitude;
  1105. ellipsoid = Ellipsoid.fromCartesian3(radii, scratchEllipsoid);
  1106. pan3D(controller, startPosition, movement, ellipsoid);
  1107. }
  1108. return;
  1109. }
  1110. controller._looking = false;
  1111. controller._rotating = false;
  1112. controller._strafing = false;
  1113. if (defined(globe) && height < controller._minimumPickingTerrainHeight) {
  1114. if (defined(mousePos)) {
  1115. if (Cartesian3.magnitude(camera.position) < Cartesian3.magnitude(mousePos)) {
  1116. Cartesian3.clone(mousePos, controller._strafeStartPosition);
  1117. controller._strafing = true;
  1118. strafe(controller, startPosition, movement);
  1119. } else {
  1120. magnitude = Cartesian3.magnitude(mousePos);
  1121. radii = scratchRadii;
  1122. radii.x = radii.y = radii.z = magnitude;
  1123. ellipsoid = Ellipsoid.fromCartesian3(radii, scratchEllipsoid);
  1124. pan3D(controller, startPosition, movement, ellipsoid);
  1125. Cartesian3.clone(mousePos, controller._rotateStartPosition);
  1126. }
  1127. } else {
  1128. controller._looking = true;
  1129. look3D(controller, startPosition, movement, up);
  1130. }
  1131. } else if (defined(camera.pickEllipsoid(movement.startPosition, controller._ellipsoid, spin3DPick))) {
  1132. pan3D(controller, startPosition, movement, controller._ellipsoid);
  1133. Cartesian3.clone(spin3DPick, controller._rotateStartPosition);
  1134. } else if (height > controller._minimumTrackBallHeight) {
  1135. controller._rotating = true;
  1136. rotate3D(controller, startPosition, movement);
  1137. } else {
  1138. controller._looking = true;
  1139. look3D(controller, startPosition, movement, up);
  1140. }
  1141. Cartesian2.clone(startPosition, controller._rotateMousePosition);
  1142. }
  1143. function rotate3D(controller, startPosition, movement, constrainedAxis, rotateOnlyVertical, rotateOnlyHorizontal) {
  1144. rotateOnlyVertical = defaultValue(rotateOnlyVertical, false);
  1145. rotateOnlyHorizontal = defaultValue(rotateOnlyHorizontal, false);
  1146. var scene = controller._scene;
  1147. var camera = scene.camera;
  1148. var canvas = scene.canvas;
  1149. var oldAxis = camera.constrainedAxis;
  1150. if (defined(constrainedAxis)) {
  1151. camera.constrainedAxis = constrainedAxis;
  1152. }
  1153. var rho = Cartesian3.magnitude(camera.position);
  1154. var rotateRate = controller._rotateFactor * (rho - controller._rotateRateRangeAdjustment);
  1155. if (rotateRate > controller._maximumRotateRate) {
  1156. rotateRate = controller._maximumRotateRate;
  1157. }
  1158. if (rotateRate < controller._minimumRotateRate) {
  1159. rotateRate = controller._minimumRotateRate;
  1160. }
  1161. var phiWindowRatio = (movement.startPosition.x - movement.endPosition.x) / canvas.clientWidth;
  1162. var thetaWindowRatio = (movement.startPosition.y - movement.endPosition.y) / canvas.clientHeight;
  1163. phiWindowRatio = Math.min(phiWindowRatio, controller.maximumMovementRatio);
  1164. thetaWindowRatio = Math.min(thetaWindowRatio, controller.maximumMovementRatio);
  1165. var deltaPhi = rotateRate * phiWindowRatio * Math.PI * 2.0;
  1166. var deltaTheta = rotateRate * thetaWindowRatio * Math.PI;
  1167. if (!rotateOnlyVertical) {
  1168. camera.rotateRight(deltaPhi);
  1169. }
  1170. if (!rotateOnlyHorizontal) {
  1171. camera.rotateUp(deltaTheta);
  1172. }
  1173. camera.constrainedAxis = oldAxis;
  1174. }
  1175. var pan3DP0 = Cartesian4.clone(Cartesian4.UNIT_W);
  1176. var pan3DP1 = Cartesian4.clone(Cartesian4.UNIT_W);
  1177. var pan3DTemp0 = new Cartesian3();
  1178. var pan3DTemp1 = new Cartesian3();
  1179. var pan3DTemp2 = new Cartesian3();
  1180. var pan3DTemp3 = new Cartesian3();
  1181. var pan3DStartMousePosition = new Cartesian2();
  1182. var pan3DEndMousePosition = new Cartesian2();
  1183. function pan3D(controller, startPosition, movement, ellipsoid) {
  1184. var scene = controller._scene;
  1185. var camera = scene.camera;
  1186. var startMousePosition = Cartesian2.clone(movement.startPosition, pan3DStartMousePosition);
  1187. var endMousePosition = Cartesian2.clone(movement.endPosition, pan3DEndMousePosition);
  1188. var p0 = camera.pickEllipsoid(startMousePosition, ellipsoid, pan3DP0);
  1189. var p1 = camera.pickEllipsoid(endMousePosition, ellipsoid, pan3DP1);
  1190. if (!defined(p0) || !defined(p1)) {
  1191. controller._rotating = true;
  1192. rotate3D(controller, startPosition, movement);
  1193. return;
  1194. }
  1195. p0 = camera.worldToCameraCoordinates(p0, p0);
  1196. p1 = camera.worldToCameraCoordinates(p1, p1);
  1197. if (!defined(camera.constrainedAxis)) {
  1198. Cartesian3.normalize(p0, p0);
  1199. Cartesian3.normalize(p1, p1);
  1200. var dot = Cartesian3.dot(p0, p1);
  1201. var axis = Cartesian3.cross(p0, p1, pan3DTemp0);
  1202. if (dot < 1.0 && !Cartesian3.equalsEpsilon(axis, Cartesian3.ZERO, CesiumMath.EPSILON14)) { // dot is in [0, 1]
  1203. var angle = Math.acos(dot);
  1204. camera.rotate(axis, angle);
  1205. }
  1206. } else {
  1207. var basis0 = camera.constrainedAxis;
  1208. var basis1 = Cartesian3.mostOrthogonalAxis(basis0, pan3DTemp0);
  1209. Cartesian3.cross(basis1, basis0, basis1);
  1210. Cartesian3.normalize(basis1, basis1);
  1211. var basis2 = Cartesian3.cross(basis0, basis1, pan3DTemp1);
  1212. var startRho = Cartesian3.magnitude(p0);
  1213. var startDot = Cartesian3.dot(basis0, p0);
  1214. var startTheta = Math.acos(startDot / startRho);
  1215. var startRej = Cartesian3.multiplyByScalar(basis0, startDot, pan3DTemp2);
  1216. Cartesian3.subtract(p0, startRej, startRej);
  1217. Cartesian3.normalize(startRej, startRej);
  1218. var endRho = Cartesian3.magnitude(p1);
  1219. var endDot = Cartesian3.dot(basis0, p1);
  1220. var endTheta = Math.acos(endDot / endRho);
  1221. var endRej = Cartesian3.multiplyByScalar(basis0, endDot, pan3DTemp3);
  1222. Cartesian3.subtract(p1, endRej, endRej);
  1223. Cartesian3.normalize(endRej, endRej);
  1224. var startPhi = Math.acos(Cartesian3.dot(startRej, basis1));
  1225. if (Cartesian3.dot(startRej, basis2) < 0) {
  1226. startPhi = CesiumMath.TWO_PI - startPhi;
  1227. }
  1228. var endPhi = Math.acos(Cartesian3.dot(endRej, basis1));
  1229. if (Cartesian3.dot(endRej, basis2) < 0) {
  1230. endPhi = CesiumMath.TWO_PI - endPhi;
  1231. }
  1232. var deltaPhi = startPhi - endPhi;
  1233. var east;
  1234. if (Cartesian3.equalsEpsilon(basis0, camera.position, CesiumMath.EPSILON2)) {
  1235. east = camera.right;
  1236. } else {
  1237. east = Cartesian3.cross(basis0, camera.position, pan3DTemp0);
  1238. }
  1239. var planeNormal = Cartesian3.cross(basis0, east, pan3DTemp0);
  1240. var side0 = Cartesian3.dot(planeNormal, Cartesian3.subtract(p0, basis0, pan3DTemp1));
  1241. var side1 = Cartesian3.dot(planeNormal, Cartesian3.subtract(p1, basis0, pan3DTemp1));
  1242. var deltaTheta;
  1243. if (side0 > 0 && side1 > 0) {
  1244. deltaTheta = endTheta - startTheta;
  1245. } else if (side0 > 0 && side1 <= 0) {
  1246. if (Cartesian3.dot(camera.position, basis0) > 0) {
  1247. deltaTheta = -startTheta - endTheta;
  1248. } else {
  1249. deltaTheta = startTheta + endTheta;
  1250. }
  1251. } else {
  1252. deltaTheta = startTheta - endTheta;
  1253. }
  1254. camera.rotateRight(deltaPhi);
  1255. camera.rotateUp(deltaTheta);
  1256. }
  1257. }
  1258. var zoom3DUnitPosition = new Cartesian3();
  1259. var zoom3DCartographic = new Cartographic();
  1260. function zoom3D(controller, startPosition, movement) {
  1261. if (defined(movement.distance)) {
  1262. movement = movement.distance;
  1263. }
  1264. var ellipsoid = controller._ellipsoid;
  1265. var scene = controller._scene;
  1266. var camera = scene.camera;
  1267. var canvas = scene.canvas;
  1268. var windowPosition = zoomCVWindowPos;
  1269. windowPosition.x = canvas.clientWidth / 2;
  1270. windowPosition.y = canvas.clientHeight / 2;
  1271. var ray = camera.getPickRay(windowPosition, zoomCVWindowRay);
  1272. var intersection;
  1273. var height = ellipsoid.cartesianToCartographic(camera.position, zoom3DCartographic).height;
  1274. if (height < controller._minimumPickingTerrainHeight) {
  1275. intersection = pickGlobe(controller, windowPosition, zoomCVIntersection);
  1276. }
  1277. var distance;
  1278. if (defined(intersection)) {
  1279. distance = Cartesian3.distance(ray.origin, intersection);
  1280. } else {
  1281. distance = height;
  1282. }
  1283. var unitPosition = Cartesian3.normalize(camera.position, zoom3DUnitPosition);
  1284. handleZoom(controller, startPosition, movement, controller._zoomFactor, distance, Cartesian3.dot(unitPosition, camera.direction));
  1285. }
  1286. var tilt3DWindowPos = new Cartesian2();
  1287. var tilt3DRay = new Ray();
  1288. var tilt3DCenter = new Cartesian3();
  1289. var tilt3DVerticalCenter = new Cartesian3();
  1290. var tilt3DTransform = new Matrix4();
  1291. var tilt3DVerticalTransform = new Matrix4();
  1292. var tilt3DOldTransform = new Matrix4();
  1293. var tilt3DQuaternion = new Quaternion();
  1294. var tilt3DMatrix = new Matrix3();
  1295. var tilt3DCart = new Cartographic();
  1296. var tilt3DLookUp = new Cartesian3();
  1297. function tilt3D(controller, startPosition, movement) {
  1298. var scene = controller._scene;
  1299. var camera = scene.camera;
  1300. if (!Matrix4.equals(camera.transform, Matrix4.IDENTITY)) {
  1301. return;
  1302. }
  1303. if (defined(movement.angleAndHeight)) {
  1304. movement = movement.angleAndHeight;
  1305. }
  1306. if (!Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
  1307. controller._tiltOnEllipsoid = false;
  1308. controller._looking = false;
  1309. }
  1310. if (controller._looking) {
  1311. var up = controller._ellipsoid.geodeticSurfaceNormal(camera.position, tilt3DLookUp);
  1312. look3D(controller, startPosition, movement, up);
  1313. return;
  1314. }
  1315. var ellipsoid = controller._ellipsoid;
  1316. var cartographic = ellipsoid.cartesianToCartographic(camera.position, tilt3DCart);
  1317. if (controller._tiltOnEllipsoid || cartographic.height > controller._minimumCollisionTerrainHeight) {
  1318. controller._tiltOnEllipsoid = true;
  1319. tilt3DOnEllipsoid(controller, startPosition, movement);
  1320. } else {
  1321. tilt3DOnTerrain(controller, startPosition, movement);
  1322. }
  1323. }
  1324. var tilt3DOnEllipsoidCartographic = new Cartographic();
  1325. function tilt3DOnEllipsoid(controller, startPosition, movement) {
  1326. var ellipsoid = controller._ellipsoid;
  1327. var scene = controller._scene;
  1328. var camera = scene.camera;
  1329. var minHeight = controller.minimumZoomDistance * 0.25;
  1330. var height = ellipsoid.cartesianToCartographic(camera.positionWC, tilt3DOnEllipsoidCartographic).height;
  1331. if (height - minHeight - 1.0 < CesiumMath.EPSILON3 &&
  1332. movement.endPosition.y - movement.startPosition.y < 0) {
  1333. return;
  1334. }
  1335. var canvas = scene.canvas;
  1336. var windowPosition = tilt3DWindowPos;
  1337. windowPosition.x = canvas.clientWidth / 2;
  1338. windowPosition.y = canvas.clientHeight / 2;
  1339. var ray = camera.getPickRay(windowPosition, tilt3DRay);
  1340. var center;
  1341. var intersection = IntersectionTests.rayEllipsoid(ray, ellipsoid);
  1342. if (defined(intersection)) {
  1343. center = Ray.getPoint(ray, intersection.start, tilt3DCenter);
  1344. } else if (height > controller._minimumTrackBallHeight) {
  1345. var grazingAltitudeLocation = IntersectionTests.grazingAltitudeLocation(ray, ellipsoid);
  1346. if (!defined(grazingAltitudeLocation)) {
  1347. return;
  1348. }
  1349. var grazingAltitudeCart = ellipsoid.cartesianToCartographic(grazingAltitudeLocation, tilt3DCart);
  1350. grazingAltitudeCart.height = 0.0;
  1351. center = ellipsoid.cartographicToCartesian(grazingAltitudeCart, tilt3DCenter);
  1352. } else {
  1353. controller._looking = true;
  1354. var up = controller._ellipsoid.geodeticSurfaceNormal(camera.position, tilt3DLookUp);
  1355. look3D(controller, startPosition, movement, up);
  1356. Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
  1357. return;
  1358. }
  1359. var transform = Transforms.eastNorthUpToFixedFrame(center, ellipsoid, tilt3DTransform);
  1360. var oldGlobe = controller._globe;
  1361. var oldEllipsoid = controller._ellipsoid;
  1362. controller._globe = undefined;
  1363. controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
  1364. controller._rotateFactor = 1.0;
  1365. controller._rotateRateRangeAdjustment = 1.0;
  1366. var oldTransform = Matrix4.clone(camera.transform, tilt3DOldTransform);
  1367. camera._setTransform(transform);
  1368. rotate3D(controller, startPosition, movement, Cartesian3.UNIT_Z);
  1369. camera._setTransform(oldTransform);
  1370. controller._globe = oldGlobe;
  1371. controller._ellipsoid = oldEllipsoid;
  1372. var radius = oldEllipsoid.maximumRadius;
  1373. controller._rotateFactor = 1.0 / radius;
  1374. controller._rotateRateRangeAdjustment = radius;
  1375. }
  1376. function tilt3DOnTerrain(controller, startPosition, movement) {
  1377. var ellipsoid = controller._ellipsoid;
  1378. var scene = controller._scene;
  1379. var camera = scene.camera;
  1380. var center;
  1381. var ray;
  1382. var intersection;
  1383. if (Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
  1384. center = Cartesian3.clone(controller._tiltCenter, tilt3DCenter);
  1385. } else {
  1386. center = pickGlobe(controller, startPosition, tilt3DCenter);
  1387. if (!defined(center)) {
  1388. ray = camera.getPickRay(startPosition, tilt3DRay);
  1389. intersection = IntersectionTests.rayEllipsoid(ray, ellipsoid);
  1390. if (!defined(intersection)) {
  1391. var cartographic = ellipsoid.cartesianToCartographic(camera.position, tilt3DCart);
  1392. if (cartographic.height <= controller._minimumTrackBallHeight) {
  1393. controller._looking = true;
  1394. var up = controller._ellipsoid.geodeticSurfaceNormal(camera.position, tilt3DLookUp);
  1395. look3D(controller, startPosition, movement, up);
  1396. Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
  1397. }
  1398. return;
  1399. }
  1400. center = Ray.getPoint(ray, intersection.start, tilt3DCenter);
  1401. }
  1402. Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
  1403. Cartesian3.clone(center, controller._tiltCenter);
  1404. }
  1405. var canvas = scene.canvas;
  1406. var windowPosition = tilt3DWindowPos;
  1407. windowPosition.x = canvas.clientWidth / 2;
  1408. windowPosition.y = controller._tiltCenterMousePosition.y;
  1409. ray = camera.getPickRay(windowPosition, tilt3DRay);
  1410. var mag = Cartesian3.magnitude(center);
  1411. var radii = Cartesian3.fromElements(mag, mag, mag, scratchRadii);
  1412. var newEllipsoid = Ellipsoid.fromCartesian3(radii, scratchEllipsoid);
  1413. intersection = IntersectionTests.rayEllipsoid(ray, newEllipsoid);
  1414. if (!defined(intersection)) {
  1415. return;
  1416. }
  1417. var t = Cartesian3.magnitude(ray.origin) > mag ? intersection.start : intersection.stop;
  1418. var verticalCenter = Ray.getPoint(ray, t, tilt3DVerticalCenter);
  1419. var transform = Transforms.eastNorthUpToFixedFrame(center, ellipsoid, tilt3DTransform);
  1420. var verticalTransform = Transforms.eastNorthUpToFixedFrame(verticalCenter, newEllipsoid, tilt3DVerticalTransform);
  1421. var oldGlobe = controller._globe;
  1422. var oldEllipsoid = controller._ellipsoid;
  1423. controller._globe = undefined;
  1424. controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
  1425. controller._rotateFactor = 1.0;
  1426. controller._rotateRateRangeAdjustment = 1.0;
  1427. var constrainedAxis = Cartesian3.UNIT_Z;
  1428. var oldTransform = Matrix4.clone(camera.transform, tilt3DOldTransform);
  1429. camera._setTransform(transform);
  1430. var tangent = Cartesian3.cross(verticalCenter, camera.positionWC, tilt3DCartesian3);
  1431. var dot = Cartesian3.dot(camera.rightWC, tangent);
  1432. rotate3D(controller, startPosition, movement, constrainedAxis, false, true);
  1433. camera._setTransform(verticalTransform);
  1434. if (dot < 0.0) {
  1435. if (movement.startPosition.y > movement.endPosition.y) {
  1436. constrainedAxis = undefined;
  1437. }
  1438. var oldConstrainedAxis = camera.constrainedAxis;
  1439. camera.constrainedAxis = undefined;
  1440. rotate3D(controller, startPosition, movement, constrainedAxis, true, false);
  1441. camera.constrainedAxis = oldConstrainedAxis;
  1442. } else {
  1443. rotate3D(controller, startPosition, movement, constrainedAxis, true, false);
  1444. }
  1445. if (defined(camera.constrainedAxis)) {
  1446. var right = Cartesian3.cross(camera.direction, camera.constrainedAxis, tilt3DCartesian3);
  1447. if (!Cartesian3.equalsEpsilon(right, Cartesian3.ZERO, CesiumMath.EPSILON6)) {
  1448. if (Cartesian3.dot(right, camera.right) < 0.0) {
  1449. Cartesian3.negate(right, right);
  1450. }
  1451. Cartesian3.cross(right, camera.direction, camera.up);
  1452. Cartesian3.cross(camera.direction, camera.up, camera.right);
  1453. Cartesian3.normalize(camera.up, camera.up);
  1454. Cartesian3.normalize(camera.right, camera.right);
  1455. }
  1456. }
  1457. camera._setTransform(oldTransform);
  1458. controller._globe = oldGlobe;
  1459. controller._ellipsoid = oldEllipsoid;
  1460. var radius = oldEllipsoid.maximumRadius;
  1461. controller._rotateFactor = 1.0 / radius;
  1462. controller._rotateRateRangeAdjustment = radius;
  1463. var originalPosition = Cartesian3.clone(camera.positionWC, tilt3DCartesian3);
  1464. camera._adjustHeightForTerrain();
  1465. if (!Cartesian3.equals(camera.positionWC, originalPosition)) {
  1466. camera._setTransform(verticalTransform);
  1467. camera.worldToCameraCoordinatesPoint(originalPosition, originalPosition);
  1468. var magSqrd = Cartesian3.magnitudeSquared(originalPosition);
  1469. if (Cartesian3.magnitudeSquared(camera.position) > magSqrd) {
  1470. Cartesian3.normalize(camera.position, camera.position);
  1471. Cartesian3.multiplyByScalar(camera.position, Math.sqrt(magSqrd), camera.position);
  1472. }
  1473. var angle = Cartesian3.angleBetween(originalPosition, camera.position);
  1474. var axis = Cartesian3.cross(originalPosition, camera.position, originalPosition);
  1475. Cartesian3.normalize(axis, axis);
  1476. var quaternion = Quaternion.fromAxisAngle(axis, angle, tilt3DQuaternion);
  1477. var rotation = Matrix3.fromQuaternion(quaternion, tilt3DMatrix);
  1478. Matrix3.multiplyByVector(rotation, camera.direction, camera.direction);
  1479. Matrix3.multiplyByVector(rotation, camera.up, camera.up);
  1480. Cartesian3.cross(camera.direction, camera.up, camera.right);
  1481. Cartesian3.cross(camera.right, camera.direction, camera.up);
  1482. camera._setTransform(oldTransform);
  1483. }
  1484. }
  1485. var look3DStartPos = new Cartesian2();
  1486. var look3DEndPos = new Cartesian2();
  1487. var look3DStartRay = new Ray();
  1488. var look3DEndRay = new Ray();
  1489. var look3DNegativeRot = new Cartesian3();
  1490. var look3DTan = new Cartesian3();
  1491. function look3D(controller, startPosition, movement, rotationAxis) {
  1492. var scene = controller._scene;
  1493. var camera = scene.camera;
  1494. var startPos = look3DStartPos;
  1495. startPos.x = movement.startPosition.x;
  1496. startPos.y = 0.0;
  1497. var endPos = look3DEndPos;
  1498. endPos.x = movement.endPosition.x;
  1499. endPos.y = 0.0;
  1500. var startRay = camera.getPickRay(startPos, look3DStartRay);
  1501. var endRay = camera.getPickRay(endPos, look3DEndRay);
  1502. var angle = 0.0;
  1503. var start;
  1504. var end;
  1505. if (camera.frustum instanceof OrthographicFrustum) {
  1506. start = startRay.origin;
  1507. end = endRay.origin;
  1508. Cartesian3.add(camera.direction, start, start);
  1509. Cartesian3.add(camera.direction, end, end);
  1510. Cartesian3.subtract(start, camera.position, start);
  1511. Cartesian3.subtract(end, camera.position, end);
  1512. Cartesian3.normalize(start, start);
  1513. Cartesian3.normalize(end, end);
  1514. } else {
  1515. start = startRay.direction;
  1516. end = endRay.direction;
  1517. }
  1518. var dot = Cartesian3.dot(start, end);
  1519. if (dot < 1.0) { // dot is in [0, 1]
  1520. angle = Math.acos(dot);
  1521. }
  1522. angle = (movement.startPosition.x > movement.endPosition.x) ? -angle : angle;
  1523. var horizontalRotationAxis = controller._horizontalRotationAxis;
  1524. if (defined(rotationAxis)) {
  1525. camera.look(rotationAxis, -angle);
  1526. } else if (defined(horizontalRotationAxis)) {
  1527. camera.look(horizontalRotationAxis, -angle);
  1528. } else {
  1529. camera.lookLeft(angle);
  1530. }
  1531. startPos.x = 0.0;
  1532. startPos.y = movement.startPosition.y;
  1533. endPos.x = 0.0;
  1534. endPos.y = movement.endPosition.y;
  1535. startRay = camera.getPickRay(startPos, look3DStartRay);
  1536. endRay = camera.getPickRay(endPos, look3DEndRay);
  1537. angle = 0.0;
  1538. if (camera.frustum instanceof OrthographicFrustum) {
  1539. start = startRay.origin;
  1540. end = endRay.origin;
  1541. Cartesian3.add(camera.direction, start, start);
  1542. Cartesian3.add(camera.direction, end, end);
  1543. Cartesian3.subtract(start, camera.position, start);
  1544. Cartesian3.subtract(end, camera.position, end);
  1545. Cartesian3.normalize(start, start);
  1546. Cartesian3.normalize(end, end);
  1547. } else {
  1548. start = startRay.direction;
  1549. end = endRay.direction;
  1550. }
  1551. dot = Cartesian3.dot(start, end);
  1552. if (dot < 1.0) { // dot is in [0, 1]
  1553. angle = Math.acos(dot);
  1554. }
  1555. angle = (movement.startPosition.y > movement.endPosition.y) ? -angle : angle;
  1556. rotationAxis = defaultValue(rotationAxis, horizontalRotationAxis);
  1557. if (defined(rotationAxis)) {
  1558. var direction = camera.direction;
  1559. var negativeRotationAxis = Cartesian3.negate(rotationAxis, look3DNegativeRot);
  1560. var northParallel = Cartesian3.equalsEpsilon(direction, rotationAxis, CesiumMath.EPSILON2);
  1561. var southParallel = Cartesian3.equalsEpsilon(direction, negativeRotationAxis, CesiumMath.EPSILON2);
  1562. if ((!northParallel && !southParallel)) {
  1563. dot = Cartesian3.dot(direction, rotationAxis);
  1564. var angleToAxis = CesiumMath.acosClamped(dot);
  1565. if (angle > 0 && angle > angleToAxis) {
  1566. angle = angleToAxis - CesiumMath.EPSILON4;
  1567. }
  1568. dot = Cartesian3.dot(direction, negativeRotationAxis);
  1569. angleToAxis = CesiumMath.acosClamped(dot);
  1570. if (angle < 0 && -angle > angleToAxis) {
  1571. angle = -angleToAxis + CesiumMath.EPSILON4;
  1572. }
  1573. var tangent = Cartesian3.cross(rotationAxis, direction, look3DTan);
  1574. camera.look(tangent, angle);
  1575. } else if ((northParallel && angle < 0) || (southParallel && angle > 0)) {
  1576. camera.look(camera.right, -angle);
  1577. }
  1578. } else {
  1579. camera.lookUp(angle);
  1580. }
  1581. }
  1582. function update3D(controller) {
  1583. reactToInput(controller, controller.enableRotate, controller.rotateEventTypes, spin3D, controller.inertiaSpin, '_lastInertiaSpinMovement');
  1584. reactToInput(controller, controller.enableZoom, controller.zoomEventTypes, zoom3D, controller.inertiaZoom, '_lastInertiaZoomMovement');
  1585. reactToInput(controller, controller.enableTilt, controller.tiltEventTypes, tilt3D, controller.inertiaSpin, '_lastInertiaTiltMovement');
  1586. reactToInput(controller, controller.enableLook, controller.lookEventTypes, look3D);
  1587. }
  1588. /**
  1589. * @private
  1590. */
  1591. ScreenSpaceCameraController.prototype.update = function() {
  1592. if (!Matrix4.equals(this._scene.camera.transform, Matrix4.IDENTITY)) {
  1593. this._globe = undefined;
  1594. this._ellipsoid = Ellipsoid.UNIT_SPHERE;
  1595. } else {
  1596. this._globe = this._scene.globe;
  1597. this._ellipsoid = defined(this._globe) ? this._globe.ellipsoid : this._scene.mapProjection.ellipsoid;
  1598. }
  1599. this._minimumCollisionTerrainHeight = this.minimumCollisionTerrainHeight * this._scene.terrainExaggeration;
  1600. this._minimumPickingTerrainHeight = this.minimumPickingTerrainHeight * this._scene.terrainExaggeration;
  1601. this._minimumTrackBallHeight = this.minimumTrackBallHeight * this._scene.terrainExaggeration;
  1602. var radius = this._ellipsoid.maximumRadius;
  1603. this._rotateFactor = 1.0 / radius;
  1604. this._rotateRateRangeAdjustment = radius;
  1605. var scene = this._scene;
  1606. var mode = scene.mode;
  1607. if (mode === SceneMode.SCENE2D) {
  1608. update2D(this);
  1609. } else if (mode === SceneMode.COLUMBUS_VIEW) {
  1610. this._horizontalRotationAxis = Cartesian3.UNIT_Z;
  1611. updateCV(this);
  1612. } else if (mode === SceneMode.SCENE3D) {
  1613. this._horizontalRotationAxis = undefined;
  1614. update3D(this);
  1615. }
  1616. this._aggregator.reset();
  1617. };
  1618. /**
  1619. * Returns true if this object was destroyed; otherwise, false.
  1620. * <br /><br />
  1621. * If this object was destroyed, it should not be used; calling any function other than
  1622. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  1623. *
  1624. * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  1625. *
  1626. * @see ScreenSpaceCameraController#destroy
  1627. */
  1628. ScreenSpaceCameraController.prototype.isDestroyed = function() {
  1629. return false;
  1630. };
  1631. /**
  1632. * Removes mouse listeners held by this object.
  1633. * <br /><br />
  1634. * Once an object is destroyed, it should not be used; calling any function other than
  1635. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  1636. * assign the return value (<code>undefined</code>) to the object as done in the example.
  1637. *
  1638. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  1639. *
  1640. *
  1641. * @example
  1642. * controller = controller && controller.destroy();
  1643. *
  1644. * @see ScreenSpaceCameraController#isDestroyed
  1645. */
  1646. ScreenSpaceCameraController.prototype.destroy = function() {
  1647. this._tweens.removeAll();
  1648. this._aggregator = this._aggregator && this._aggregator.destroy();
  1649. return destroyObject(this);
  1650. };
  1651. export default ScreenSpaceCameraController;