UniformState.js 45 KB

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