ScreenSpaceCameraController.js 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933
  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 KeyboardEventModifier from "../Core/KeyboardEventModifier.js";
  13. import CesiumMath from "../Core/Math.js";
  14. import Matrix3 from "../Core/Matrix3.js";
  15. import Matrix4 from "../Core/Matrix4.js";
  16. import OrthographicFrustum from "../Core/OrthographicFrustum.js";
  17. import Plane from "../Core/Plane.js";
  18. import Quaternion from "../Core/Quaternion.js";
  19. import Ray from "../Core/Ray.js";
  20. import TerrainExaggeration from "../Core/TerrainExaggeration.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 = [
  160. CameraEventType.RIGHT_DRAG,
  161. CameraEventType.WHEEL,
  162. CameraEventType.PINCH,
  163. ];
  164. /**
  165. * The input that allows the user to rotate around the globe or another object. This only applies in 3D and Columbus view modes.
  166. * <p>
  167. * The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
  168. * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
  169. * or an array of any of the preceding.
  170. * </p>
  171. * @type {CameraEventType|Array|undefined}
  172. * @default {@link CameraEventType.LEFT_DRAG}
  173. */
  174. this.rotateEventTypes = CameraEventType.LEFT_DRAG;
  175. /**
  176. * The input that allows the user to tilt in 3D and Columbus view or twist in 2D.
  177. * <p>
  178. * The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
  179. * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
  180. * or an array of any of the preceding.
  181. * </p>
  182. * @type {CameraEventType|Array|undefined}
  183. * @default [{@link CameraEventType.MIDDLE_DRAG}, {@link CameraEventType.PINCH}, {
  184. * eventType : {@link CameraEventType.LEFT_DRAG},
  185. * modifier : {@link KeyboardEventModifier.CTRL}
  186. * }, {
  187. * eventType : {@link CameraEventType.RIGHT_DRAG},
  188. * modifier : {@link KeyboardEventModifier.CTRL}
  189. * }]
  190. */
  191. this.tiltEventTypes = [
  192. CameraEventType.MIDDLE_DRAG,
  193. CameraEventType.PINCH,
  194. {
  195. eventType: CameraEventType.LEFT_DRAG,
  196. modifier: KeyboardEventModifier.CTRL,
  197. },
  198. {
  199. eventType: CameraEventType.RIGHT_DRAG,
  200. modifier: KeyboardEventModifier.CTRL,
  201. },
  202. ];
  203. /**
  204. * The input that allows the user to change the direction the camera is viewing. This only applies in 3D and Columbus view modes.
  205. * <p>
  206. * The type came be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
  207. * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
  208. * or an array of any of the preceding.
  209. * </p>
  210. * @type {CameraEventType|Array|undefined}
  211. * @default { eventType : {@link CameraEventType.LEFT_DRAG}, modifier : {@link KeyboardEventModifier.SHIFT} }
  212. */
  213. this.lookEventTypes = {
  214. eventType: CameraEventType.LEFT_DRAG,
  215. modifier: KeyboardEventModifier.SHIFT,
  216. };
  217. /**
  218. * The minimum height the camera must be before picking the terrain instead of the ellipsoid.
  219. * @type {Number}
  220. * @default 150000.0
  221. */
  222. this.minimumPickingTerrainHeight = 150000.0;
  223. this._minimumPickingTerrainHeight = this.minimumPickingTerrainHeight;
  224. /**
  225. * The minimum height the camera must be before testing for collision with terrain.
  226. * @type {Number}
  227. * @default 15000.0
  228. */
  229. this.minimumCollisionTerrainHeight = 15000.0;
  230. this._minimumCollisionTerrainHeight = this.minimumCollisionTerrainHeight;
  231. /**
  232. * The minimum height the camera must be before switching from rotating a track ball to
  233. * free look when clicks originate on the sky or in space.
  234. * @type {Number}
  235. * @default 7500000.0
  236. */
  237. this.minimumTrackBallHeight = 7500000.0;
  238. this._minimumTrackBallHeight = this.minimumTrackBallHeight;
  239. /**
  240. * Enables or disables camera collision detection with terrain.
  241. * @type {Boolean}
  242. * @default true
  243. */
  244. this.enableCollisionDetection = true;
  245. this._scene = scene;
  246. this._globe = undefined;
  247. this._ellipsoid = undefined;
  248. this._aggregator = new CameraEventAggregator(scene.canvas);
  249. this._lastInertiaSpinMovement = undefined;
  250. this._lastInertiaZoomMovement = undefined;
  251. this._lastInertiaTranslateMovement = undefined;
  252. this._lastInertiaTiltMovement = undefined;
  253. // Zoom disables tilt, spin, and translate inertia
  254. // Tilt disables spin and translate inertia
  255. this._inertiaDisablers = {
  256. _lastInertiaZoomMovement: [
  257. "_lastInertiaSpinMovement",
  258. "_lastInertiaTranslateMovement",
  259. "_lastInertiaTiltMovement",
  260. ],
  261. _lastInertiaTiltMovement: [
  262. "_lastInertiaSpinMovement",
  263. "_lastInertiaTranslateMovement",
  264. ],
  265. };
  266. this._tweens = new TweenCollection();
  267. this._tween = undefined;
  268. this._horizontalRotationAxis = undefined;
  269. this._tiltCenterMousePosition = new Cartesian2(-1.0, -1.0);
  270. this._tiltCenter = new Cartesian3();
  271. this._rotateMousePosition = new Cartesian2(-1.0, -1.0);
  272. this._rotateStartPosition = new Cartesian3();
  273. this._strafeStartPosition = new Cartesian3();
  274. this._strafeMousePosition = new Cartesian2();
  275. this._strafeEndMousePosition = new Cartesian2();
  276. this._zoomMouseStart = new Cartesian2(-1.0, -1.0);
  277. this._zoomWorldPosition = new Cartesian3();
  278. this._useZoomWorldPosition = false;
  279. this._tiltCVOffMap = false;
  280. this._looking = false;
  281. this._rotating = false;
  282. this._strafing = false;
  283. this._zoomingOnVector = false;
  284. this._zoomingUnderground = false;
  285. this._rotatingZoom = false;
  286. this._adjustedHeightForTerrain = false;
  287. this._cameraUnderground = false;
  288. const projection = scene.mapProjection;
  289. this._maxCoord = projection.project(
  290. new Cartographic(Math.PI, CesiumMath.PI_OVER_TWO)
  291. );
  292. // Constants, Make any of these public?
  293. this._zoomFactor = 5.0;
  294. this._rotateFactor = undefined;
  295. this._rotateRateRangeAdjustment = undefined;
  296. this._maximumRotateRate = 1.77;
  297. this._minimumRotateRate = 1.0 / 5000.0;
  298. this._minimumZoomRate = 20.0;
  299. this._maximumZoomRate = 5906376272000.0; // distance from the Sun to Pluto in meters.
  300. this._minimumUndergroundPickDistance = 2000.0;
  301. this._maximumUndergroundPickDistance = 10000.0;
  302. }
  303. function decay(time, coefficient) {
  304. if (time < 0) {
  305. return 0.0;
  306. }
  307. const tau = (1.0 - coefficient) * 25.0;
  308. return Math.exp(-tau * time);
  309. }
  310. function sameMousePosition(movement) {
  311. return Cartesian2.equalsEpsilon(
  312. movement.startPosition,
  313. movement.endPosition,
  314. CesiumMath.EPSILON14
  315. );
  316. }
  317. // If the time between mouse down and mouse up is not between
  318. // these thresholds, the camera will not move with inertia.
  319. // This value is probably dependent on the browser and/or the
  320. // hardware. Should be investigated further.
  321. const inertiaMaxClickTimeThreshold = 0.4;
  322. function maintainInertia(
  323. aggregator,
  324. type,
  325. modifier,
  326. decayCoef,
  327. action,
  328. object,
  329. lastMovementName
  330. ) {
  331. let movementState = object[lastMovementName];
  332. if (!defined(movementState)) {
  333. movementState = object[lastMovementName] = {
  334. startPosition: new Cartesian2(),
  335. endPosition: new Cartesian2(),
  336. motion: new Cartesian2(),
  337. inertiaEnabled: true,
  338. };
  339. }
  340. const ts = aggregator.getButtonPressTime(type, modifier);
  341. const tr = aggregator.getButtonReleaseTime(type, modifier);
  342. const threshold = ts && tr && (tr.getTime() - ts.getTime()) / 1000.0;
  343. const now = new Date();
  344. const fromNow = tr && (now.getTime() - tr.getTime()) / 1000.0;
  345. if (ts && tr && threshold < inertiaMaxClickTimeThreshold) {
  346. const d = decay(fromNow, decayCoef);
  347. const lastMovement = aggregator.getLastMovement(type, modifier);
  348. if (
  349. !defined(lastMovement) ||
  350. sameMousePosition(lastMovement) ||
  351. !movementState.inertiaEnabled
  352. ) {
  353. return;
  354. }
  355. movementState.motion.x =
  356. (lastMovement.endPosition.x - lastMovement.startPosition.x) * 0.5;
  357. movementState.motion.y =
  358. (lastMovement.endPosition.y - lastMovement.startPosition.y) * 0.5;
  359. movementState.startPosition = Cartesian2.clone(
  360. lastMovement.startPosition,
  361. movementState.startPosition
  362. );
  363. movementState.endPosition = Cartesian2.multiplyByScalar(
  364. movementState.motion,
  365. d,
  366. movementState.endPosition
  367. );
  368. movementState.endPosition = Cartesian2.add(
  369. movementState.startPosition,
  370. movementState.endPosition,
  371. movementState.endPosition
  372. );
  373. // If value from the decreasing exponential function is close to zero,
  374. // the end coordinates may be NaN.
  375. if (
  376. isNaN(movementState.endPosition.x) ||
  377. isNaN(movementState.endPosition.y) ||
  378. Cartesian2.distance(
  379. movementState.startPosition,
  380. movementState.endPosition
  381. ) < 0.5
  382. ) {
  383. return;
  384. }
  385. if (!aggregator.isButtonDown(type, modifier)) {
  386. const startPosition = aggregator.getStartMousePosition(type, modifier);
  387. action(object, startPosition, movementState);
  388. }
  389. }
  390. }
  391. function activateInertia(controller, inertiaStateName) {
  392. if (defined(inertiaStateName)) {
  393. // Re-enable inertia if it was disabled
  394. let movementState = controller[inertiaStateName];
  395. if (defined(movementState)) {
  396. movementState.inertiaEnabled = true;
  397. }
  398. // Disable inertia on other movements
  399. const inertiasToDisable = controller._inertiaDisablers[inertiaStateName];
  400. if (defined(inertiasToDisable)) {
  401. const length = inertiasToDisable.length;
  402. for (let i = 0; i < length; ++i) {
  403. movementState = controller[inertiasToDisable[i]];
  404. if (defined(movementState)) {
  405. movementState.inertiaEnabled = false;
  406. }
  407. }
  408. }
  409. }
  410. }
  411. const scratchEventTypeArray = [];
  412. function reactToInput(
  413. controller,
  414. enabled,
  415. eventTypes,
  416. action,
  417. inertiaConstant,
  418. inertiaStateName
  419. ) {
  420. if (!defined(eventTypes)) {
  421. return;
  422. }
  423. const aggregator = controller._aggregator;
  424. if (!Array.isArray(eventTypes)) {
  425. scratchEventTypeArray[0] = eventTypes;
  426. eventTypes = scratchEventTypeArray;
  427. }
  428. const length = eventTypes.length;
  429. for (let i = 0; i < length; ++i) {
  430. const eventType = eventTypes[i];
  431. const type = defined(eventType.eventType) ? eventType.eventType : eventType;
  432. const modifier = eventType.modifier;
  433. const movement =
  434. aggregator.isMoving(type, modifier) &&
  435. aggregator.getMovement(type, modifier);
  436. const startPosition = aggregator.getStartMousePosition(type, modifier);
  437. if (controller.enableInputs && enabled) {
  438. if (movement) {
  439. action(controller, startPosition, movement);
  440. activateInertia(controller, inertiaStateName);
  441. } else if (inertiaConstant < 1.0) {
  442. maintainInertia(
  443. aggregator,
  444. type,
  445. modifier,
  446. inertiaConstant,
  447. action,
  448. controller,
  449. inertiaStateName
  450. );
  451. }
  452. }
  453. }
  454. }
  455. const scratchZoomPickRay = new Ray();
  456. const scratchPickCartesian = new Cartesian3();
  457. const scratchZoomOffset = new Cartesian2();
  458. const scratchZoomDirection = new Cartesian3();
  459. const scratchCenterPixel = new Cartesian2();
  460. const scratchCenterPosition = new Cartesian3();
  461. const scratchPositionNormal = new Cartesian3();
  462. const scratchPickNormal = new Cartesian3();
  463. const scratchZoomAxis = new Cartesian3();
  464. const scratchCameraPositionNormal = new Cartesian3();
  465. // Scratch variables used in zooming algorithm
  466. const scratchTargetNormal = new Cartesian3();
  467. const scratchCameraPosition = new Cartesian3();
  468. const scratchCameraUpNormal = new Cartesian3();
  469. const scratchCameraRightNormal = new Cartesian3();
  470. const scratchForwardNormal = new Cartesian3();
  471. const scratchPositionToTarget = new Cartesian3();
  472. const scratchPositionToTargetNormal = new Cartesian3();
  473. const scratchPan = new Cartesian3();
  474. const scratchCenterMovement = new Cartesian3();
  475. const scratchCenter = new Cartesian3();
  476. const scratchCartesian = new Cartesian3();
  477. const scratchCartesianTwo = new Cartesian3();
  478. const scratchCartesianThree = new Cartesian3();
  479. const scratchZoomViewOptions = {
  480. orientation: new HeadingPitchRoll(),
  481. };
  482. function handleZoom(
  483. object,
  484. startPosition,
  485. movement,
  486. zoomFactor,
  487. distanceMeasure,
  488. unitPositionDotDirection
  489. ) {
  490. let percentage = 1.0;
  491. if (defined(unitPositionDotDirection)) {
  492. percentage = CesiumMath.clamp(
  493. Math.abs(unitPositionDotDirection),
  494. 0.25,
  495. 1.0
  496. );
  497. }
  498. const diff = movement.endPosition.y - movement.startPosition.y;
  499. // distanceMeasure should be the height above the ellipsoid.
  500. // When approaching the surface, the zoomRate slows and stops minimumZoomDistance above it.
  501. const approachingSurface = diff > 0;
  502. const minHeight = approachingSurface
  503. ? object.minimumZoomDistance * percentage
  504. : 0;
  505. const maxHeight = object.maximumZoomDistance;
  506. const minDistance = distanceMeasure - minHeight;
  507. let zoomRate = zoomFactor * minDistance;
  508. zoomRate = CesiumMath.clamp(
  509. zoomRate,
  510. object._minimumZoomRate,
  511. object._maximumZoomRate
  512. );
  513. let rangeWindowRatio = diff / object._scene.canvas.clientHeight;
  514. rangeWindowRatio = Math.min(rangeWindowRatio, object.maximumMovementRatio);
  515. let distance = zoomRate * rangeWindowRatio;
  516. if (
  517. object.enableCollisionDetection ||
  518. object.minimumZoomDistance === 0.0 ||
  519. !defined(object._globe) // look-at mode
  520. ) {
  521. if (distance > 0.0 && Math.abs(distanceMeasure - minHeight) < 1.0) {
  522. return;
  523. }
  524. if (distance < 0.0 && Math.abs(distanceMeasure - maxHeight) < 1.0) {
  525. return;
  526. }
  527. if (distanceMeasure - distance < minHeight) {
  528. distance = distanceMeasure - minHeight - 1.0;
  529. } else if (distanceMeasure - distance > maxHeight) {
  530. distance = distanceMeasure - maxHeight;
  531. }
  532. }
  533. const scene = object._scene;
  534. const camera = scene.camera;
  535. const mode = scene.mode;
  536. const orientation = scratchZoomViewOptions.orientation;
  537. orientation.heading = camera.heading;
  538. orientation.pitch = camera.pitch;
  539. orientation.roll = camera.roll;
  540. if (camera.frustum instanceof OrthographicFrustum) {
  541. if (Math.abs(distance) > 0.0) {
  542. camera.zoomIn(distance);
  543. camera._adjustOrthographicFrustum();
  544. }
  545. return;
  546. }
  547. const sameStartPosition = Cartesian2.equals(
  548. startPosition,
  549. object._zoomMouseStart
  550. );
  551. let zoomingOnVector = object._zoomingOnVector;
  552. let rotatingZoom = object._rotatingZoom;
  553. let pickedPosition;
  554. if (!sameStartPosition) {
  555. object._zoomMouseStart = Cartesian2.clone(
  556. startPosition,
  557. object._zoomMouseStart
  558. );
  559. if (defined(object._globe)) {
  560. if (mode === SceneMode.SCENE2D) {
  561. pickedPosition = camera.getPickRay(startPosition, scratchZoomPickRay)
  562. .origin;
  563. pickedPosition = Cartesian3.fromElements(
  564. pickedPosition.y,
  565. pickedPosition.z,
  566. pickedPosition.x
  567. );
  568. } else {
  569. pickedPosition = pickGlobe(object, startPosition, scratchPickCartesian);
  570. }
  571. }
  572. if (defined(pickedPosition)) {
  573. object._useZoomWorldPosition = true;
  574. object._zoomWorldPosition = Cartesian3.clone(
  575. pickedPosition,
  576. object._zoomWorldPosition
  577. );
  578. } else {
  579. object._useZoomWorldPosition = false;
  580. }
  581. zoomingOnVector = object._zoomingOnVector = false;
  582. rotatingZoom = object._rotatingZoom = false;
  583. object._zoomingUnderground = object._cameraUnderground;
  584. }
  585. if (!object._useZoomWorldPosition) {
  586. camera.zoomIn(distance);
  587. return;
  588. }
  589. let zoomOnVector = mode === SceneMode.COLUMBUS_VIEW;
  590. if (camera.positionCartographic.height < 2000000) {
  591. rotatingZoom = true;
  592. }
  593. if (!sameStartPosition || rotatingZoom) {
  594. if (mode === SceneMode.SCENE2D) {
  595. const worldPosition = object._zoomWorldPosition;
  596. const endPosition = camera.position;
  597. if (
  598. !Cartesian3.equals(worldPosition, endPosition) &&
  599. camera.positionCartographic.height < object._maxCoord.x * 2.0
  600. ) {
  601. const savedX = camera.position.x;
  602. const direction = Cartesian3.subtract(
  603. worldPosition,
  604. endPosition,
  605. scratchZoomDirection
  606. );
  607. Cartesian3.normalize(direction, direction);
  608. const d =
  609. (Cartesian3.distance(worldPosition, endPosition) * distance) /
  610. (camera.getMagnitude() * 0.5);
  611. camera.move(direction, d * 0.5);
  612. if (
  613. (camera.position.x < 0.0 && savedX > 0.0) ||
  614. (camera.position.x > 0.0 && savedX < 0.0)
  615. ) {
  616. pickedPosition = camera.getPickRay(startPosition, scratchZoomPickRay)
  617. .origin;
  618. pickedPosition = Cartesian3.fromElements(
  619. pickedPosition.y,
  620. pickedPosition.z,
  621. pickedPosition.x
  622. );
  623. object._zoomWorldPosition = Cartesian3.clone(
  624. pickedPosition,
  625. object._zoomWorldPosition
  626. );
  627. }
  628. }
  629. } else if (mode === SceneMode.SCENE3D) {
  630. const cameraPositionNormal = Cartesian3.normalize(
  631. camera.position,
  632. scratchCameraPositionNormal
  633. );
  634. if (
  635. object._cameraUnderground ||
  636. object._zoomingUnderground ||
  637. (camera.positionCartographic.height < 3000.0 &&
  638. Math.abs(Cartesian3.dot(camera.direction, cameraPositionNormal)) <
  639. 0.6)
  640. ) {
  641. zoomOnVector = true;
  642. } else {
  643. const canvas = scene.canvas;
  644. const centerPixel = scratchCenterPixel;
  645. centerPixel.x = canvas.clientWidth / 2;
  646. centerPixel.y = canvas.clientHeight / 2;
  647. const centerPosition = pickGlobe(
  648. object,
  649. centerPixel,
  650. scratchCenterPosition
  651. );
  652. // If centerPosition is not defined, it means the globe does not cover the center position of screen
  653. if (!defined(centerPosition)) {
  654. zoomOnVector = true;
  655. } else if (camera.positionCartographic.height < 1000000) {
  656. // The math in the else block assumes the camera
  657. // points toward the earth surface, so we check it here.
  658. // Theoretically, we should check for 90 degree, but it doesn't behave well when parallel
  659. // to the earth surface
  660. if (Cartesian3.dot(camera.direction, cameraPositionNormal) >= -0.5) {
  661. zoomOnVector = true;
  662. } else {
  663. const cameraPosition = scratchCameraPosition;
  664. Cartesian3.clone(camera.position, cameraPosition);
  665. const target = object._zoomWorldPosition;
  666. let targetNormal = scratchTargetNormal;
  667. targetNormal = Cartesian3.normalize(target, targetNormal);
  668. if (Cartesian3.dot(targetNormal, cameraPositionNormal) < 0.0) {
  669. return;
  670. }
  671. const center = scratchCenter;
  672. const forward = scratchForwardNormal;
  673. Cartesian3.clone(camera.direction, forward);
  674. Cartesian3.add(
  675. cameraPosition,
  676. Cartesian3.multiplyByScalar(forward, 1000, scratchCartesian),
  677. center
  678. );
  679. const positionToTarget = scratchPositionToTarget;
  680. const positionToTargetNormal = scratchPositionToTargetNormal;
  681. Cartesian3.subtract(target, cameraPosition, positionToTarget);
  682. Cartesian3.normalize(positionToTarget, positionToTargetNormal);
  683. const alphaDot = Cartesian3.dot(
  684. cameraPositionNormal,
  685. positionToTargetNormal
  686. );
  687. if (alphaDot >= 0.0) {
  688. // We zoomed past the target, and this zoom is not valid anymore.
  689. // This line causes the next zoom movement to pick a new starting point.
  690. object._zoomMouseStart.x = -1;
  691. return;
  692. }
  693. const alpha = Math.acos(-alphaDot);
  694. const cameraDistance = Cartesian3.magnitude(cameraPosition);
  695. const targetDistance = Cartesian3.magnitude(target);
  696. const remainingDistance = cameraDistance - distance;
  697. const positionToTargetDistance = Cartesian3.magnitude(
  698. positionToTarget
  699. );
  700. const gamma = Math.asin(
  701. CesiumMath.clamp(
  702. (positionToTargetDistance / targetDistance) * Math.sin(alpha),
  703. -1.0,
  704. 1.0
  705. )
  706. );
  707. const delta = Math.asin(
  708. CesiumMath.clamp(
  709. (remainingDistance / targetDistance) * Math.sin(alpha),
  710. -1.0,
  711. 1.0
  712. )
  713. );
  714. const beta = gamma - delta + alpha;
  715. const up = scratchCameraUpNormal;
  716. Cartesian3.normalize(cameraPosition, up);
  717. let right = scratchCameraRightNormal;
  718. right = Cartesian3.cross(positionToTargetNormal, up, right);
  719. right = Cartesian3.normalize(right, right);
  720. Cartesian3.normalize(
  721. Cartesian3.cross(up, right, scratchCartesian),
  722. forward
  723. );
  724. // Calculate new position to move to
  725. Cartesian3.multiplyByScalar(
  726. Cartesian3.normalize(center, scratchCartesian),
  727. Cartesian3.magnitude(center) - distance,
  728. center
  729. );
  730. Cartesian3.normalize(cameraPosition, cameraPosition);
  731. Cartesian3.multiplyByScalar(
  732. cameraPosition,
  733. remainingDistance,
  734. cameraPosition
  735. );
  736. // Pan
  737. const pMid = scratchPan;
  738. Cartesian3.multiplyByScalar(
  739. Cartesian3.add(
  740. Cartesian3.multiplyByScalar(
  741. up,
  742. Math.cos(beta) - 1,
  743. scratchCartesianTwo
  744. ),
  745. Cartesian3.multiplyByScalar(
  746. forward,
  747. Math.sin(beta),
  748. scratchCartesianThree
  749. ),
  750. scratchCartesian
  751. ),
  752. remainingDistance,
  753. pMid
  754. );
  755. Cartesian3.add(cameraPosition, pMid, cameraPosition);
  756. Cartesian3.normalize(center, up);
  757. Cartesian3.normalize(
  758. Cartesian3.cross(up, right, scratchCartesian),
  759. forward
  760. );
  761. const cMid = scratchCenterMovement;
  762. Cartesian3.multiplyByScalar(
  763. Cartesian3.add(
  764. Cartesian3.multiplyByScalar(
  765. up,
  766. Math.cos(beta) - 1,
  767. scratchCartesianTwo
  768. ),
  769. Cartesian3.multiplyByScalar(
  770. forward,
  771. Math.sin(beta),
  772. scratchCartesianThree
  773. ),
  774. scratchCartesian
  775. ),
  776. Cartesian3.magnitude(center),
  777. cMid
  778. );
  779. Cartesian3.add(center, cMid, center);
  780. // Update camera
  781. // Set new position
  782. Cartesian3.clone(cameraPosition, camera.position);
  783. // Set new direction
  784. Cartesian3.normalize(
  785. Cartesian3.subtract(center, cameraPosition, scratchCartesian),
  786. camera.direction
  787. );
  788. Cartesian3.clone(camera.direction, camera.direction);
  789. // Set new right & up vectors
  790. Cartesian3.cross(camera.direction, camera.up, camera.right);
  791. Cartesian3.cross(camera.right, camera.direction, camera.up);
  792. camera.setView(scratchZoomViewOptions);
  793. return;
  794. }
  795. } else {
  796. const positionNormal = Cartesian3.normalize(
  797. centerPosition,
  798. scratchPositionNormal
  799. );
  800. const pickedNormal = Cartesian3.normalize(
  801. object._zoomWorldPosition,
  802. scratchPickNormal
  803. );
  804. const dotProduct = Cartesian3.dot(pickedNormal, positionNormal);
  805. if (dotProduct > 0.0 && dotProduct < 1.0) {
  806. const angle = CesiumMath.acosClamped(dotProduct);
  807. const axis = Cartesian3.cross(
  808. pickedNormal,
  809. positionNormal,
  810. scratchZoomAxis
  811. );
  812. const denom =
  813. Math.abs(angle) > CesiumMath.toRadians(20.0)
  814. ? camera.positionCartographic.height * 0.75
  815. : camera.positionCartographic.height - distance;
  816. const scalar = distance / denom;
  817. camera.rotate(axis, angle * scalar);
  818. }
  819. }
  820. }
  821. }
  822. object._rotatingZoom = !zoomOnVector;
  823. }
  824. if ((!sameStartPosition && zoomOnVector) || zoomingOnVector) {
  825. let ray;
  826. const zoomMouseStart = SceneTransforms.wgs84ToWindowCoordinates(
  827. scene,
  828. object._zoomWorldPosition,
  829. scratchZoomOffset
  830. );
  831. if (
  832. mode !== SceneMode.COLUMBUS_VIEW &&
  833. Cartesian2.equals(startPosition, object._zoomMouseStart) &&
  834. defined(zoomMouseStart)
  835. ) {
  836. ray = camera.getPickRay(zoomMouseStart, scratchZoomPickRay);
  837. } else {
  838. ray = camera.getPickRay(startPosition, scratchZoomPickRay);
  839. }
  840. const rayDirection = ray.direction;
  841. if (mode === SceneMode.COLUMBUS_VIEW || mode === SceneMode.SCENE2D) {
  842. Cartesian3.fromElements(
  843. rayDirection.y,
  844. rayDirection.z,
  845. rayDirection.x,
  846. rayDirection
  847. );
  848. }
  849. camera.move(rayDirection, distance);
  850. object._zoomingOnVector = true;
  851. } else {
  852. camera.zoomIn(distance);
  853. }
  854. if (!object._cameraUnderground) {
  855. camera.setView(scratchZoomViewOptions);
  856. }
  857. }
  858. const translate2DStart = new Ray();
  859. const translate2DEnd = new Ray();
  860. const scratchTranslateP0 = new Cartesian3();
  861. function translate2D(controller, startPosition, movement) {
  862. const scene = controller._scene;
  863. const camera = scene.camera;
  864. let start = camera.getPickRay(movement.startPosition, translate2DStart)
  865. .origin;
  866. let end = camera.getPickRay(movement.endPosition, translate2DEnd).origin;
  867. start = Cartesian3.fromElements(start.y, start.z, start.x, start);
  868. end = Cartesian3.fromElements(end.y, end.z, end.x, end);
  869. const direction = Cartesian3.subtract(start, end, scratchTranslateP0);
  870. const distance = Cartesian3.magnitude(direction);
  871. if (distance > 0.0) {
  872. Cartesian3.normalize(direction, direction);
  873. camera.move(direction, distance);
  874. }
  875. }
  876. function zoom2D(controller, startPosition, movement) {
  877. if (defined(movement.distance)) {
  878. movement = movement.distance;
  879. }
  880. const scene = controller._scene;
  881. const camera = scene.camera;
  882. handleZoom(
  883. controller,
  884. startPosition,
  885. movement,
  886. controller._zoomFactor,
  887. camera.getMagnitude()
  888. );
  889. }
  890. const twist2DStart = new Cartesian2();
  891. const twist2DEnd = new Cartesian2();
  892. function twist2D(controller, startPosition, movement) {
  893. if (defined(movement.angleAndHeight)) {
  894. singleAxisTwist2D(controller, startPosition, movement.angleAndHeight);
  895. return;
  896. }
  897. const scene = controller._scene;
  898. const camera = scene.camera;
  899. const canvas = scene.canvas;
  900. const width = canvas.clientWidth;
  901. const height = canvas.clientHeight;
  902. let start = twist2DStart;
  903. start.x = (2.0 / width) * movement.startPosition.x - 1.0;
  904. start.y = (2.0 / height) * (height - movement.startPosition.y) - 1.0;
  905. start = Cartesian2.normalize(start, start);
  906. let end = twist2DEnd;
  907. end.x = (2.0 / width) * movement.endPosition.x - 1.0;
  908. end.y = (2.0 / height) * (height - movement.endPosition.y) - 1.0;
  909. end = Cartesian2.normalize(end, end);
  910. let startTheta = CesiumMath.acosClamped(start.x);
  911. if (start.y < 0) {
  912. startTheta = CesiumMath.TWO_PI - startTheta;
  913. }
  914. let endTheta = CesiumMath.acosClamped(end.x);
  915. if (end.y < 0) {
  916. endTheta = CesiumMath.TWO_PI - endTheta;
  917. }
  918. const theta = endTheta - startTheta;
  919. camera.twistRight(theta);
  920. }
  921. function singleAxisTwist2D(controller, startPosition, movement) {
  922. let rotateRate =
  923. controller._rotateFactor * controller._rotateRateRangeAdjustment;
  924. if (rotateRate > controller._maximumRotateRate) {
  925. rotateRate = controller._maximumRotateRate;
  926. }
  927. if (rotateRate < controller._minimumRotateRate) {
  928. rotateRate = controller._minimumRotateRate;
  929. }
  930. const scene = controller._scene;
  931. const camera = scene.camera;
  932. const canvas = scene.canvas;
  933. let phiWindowRatio =
  934. (movement.endPosition.x - movement.startPosition.x) / canvas.clientWidth;
  935. phiWindowRatio = Math.min(phiWindowRatio, controller.maximumMovementRatio);
  936. const deltaPhi = rotateRate * phiWindowRatio * Math.PI * 4.0;
  937. camera.twistRight(deltaPhi);
  938. }
  939. function update2D(controller) {
  940. const rotatable2D = controller._scene.mapMode2D === MapMode2D.ROTATE;
  941. if (!Matrix4.equals(Matrix4.IDENTITY, controller._scene.camera.transform)) {
  942. reactToInput(
  943. controller,
  944. controller.enableZoom,
  945. controller.zoomEventTypes,
  946. zoom2D,
  947. controller.inertiaZoom,
  948. "_lastInertiaZoomMovement"
  949. );
  950. if (rotatable2D) {
  951. reactToInput(
  952. controller,
  953. controller.enableRotate,
  954. controller.translateEventTypes,
  955. twist2D,
  956. controller.inertiaSpin,
  957. "_lastInertiaSpinMovement"
  958. );
  959. }
  960. } else {
  961. reactToInput(
  962. controller,
  963. controller.enableTranslate,
  964. controller.translateEventTypes,
  965. translate2D,
  966. controller.inertiaTranslate,
  967. "_lastInertiaTranslateMovement"
  968. );
  969. reactToInput(
  970. controller,
  971. controller.enableZoom,
  972. controller.zoomEventTypes,
  973. zoom2D,
  974. controller.inertiaZoom,
  975. "_lastInertiaZoomMovement"
  976. );
  977. if (rotatable2D) {
  978. reactToInput(
  979. controller,
  980. controller.enableRotate,
  981. controller.tiltEventTypes,
  982. twist2D,
  983. controller.inertiaSpin,
  984. "_lastInertiaTiltMovement"
  985. );
  986. }
  987. }
  988. }
  989. const pickGlobeScratchRay = new Ray();
  990. const scratchDepthIntersection = new Cartesian3();
  991. const scratchRayIntersection = new Cartesian3();
  992. function pickGlobe(controller, mousePosition, result) {
  993. const scene = controller._scene;
  994. const globe = controller._globe;
  995. const camera = scene.camera;
  996. if (!defined(globe)) {
  997. return undefined;
  998. }
  999. const cullBackFaces = !controller._cameraUnderground;
  1000. let depthIntersection;
  1001. if (scene.pickPositionSupported) {
  1002. depthIntersection = scene.pickPositionWorldCoordinates(
  1003. mousePosition,
  1004. scratchDepthIntersection
  1005. );
  1006. }
  1007. const ray = camera.getPickRay(mousePosition, pickGlobeScratchRay);
  1008. const rayIntersection = globe.pickWorldCoordinates(
  1009. ray,
  1010. scene,
  1011. cullBackFaces,
  1012. scratchRayIntersection
  1013. );
  1014. const pickDistance = defined(depthIntersection)
  1015. ? Cartesian3.distance(depthIntersection, camera.positionWC)
  1016. : Number.POSITIVE_INFINITY;
  1017. const rayDistance = defined(rayIntersection)
  1018. ? Cartesian3.distance(rayIntersection, camera.positionWC)
  1019. : Number.POSITIVE_INFINITY;
  1020. if (pickDistance < rayDistance) {
  1021. return Cartesian3.clone(depthIntersection, result);
  1022. }
  1023. return Cartesian3.clone(rayIntersection, result);
  1024. }
  1025. const scratchDistanceCartographic = new Cartographic();
  1026. function getDistanceFromSurface(controller) {
  1027. const ellipsoid = controller._ellipsoid;
  1028. const scene = controller._scene;
  1029. const camera = scene.camera;
  1030. const mode = scene.mode;
  1031. let height = 0.0;
  1032. if (mode === SceneMode.SCENE3D) {
  1033. const cartographic = ellipsoid.cartesianToCartographic(
  1034. camera.position,
  1035. scratchDistanceCartographic
  1036. );
  1037. if (defined(cartographic)) {
  1038. height = cartographic.height;
  1039. }
  1040. } else {
  1041. height = camera.position.z;
  1042. }
  1043. const globeHeight = defaultValue(controller._scene.globeHeight, 0.0);
  1044. const distanceFromSurface = Math.abs(globeHeight - height);
  1045. return distanceFromSurface;
  1046. }
  1047. const scratchSurfaceNormal = new Cartesian3();
  1048. function getZoomDistanceUnderground(controller, ray) {
  1049. const origin = ray.origin;
  1050. const direction = ray.direction;
  1051. const distanceFromSurface = getDistanceFromSurface(controller);
  1052. // Weight zoom distance based on how strongly the pick ray is pointing inward.
  1053. // Geocentric normal is accurate enough for these purposes
  1054. const surfaceNormal = Cartesian3.normalize(origin, scratchSurfaceNormal);
  1055. let strength = Math.abs(Cartesian3.dot(surfaceNormal, direction));
  1056. strength = Math.max(strength, 0.5) * 2.0;
  1057. return distanceFromSurface * strength;
  1058. }
  1059. function getTiltCenterUnderground(controller, ray, pickedPosition, result) {
  1060. let distance = Cartesian3.distance(ray.origin, pickedPosition);
  1061. const distanceFromSurface = getDistanceFromSurface(controller);
  1062. const maximumDistance = CesiumMath.clamp(
  1063. distanceFromSurface * 5.0,
  1064. controller._minimumUndergroundPickDistance,
  1065. controller._maximumUndergroundPickDistance
  1066. );
  1067. if (distance > maximumDistance) {
  1068. // Simulate look-at behavior by tilting around a small invisible sphere
  1069. distance = Math.min(distance, distanceFromSurface / 5.0);
  1070. distance = Math.max(distance, 100.0);
  1071. }
  1072. return Ray.getPoint(ray, distance, result);
  1073. }
  1074. function getStrafeStartPositionUnderground(
  1075. controller,
  1076. ray,
  1077. pickedPosition,
  1078. result
  1079. ) {
  1080. let distance;
  1081. if (!defined(pickedPosition)) {
  1082. distance = getDistanceFromSurface(controller);
  1083. } else {
  1084. distance = Cartesian3.distance(ray.origin, pickedPosition);
  1085. if (distance > controller._maximumUndergroundPickDistance) {
  1086. // If the picked position is too far away set the strafe speed based on the
  1087. // camera's height from the globe surface
  1088. distance = getDistanceFromSurface(controller);
  1089. }
  1090. }
  1091. return Ray.getPoint(ray, distance, result);
  1092. }
  1093. const scratchInertialDelta = new Cartesian2();
  1094. function continueStrafing(controller, movement) {
  1095. // Update the end position continually based on the inertial delta
  1096. const originalEndPosition = movement.endPosition;
  1097. const inertialDelta = Cartesian2.subtract(
  1098. movement.endPosition,
  1099. movement.startPosition,
  1100. scratchInertialDelta
  1101. );
  1102. const endPosition = controller._strafeEndMousePosition;
  1103. Cartesian2.add(endPosition, inertialDelta, endPosition);
  1104. movement.endPosition = endPosition;
  1105. strafe(controller, movement, controller._strafeStartPosition);
  1106. movement.endPosition = originalEndPosition;
  1107. }
  1108. const translateCVStartRay = new Ray();
  1109. const translateCVEndRay = new Ray();
  1110. const translateCVStartPos = new Cartesian3();
  1111. const translateCVEndPos = new Cartesian3();
  1112. const translateCVDifference = new Cartesian3();
  1113. const translateCVOrigin = new Cartesian3();
  1114. const translateCVPlane = new Plane(Cartesian3.UNIT_X, 0.0);
  1115. const translateCVStartMouse = new Cartesian2();
  1116. const translateCVEndMouse = new Cartesian2();
  1117. function translateCV(controller, startPosition, movement) {
  1118. if (!Cartesian3.equals(startPosition, controller._translateMousePosition)) {
  1119. controller._looking = false;
  1120. }
  1121. if (!Cartesian3.equals(startPosition, controller._strafeMousePosition)) {
  1122. controller._strafing = false;
  1123. }
  1124. if (controller._looking) {
  1125. look3D(controller, startPosition, movement);
  1126. return;
  1127. }
  1128. if (controller._strafing) {
  1129. continueStrafing(controller, movement);
  1130. return;
  1131. }
  1132. const scene = controller._scene;
  1133. const camera = scene.camera;
  1134. const cameraUnderground = controller._cameraUnderground;
  1135. const startMouse = Cartesian2.clone(
  1136. movement.startPosition,
  1137. translateCVStartMouse
  1138. );
  1139. const endMouse = Cartesian2.clone(movement.endPosition, translateCVEndMouse);
  1140. let startRay = camera.getPickRay(startMouse, translateCVStartRay);
  1141. const origin = Cartesian3.clone(Cartesian3.ZERO, translateCVOrigin);
  1142. const normal = Cartesian3.UNIT_X;
  1143. let globePos;
  1144. if (camera.position.z < controller._minimumPickingTerrainHeight) {
  1145. globePos = pickGlobe(controller, startMouse, translateCVStartPos);
  1146. if (defined(globePos)) {
  1147. origin.x = globePos.x;
  1148. }
  1149. }
  1150. if (
  1151. cameraUnderground ||
  1152. (origin.x > camera.position.z && defined(globePos))
  1153. ) {
  1154. let pickPosition = globePos;
  1155. if (cameraUnderground) {
  1156. pickPosition = getStrafeStartPositionUnderground(
  1157. controller,
  1158. startRay,
  1159. globePos,
  1160. translateCVStartPos
  1161. );
  1162. }
  1163. Cartesian2.clone(startPosition, controller._strafeMousePosition);
  1164. Cartesian2.clone(startPosition, controller._strafeEndMousePosition);
  1165. Cartesian3.clone(pickPosition, controller._strafeStartPosition);
  1166. controller._strafing = true;
  1167. strafe(controller, movement, controller._strafeStartPosition);
  1168. return;
  1169. }
  1170. const plane = Plane.fromPointNormal(origin, normal, translateCVPlane);
  1171. startRay = camera.getPickRay(startMouse, translateCVStartRay);
  1172. const startPlanePos = IntersectionTests.rayPlane(
  1173. startRay,
  1174. plane,
  1175. translateCVStartPos
  1176. );
  1177. const endRay = camera.getPickRay(endMouse, translateCVEndRay);
  1178. const endPlanePos = IntersectionTests.rayPlane(
  1179. endRay,
  1180. plane,
  1181. translateCVEndPos
  1182. );
  1183. if (!defined(startPlanePos) || !defined(endPlanePos)) {
  1184. controller._looking = true;
  1185. look3D(controller, startPosition, movement);
  1186. Cartesian2.clone(startPosition, controller._translateMousePosition);
  1187. return;
  1188. }
  1189. const diff = Cartesian3.subtract(
  1190. startPlanePos,
  1191. endPlanePos,
  1192. translateCVDifference
  1193. );
  1194. const temp = diff.x;
  1195. diff.x = diff.y;
  1196. diff.y = diff.z;
  1197. diff.z = temp;
  1198. const mag = Cartesian3.magnitude(diff);
  1199. if (mag > CesiumMath.EPSILON6) {
  1200. Cartesian3.normalize(diff, diff);
  1201. camera.move(diff, mag);
  1202. }
  1203. }
  1204. const rotateCVWindowPos = new Cartesian2();
  1205. const rotateCVWindowRay = new Ray();
  1206. const rotateCVCenter = new Cartesian3();
  1207. const rotateCVVerticalCenter = new Cartesian3();
  1208. const rotateCVTransform = new Matrix4();
  1209. const rotateCVVerticalTransform = new Matrix4();
  1210. const rotateCVOrigin = new Cartesian3();
  1211. const rotateCVPlane = new Plane(Cartesian3.UNIT_X, 0.0);
  1212. const rotateCVCartesian3 = new Cartesian3();
  1213. const rotateCVCart = new Cartographic();
  1214. const rotateCVOldTransform = new Matrix4();
  1215. const rotateCVQuaternion = new Quaternion();
  1216. const rotateCVMatrix = new Matrix3();
  1217. const tilt3DCartesian3 = new Cartesian3();
  1218. function rotateCV(controller, startPosition, movement) {
  1219. if (defined(movement.angleAndHeight)) {
  1220. movement = movement.angleAndHeight;
  1221. }
  1222. if (!Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
  1223. controller._tiltCVOffMap = false;
  1224. controller._looking = false;
  1225. }
  1226. if (controller._looking) {
  1227. look3D(controller, startPosition, movement);
  1228. return;
  1229. }
  1230. const scene = controller._scene;
  1231. const camera = scene.camera;
  1232. if (
  1233. controller._tiltCVOffMap ||
  1234. !controller.onMap() ||
  1235. Math.abs(camera.position.z) > controller._minimumPickingTerrainHeight
  1236. ) {
  1237. controller._tiltCVOffMap = true;
  1238. rotateCVOnPlane(controller, startPosition, movement);
  1239. } else {
  1240. rotateCVOnTerrain(controller, startPosition, movement);
  1241. }
  1242. }
  1243. function rotateCVOnPlane(controller, startPosition, movement) {
  1244. const scene = controller._scene;
  1245. const camera = scene.camera;
  1246. const canvas = scene.canvas;
  1247. const windowPosition = rotateCVWindowPos;
  1248. windowPosition.x = canvas.clientWidth / 2;
  1249. windowPosition.y = canvas.clientHeight / 2;
  1250. const ray = camera.getPickRay(windowPosition, rotateCVWindowRay);
  1251. const normal = Cartesian3.UNIT_X;
  1252. const position = ray.origin;
  1253. const direction = ray.direction;
  1254. let scalar;
  1255. const normalDotDirection = Cartesian3.dot(normal, direction);
  1256. if (Math.abs(normalDotDirection) > CesiumMath.EPSILON6) {
  1257. scalar = -Cartesian3.dot(normal, position) / normalDotDirection;
  1258. }
  1259. if (!defined(scalar) || scalar <= 0.0) {
  1260. controller._looking = true;
  1261. look3D(controller, startPosition, movement);
  1262. Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
  1263. return;
  1264. }
  1265. const center = Cartesian3.multiplyByScalar(direction, scalar, rotateCVCenter);
  1266. Cartesian3.add(position, center, center);
  1267. const projection = scene.mapProjection;
  1268. const ellipsoid = projection.ellipsoid;
  1269. Cartesian3.fromElements(center.y, center.z, center.x, center);
  1270. const cart = projection.unproject(center, rotateCVCart);
  1271. ellipsoid.cartographicToCartesian(cart, center);
  1272. const transform = Transforms.eastNorthUpToFixedFrame(
  1273. center,
  1274. ellipsoid,
  1275. rotateCVTransform
  1276. );
  1277. const oldGlobe = controller._globe;
  1278. const oldEllipsoid = controller._ellipsoid;
  1279. controller._globe = undefined;
  1280. controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
  1281. controller._rotateFactor = 1.0;
  1282. controller._rotateRateRangeAdjustment = 1.0;
  1283. const oldTransform = Matrix4.clone(camera.transform, rotateCVOldTransform);
  1284. camera._setTransform(transform);
  1285. rotate3D(controller, startPosition, movement, Cartesian3.UNIT_Z);
  1286. camera._setTransform(oldTransform);
  1287. controller._globe = oldGlobe;
  1288. controller._ellipsoid = oldEllipsoid;
  1289. const radius = oldEllipsoid.maximumRadius;
  1290. controller._rotateFactor = 1.0 / radius;
  1291. controller._rotateRateRangeAdjustment = radius;
  1292. }
  1293. function rotateCVOnTerrain(controller, startPosition, movement) {
  1294. const scene = controller._scene;
  1295. const camera = scene.camera;
  1296. const cameraUnderground = controller._cameraUnderground;
  1297. let center;
  1298. let ray;
  1299. const normal = Cartesian3.UNIT_X;
  1300. if (Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
  1301. center = Cartesian3.clone(controller._tiltCenter, rotateCVCenter);
  1302. } else {
  1303. if (camera.position.z < controller._minimumPickingTerrainHeight) {
  1304. center = pickGlobe(controller, startPosition, rotateCVCenter);
  1305. }
  1306. if (!defined(center)) {
  1307. ray = camera.getPickRay(startPosition, rotateCVWindowRay);
  1308. const position = ray.origin;
  1309. const direction = ray.direction;
  1310. let scalar;
  1311. const normalDotDirection = Cartesian3.dot(normal, direction);
  1312. if (Math.abs(normalDotDirection) > CesiumMath.EPSILON6) {
  1313. scalar = -Cartesian3.dot(normal, position) / normalDotDirection;
  1314. }
  1315. if (!defined(scalar) || scalar <= 0.0) {
  1316. controller._looking = true;
  1317. look3D(controller, startPosition, movement);
  1318. Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
  1319. return;
  1320. }
  1321. center = Cartesian3.multiplyByScalar(direction, scalar, rotateCVCenter);
  1322. Cartesian3.add(position, center, center);
  1323. }
  1324. if (cameraUnderground) {
  1325. if (!defined(ray)) {
  1326. ray = camera.getPickRay(startPosition, rotateCVWindowRay);
  1327. }
  1328. getTiltCenterUnderground(controller, ray, center, center);
  1329. }
  1330. Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
  1331. Cartesian3.clone(center, controller._tiltCenter);
  1332. }
  1333. const canvas = scene.canvas;
  1334. const windowPosition = rotateCVWindowPos;
  1335. windowPosition.x = canvas.clientWidth / 2;
  1336. windowPosition.y = controller._tiltCenterMousePosition.y;
  1337. ray = camera.getPickRay(windowPosition, rotateCVWindowRay);
  1338. const origin = Cartesian3.clone(Cartesian3.ZERO, rotateCVOrigin);
  1339. origin.x = center.x;
  1340. const plane = Plane.fromPointNormal(origin, normal, rotateCVPlane);
  1341. const verticalCenter = IntersectionTests.rayPlane(
  1342. ray,
  1343. plane,
  1344. rotateCVVerticalCenter
  1345. );
  1346. const projection = camera._projection;
  1347. const ellipsoid = projection.ellipsoid;
  1348. Cartesian3.fromElements(center.y, center.z, center.x, center);
  1349. let cart = projection.unproject(center, rotateCVCart);
  1350. ellipsoid.cartographicToCartesian(cart, center);
  1351. const transform = Transforms.eastNorthUpToFixedFrame(
  1352. center,
  1353. ellipsoid,
  1354. rotateCVTransform
  1355. );
  1356. let verticalTransform;
  1357. if (defined(verticalCenter)) {
  1358. Cartesian3.fromElements(
  1359. verticalCenter.y,
  1360. verticalCenter.z,
  1361. verticalCenter.x,
  1362. verticalCenter
  1363. );
  1364. cart = projection.unproject(verticalCenter, rotateCVCart);
  1365. ellipsoid.cartographicToCartesian(cart, verticalCenter);
  1366. verticalTransform = Transforms.eastNorthUpToFixedFrame(
  1367. verticalCenter,
  1368. ellipsoid,
  1369. rotateCVVerticalTransform
  1370. );
  1371. } else {
  1372. verticalTransform = transform;
  1373. }
  1374. const oldGlobe = controller._globe;
  1375. const oldEllipsoid = controller._ellipsoid;
  1376. controller._globe = undefined;
  1377. controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
  1378. controller._rotateFactor = 1.0;
  1379. controller._rotateRateRangeAdjustment = 1.0;
  1380. let constrainedAxis = Cartesian3.UNIT_Z;
  1381. const oldTransform = Matrix4.clone(camera.transform, rotateCVOldTransform);
  1382. camera._setTransform(transform);
  1383. const tangent = Cartesian3.cross(
  1384. Cartesian3.UNIT_Z,
  1385. Cartesian3.normalize(camera.position, rotateCVCartesian3),
  1386. rotateCVCartesian3
  1387. );
  1388. const dot = Cartesian3.dot(camera.right, tangent);
  1389. rotate3D(controller, startPosition, movement, constrainedAxis, false, true);
  1390. camera._setTransform(verticalTransform);
  1391. if (dot < 0.0) {
  1392. const movementDelta = movement.startPosition.y - movement.endPosition.y;
  1393. if (
  1394. (cameraUnderground && movementDelta < 0.0) ||
  1395. (!cameraUnderground && movementDelta > 0.0)
  1396. ) {
  1397. // Prevent camera from flipping past the up axis
  1398. constrainedAxis = undefined;
  1399. }
  1400. const oldConstrainedAxis = camera.constrainedAxis;
  1401. camera.constrainedAxis = undefined;
  1402. rotate3D(controller, startPosition, movement, constrainedAxis, true, false);
  1403. camera.constrainedAxis = oldConstrainedAxis;
  1404. } else {
  1405. rotate3D(controller, startPosition, movement, constrainedAxis, true, false);
  1406. }
  1407. if (defined(camera.constrainedAxis)) {
  1408. const right = Cartesian3.cross(
  1409. camera.direction,
  1410. camera.constrainedAxis,
  1411. tilt3DCartesian3
  1412. );
  1413. if (
  1414. !Cartesian3.equalsEpsilon(right, Cartesian3.ZERO, CesiumMath.EPSILON6)
  1415. ) {
  1416. if (Cartesian3.dot(right, camera.right) < 0.0) {
  1417. Cartesian3.negate(right, right);
  1418. }
  1419. Cartesian3.cross(right, camera.direction, camera.up);
  1420. Cartesian3.cross(camera.direction, camera.up, camera.right);
  1421. Cartesian3.normalize(camera.up, camera.up);
  1422. Cartesian3.normalize(camera.right, camera.right);
  1423. }
  1424. }
  1425. camera._setTransform(oldTransform);
  1426. controller._globe = oldGlobe;
  1427. controller._ellipsoid = oldEllipsoid;
  1428. const radius = oldEllipsoid.maximumRadius;
  1429. controller._rotateFactor = 1.0 / radius;
  1430. controller._rotateRateRangeAdjustment = radius;
  1431. const originalPosition = Cartesian3.clone(
  1432. camera.positionWC,
  1433. rotateCVCartesian3
  1434. );
  1435. if (controller.enableCollisionDetection) {
  1436. adjustHeightForTerrain(controller);
  1437. }
  1438. if (!Cartesian3.equals(camera.positionWC, originalPosition)) {
  1439. camera._setTransform(verticalTransform);
  1440. camera.worldToCameraCoordinatesPoint(originalPosition, originalPosition);
  1441. const magSqrd = Cartesian3.magnitudeSquared(originalPosition);
  1442. if (Cartesian3.magnitudeSquared(camera.position) > magSqrd) {
  1443. Cartesian3.normalize(camera.position, camera.position);
  1444. Cartesian3.multiplyByScalar(
  1445. camera.position,
  1446. Math.sqrt(magSqrd),
  1447. camera.position
  1448. );
  1449. }
  1450. const angle = Cartesian3.angleBetween(originalPosition, camera.position);
  1451. const axis = Cartesian3.cross(
  1452. originalPosition,
  1453. camera.position,
  1454. originalPosition
  1455. );
  1456. Cartesian3.normalize(axis, axis);
  1457. const quaternion = Quaternion.fromAxisAngle(
  1458. axis,
  1459. angle,
  1460. rotateCVQuaternion
  1461. );
  1462. const rotation = Matrix3.fromQuaternion(quaternion, rotateCVMatrix);
  1463. Matrix3.multiplyByVector(rotation, camera.direction, camera.direction);
  1464. Matrix3.multiplyByVector(rotation, camera.up, camera.up);
  1465. Cartesian3.cross(camera.direction, camera.up, camera.right);
  1466. Cartesian3.cross(camera.right, camera.direction, camera.up);
  1467. camera._setTransform(oldTransform);
  1468. }
  1469. }
  1470. const zoomCVWindowPos = new Cartesian2();
  1471. const zoomCVWindowRay = new Ray();
  1472. const zoomCVIntersection = new Cartesian3();
  1473. function zoomCV(controller, startPosition, movement) {
  1474. if (defined(movement.distance)) {
  1475. movement = movement.distance;
  1476. }
  1477. const scene = controller._scene;
  1478. const camera = scene.camera;
  1479. const canvas = scene.canvas;
  1480. const cameraUnderground = controller._cameraUnderground;
  1481. let windowPosition;
  1482. if (cameraUnderground) {
  1483. windowPosition = startPosition;
  1484. } else {
  1485. windowPosition = zoomCVWindowPos;
  1486. windowPosition.x = canvas.clientWidth / 2;
  1487. windowPosition.y = canvas.clientHeight / 2;
  1488. }
  1489. const ray = camera.getPickRay(windowPosition, zoomCVWindowRay);
  1490. const position = ray.origin;
  1491. const direction = ray.direction;
  1492. const height = camera.position.z;
  1493. let intersection;
  1494. if (height < controller._minimumPickingTerrainHeight) {
  1495. intersection = pickGlobe(controller, windowPosition, zoomCVIntersection);
  1496. }
  1497. let distance;
  1498. if (defined(intersection)) {
  1499. distance = Cartesian3.distance(position, intersection);
  1500. }
  1501. if (cameraUnderground) {
  1502. const distanceUnderground = getZoomDistanceUnderground(
  1503. controller,
  1504. ray,
  1505. height
  1506. );
  1507. if (defined(distance)) {
  1508. distance = Math.min(distance, distanceUnderground);
  1509. } else {
  1510. distance = distanceUnderground;
  1511. }
  1512. }
  1513. if (!defined(distance)) {
  1514. const normal = Cartesian3.UNIT_X;
  1515. distance =
  1516. -Cartesian3.dot(normal, position) / Cartesian3.dot(normal, direction);
  1517. }
  1518. handleZoom(
  1519. controller,
  1520. startPosition,
  1521. movement,
  1522. controller._zoomFactor,
  1523. distance
  1524. );
  1525. }
  1526. function updateCV(controller) {
  1527. const scene = controller._scene;
  1528. const camera = scene.camera;
  1529. if (!Matrix4.equals(Matrix4.IDENTITY, camera.transform)) {
  1530. reactToInput(
  1531. controller,
  1532. controller.enableRotate,
  1533. controller.rotateEventTypes,
  1534. rotate3D,
  1535. controller.inertiaSpin,
  1536. "_lastInertiaSpinMovement"
  1537. );
  1538. reactToInput(
  1539. controller,
  1540. controller.enableZoom,
  1541. controller.zoomEventTypes,
  1542. zoom3D,
  1543. controller.inertiaZoom,
  1544. "_lastInertiaZoomMovement"
  1545. );
  1546. } else {
  1547. const tweens = controller._tweens;
  1548. if (controller._aggregator.anyButtonDown) {
  1549. tweens.removeAll();
  1550. }
  1551. reactToInput(
  1552. controller,
  1553. controller.enableTilt,
  1554. controller.tiltEventTypes,
  1555. rotateCV,
  1556. controller.inertiaSpin,
  1557. "_lastInertiaTiltMovement"
  1558. );
  1559. reactToInput(
  1560. controller,
  1561. controller.enableTranslate,
  1562. controller.translateEventTypes,
  1563. translateCV,
  1564. controller.inertiaTranslate,
  1565. "_lastInertiaTranslateMovement"
  1566. );
  1567. reactToInput(
  1568. controller,
  1569. controller.enableZoom,
  1570. controller.zoomEventTypes,
  1571. zoomCV,
  1572. controller.inertiaZoom,
  1573. "_lastInertiaZoomMovement"
  1574. );
  1575. reactToInput(
  1576. controller,
  1577. controller.enableLook,
  1578. controller.lookEventTypes,
  1579. look3D
  1580. );
  1581. if (
  1582. !controller._aggregator.anyButtonDown &&
  1583. !tweens.contains(controller._tween)
  1584. ) {
  1585. const tween = camera.createCorrectPositionTween(
  1586. controller.bounceAnimationTime
  1587. );
  1588. if (defined(tween)) {
  1589. controller._tween = tweens.add(tween);
  1590. }
  1591. }
  1592. tweens.update();
  1593. }
  1594. }
  1595. const scratchStrafeRay = new Ray();
  1596. const scratchStrafePlane = new Plane(Cartesian3.UNIT_X, 0.0);
  1597. const scratchStrafeIntersection = new Cartesian3();
  1598. const scratchStrafeDirection = new Cartesian3();
  1599. const scratchMousePos = new Cartesian3();
  1600. function strafe(controller, movement, strafeStartPosition) {
  1601. const scene = controller._scene;
  1602. const camera = scene.camera;
  1603. const ray = camera.getPickRay(movement.endPosition, scratchStrafeRay);
  1604. let direction = Cartesian3.clone(camera.direction, scratchStrafeDirection);
  1605. if (scene.mode === SceneMode.COLUMBUS_VIEW) {
  1606. Cartesian3.fromElements(direction.z, direction.x, direction.y, direction);
  1607. }
  1608. const plane = Plane.fromPointNormal(
  1609. strafeStartPosition,
  1610. direction,
  1611. scratchStrafePlane
  1612. );
  1613. const intersection = IntersectionTests.rayPlane(
  1614. ray,
  1615. plane,
  1616. scratchStrafeIntersection
  1617. );
  1618. if (!defined(intersection)) {
  1619. return;
  1620. }
  1621. direction = Cartesian3.subtract(strafeStartPosition, intersection, direction);
  1622. if (scene.mode === SceneMode.COLUMBUS_VIEW) {
  1623. Cartesian3.fromElements(direction.y, direction.z, direction.x, direction);
  1624. }
  1625. Cartesian3.add(camera.position, direction, camera.position);
  1626. }
  1627. const spin3DPick = new Cartesian3();
  1628. const scratchCartographic = new Cartographic();
  1629. const scratchRadii = new Cartesian3();
  1630. const scratchEllipsoid = new Ellipsoid();
  1631. const scratchLookUp = new Cartesian3();
  1632. const scratchNormal = new Cartesian3();
  1633. function spin3D(controller, startPosition, movement) {
  1634. const scene = controller._scene;
  1635. const camera = scene.camera;
  1636. const cameraUnderground = controller._cameraUnderground;
  1637. let ellipsoid = controller._ellipsoid;
  1638. if (!Matrix4.equals(camera.transform, Matrix4.IDENTITY)) {
  1639. rotate3D(controller, startPosition, movement);
  1640. return;
  1641. }
  1642. let magnitude;
  1643. let radii;
  1644. const up = ellipsoid.geodeticSurfaceNormal(camera.position, scratchLookUp);
  1645. if (Cartesian2.equals(startPosition, controller._rotateMousePosition)) {
  1646. if (controller._looking) {
  1647. look3D(controller, startPosition, movement, up);
  1648. } else if (controller._rotating) {
  1649. rotate3D(controller, startPosition, movement);
  1650. } else if (controller._strafing) {
  1651. continueStrafing(controller, movement);
  1652. } else {
  1653. if (
  1654. Cartesian3.magnitude(camera.position) <
  1655. Cartesian3.magnitude(controller._rotateStartPosition)
  1656. ) {
  1657. // Pan action is no longer valid if camera moves below the pan ellipsoid
  1658. return;
  1659. }
  1660. magnitude = Cartesian3.magnitude(controller._rotateStartPosition);
  1661. radii = scratchRadii;
  1662. radii.x = radii.y = radii.z = magnitude;
  1663. ellipsoid = Ellipsoid.fromCartesian3(radii, scratchEllipsoid);
  1664. pan3D(controller, startPosition, movement, ellipsoid);
  1665. }
  1666. return;
  1667. }
  1668. controller._looking = false;
  1669. controller._rotating = false;
  1670. controller._strafing = false;
  1671. const height = ellipsoid.cartesianToCartographic(
  1672. camera.positionWC,
  1673. scratchCartographic
  1674. ).height;
  1675. const globe = controller._globe;
  1676. if (defined(globe) && height < controller._minimumPickingTerrainHeight) {
  1677. const mousePos = pickGlobe(
  1678. controller,
  1679. movement.startPosition,
  1680. scratchMousePos
  1681. );
  1682. if (defined(mousePos)) {
  1683. let strafing = false;
  1684. const ray = camera.getPickRay(
  1685. movement.startPosition,
  1686. pickGlobeScratchRay
  1687. );
  1688. if (cameraUnderground) {
  1689. strafing = true;
  1690. getStrafeStartPositionUnderground(controller, ray, mousePos, mousePos);
  1691. } else {
  1692. const normal = ellipsoid.geodeticSurfaceNormal(mousePos, scratchNormal);
  1693. const tangentPick =
  1694. Math.abs(Cartesian3.dot(ray.direction, normal)) < 0.05;
  1695. if (tangentPick) {
  1696. strafing = true;
  1697. } else {
  1698. strafing =
  1699. Cartesian3.magnitude(camera.position) <
  1700. Cartesian3.magnitude(mousePos);
  1701. }
  1702. }
  1703. if (strafing) {
  1704. Cartesian2.clone(startPosition, controller._strafeEndMousePosition);
  1705. Cartesian3.clone(mousePos, controller._strafeStartPosition);
  1706. controller._strafing = true;
  1707. strafe(controller, movement, controller._strafeStartPosition);
  1708. } else {
  1709. magnitude = Cartesian3.magnitude(mousePos);
  1710. radii = scratchRadii;
  1711. radii.x = radii.y = radii.z = magnitude;
  1712. ellipsoid = Ellipsoid.fromCartesian3(radii, scratchEllipsoid);
  1713. pan3D(controller, startPosition, movement, ellipsoid);
  1714. Cartesian3.clone(mousePos, controller._rotateStartPosition);
  1715. }
  1716. } else {
  1717. controller._looking = true;
  1718. look3D(controller, startPosition, movement, up);
  1719. }
  1720. } else if (
  1721. defined(
  1722. camera.pickEllipsoid(
  1723. movement.startPosition,
  1724. controller._ellipsoid,
  1725. spin3DPick
  1726. )
  1727. )
  1728. ) {
  1729. pan3D(controller, startPosition, movement, controller._ellipsoid);
  1730. Cartesian3.clone(spin3DPick, controller._rotateStartPosition);
  1731. } else if (height > controller._minimumTrackBallHeight) {
  1732. controller._rotating = true;
  1733. rotate3D(controller, startPosition, movement);
  1734. } else {
  1735. controller._looking = true;
  1736. look3D(controller, startPosition, movement, up);
  1737. }
  1738. Cartesian2.clone(startPosition, controller._rotateMousePosition);
  1739. }
  1740. function rotate3D(
  1741. controller,
  1742. startPosition,
  1743. movement,
  1744. constrainedAxis,
  1745. rotateOnlyVertical,
  1746. rotateOnlyHorizontal
  1747. ) {
  1748. rotateOnlyVertical = defaultValue(rotateOnlyVertical, false);
  1749. rotateOnlyHorizontal = defaultValue(rotateOnlyHorizontal, false);
  1750. const scene = controller._scene;
  1751. const camera = scene.camera;
  1752. const canvas = scene.canvas;
  1753. const oldAxis = camera.constrainedAxis;
  1754. if (defined(constrainedAxis)) {
  1755. camera.constrainedAxis = constrainedAxis;
  1756. }
  1757. const rho = Cartesian3.magnitude(camera.position);
  1758. let rotateRate =
  1759. controller._rotateFactor * (rho - controller._rotateRateRangeAdjustment);
  1760. if (rotateRate > controller._maximumRotateRate) {
  1761. rotateRate = controller._maximumRotateRate;
  1762. }
  1763. if (rotateRate < controller._minimumRotateRate) {
  1764. rotateRate = controller._minimumRotateRate;
  1765. }
  1766. let phiWindowRatio =
  1767. (movement.startPosition.x - movement.endPosition.x) / canvas.clientWidth;
  1768. let thetaWindowRatio =
  1769. (movement.startPosition.y - movement.endPosition.y) / canvas.clientHeight;
  1770. phiWindowRatio = Math.min(phiWindowRatio, controller.maximumMovementRatio);
  1771. thetaWindowRatio = Math.min(
  1772. thetaWindowRatio,
  1773. controller.maximumMovementRatio
  1774. );
  1775. const deltaPhi = rotateRate * phiWindowRatio * Math.PI * 2.0;
  1776. const deltaTheta = rotateRate * thetaWindowRatio * Math.PI;
  1777. if (!rotateOnlyVertical) {
  1778. camera.rotateRight(deltaPhi);
  1779. }
  1780. if (!rotateOnlyHorizontal) {
  1781. camera.rotateUp(deltaTheta);
  1782. }
  1783. camera.constrainedAxis = oldAxis;
  1784. }
  1785. const pan3DP0 = Cartesian4.clone(Cartesian4.UNIT_W);
  1786. const pan3DP1 = Cartesian4.clone(Cartesian4.UNIT_W);
  1787. const pan3DTemp0 = new Cartesian3();
  1788. const pan3DTemp1 = new Cartesian3();
  1789. const pan3DTemp2 = new Cartesian3();
  1790. const pan3DTemp3 = new Cartesian3();
  1791. const pan3DStartMousePosition = new Cartesian2();
  1792. const pan3DEndMousePosition = new Cartesian2();
  1793. function pan3D(controller, startPosition, movement, ellipsoid) {
  1794. const scene = controller._scene;
  1795. const camera = scene.camera;
  1796. const startMousePosition = Cartesian2.clone(
  1797. movement.startPosition,
  1798. pan3DStartMousePosition
  1799. );
  1800. const endMousePosition = Cartesian2.clone(
  1801. movement.endPosition,
  1802. pan3DEndMousePosition
  1803. );
  1804. let p0 = camera.pickEllipsoid(startMousePosition, ellipsoid, pan3DP0);
  1805. let p1 = camera.pickEllipsoid(endMousePosition, ellipsoid, pan3DP1);
  1806. if (!defined(p0) || !defined(p1)) {
  1807. controller._rotating = true;
  1808. rotate3D(controller, startPosition, movement);
  1809. return;
  1810. }
  1811. p0 = camera.worldToCameraCoordinates(p0, p0);
  1812. p1 = camera.worldToCameraCoordinates(p1, p1);
  1813. if (!defined(camera.constrainedAxis)) {
  1814. Cartesian3.normalize(p0, p0);
  1815. Cartesian3.normalize(p1, p1);
  1816. const dot = Cartesian3.dot(p0, p1);
  1817. const axis = Cartesian3.cross(p0, p1, pan3DTemp0);
  1818. if (
  1819. dot < 1.0 &&
  1820. !Cartesian3.equalsEpsilon(axis, Cartesian3.ZERO, CesiumMath.EPSILON14)
  1821. ) {
  1822. // dot is in [0, 1]
  1823. const angle = Math.acos(dot);
  1824. camera.rotate(axis, angle);
  1825. }
  1826. } else {
  1827. const basis0 = camera.constrainedAxis;
  1828. const basis1 = Cartesian3.mostOrthogonalAxis(basis0, pan3DTemp0);
  1829. Cartesian3.cross(basis1, basis0, basis1);
  1830. Cartesian3.normalize(basis1, basis1);
  1831. const basis2 = Cartesian3.cross(basis0, basis1, pan3DTemp1);
  1832. const startRho = Cartesian3.magnitude(p0);
  1833. const startDot = Cartesian3.dot(basis0, p0);
  1834. const startTheta = Math.acos(startDot / startRho);
  1835. const startRej = Cartesian3.multiplyByScalar(basis0, startDot, pan3DTemp2);
  1836. Cartesian3.subtract(p0, startRej, startRej);
  1837. Cartesian3.normalize(startRej, startRej);
  1838. const endRho = Cartesian3.magnitude(p1);
  1839. const endDot = Cartesian3.dot(basis0, p1);
  1840. const endTheta = Math.acos(endDot / endRho);
  1841. const endRej = Cartesian3.multiplyByScalar(basis0, endDot, pan3DTemp3);
  1842. Cartesian3.subtract(p1, endRej, endRej);
  1843. Cartesian3.normalize(endRej, endRej);
  1844. let startPhi = Math.acos(Cartesian3.dot(startRej, basis1));
  1845. if (Cartesian3.dot(startRej, basis2) < 0) {
  1846. startPhi = CesiumMath.TWO_PI - startPhi;
  1847. }
  1848. let endPhi = Math.acos(Cartesian3.dot(endRej, basis1));
  1849. if (Cartesian3.dot(endRej, basis2) < 0) {
  1850. endPhi = CesiumMath.TWO_PI - endPhi;
  1851. }
  1852. const deltaPhi = startPhi - endPhi;
  1853. let east;
  1854. if (
  1855. Cartesian3.equalsEpsilon(basis0, camera.position, CesiumMath.EPSILON2)
  1856. ) {
  1857. east = camera.right;
  1858. } else {
  1859. east = Cartesian3.cross(basis0, camera.position, pan3DTemp0);
  1860. }
  1861. const planeNormal = Cartesian3.cross(basis0, east, pan3DTemp0);
  1862. const side0 = Cartesian3.dot(
  1863. planeNormal,
  1864. Cartesian3.subtract(p0, basis0, pan3DTemp1)
  1865. );
  1866. const side1 = Cartesian3.dot(
  1867. planeNormal,
  1868. Cartesian3.subtract(p1, basis0, pan3DTemp1)
  1869. );
  1870. let deltaTheta;
  1871. if (side0 > 0 && side1 > 0) {
  1872. deltaTheta = endTheta - startTheta;
  1873. } else if (side0 > 0 && side1 <= 0) {
  1874. if (Cartesian3.dot(camera.position, basis0) > 0) {
  1875. deltaTheta = -startTheta - endTheta;
  1876. } else {
  1877. deltaTheta = startTheta + endTheta;
  1878. }
  1879. } else {
  1880. deltaTheta = startTheta - endTheta;
  1881. }
  1882. camera.rotateRight(deltaPhi);
  1883. camera.rotateUp(deltaTheta);
  1884. }
  1885. }
  1886. const zoom3DUnitPosition = new Cartesian3();
  1887. const zoom3DCartographic = new Cartographic();
  1888. function zoom3D(controller, startPosition, movement) {
  1889. if (defined(movement.distance)) {
  1890. movement = movement.distance;
  1891. }
  1892. const ellipsoid = controller._ellipsoid;
  1893. const scene = controller._scene;
  1894. const camera = scene.camera;
  1895. const canvas = scene.canvas;
  1896. const cameraUnderground = controller._cameraUnderground;
  1897. let windowPosition;
  1898. if (cameraUnderground) {
  1899. windowPosition = startPosition;
  1900. } else {
  1901. windowPosition = zoomCVWindowPos;
  1902. windowPosition.x = canvas.clientWidth / 2;
  1903. windowPosition.y = canvas.clientHeight / 2;
  1904. }
  1905. const ray = camera.getPickRay(windowPosition, zoomCVWindowRay);
  1906. let intersection;
  1907. const height = ellipsoid.cartesianToCartographic(
  1908. camera.position,
  1909. zoom3DCartographic
  1910. ).height;
  1911. if (height < controller._minimumPickingTerrainHeight) {
  1912. intersection = pickGlobe(controller, windowPosition, zoomCVIntersection);
  1913. }
  1914. let distance;
  1915. if (defined(intersection)) {
  1916. distance = Cartesian3.distance(ray.origin, intersection);
  1917. }
  1918. if (cameraUnderground) {
  1919. const distanceUnderground = getZoomDistanceUnderground(
  1920. controller,
  1921. ray,
  1922. height
  1923. );
  1924. if (defined(distance)) {
  1925. distance = Math.min(distance, distanceUnderground);
  1926. } else {
  1927. distance = distanceUnderground;
  1928. }
  1929. }
  1930. if (!defined(distance)) {
  1931. distance = height;
  1932. }
  1933. const unitPosition = Cartesian3.normalize(
  1934. camera.position,
  1935. zoom3DUnitPosition
  1936. );
  1937. handleZoom(
  1938. controller,
  1939. startPosition,
  1940. movement,
  1941. controller._zoomFactor,
  1942. distance,
  1943. Cartesian3.dot(unitPosition, camera.direction)
  1944. );
  1945. }
  1946. const tilt3DWindowPos = new Cartesian2();
  1947. const tilt3DRay = new Ray();
  1948. const tilt3DCenter = new Cartesian3();
  1949. const tilt3DVerticalCenter = new Cartesian3();
  1950. const tilt3DTransform = new Matrix4();
  1951. const tilt3DVerticalTransform = new Matrix4();
  1952. const tilt3DOldTransform = new Matrix4();
  1953. const tilt3DQuaternion = new Quaternion();
  1954. const tilt3DMatrix = new Matrix3();
  1955. const tilt3DCart = new Cartographic();
  1956. const tilt3DLookUp = new Cartesian3();
  1957. function tilt3D(controller, startPosition, movement) {
  1958. const scene = controller._scene;
  1959. const camera = scene.camera;
  1960. if (!Matrix4.equals(camera.transform, Matrix4.IDENTITY)) {
  1961. return;
  1962. }
  1963. if (defined(movement.angleAndHeight)) {
  1964. movement = movement.angleAndHeight;
  1965. }
  1966. if (!Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
  1967. controller._tiltOnEllipsoid = false;
  1968. controller._looking = false;
  1969. }
  1970. if (controller._looking) {
  1971. const up = controller._ellipsoid.geodeticSurfaceNormal(
  1972. camera.position,
  1973. tilt3DLookUp
  1974. );
  1975. look3D(controller, startPosition, movement, up);
  1976. return;
  1977. }
  1978. const ellipsoid = controller._ellipsoid;
  1979. const cartographic = ellipsoid.cartesianToCartographic(
  1980. camera.position,
  1981. tilt3DCart
  1982. );
  1983. if (
  1984. controller._tiltOnEllipsoid ||
  1985. cartographic.height > controller._minimumCollisionTerrainHeight
  1986. ) {
  1987. controller._tiltOnEllipsoid = true;
  1988. tilt3DOnEllipsoid(controller, startPosition, movement);
  1989. } else {
  1990. tilt3DOnTerrain(controller, startPosition, movement);
  1991. }
  1992. }
  1993. const tilt3DOnEllipsoidCartographic = new Cartographic();
  1994. function tilt3DOnEllipsoid(controller, startPosition, movement) {
  1995. const ellipsoid = controller._ellipsoid;
  1996. const scene = controller._scene;
  1997. const camera = scene.camera;
  1998. const minHeight = controller.minimumZoomDistance * 0.25;
  1999. const height = ellipsoid.cartesianToCartographic(
  2000. camera.positionWC,
  2001. tilt3DOnEllipsoidCartographic
  2002. ).height;
  2003. if (
  2004. height - minHeight - 1.0 < CesiumMath.EPSILON3 &&
  2005. movement.endPosition.y - movement.startPosition.y < 0
  2006. ) {
  2007. return;
  2008. }
  2009. const canvas = scene.canvas;
  2010. const windowPosition = tilt3DWindowPos;
  2011. windowPosition.x = canvas.clientWidth / 2;
  2012. windowPosition.y = canvas.clientHeight / 2;
  2013. const ray = camera.getPickRay(windowPosition, tilt3DRay);
  2014. let center;
  2015. const intersection = IntersectionTests.rayEllipsoid(ray, ellipsoid);
  2016. if (defined(intersection)) {
  2017. center = Ray.getPoint(ray, intersection.start, tilt3DCenter);
  2018. } else if (height > controller._minimumTrackBallHeight) {
  2019. const grazingAltitudeLocation = IntersectionTests.grazingAltitudeLocation(
  2020. ray,
  2021. ellipsoid
  2022. );
  2023. if (!defined(grazingAltitudeLocation)) {
  2024. return;
  2025. }
  2026. const grazingAltitudeCart = ellipsoid.cartesianToCartographic(
  2027. grazingAltitudeLocation,
  2028. tilt3DCart
  2029. );
  2030. grazingAltitudeCart.height = 0.0;
  2031. center = ellipsoid.cartographicToCartesian(
  2032. grazingAltitudeCart,
  2033. tilt3DCenter
  2034. );
  2035. } else {
  2036. controller._looking = true;
  2037. const up = controller._ellipsoid.geodeticSurfaceNormal(
  2038. camera.position,
  2039. tilt3DLookUp
  2040. );
  2041. look3D(controller, startPosition, movement, up);
  2042. Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
  2043. return;
  2044. }
  2045. const transform = Transforms.eastNorthUpToFixedFrame(
  2046. center,
  2047. ellipsoid,
  2048. tilt3DTransform
  2049. );
  2050. const oldGlobe = controller._globe;
  2051. const oldEllipsoid = controller._ellipsoid;
  2052. controller._globe = undefined;
  2053. controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
  2054. controller._rotateFactor = 1.0;
  2055. controller._rotateRateRangeAdjustment = 1.0;
  2056. const oldTransform = Matrix4.clone(camera.transform, tilt3DOldTransform);
  2057. camera._setTransform(transform);
  2058. rotate3D(controller, startPosition, movement, Cartesian3.UNIT_Z);
  2059. camera._setTransform(oldTransform);
  2060. controller._globe = oldGlobe;
  2061. controller._ellipsoid = oldEllipsoid;
  2062. const radius = oldEllipsoid.maximumRadius;
  2063. controller._rotateFactor = 1.0 / radius;
  2064. controller._rotateRateRangeAdjustment = radius;
  2065. }
  2066. function tilt3DOnTerrain(controller, startPosition, movement) {
  2067. const ellipsoid = controller._ellipsoid;
  2068. const scene = controller._scene;
  2069. const camera = scene.camera;
  2070. const cameraUnderground = controller._cameraUnderground;
  2071. let center;
  2072. let ray;
  2073. let intersection;
  2074. if (Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
  2075. center = Cartesian3.clone(controller._tiltCenter, tilt3DCenter);
  2076. } else {
  2077. center = pickGlobe(controller, startPosition, tilt3DCenter);
  2078. if (!defined(center)) {
  2079. ray = camera.getPickRay(startPosition, tilt3DRay);
  2080. intersection = IntersectionTests.rayEllipsoid(ray, ellipsoid);
  2081. if (!defined(intersection)) {
  2082. const cartographic = ellipsoid.cartesianToCartographic(
  2083. camera.position,
  2084. tilt3DCart
  2085. );
  2086. if (cartographic.height <= controller._minimumTrackBallHeight) {
  2087. controller._looking = true;
  2088. const up = controller._ellipsoid.geodeticSurfaceNormal(
  2089. camera.position,
  2090. tilt3DLookUp
  2091. );
  2092. look3D(controller, startPosition, movement, up);
  2093. Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
  2094. }
  2095. return;
  2096. }
  2097. center = Ray.getPoint(ray, intersection.start, tilt3DCenter);
  2098. }
  2099. if (cameraUnderground) {
  2100. if (!defined(ray)) {
  2101. ray = camera.getPickRay(startPosition, tilt3DRay);
  2102. }
  2103. getTiltCenterUnderground(controller, ray, center, center);
  2104. }
  2105. Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
  2106. Cartesian3.clone(center, controller._tiltCenter);
  2107. }
  2108. const canvas = scene.canvas;
  2109. const windowPosition = tilt3DWindowPos;
  2110. windowPosition.x = canvas.clientWidth / 2;
  2111. windowPosition.y = controller._tiltCenterMousePosition.y;
  2112. ray = camera.getPickRay(windowPosition, tilt3DRay);
  2113. const mag = Cartesian3.magnitude(center);
  2114. const radii = Cartesian3.fromElements(mag, mag, mag, scratchRadii);
  2115. const newEllipsoid = Ellipsoid.fromCartesian3(radii, scratchEllipsoid);
  2116. intersection = IntersectionTests.rayEllipsoid(ray, newEllipsoid);
  2117. if (!defined(intersection)) {
  2118. return;
  2119. }
  2120. const t =
  2121. Cartesian3.magnitude(ray.origin) > mag
  2122. ? intersection.start
  2123. : intersection.stop;
  2124. const verticalCenter = Ray.getPoint(ray, t, tilt3DVerticalCenter);
  2125. const transform = Transforms.eastNorthUpToFixedFrame(
  2126. center,
  2127. ellipsoid,
  2128. tilt3DTransform
  2129. );
  2130. const verticalTransform = Transforms.eastNorthUpToFixedFrame(
  2131. verticalCenter,
  2132. newEllipsoid,
  2133. tilt3DVerticalTransform
  2134. );
  2135. const oldGlobe = controller._globe;
  2136. const oldEllipsoid = controller._ellipsoid;
  2137. controller._globe = undefined;
  2138. controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
  2139. controller._rotateFactor = 1.0;
  2140. controller._rotateRateRangeAdjustment = 1.0;
  2141. let constrainedAxis = Cartesian3.UNIT_Z;
  2142. const oldTransform = Matrix4.clone(camera.transform, tilt3DOldTransform);
  2143. camera._setTransform(verticalTransform);
  2144. const tangent = Cartesian3.cross(
  2145. verticalCenter,
  2146. camera.positionWC,
  2147. tilt3DCartesian3
  2148. );
  2149. const dot = Cartesian3.dot(camera.rightWC, tangent);
  2150. if (dot < 0.0) {
  2151. const movementDelta = movement.startPosition.y - movement.endPosition.y;
  2152. if (
  2153. (cameraUnderground && movementDelta < 0.0) ||
  2154. (!cameraUnderground && movementDelta > 0.0)
  2155. ) {
  2156. // Prevent camera from flipping past the up axis
  2157. constrainedAxis = undefined;
  2158. }
  2159. const oldConstrainedAxis = camera.constrainedAxis;
  2160. camera.constrainedAxis = undefined;
  2161. rotate3D(controller, startPosition, movement, constrainedAxis, true, false);
  2162. camera.constrainedAxis = oldConstrainedAxis;
  2163. } else {
  2164. rotate3D(controller, startPosition, movement, constrainedAxis, true, false);
  2165. }
  2166. camera._setTransform(transform);
  2167. rotate3D(controller, startPosition, movement, constrainedAxis, false, true);
  2168. if (defined(camera.constrainedAxis)) {
  2169. const right = Cartesian3.cross(
  2170. camera.direction,
  2171. camera.constrainedAxis,
  2172. tilt3DCartesian3
  2173. );
  2174. if (
  2175. !Cartesian3.equalsEpsilon(right, Cartesian3.ZERO, CesiumMath.EPSILON6)
  2176. ) {
  2177. if (Cartesian3.dot(right, camera.right) < 0.0) {
  2178. Cartesian3.negate(right, right);
  2179. }
  2180. Cartesian3.cross(right, camera.direction, camera.up);
  2181. Cartesian3.cross(camera.direction, camera.up, camera.right);
  2182. Cartesian3.normalize(camera.up, camera.up);
  2183. Cartesian3.normalize(camera.right, camera.right);
  2184. }
  2185. }
  2186. camera._setTransform(oldTransform);
  2187. controller._globe = oldGlobe;
  2188. controller._ellipsoid = oldEllipsoid;
  2189. const radius = oldEllipsoid.maximumRadius;
  2190. controller._rotateFactor = 1.0 / radius;
  2191. controller._rotateRateRangeAdjustment = radius;
  2192. const originalPosition = Cartesian3.clone(
  2193. camera.positionWC,
  2194. tilt3DCartesian3
  2195. );
  2196. if (controller.enableCollisionDetection) {
  2197. adjustHeightForTerrain(controller);
  2198. }
  2199. if (!Cartesian3.equals(camera.positionWC, originalPosition)) {
  2200. camera._setTransform(verticalTransform);
  2201. camera.worldToCameraCoordinatesPoint(originalPosition, originalPosition);
  2202. const magSqrd = Cartesian3.magnitudeSquared(originalPosition);
  2203. if (Cartesian3.magnitudeSquared(camera.position) > magSqrd) {
  2204. Cartesian3.normalize(camera.position, camera.position);
  2205. Cartesian3.multiplyByScalar(
  2206. camera.position,
  2207. Math.sqrt(magSqrd),
  2208. camera.position
  2209. );
  2210. }
  2211. const angle = Cartesian3.angleBetween(originalPosition, camera.position);
  2212. const axis = Cartesian3.cross(
  2213. originalPosition,
  2214. camera.position,
  2215. originalPosition
  2216. );
  2217. Cartesian3.normalize(axis, axis);
  2218. const quaternion = Quaternion.fromAxisAngle(axis, angle, tilt3DQuaternion);
  2219. const rotation = Matrix3.fromQuaternion(quaternion, tilt3DMatrix);
  2220. Matrix3.multiplyByVector(rotation, camera.direction, camera.direction);
  2221. Matrix3.multiplyByVector(rotation, camera.up, camera.up);
  2222. Cartesian3.cross(camera.direction, camera.up, camera.right);
  2223. Cartesian3.cross(camera.right, camera.direction, camera.up);
  2224. camera._setTransform(oldTransform);
  2225. }
  2226. }
  2227. const look3DStartPos = new Cartesian2();
  2228. const look3DEndPos = new Cartesian2();
  2229. const look3DStartRay = new Ray();
  2230. const look3DEndRay = new Ray();
  2231. const look3DNegativeRot = new Cartesian3();
  2232. const look3DTan = new Cartesian3();
  2233. function look3D(controller, startPosition, movement, rotationAxis) {
  2234. const scene = controller._scene;
  2235. const camera = scene.camera;
  2236. const startPos = look3DStartPos;
  2237. startPos.x = movement.startPosition.x;
  2238. startPos.y = 0.0;
  2239. const endPos = look3DEndPos;
  2240. endPos.x = movement.endPosition.x;
  2241. endPos.y = 0.0;
  2242. let startRay = camera.getPickRay(startPos, look3DStartRay);
  2243. let endRay = camera.getPickRay(endPos, look3DEndRay);
  2244. let angle = 0.0;
  2245. let start;
  2246. let end;
  2247. if (camera.frustum instanceof OrthographicFrustum) {
  2248. start = startRay.origin;
  2249. end = endRay.origin;
  2250. Cartesian3.add(camera.direction, start, start);
  2251. Cartesian3.add(camera.direction, end, end);
  2252. Cartesian3.subtract(start, camera.position, start);
  2253. Cartesian3.subtract(end, camera.position, end);
  2254. Cartesian3.normalize(start, start);
  2255. Cartesian3.normalize(end, end);
  2256. } else {
  2257. start = startRay.direction;
  2258. end = endRay.direction;
  2259. }
  2260. let dot = Cartesian3.dot(start, end);
  2261. if (dot < 1.0) {
  2262. // dot is in [0, 1]
  2263. angle = Math.acos(dot);
  2264. }
  2265. angle = movement.startPosition.x > movement.endPosition.x ? -angle : angle;
  2266. const horizontalRotationAxis = controller._horizontalRotationAxis;
  2267. if (defined(rotationAxis)) {
  2268. camera.look(rotationAxis, -angle);
  2269. } else if (defined(horizontalRotationAxis)) {
  2270. camera.look(horizontalRotationAxis, -angle);
  2271. } else {
  2272. camera.lookLeft(angle);
  2273. }
  2274. startPos.x = 0.0;
  2275. startPos.y = movement.startPosition.y;
  2276. endPos.x = 0.0;
  2277. endPos.y = movement.endPosition.y;
  2278. startRay = camera.getPickRay(startPos, look3DStartRay);
  2279. endRay = camera.getPickRay(endPos, look3DEndRay);
  2280. angle = 0.0;
  2281. if (camera.frustum instanceof OrthographicFrustum) {
  2282. start = startRay.origin;
  2283. end = endRay.origin;
  2284. Cartesian3.add(camera.direction, start, start);
  2285. Cartesian3.add(camera.direction, end, end);
  2286. Cartesian3.subtract(start, camera.position, start);
  2287. Cartesian3.subtract(end, camera.position, end);
  2288. Cartesian3.normalize(start, start);
  2289. Cartesian3.normalize(end, end);
  2290. } else {
  2291. start = startRay.direction;
  2292. end = endRay.direction;
  2293. }
  2294. dot = Cartesian3.dot(start, end);
  2295. if (dot < 1.0) {
  2296. // dot is in [0, 1]
  2297. angle = Math.acos(dot);
  2298. }
  2299. angle = movement.startPosition.y > movement.endPosition.y ? -angle : angle;
  2300. rotationAxis = defaultValue(rotationAxis, horizontalRotationAxis);
  2301. if (defined(rotationAxis)) {
  2302. const direction = camera.direction;
  2303. const negativeRotationAxis = Cartesian3.negate(
  2304. rotationAxis,
  2305. look3DNegativeRot
  2306. );
  2307. const northParallel = Cartesian3.equalsEpsilon(
  2308. direction,
  2309. rotationAxis,
  2310. CesiumMath.EPSILON2
  2311. );
  2312. const southParallel = Cartesian3.equalsEpsilon(
  2313. direction,
  2314. negativeRotationAxis,
  2315. CesiumMath.EPSILON2
  2316. );
  2317. if (!northParallel && !southParallel) {
  2318. dot = Cartesian3.dot(direction, rotationAxis);
  2319. let angleToAxis = CesiumMath.acosClamped(dot);
  2320. if (angle > 0 && angle > angleToAxis) {
  2321. angle = angleToAxis - CesiumMath.EPSILON4;
  2322. }
  2323. dot = Cartesian3.dot(direction, negativeRotationAxis);
  2324. angleToAxis = CesiumMath.acosClamped(dot);
  2325. if (angle < 0 && -angle > angleToAxis) {
  2326. angle = -angleToAxis + CesiumMath.EPSILON4;
  2327. }
  2328. const tangent = Cartesian3.cross(rotationAxis, direction, look3DTan);
  2329. camera.look(tangent, angle);
  2330. } else if ((northParallel && angle < 0) || (southParallel && angle > 0)) {
  2331. camera.look(camera.right, -angle);
  2332. }
  2333. } else {
  2334. camera.lookUp(angle);
  2335. }
  2336. }
  2337. function update3D(controller) {
  2338. reactToInput(
  2339. controller,
  2340. controller.enableRotate,
  2341. controller.rotateEventTypes,
  2342. spin3D,
  2343. controller.inertiaSpin,
  2344. "_lastInertiaSpinMovement"
  2345. );
  2346. reactToInput(
  2347. controller,
  2348. controller.enableZoom,
  2349. controller.zoomEventTypes,
  2350. zoom3D,
  2351. controller.inertiaZoom,
  2352. "_lastInertiaZoomMovement"
  2353. );
  2354. reactToInput(
  2355. controller,
  2356. controller.enableTilt,
  2357. controller.tiltEventTypes,
  2358. tilt3D,
  2359. controller.inertiaSpin,
  2360. "_lastInertiaTiltMovement"
  2361. );
  2362. reactToInput(
  2363. controller,
  2364. controller.enableLook,
  2365. controller.lookEventTypes,
  2366. look3D
  2367. );
  2368. }
  2369. const scratchAdjustHeightTransform = new Matrix4();
  2370. const scratchAdjustHeightCartographic = new Cartographic();
  2371. function adjustHeightForTerrain(controller) {
  2372. controller._adjustedHeightForTerrain = true;
  2373. const scene = controller._scene;
  2374. const mode = scene.mode;
  2375. const globe = scene.globe;
  2376. if (
  2377. !defined(globe) ||
  2378. mode === SceneMode.SCENE2D ||
  2379. mode === SceneMode.MORPHING
  2380. ) {
  2381. return;
  2382. }
  2383. const camera = scene.camera;
  2384. const ellipsoid = globe.ellipsoid;
  2385. const projection = scene.mapProjection;
  2386. let transform;
  2387. let mag;
  2388. if (!Matrix4.equals(camera.transform, Matrix4.IDENTITY)) {
  2389. transform = Matrix4.clone(camera.transform, scratchAdjustHeightTransform);
  2390. mag = Cartesian3.magnitude(camera.position);
  2391. camera._setTransform(Matrix4.IDENTITY);
  2392. }
  2393. const cartographic = scratchAdjustHeightCartographic;
  2394. if (mode === SceneMode.SCENE3D) {
  2395. ellipsoid.cartesianToCartographic(camera.position, cartographic);
  2396. } else {
  2397. projection.unproject(camera.position, cartographic);
  2398. }
  2399. let heightUpdated = false;
  2400. if (cartographic.height < controller._minimumCollisionTerrainHeight) {
  2401. const globeHeight = controller._scene.globeHeight;
  2402. if (defined(globeHeight)) {
  2403. const height = globeHeight + controller.minimumZoomDistance;
  2404. if (cartographic.height < height) {
  2405. cartographic.height = height;
  2406. if (mode === SceneMode.SCENE3D) {
  2407. ellipsoid.cartographicToCartesian(cartographic, camera.position);
  2408. } else {
  2409. projection.project(cartographic, camera.position);
  2410. }
  2411. heightUpdated = true;
  2412. }
  2413. }
  2414. }
  2415. if (defined(transform)) {
  2416. camera._setTransform(transform);
  2417. if (heightUpdated) {
  2418. Cartesian3.normalize(camera.position, camera.position);
  2419. Cartesian3.negate(camera.position, camera.direction);
  2420. Cartesian3.multiplyByScalar(
  2421. camera.position,
  2422. Math.max(mag, controller.minimumZoomDistance),
  2423. camera.position
  2424. );
  2425. Cartesian3.normalize(camera.direction, camera.direction);
  2426. Cartesian3.cross(camera.direction, camera.up, camera.right);
  2427. Cartesian3.cross(camera.right, camera.direction, camera.up);
  2428. }
  2429. }
  2430. }
  2431. /**
  2432. * @private
  2433. */
  2434. ScreenSpaceCameraController.prototype.onMap = function () {
  2435. const scene = this._scene;
  2436. const mode = scene.mode;
  2437. const camera = scene.camera;
  2438. if (mode === SceneMode.COLUMBUS_VIEW) {
  2439. return (
  2440. Math.abs(camera.position.x) - this._maxCoord.x < 0 &&
  2441. Math.abs(camera.position.y) - this._maxCoord.y < 0
  2442. );
  2443. }
  2444. return true;
  2445. };
  2446. const scratchPreviousPosition = new Cartesian3();
  2447. const scratchPreviousDirection = new Cartesian3();
  2448. /**
  2449. * @private
  2450. */
  2451. ScreenSpaceCameraController.prototype.update = function () {
  2452. const scene = this._scene;
  2453. const camera = scene.camera;
  2454. const globe = scene.globe;
  2455. const mode = scene.mode;
  2456. if (!Matrix4.equals(camera.transform, Matrix4.IDENTITY)) {
  2457. this._globe = undefined;
  2458. this._ellipsoid = Ellipsoid.UNIT_SPHERE;
  2459. } else {
  2460. this._globe = globe;
  2461. this._ellipsoid = defined(this._globe)
  2462. ? this._globe.ellipsoid
  2463. : scene.mapProjection.ellipsoid;
  2464. }
  2465. const exaggeration = defined(this._globe)
  2466. ? this._globe.terrainExaggeration
  2467. : 1.0;
  2468. const exaggerationRelativeHeight = defined(this._globe)
  2469. ? this._globe.terrainExaggerationRelativeHeight
  2470. : 0.0;
  2471. this._minimumCollisionTerrainHeight = TerrainExaggeration.getHeight(
  2472. this.minimumCollisionTerrainHeight,
  2473. exaggeration,
  2474. exaggerationRelativeHeight
  2475. );
  2476. this._minimumPickingTerrainHeight = TerrainExaggeration.getHeight(
  2477. this.minimumPickingTerrainHeight,
  2478. exaggeration,
  2479. exaggerationRelativeHeight
  2480. );
  2481. this._minimumTrackBallHeight = TerrainExaggeration.getHeight(
  2482. this.minimumTrackBallHeight,
  2483. exaggeration,
  2484. exaggerationRelativeHeight
  2485. );
  2486. this._cameraUnderground = scene.cameraUnderground && defined(this._globe);
  2487. const radius = this._ellipsoid.maximumRadius;
  2488. this._rotateFactor = 1.0 / radius;
  2489. this._rotateRateRangeAdjustment = radius;
  2490. this._adjustedHeightForTerrain = false;
  2491. const previousPosition = Cartesian3.clone(
  2492. camera.positionWC,
  2493. scratchPreviousPosition
  2494. );
  2495. const previousDirection = Cartesian3.clone(
  2496. camera.directionWC,
  2497. scratchPreviousDirection
  2498. );
  2499. if (mode === SceneMode.SCENE2D) {
  2500. update2D(this);
  2501. } else if (mode === SceneMode.COLUMBUS_VIEW) {
  2502. this._horizontalRotationAxis = Cartesian3.UNIT_Z;
  2503. updateCV(this);
  2504. } else if (mode === SceneMode.SCENE3D) {
  2505. this._horizontalRotationAxis = undefined;
  2506. update3D(this);
  2507. }
  2508. if (this.enableCollisionDetection && !this._adjustedHeightForTerrain) {
  2509. // Adjust the camera height if the camera moved at all (user input or inertia) and an action didn't already adjust the camera height
  2510. const cameraChanged =
  2511. !Cartesian3.equals(previousPosition, camera.positionWC) ||
  2512. !Cartesian3.equals(previousDirection, camera.directionWC);
  2513. if (cameraChanged) {
  2514. adjustHeightForTerrain(this);
  2515. }
  2516. }
  2517. this._aggregator.reset();
  2518. };
  2519. /**
  2520. * Returns true if this object was destroyed; otherwise, false.
  2521. * <br /><br />
  2522. * If this object was destroyed, it should not be used; calling any function other than
  2523. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  2524. *
  2525. * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  2526. *
  2527. * @see ScreenSpaceCameraController#destroy
  2528. */
  2529. ScreenSpaceCameraController.prototype.isDestroyed = function () {
  2530. return false;
  2531. };
  2532. /**
  2533. * Removes mouse listeners held by this object.
  2534. * <br /><br />
  2535. * Once an object is destroyed, it should not be used; calling any function other than
  2536. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  2537. * assign the return value (<code>undefined</code>) to the object as done in the example.
  2538. *
  2539. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  2540. *
  2541. *
  2542. * @example
  2543. * controller = controller && controller.destroy();
  2544. *
  2545. * @see ScreenSpaceCameraController#isDestroyed
  2546. */
  2547. ScreenSpaceCameraController.prototype.destroy = function () {
  2548. this._tweens.removeAll();
  2549. this._aggregator = this._aggregator && this._aggregator.destroy();
  2550. return destroyObject(this);
  2551. };
  2552. export default ScreenSpaceCameraController;