ScreenSpaceCameraController.js 89 KB

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