UniformState.js 45 KB

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