智慧水务管理系统 - 精河县供水工程综合管理平台

ScreenSpaceCameraController.js 95KB

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