ScreenSpaceCameraController.js 68 KB

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