UniformState.js 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. import BoundingRectangle from '../Core/BoundingRectangle.js';
  2. import Cartesian2 from '../Core/Cartesian2.js';
  3. import Cartesian3 from '../Core/Cartesian3.js';
  4. import Cartesian4 from '../Core/Cartesian4.js';
  5. import Cartographic from '../Core/Cartographic.js';
  6. import Color from '../Core/Color.js';
  7. import defaultValue from '../Core/defaultValue.js';
  8. import defined from '../Core/defined.js';
  9. import defineProperties from '../Core/defineProperties.js';
  10. import EncodedCartesian3 from '../Core/EncodedCartesian3.js';
  11. import CesiumMath from '../Core/Math.js';
  12. import Matrix3 from '../Core/Matrix3.js';
  13. import Matrix4 from '../Core/Matrix4.js';
  14. import OrthographicFrustum from '../Core/OrthographicFrustum.js';
  15. import Simon1994PlanetaryPositions from '../Core/Simon1994PlanetaryPositions.js';
  16. import Transforms from '../Core/Transforms.js';
  17. import SceneMode from '../Scene/SceneMode.js';
  18. /**
  19. * @private
  20. */
  21. function UniformState() {
  22. /**
  23. * @type {Texture}
  24. */
  25. this.globeDepthTexture = undefined;
  26. /**
  27. * @type {Number}
  28. */
  29. this.gamma = undefined;
  30. this._viewport = new BoundingRectangle();
  31. this._viewportCartesian4 = new Cartesian4();
  32. this._viewportDirty = false;
  33. this._viewportOrthographicMatrix = Matrix4.clone(Matrix4.IDENTITY);
  34. this._viewportTransformation = Matrix4.clone(Matrix4.IDENTITY);
  35. this._model = Matrix4.clone(Matrix4.IDENTITY);
  36. this._view = Matrix4.clone(Matrix4.IDENTITY);
  37. this._inverseView = Matrix4.clone(Matrix4.IDENTITY);
  38. this._projection = Matrix4.clone(Matrix4.IDENTITY);
  39. this._infiniteProjection = Matrix4.clone(Matrix4.IDENTITY);
  40. this._entireFrustum = new Cartesian2();
  41. this._currentFrustum = new Cartesian2();
  42. this._frustumPlanes = new Cartesian4();
  43. this._log2FarDistance = undefined;
  44. this._log2FarPlusOne = undefined;
  45. this._log2NearDistance = undefined;
  46. this._frameState = undefined;
  47. this._temeToPseudoFixed = Matrix3.clone(Matrix4.IDENTITY);
  48. // Derived members
  49. this._view3DDirty = true;
  50. this._view3D = new Matrix4();
  51. this._inverseView3DDirty = true;
  52. this._inverseView3D = new Matrix4();
  53. this._inverseModelDirty = true;
  54. this._inverseModel = new Matrix4();
  55. this._inverseTransposeModelDirty = true;
  56. this._inverseTransposeModel = new Matrix3();
  57. this._viewRotation = new Matrix3();
  58. this._inverseViewRotation = new Matrix3();
  59. this._viewRotation3D = new Matrix3();
  60. this._inverseViewRotation3D = new Matrix3();
  61. this._inverseProjectionDirty = true;
  62. this._inverseProjection = new Matrix4();
  63. this._modelViewDirty = true;
  64. this._modelView = new Matrix4();
  65. this._modelView3DDirty = true;
  66. this._modelView3D = new Matrix4();
  67. this._modelViewRelativeToEyeDirty = true;
  68. this._modelViewRelativeToEye = new Matrix4();
  69. this._inverseModelViewDirty = true;
  70. this._inverseModelView = new Matrix4();
  71. this._inverseModelView3DDirty = true;
  72. this._inverseModelView3D = new Matrix4();
  73. this._viewProjectionDirty = true;
  74. this._viewProjection = new Matrix4();
  75. this._inverseViewProjectionDirty = true;
  76. this._inverseViewProjection = new Matrix4();
  77. this._modelViewProjectionDirty = true;
  78. this._modelViewProjection = new Matrix4();
  79. this._inverseModelViewProjectionDirty = true;
  80. this._inverseModelViewProjection = new Matrix4();
  81. this._modelViewProjectionRelativeToEyeDirty = true;
  82. this._modelViewProjectionRelativeToEye = new Matrix4();
  83. this._modelViewInfiniteProjectionDirty = true;
  84. this._modelViewInfiniteProjection = new Matrix4();
  85. this._normalDirty = true;
  86. this._normal = new Matrix3();
  87. this._normal3DDirty = true;
  88. this._normal3D = new Matrix3();
  89. this._inverseNormalDirty = true;
  90. this._inverseNormal = new Matrix3();
  91. this._inverseNormal3DDirty = true;
  92. this._inverseNormal3D = new Matrix3();
  93. this._encodedCameraPositionMCDirty = true;
  94. this._encodedCameraPositionMC = new EncodedCartesian3();
  95. this._cameraPosition = new Cartesian3();
  96. this._sunPositionWC = new Cartesian3();
  97. this._sunPositionColumbusView = new Cartesian3();
  98. this._sunDirectionWC = new Cartesian3();
  99. this._sunDirectionEC = new Cartesian3();
  100. this._sunColor = new Cartesian3();
  101. this._moonDirectionEC = new Cartesian3();
  102. this._pass = undefined;
  103. this._mode = undefined;
  104. this._mapProjection = undefined;
  105. this._cameraDirection = new Cartesian3();
  106. this._cameraRight = new Cartesian3();
  107. this._cameraUp = new Cartesian3();
  108. this._frustum2DWidth = 0.0;
  109. this._eyeHeight2D = new Cartesian2();
  110. this._pixelRatio = 1.0;
  111. this._orthographicIn3D = false;
  112. this._backgroundColor = new Color();
  113. this._brdfLut = undefined;
  114. this._environmentMap = undefined;
  115. this._sphericalHarmonicCoefficients = undefined;
  116. this._specularEnvironmentMaps = undefined;
  117. this._specularEnvironmentMapsDimensions = new Cartesian2();
  118. this._specularEnvironmentMapsMaximumLOD = undefined;
  119. this._fogDensity = undefined;
  120. this._invertClassificationColor = undefined;
  121. this._imagerySplitPosition = 0.0;
  122. this._pixelSizePerMeter = undefined;
  123. this._geometricToleranceOverMeter = undefined;
  124. this._minimumDisableDepthTestDistance = undefined;
  125. }
  126. defineProperties(UniformState.prototype, {
  127. /**
  128. * @memberof UniformState.prototype
  129. * @type {FrameState}
  130. * @readonly
  131. */
  132. frameState : {
  133. get : function() {
  134. return this._frameState;
  135. }
  136. },
  137. /**
  138. * @memberof UniformState.prototype
  139. * @type {BoundingRectangle}
  140. */
  141. viewport : {
  142. get : function() {
  143. return this._viewport;
  144. },
  145. set : function(viewport) {
  146. if (!BoundingRectangle.equals(viewport, this._viewport)) {
  147. BoundingRectangle.clone(viewport, this._viewport);
  148. var v = this._viewport;
  149. var vc = this._viewportCartesian4;
  150. vc.x = v.x;
  151. vc.y = v.y;
  152. vc.z = v.width;
  153. vc.w = v.height;
  154. this._viewportDirty = true;
  155. }
  156. }
  157. },
  158. /**
  159. * @memberof UniformState.prototype
  160. * @private
  161. */
  162. viewportCartesian4 : {
  163. get : function() {
  164. return this._viewportCartesian4;
  165. }
  166. },
  167. viewportOrthographic : {
  168. get : function() {
  169. cleanViewport(this);
  170. return this._viewportOrthographicMatrix;
  171. }
  172. },
  173. viewportTransformation : {
  174. get : function() {
  175. cleanViewport(this);
  176. return this._viewportTransformation;
  177. }
  178. },
  179. /**
  180. * @memberof UniformState.prototype
  181. * @type {Matrix4}
  182. */
  183. model : {
  184. get : function() {
  185. return this._model;
  186. },
  187. set : function(matrix) {
  188. Matrix4.clone(matrix, this._model);
  189. this._modelView3DDirty = true;
  190. this._inverseModelView3DDirty = true;
  191. this._inverseModelDirty = true;
  192. this._inverseTransposeModelDirty = true;
  193. this._modelViewDirty = true;
  194. this._inverseModelViewDirty = true;
  195. this._modelViewRelativeToEyeDirty = true;
  196. this._inverseModelViewDirty = true;
  197. this._modelViewProjectionDirty = true;
  198. this._inverseModelViewProjectionDirty = true;
  199. this._modelViewProjectionRelativeToEyeDirty = true;
  200. this._modelViewInfiniteProjectionDirty = true;
  201. this._normalDirty = true;
  202. this._inverseNormalDirty = true;
  203. this._normal3DDirty = true;
  204. this._inverseNormal3DDirty = true;
  205. this._encodedCameraPositionMCDirty = true;
  206. }
  207. },
  208. /**
  209. * @memberof UniformState.prototype
  210. * @type {Matrix4}
  211. */
  212. inverseModel : {
  213. get : function() {
  214. if (this._inverseModelDirty) {
  215. this._inverseModelDirty = false;
  216. Matrix4.inverse(this._model, this._inverseModel);
  217. }
  218. return this._inverseModel;
  219. }
  220. },
  221. /**
  222. * @memberof UniformState.prototype
  223. * @private
  224. */
  225. inverseTransposeModel : {
  226. get : function() {
  227. var m = this._inverseTransposeModel;
  228. if (this._inverseTransposeModelDirty) {
  229. this._inverseTransposeModelDirty = false;
  230. Matrix4.getMatrix3(this.inverseModel, m);
  231. Matrix3.transpose(m, m);
  232. }
  233. return m;
  234. }
  235. },
  236. /**
  237. * @memberof UniformState.prototype
  238. * @type {Matrix4}
  239. */
  240. view : {
  241. get : function() {
  242. return this._view;
  243. }
  244. },
  245. /**
  246. * The 3D view matrix. In 3D mode, this is identical to {@link UniformState#view},
  247. * but in 2D and Columbus View it is a synthetic matrix based on the equivalent position
  248. * of the camera in the 3D world.
  249. * @memberof UniformState.prototype
  250. * @type {Matrix4}
  251. */
  252. view3D : {
  253. get : function() {
  254. updateView3D(this);
  255. return this._view3D;
  256. }
  257. },
  258. /**
  259. * The 3x3 rotation matrix of the current view matrix ({@link UniformState#view}).
  260. * @memberof UniformState.prototype
  261. * @type {Matrix3}
  262. */
  263. viewRotation : {
  264. get : function() {
  265. updateView3D(this);
  266. return this._viewRotation;
  267. }
  268. },
  269. /**
  270. * @memberof UniformState.prototype
  271. * @type {Matrix3}
  272. */
  273. viewRotation3D : {
  274. get : function() {
  275. updateView3D(this);
  276. return this._viewRotation3D;
  277. }
  278. },
  279. /**
  280. * @memberof UniformState.prototype
  281. * @type {Matrix4}
  282. */
  283. inverseView : {
  284. get : function() {
  285. return this._inverseView;
  286. }
  287. },
  288. /**
  289. * the 4x4 inverse-view matrix that transforms from eye to 3D world coordinates. In 3D mode, this is
  290. * identical to {@link UniformState#inverseView}, but in 2D and Columbus View it is a synthetic matrix
  291. * based on the equivalent position of the camera in the 3D world.
  292. * @memberof UniformState.prototype
  293. * @type {Matrix4}
  294. */
  295. inverseView3D : {
  296. get : function() {
  297. updateInverseView3D(this);
  298. return this._inverseView3D;
  299. }
  300. },
  301. /**
  302. * @memberof UniformState.prototype
  303. * @type {Matrix3}
  304. */
  305. inverseViewRotation : {
  306. get : function() {
  307. return this._inverseViewRotation;
  308. }
  309. },
  310. /**
  311. * The 3x3 rotation matrix of the current 3D inverse-view matrix ({@link UniformState#inverseView3D}).
  312. * @memberof UniformState.prototype
  313. * @type {Matrix3}
  314. */
  315. inverseViewRotation3D : {
  316. get : function() {
  317. updateInverseView3D(this);
  318. return this._inverseViewRotation3D;
  319. }
  320. },
  321. /**
  322. * @memberof UniformState.prototype
  323. * @type {Matrix4}
  324. */
  325. projection : {
  326. get : function() {
  327. return this._projection;
  328. }
  329. },
  330. /**
  331. * @memberof UniformState.prototype
  332. * @type {Matrix4}
  333. */
  334. inverseProjection : {
  335. get : function() {
  336. cleanInverseProjection(this);
  337. return this._inverseProjection;
  338. }
  339. },
  340. /**
  341. * @memberof UniformState.prototype
  342. * @type {Matrix4}
  343. */
  344. infiniteProjection : {
  345. get : function() {
  346. return this._infiniteProjection;
  347. }
  348. },
  349. /**
  350. * @memberof UniformState.prototype
  351. * @type {Matrix4}
  352. */
  353. modelView : {
  354. get : function() {
  355. cleanModelView(this);
  356. return this._modelView;
  357. }
  358. },
  359. /**
  360. * The 3D model-view matrix. In 3D mode, this is equivalent to {@link UniformState#modelView}. In 2D and
  361. * Columbus View, however, it is a synthetic matrix based on the equivalent position of the camera in the 3D world.
  362. * @memberof UniformState.prototype
  363. * @type {Matrix4}
  364. */
  365. modelView3D : {
  366. get : function() {
  367. cleanModelView3D(this);
  368. return this._modelView3D;
  369. }
  370. },
  371. /**
  372. * Model-view relative to eye matrix.
  373. *
  374. * @memberof UniformState.prototype
  375. * @type {Matrix4}
  376. */
  377. modelViewRelativeToEye : {
  378. get : function() {
  379. cleanModelViewRelativeToEye(this);
  380. return this._modelViewRelativeToEye;
  381. }
  382. },
  383. /**
  384. * @memberof UniformState.prototype
  385. * @type {Matrix4}
  386. */
  387. inverseModelView : {
  388. get : function() {
  389. cleanInverseModelView(this);
  390. return this._inverseModelView;
  391. }
  392. },
  393. /**
  394. * The inverse of the 3D model-view matrix. In 3D mode, this is equivalent to {@link UniformState#inverseModelView}.
  395. * In 2D and Columbus View, however, it is a synthetic matrix based on the equivalent position of the camera in the 3D world.
  396. * @memberof UniformState.prototype
  397. * @type {Matrix4}
  398. */
  399. inverseModelView3D : {
  400. get : function() {
  401. cleanInverseModelView3D(this);
  402. return this._inverseModelView3D;
  403. }
  404. },
  405. /**
  406. * @memberof UniformState.prototype
  407. * @type {Matrix4}
  408. */
  409. viewProjection : {
  410. get : function() {
  411. cleanViewProjection(this);
  412. return this._viewProjection;
  413. }
  414. },
  415. /**
  416. * @memberof UniformState.prototype
  417. * @type {Matrix4}
  418. */
  419. inverseViewProjection : {
  420. get : function() {
  421. cleanInverseViewProjection(this);
  422. return this._inverseViewProjection;
  423. }
  424. },
  425. /**
  426. * @memberof UniformState.prototype
  427. * @type {Matrix4}
  428. */
  429. modelViewProjection : {
  430. get : function() {
  431. cleanModelViewProjection(this);
  432. return this._modelViewProjection;
  433. }
  434. },
  435. /**
  436. * @memberof UniformState.prototype
  437. * @type {Matrix4}
  438. */
  439. inverseModelViewProjection : {
  440. get : function() {
  441. cleanInverseModelViewProjection(this);
  442. return this._inverseModelViewProjection;
  443. }
  444. },
  445. /**
  446. * Model-view-projection relative to eye matrix.
  447. *
  448. * @memberof UniformState.prototype
  449. * @type {Matrix4}
  450. */
  451. modelViewProjectionRelativeToEye : {
  452. get : function() {
  453. cleanModelViewProjectionRelativeToEye(this);
  454. return this._modelViewProjectionRelativeToEye;
  455. }
  456. },
  457. /**
  458. * @memberof UniformState.prototype
  459. * @type {Matrix4}
  460. */
  461. modelViewInfiniteProjection : {
  462. get : function() {
  463. cleanModelViewInfiniteProjection(this);
  464. return this._modelViewInfiniteProjection;
  465. }
  466. },
  467. /**
  468. * A 3x3 normal transformation matrix that transforms normal vectors in model coordinates to
  469. * eye coordinates.
  470. * @memberof UniformState.prototype
  471. * @type {Matrix3}
  472. */
  473. normal : {
  474. get : function() {
  475. cleanNormal(this);
  476. return this._normal;
  477. }
  478. },
  479. /**
  480. * A 3x3 normal transformation matrix that transforms normal vectors in 3D model
  481. * coordinates to eye coordinates. In 3D mode, this is identical to
  482. * {@link UniformState#normal}, but in 2D and Columbus View it represents the normal transformation
  483. * matrix as if the camera were at an equivalent location in 3D mode.
  484. * @memberof UniformState.prototype
  485. * @type {Matrix3}
  486. */
  487. normal3D : {
  488. get : function() {
  489. cleanNormal3D(this);
  490. return this._normal3D;
  491. }
  492. },
  493. /**
  494. * An inverse 3x3 normal transformation matrix that transforms normal vectors in model coordinates
  495. * to eye coordinates.
  496. * @memberof UniformState.prototype
  497. * @type {Matrix3}
  498. */
  499. inverseNormal : {
  500. get : function() {
  501. cleanInverseNormal(this);
  502. return this._inverseNormal;
  503. }
  504. },
  505. /**
  506. * An inverse 3x3 normal transformation matrix that transforms normal vectors in eye coordinates
  507. * to 3D model coordinates. In 3D mode, this is identical to
  508. * {@link UniformState#inverseNormal}, but in 2D and Columbus View it represents the normal transformation
  509. * matrix as if the camera were at an equivalent location in 3D mode.
  510. * @memberof UniformState.prototype
  511. * @type {Matrix3}
  512. */
  513. inverseNormal3D : {
  514. get : function() {
  515. cleanInverseNormal3D(this);
  516. return this._inverseNormal3D;
  517. }
  518. },
  519. /**
  520. * The near distance (<code>x</code>) and the far distance (<code>y</code>) of the frustum defined by the camera.
  521. * This is the largest possible frustum, not an individual frustum used for multi-frustum rendering.
  522. * @memberof UniformState.prototype
  523. * @type {Cartesian2}
  524. */
  525. entireFrustum : {
  526. get : function() {
  527. return this._entireFrustum;
  528. }
  529. },
  530. /**
  531. * The near distance (<code>x</code>) and the far distance (<code>y</code>) of the frustum defined by the camera.
  532. * This is the individual frustum used for multi-frustum rendering.
  533. * @memberof UniformState.prototype
  534. * @type {Cartesian2}
  535. */
  536. currentFrustum : {
  537. get : function() {
  538. return this._currentFrustum;
  539. }
  540. },
  541. /**
  542. * The distances to the frustum planes. The top, bottom, left and right distances are
  543. * the x, y, z, and w components, respectively.
  544. * @memberof UniformState.prototype
  545. * @type {Cartesian4}
  546. */
  547. frustumPlanes : {
  548. get : function() {
  549. return this._frustumPlanes;
  550. }
  551. },
  552. /**
  553. * The log2 of the current frustum's far distance. Used to compute the log depth.
  554. * @memberof UniformState.prototype
  555. * @type {Number}
  556. */
  557. log2FarDistance : {
  558. get : function() {
  559. return this._log2FarDistance;
  560. }
  561. },
  562. /**
  563. * The log2 of 1 + the current frustum's far distance. Used to reverse log depth.
  564. * @memberof UniformState.prototype
  565. * @type {Number}
  566. */
  567. log2FarPlusOne : {
  568. get : function() {
  569. return this._log2FarPlusOne;
  570. }
  571. },
  572. /**
  573. * The log2 current frustum's near distance. Used when writing log depth in the fragment shader.
  574. * @memberof UniformState.prototype
  575. * @type {Number}
  576. */
  577. log2NearDistance : {
  578. get : function() {
  579. return this._log2NearDistance;
  580. }
  581. },
  582. /**
  583. * The the height (<code>x</code>) and the height squared (<code>y</code>)
  584. * in meters of the camera above the 2D world plane. This uniform is only valid
  585. * when the {@link SceneMode} equal to <code>SCENE2D</code>.
  586. * @memberof UniformState.prototype
  587. * @type {Cartesian2}
  588. */
  589. eyeHeight2D : {
  590. get : function() {
  591. return this._eyeHeight2D;
  592. }
  593. },
  594. /**
  595. * The sun position in 3D world coordinates at the current scene time.
  596. * @memberof UniformState.prototype
  597. * @type {Cartesian3}
  598. */
  599. sunPositionWC : {
  600. get : function() {
  601. return this._sunPositionWC;
  602. }
  603. },
  604. /**
  605. * The sun position in 2D world coordinates at the current scene time.
  606. * @memberof UniformState.prototype
  607. * @type {Cartesian3}
  608. */
  609. sunPositionColumbusView : {
  610. get : function(){
  611. return this._sunPositionColumbusView;
  612. }
  613. },
  614. /**
  615. * A normalized vector to the sun in 3D world coordinates at the current scene time. Even in 2D or
  616. * Columbus View mode, this returns the position of the sun in the 3D scene.
  617. * @memberof UniformState.prototype
  618. * @type {Cartesian3}
  619. */
  620. sunDirectionWC : {
  621. get : function() {
  622. return this._sunDirectionWC;
  623. }
  624. },
  625. /**
  626. * A normalized vector to the sun in eye coordinates at the current scene time. In 3D mode, this
  627. * returns the actual vector from the camera position to the sun position. In 2D and Columbus View, it returns
  628. * the vector from the equivalent 3D camera position to the position of the sun in the 3D scene.
  629. * @memberof UniformState.prototype
  630. * @type {Cartesian3}
  631. */
  632. sunDirectionEC : {
  633. get : function() {
  634. return this._sunDirectionEC;
  635. }
  636. },
  637. /**
  638. * The color of the light emitted by the sun.
  639. * @memberof UniformState.prototype
  640. * @type {Color}
  641. */
  642. sunColor: {
  643. get: function() {
  644. return this._sunColor;
  645. }
  646. },
  647. /**
  648. * A normalized vector to the moon in eye coordinates at the current scene time. In 3D mode, this
  649. * returns the actual vector from the camera position to the moon position. In 2D and Columbus View, it returns
  650. * the vector from the equivalent 3D camera position to the position of the moon in the 3D scene.
  651. * @memberof UniformState.prototype
  652. * @type {Cartesian3}
  653. */
  654. moonDirectionEC : {
  655. get : function() {
  656. return this._moonDirectionEC;
  657. }
  658. },
  659. /**
  660. * The high bits of the camera position.
  661. * @memberof UniformState.prototype
  662. * @type {Cartesian3}
  663. */
  664. encodedCameraPositionMCHigh : {
  665. get : function() {
  666. cleanEncodedCameraPositionMC(this);
  667. return this._encodedCameraPositionMC.high;
  668. }
  669. },
  670. /**
  671. * The low bits of the camera position.
  672. * @memberof UniformState.prototype
  673. * @type {Cartesian3}
  674. */
  675. encodedCameraPositionMCLow : {
  676. get : function() {
  677. cleanEncodedCameraPositionMC(this);
  678. return this._encodedCameraPositionMC.low;
  679. }
  680. },
  681. /**
  682. * A 3x3 matrix that transforms from True Equator Mean Equinox (TEME) axes to the
  683. * pseudo-fixed axes at the Scene's current time.
  684. * @memberof UniformState.prototype
  685. * @type {Matrix3}
  686. */
  687. temeToPseudoFixedMatrix : {
  688. get : function() {
  689. return this._temeToPseudoFixed;
  690. }
  691. },
  692. /**
  693. * Gets the scaling factor for transforming from the canvas
  694. * pixel space to canvas coordinate space.
  695. * @memberof UniformState.prototype
  696. * @type {Number}
  697. */
  698. pixelRatio : {
  699. get : function() {
  700. return this._pixelRatio;
  701. }
  702. },
  703. /**
  704. * A scalar used to mix a color with the fog color based on the distance to the camera.
  705. * @memberof UniformState.prototype
  706. * @type {Number}
  707. */
  708. fogDensity : {
  709. get : function() {
  710. return this._fogDensity;
  711. }
  712. },
  713. /**
  714. * A scalar that represents the geometric tolerance per meter
  715. * @memberof UniformStat.prototype
  716. * @type {Number}
  717. */
  718. geometricToleranceOverMeter: {
  719. get: function() {
  720. return this._geometricToleranceOverMeter;
  721. }
  722. },
  723. /**
  724. * @memberof UniformState.prototype
  725. * @type {Pass}
  726. */
  727. pass : {
  728. get : function() {
  729. return this._pass;
  730. }
  731. },
  732. /**
  733. * The current background color
  734. * @memberof UniformState.prototype
  735. * @type {Color}
  736. */
  737. backgroundColor : {
  738. get : function() {
  739. return this._backgroundColor;
  740. }
  741. },
  742. /**
  743. * The look up texture used to find the BRDF for a material
  744. * @memberof UniformState.prototype
  745. * @type {Texture}
  746. */
  747. brdfLut : {
  748. get : function() {
  749. return this._brdfLut;
  750. }
  751. },
  752. /**
  753. * The environment map of the scene
  754. * @memberof UniformState.prototype
  755. * @type {CubeMap}
  756. */
  757. environmentMap : {
  758. get : function() {
  759. return this._environmentMap;
  760. }
  761. },
  762. /**
  763. * The spherical harmonic coefficients of the scene.
  764. * @memberof UniformState.prototype
  765. * @type {Cartesian3[]}
  766. */
  767. sphericalHarmonicCoefficients : {
  768. get : function() {
  769. return this._sphericalHarmonicCoefficients;
  770. }
  771. },
  772. /**
  773. * The specular environment map atlas of the scene.
  774. * @memberof UniformState.prototype
  775. * @type {Texture}
  776. */
  777. specularEnvironmentMaps : {
  778. get : function() {
  779. return this._specularEnvironmentMaps;
  780. }
  781. },
  782. /**
  783. * The dimensions of the specular environment map atlas of the scene.
  784. * @memberof UniformState.prototype
  785. * @type {Cartesian2}
  786. */
  787. specularEnvironmentMapsDimensions : {
  788. get : function() {
  789. return this._specularEnvironmentMapsDimensions;
  790. }
  791. },
  792. /**
  793. * The maximum level-of-detail of the specular environment map atlas of the scene.
  794. * @memberof UniformState.prototype
  795. * @type {Number}
  796. */
  797. specularEnvironmentMapsMaximumLOD : {
  798. get : function() {
  799. return this._specularEnvironmentMapsMaximumLOD;
  800. }
  801. },
  802. /**
  803. * @memberof UniformState.prototype
  804. * @type {Number}
  805. */
  806. imagerySplitPosition : {
  807. get : function() {
  808. return this._imagerySplitPosition;
  809. }
  810. },
  811. /**
  812. * The distance from the camera at which to disable the depth test of billboards, labels and points
  813. * to, for example, prevent clipping against terrain. When set to zero, the depth test should always
  814. * be applied. When less than zero, the depth test should never be applied.
  815. *
  816. * @memberof UniformState.prototype
  817. * @type {Number}
  818. */
  819. minimumDisableDepthTestDistance : {
  820. get : function() {
  821. return this._minimumDisableDepthTestDistance;
  822. }
  823. },
  824. /**
  825. * The highlight color of unclassified 3D Tiles.
  826. *
  827. * @memberof UniformState.prototype
  828. * @type {Color}
  829. */
  830. invertClassificationColor : {
  831. get : function() {
  832. return this._invertClassificationColor;
  833. }
  834. },
  835. /**
  836. * Whether or not the current projection is orthographic in 3D.
  837. *
  838. * @memberOf UniformState.prototype
  839. * @type {Boolean}
  840. */
  841. orthographicIn3D : {
  842. get : function() {
  843. return this._orthographicIn3D;
  844. }
  845. }
  846. });
  847. function setView(uniformState, matrix) {
  848. Matrix4.clone(matrix, uniformState._view);
  849. Matrix4.getMatrix3(matrix, uniformState._viewRotation);
  850. uniformState._view3DDirty = true;
  851. uniformState._inverseView3DDirty = true;
  852. uniformState._modelViewDirty = true;
  853. uniformState._modelView3DDirty = true;
  854. uniformState._modelViewRelativeToEyeDirty = true;
  855. uniformState._inverseModelViewDirty = true;
  856. uniformState._inverseModelView3DDirty = true;
  857. uniformState._viewProjectionDirty = true;
  858. uniformState._inverseViewProjectionDirty = true;
  859. uniformState._modelViewProjectionDirty = true;
  860. uniformState._modelViewProjectionRelativeToEyeDirty = true;
  861. uniformState._modelViewInfiniteProjectionDirty = true;
  862. uniformState._normalDirty = true;
  863. uniformState._inverseNormalDirty = true;
  864. uniformState._normal3DDirty = true;
  865. uniformState._inverseNormal3DDirty = true;
  866. }
  867. function setInverseView(uniformState, matrix) {
  868. Matrix4.clone(matrix, uniformState._inverseView);
  869. Matrix4.getMatrix3(matrix, uniformState._inverseViewRotation);
  870. }
  871. function setProjection(uniformState, matrix) {
  872. Matrix4.clone(matrix, uniformState._projection);
  873. uniformState._inverseProjectionDirty = true;
  874. uniformState._viewProjectionDirty = true;
  875. uniformState._inverseViewProjectionDirty = true;
  876. uniformState._modelViewProjectionDirty = true;
  877. uniformState._modelViewProjectionRelativeToEyeDirty = true;
  878. }
  879. function setInfiniteProjection(uniformState, matrix) {
  880. Matrix4.clone(matrix, uniformState._infiniteProjection);
  881. uniformState._modelViewInfiniteProjectionDirty = true;
  882. }
  883. function setCamera(uniformState, camera) {
  884. Cartesian3.clone(camera.positionWC, uniformState._cameraPosition);
  885. Cartesian3.clone(camera.directionWC, uniformState._cameraDirection);
  886. Cartesian3.clone(camera.rightWC, uniformState._cameraRight);
  887. Cartesian3.clone(camera.upWC, uniformState._cameraUp);
  888. uniformState._encodedCameraPositionMCDirty = true;
  889. }
  890. var transformMatrix = new Matrix3();
  891. var sunCartographicScratch = new Cartographic();
  892. function setSunAndMoonDirections(uniformState, frameState) {
  893. if (!defined(Transforms.computeIcrfToFixedMatrix(frameState.time, transformMatrix))) {
  894. transformMatrix = Transforms.computeTemeToPseudoFixedMatrix(frameState.time, transformMatrix);
  895. }
  896. var position = Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame(frameState.time, uniformState._sunPositionWC);
  897. Matrix3.multiplyByVector(transformMatrix, position, position);
  898. Cartesian3.normalize(position, uniformState._sunDirectionWC);
  899. position = Matrix3.multiplyByVector(uniformState.viewRotation3D, position, uniformState._sunDirectionEC);
  900. Cartesian3.normalize(position, position);
  901. position = Simon1994PlanetaryPositions.computeMoonPositionInEarthInertialFrame(frameState.time, uniformState._moonDirectionEC);
  902. Matrix3.multiplyByVector(transformMatrix, position, position);
  903. Matrix3.multiplyByVector(uniformState.viewRotation3D, position, position);
  904. Cartesian3.normalize(position, position);
  905. var projection = frameState.mapProjection;
  906. var ellipsoid = projection.ellipsoid;
  907. var sunCartographic = ellipsoid.cartesianToCartographic(uniformState._sunPositionWC, sunCartographicScratch);
  908. projection.project(sunCartographic, uniformState._sunPositionColumbusView);
  909. }
  910. /**
  911. * Synchronizes the frustum's state with the camera state. This is called
  912. * by the {@link Scene} when rendering to ensure that automatic GLSL uniforms
  913. * are set to the right value.
  914. *
  915. * @param {Object} camera The camera to synchronize with.
  916. */
  917. UniformState.prototype.updateCamera = function(camera) {
  918. setView(this, camera.viewMatrix);
  919. setInverseView(this, camera.inverseViewMatrix);
  920. setCamera(this, camera);
  921. this._entireFrustum.x = camera.frustum.near;
  922. this._entireFrustum.y = camera.frustum.far;
  923. this.updateFrustum(camera.frustum);
  924. this._orthographicIn3D = this._mode !== SceneMode.SCENE2D && camera.frustum instanceof OrthographicFrustum;
  925. };
  926. /**
  927. * Synchronizes the frustum's state with the uniform state. This is called
  928. * by the {@link Scene} when rendering to ensure that automatic GLSL uniforms
  929. * are set to the right value.
  930. *
  931. * @param {Object} frustum The frustum to synchronize with.
  932. */
  933. UniformState.prototype.updateFrustum = function(frustum) {
  934. setProjection(this, frustum.projectionMatrix);
  935. if (defined(frustum.infiniteProjectionMatrix)) {
  936. setInfiniteProjection(this, frustum.infiniteProjectionMatrix);
  937. }
  938. this._currentFrustum.x = frustum.near;
  939. this._currentFrustum.y = frustum.far;
  940. this._log2FarDistance = 2.0 / CesiumMath.log2(frustum.far + 1.0);
  941. this._log2FarPlusOne = CesiumMath.log2(frustum.far + 1.0);
  942. this._log2NearDistance = CesiumMath.log2(frustum.near);
  943. if (defined(frustum._offCenterFrustum)) {
  944. frustum = frustum._offCenterFrustum;
  945. }
  946. this._frustumPlanes.x = frustum.top;
  947. this._frustumPlanes.y = frustum.bottom;
  948. this._frustumPlanes.z = frustum.left;
  949. this._frustumPlanes.w = frustum.right;
  950. };
  951. UniformState.prototype.updatePass = function(pass) {
  952. this._pass = pass;
  953. };
  954. var EMPTY_ARRAY = [];
  955. /**
  956. * Synchronizes frame state with the uniform state. This is called
  957. * by the {@link Scene} when rendering to ensure that automatic GLSL uniforms
  958. * are set to the right value.
  959. *
  960. * @param {FrameState} frameState The frameState to synchronize with.
  961. */
  962. UniformState.prototype.update = function(frameState) {
  963. this._mode = frameState.mode;
  964. this._mapProjection = frameState.mapProjection;
  965. this._pixelRatio = frameState.pixelRatio;
  966. var camera = frameState.camera;
  967. this.updateCamera(camera);
  968. if (frameState.mode === SceneMode.SCENE2D) {
  969. this._frustum2DWidth = camera.frustum.right - camera.frustum.left;
  970. this._eyeHeight2D.x = this._frustum2DWidth * 0.5;
  971. this._eyeHeight2D.y = this._eyeHeight2D.x * this._eyeHeight2D.x;
  972. } else {
  973. this._frustum2DWidth = 0.0;
  974. this._eyeHeight2D.x = 0.0;
  975. this._eyeHeight2D.y = 0.0;
  976. }
  977. setSunAndMoonDirections(this, frameState);
  978. this._sunColor = Cartesian3.clone(frameState.sunColor, this._sunColor);
  979. var brdfLutGenerator = frameState.brdfLutGenerator;
  980. var brdfLut = defined(brdfLutGenerator) ? brdfLutGenerator.colorTexture : undefined;
  981. this._brdfLut = brdfLut;
  982. this._environmentMap = defaultValue(frameState.environmentMap, frameState.context.defaultCubeMap);
  983. // IE 11 doesn't optimize out uniforms that are #ifdef'd out. So undefined values for the spherical harmonic
  984. // coefficients and specular environment map atlas dimensions cause a crash.
  985. this._sphericalHarmonicCoefficients = defaultValue(frameState.sphericalHarmonicCoefficients, EMPTY_ARRAY);
  986. this._specularEnvironmentMaps = frameState.specularEnvironmentMaps;
  987. this._specularEnvironmentMapsMaximumLOD = frameState.specularEnvironmentMapsMaximumLOD;
  988. if (defined(this._specularEnvironmentMaps)) {
  989. Cartesian2.clone(this._specularEnvironmentMaps.dimensions, this._specularEnvironmentMapsDimensions);
  990. }
  991. this._fogDensity = frameState.fog.density;
  992. this._invertClassificationColor = frameState.invertClassificationColor;
  993. this._frameState = frameState;
  994. this._temeToPseudoFixed = Transforms.computeTemeToPseudoFixedMatrix(frameState.time, this._temeToPseudoFixed);
  995. // Convert the relative imagerySplitPosition to absolute pixel coordinates
  996. this._imagerySplitPosition = frameState.imagerySplitPosition * frameState.context.drawingBufferWidth;
  997. var fov = camera.frustum.fov;
  998. var viewport = this._viewport;
  999. var pixelSizePerMeter;
  1000. if (viewport.height > viewport.width) {
  1001. pixelSizePerMeter = Math.tan(0.5 * fov) * 2.0 / viewport.height;
  1002. } else {
  1003. pixelSizePerMeter = Math.tan(0.5 * fov) * 2.0 / viewport.width;
  1004. }
  1005. this._geometricToleranceOverMeter = pixelSizePerMeter * frameState.maximumScreenSpaceError;
  1006. Color.clone(frameState.backgroundColor, this._backgroundColor);
  1007. this._minimumDisableDepthTestDistance = frameState.minimumDisableDepthTestDistance;
  1008. this._minimumDisableDepthTestDistance *= this._minimumDisableDepthTestDistance;
  1009. if (this._minimumDisableDepthTestDistance === Number.POSITIVE_INFINITY) {
  1010. this._minimumDisableDepthTestDistance = -1.0;
  1011. }
  1012. };
  1013. function cleanViewport(uniformState) {
  1014. if (uniformState._viewportDirty) {
  1015. var v = uniformState._viewport;
  1016. Matrix4.computeOrthographicOffCenter(v.x, v.x + v.width, v.y, v.y + v.height, 0.0, 1.0, uniformState._viewportOrthographicMatrix);
  1017. Matrix4.computeViewportTransformation(v, 0.0, 1.0, uniformState._viewportTransformation);
  1018. uniformState._viewportDirty = false;
  1019. }
  1020. }
  1021. function cleanInverseProjection(uniformState) {
  1022. if (uniformState._inverseProjectionDirty) {
  1023. uniformState._inverseProjectionDirty = false;
  1024. if (uniformState._mode !== SceneMode.SCENE2D && uniformState._mode !== SceneMode.MORPHING && !uniformState._orthographicIn3D) {
  1025. Matrix4.inverse(uniformState._projection, uniformState._inverseProjection);
  1026. } else {
  1027. Matrix4.clone(Matrix4.ZERO, uniformState._inverseProjection);
  1028. }
  1029. }
  1030. }
  1031. // Derived
  1032. function cleanModelView(uniformState) {
  1033. if (uniformState._modelViewDirty) {
  1034. uniformState._modelViewDirty = false;
  1035. Matrix4.multiplyTransformation(uniformState._view, uniformState._model, uniformState._modelView);
  1036. }
  1037. }
  1038. function cleanModelView3D(uniformState) {
  1039. if (uniformState._modelView3DDirty) {
  1040. uniformState._modelView3DDirty = false;
  1041. Matrix4.multiplyTransformation(uniformState.view3D, uniformState._model, uniformState._modelView3D);
  1042. }
  1043. }
  1044. function cleanInverseModelView(uniformState) {
  1045. if (uniformState._inverseModelViewDirty) {
  1046. uniformState._inverseModelViewDirty = false;
  1047. Matrix4.inverse(uniformState.modelView, uniformState._inverseModelView);
  1048. }
  1049. }
  1050. function cleanInverseModelView3D(uniformState) {
  1051. if (uniformState._inverseModelView3DDirty) {
  1052. uniformState._inverseModelView3DDirty = false;
  1053. Matrix4.inverse(uniformState.modelView3D, uniformState._inverseModelView3D);
  1054. }
  1055. }
  1056. function cleanViewProjection(uniformState) {
  1057. if (uniformState._viewProjectionDirty) {
  1058. uniformState._viewProjectionDirty = false;
  1059. Matrix4.multiply(uniformState._projection, uniformState._view, uniformState._viewProjection);
  1060. }
  1061. }
  1062. function cleanInverseViewProjection(uniformState) {
  1063. if (uniformState._inverseViewProjectionDirty) {
  1064. uniformState._inverseViewProjectionDirty = false;
  1065. Matrix4.inverse(uniformState.viewProjection, uniformState._inverseViewProjection);
  1066. }
  1067. }
  1068. function cleanModelViewProjection(uniformState) {
  1069. if (uniformState._modelViewProjectionDirty) {
  1070. uniformState._modelViewProjectionDirty = false;
  1071. Matrix4.multiply(uniformState._projection, uniformState.modelView, uniformState._modelViewProjection);
  1072. }
  1073. }
  1074. function cleanModelViewRelativeToEye(uniformState) {
  1075. if (uniformState._modelViewRelativeToEyeDirty) {
  1076. uniformState._modelViewRelativeToEyeDirty = false;
  1077. var mv = uniformState.modelView;
  1078. var mvRte = uniformState._modelViewRelativeToEye;
  1079. mvRte[0] = mv[0];
  1080. mvRte[1] = mv[1];
  1081. mvRte[2] = mv[2];
  1082. mvRte[3] = mv[3];
  1083. mvRte[4] = mv[4];
  1084. mvRte[5] = mv[5];
  1085. mvRte[6] = mv[6];
  1086. mvRte[7] = mv[7];
  1087. mvRte[8] = mv[8];
  1088. mvRte[9] = mv[9];
  1089. mvRte[10] = mv[10];
  1090. mvRte[11] = mv[11];
  1091. mvRte[12] = 0.0;
  1092. mvRte[13] = 0.0;
  1093. mvRte[14] = 0.0;
  1094. mvRte[15] = mv[15];
  1095. }
  1096. }
  1097. function cleanInverseModelViewProjection(uniformState) {
  1098. if (uniformState._inverseModelViewProjectionDirty) {
  1099. uniformState._inverseModelViewProjectionDirty = false;
  1100. Matrix4.inverse(uniformState.modelViewProjection, uniformState._inverseModelViewProjection);
  1101. }
  1102. }
  1103. function cleanModelViewProjectionRelativeToEye(uniformState) {
  1104. if (uniformState._modelViewProjectionRelativeToEyeDirty) {
  1105. uniformState._modelViewProjectionRelativeToEyeDirty = false;
  1106. Matrix4.multiply(uniformState._projection, uniformState.modelViewRelativeToEye, uniformState._modelViewProjectionRelativeToEye);
  1107. }
  1108. }
  1109. function cleanModelViewInfiniteProjection(uniformState) {
  1110. if (uniformState._modelViewInfiniteProjectionDirty) {
  1111. uniformState._modelViewInfiniteProjectionDirty = false;
  1112. Matrix4.multiply(uniformState._infiniteProjection, uniformState.modelView, uniformState._modelViewInfiniteProjection);
  1113. }
  1114. }
  1115. function cleanNormal(uniformState) {
  1116. if (uniformState._normalDirty) {
  1117. uniformState._normalDirty = false;
  1118. var m = uniformState._normal;
  1119. Matrix4.getMatrix3(uniformState.inverseModelView, m);
  1120. Matrix3.getRotation(m, m);
  1121. Matrix3.transpose(m, m);
  1122. }
  1123. }
  1124. function cleanNormal3D(uniformState) {
  1125. if (uniformState._normal3DDirty) {
  1126. uniformState._normal3DDirty = false;
  1127. var m = uniformState._normal3D;
  1128. Matrix4.getMatrix3(uniformState.inverseModelView3D, m);
  1129. Matrix3.getRotation(m, m);
  1130. Matrix3.transpose(m, m);
  1131. }
  1132. }
  1133. function cleanInverseNormal(uniformState) {
  1134. if (uniformState._inverseNormalDirty) {
  1135. uniformState._inverseNormalDirty = false;
  1136. Matrix4.getMatrix3(uniformState.inverseModelView, uniformState._inverseNormal);
  1137. Matrix3.getRotation(uniformState._inverseNormal, uniformState._inverseNormal);
  1138. }
  1139. }
  1140. function cleanInverseNormal3D(uniformState) {
  1141. if (uniformState._inverseNormal3DDirty) {
  1142. uniformState._inverseNormal3DDirty = false;
  1143. Matrix4.getMatrix3(uniformState.inverseModelView3D, uniformState._inverseNormal3D);
  1144. Matrix3.getRotation(uniformState._inverseNormal3D, uniformState._inverseNormal3D);
  1145. }
  1146. }
  1147. var cameraPositionMC = new Cartesian3();
  1148. function cleanEncodedCameraPositionMC(uniformState) {
  1149. if (uniformState._encodedCameraPositionMCDirty) {
  1150. uniformState._encodedCameraPositionMCDirty = false;
  1151. Matrix4.multiplyByPoint(uniformState.inverseModel, uniformState._cameraPosition, cameraPositionMC);
  1152. EncodedCartesian3.fromCartesian(cameraPositionMC, uniformState._encodedCameraPositionMC);
  1153. }
  1154. }
  1155. var view2Dto3DPScratch = new Cartesian3();
  1156. var view2Dto3DRScratch = new Cartesian3();
  1157. var view2Dto3DUScratch = new Cartesian3();
  1158. var view2Dto3DDScratch = new Cartesian3();
  1159. var view2Dto3DCartographicScratch = new Cartographic();
  1160. var view2Dto3DCartesian3Scratch = new Cartesian3();
  1161. var view2Dto3DMatrix4Scratch = new Matrix4();
  1162. function view2Dto3D(position2D, direction2D, right2D, up2D, frustum2DWidth, mode, projection, result) {
  1163. // The camera position and directions are expressed in the 2D coordinate system where the Y axis is to the East,
  1164. // the Z axis is to the North, and the X axis is out of the map. Express them instead in the ENU axes where
  1165. // X is to the East, Y is to the North, and Z is out of the local horizontal plane.
  1166. var p = view2Dto3DPScratch;
  1167. p.x = position2D.y;
  1168. p.y = position2D.z;
  1169. p.z = position2D.x;
  1170. var r = view2Dto3DRScratch;
  1171. r.x = right2D.y;
  1172. r.y = right2D.z;
  1173. r.z = right2D.x;
  1174. var u = view2Dto3DUScratch;
  1175. u.x = up2D.y;
  1176. u.y = up2D.z;
  1177. u.z = up2D.x;
  1178. var d = view2Dto3DDScratch;
  1179. d.x = direction2D.y;
  1180. d.y = direction2D.z;
  1181. d.z = direction2D.x;
  1182. // In 2D, the camera height is always 12.7 million meters.
  1183. // The apparent height is equal to half the frustum width.
  1184. if (mode === SceneMode.SCENE2D) {
  1185. p.z = frustum2DWidth * 0.5;
  1186. }
  1187. // Compute the equivalent camera position in the real (3D) world.
  1188. // In 2D and Columbus View, the camera can travel outside the projection, and when it does so
  1189. // there's not really any corresponding location in the real world. So clamp the unprojected
  1190. // longitude and latitude to their valid ranges.
  1191. var cartographic = projection.unproject(p, view2Dto3DCartographicScratch);
  1192. cartographic.longitude = CesiumMath.clamp(cartographic.longitude, -Math.PI, Math.PI);
  1193. cartographic.latitude = CesiumMath.clamp(cartographic.latitude, -CesiumMath.PI_OVER_TWO, CesiumMath.PI_OVER_TWO);
  1194. var ellipsoid = projection.ellipsoid;
  1195. var position3D = ellipsoid.cartographicToCartesian(cartographic, view2Dto3DCartesian3Scratch);
  1196. // Compute the rotation from the local ENU at the real world camera position to the fixed axes.
  1197. var enuToFixed = Transforms.eastNorthUpToFixedFrame(position3D, ellipsoid, view2Dto3DMatrix4Scratch);
  1198. // Transform each camera direction to the fixed axes.
  1199. Matrix4.multiplyByPointAsVector(enuToFixed, r, r);
  1200. Matrix4.multiplyByPointAsVector(enuToFixed, u, u);
  1201. Matrix4.multiplyByPointAsVector(enuToFixed, d, d);
  1202. // Compute the view matrix based on the new fixed-frame camera position and directions.
  1203. if (!defined(result)) {
  1204. result = new Matrix4();
  1205. }
  1206. result[0] = r.x;
  1207. result[1] = u.x;
  1208. result[2] = -d.x;
  1209. result[3] = 0.0;
  1210. result[4] = r.y;
  1211. result[5] = u.y;
  1212. result[6] = -d.y;
  1213. result[7] = 0.0;
  1214. result[8] = r.z;
  1215. result[9] = u.z;
  1216. result[10] = -d.z;
  1217. result[11] = 0.0;
  1218. result[12] = -Cartesian3.dot(r, position3D);
  1219. result[13] = -Cartesian3.dot(u, position3D);
  1220. result[14] = Cartesian3.dot(d, position3D);
  1221. result[15] = 1.0;
  1222. return result;
  1223. }
  1224. function updateView3D(that) {
  1225. if (that._view3DDirty) {
  1226. if (that._mode === SceneMode.SCENE3D) {
  1227. Matrix4.clone(that._view, that._view3D);
  1228. } else {
  1229. view2Dto3D(that._cameraPosition, that._cameraDirection, that._cameraRight, that._cameraUp, that._frustum2DWidth, that._mode, that._mapProjection, that._view3D);
  1230. }
  1231. Matrix4.getMatrix3(that._view3D, that._viewRotation3D);
  1232. that._view3DDirty = false;
  1233. }
  1234. }
  1235. function updateInverseView3D(that){
  1236. if (that._inverseView3DDirty) {
  1237. Matrix4.inverseTransformation(that.view3D, that._inverseView3D);
  1238. Matrix4.getMatrix3(that._inverseView3D, that._inverseViewRotation3D);
  1239. that._inverseView3DDirty = false;
  1240. }
  1241. }
  1242. export default UniformState;