UniformState.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  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._imagerySplitPosition = 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. var v = this._viewport;
  156. var 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. var 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. * @memberof UniformState.prototype
  860. * @type {Number}
  861. */
  862. imagerySplitPosition: {
  863. get: function () {
  864. return this._imagerySplitPosition;
  865. },
  866. },
  867. /**
  868. * The distance from the camera at which to disable the depth test of billboards, labels and points
  869. * to, for example, prevent clipping against terrain. When set to zero, the depth test should always
  870. * be applied. When less than zero, the depth test should never be applied.
  871. *
  872. * @memberof UniformState.prototype
  873. * @type {Number}
  874. */
  875. minimumDisableDepthTestDistance: {
  876. get: function () {
  877. return this._minimumDisableDepthTestDistance;
  878. },
  879. },
  880. /**
  881. * The highlight color of unclassified 3D Tiles.
  882. *
  883. * @memberof UniformState.prototype
  884. * @type {Color}
  885. */
  886. invertClassificationColor: {
  887. get: function () {
  888. return this._invertClassificationColor;
  889. },
  890. },
  891. /**
  892. * Whether or not the current projection is orthographic in 3D.
  893. *
  894. * @memberOf UniformState.prototype
  895. * @type {Boolean}
  896. */
  897. orthographicIn3D: {
  898. get: function () {
  899. return this._orthographicIn3D;
  900. },
  901. },
  902. /**
  903. * The current ellipsoid.
  904. *
  905. * @memberOf UniformState.prototype
  906. * @type {Ellipsoid}
  907. */
  908. ellipsoid: {
  909. get: function () {
  910. return defaultValue(this._ellipsoid, Ellipsoid.WGS84);
  911. },
  912. },
  913. });
  914. function setView(uniformState, matrix) {
  915. Matrix4.clone(matrix, uniformState._view);
  916. Matrix4.getMatrix3(matrix, uniformState._viewRotation);
  917. uniformState._view3DDirty = true;
  918. uniformState._inverseView3DDirty = true;
  919. uniformState._modelViewDirty = true;
  920. uniformState._modelView3DDirty = true;
  921. uniformState._modelViewRelativeToEyeDirty = true;
  922. uniformState._inverseModelViewDirty = true;
  923. uniformState._inverseModelView3DDirty = true;
  924. uniformState._viewProjectionDirty = true;
  925. uniformState._inverseViewProjectionDirty = true;
  926. uniformState._modelViewProjectionDirty = true;
  927. uniformState._modelViewProjectionRelativeToEyeDirty = true;
  928. uniformState._modelViewInfiniteProjectionDirty = true;
  929. uniformState._normalDirty = true;
  930. uniformState._inverseNormalDirty = true;
  931. uniformState._normal3DDirty = true;
  932. uniformState._inverseNormal3DDirty = true;
  933. }
  934. function setInverseView(uniformState, matrix) {
  935. Matrix4.clone(matrix, uniformState._inverseView);
  936. Matrix4.getMatrix3(matrix, uniformState._inverseViewRotation);
  937. }
  938. function setProjection(uniformState, matrix) {
  939. Matrix4.clone(matrix, uniformState._projection);
  940. uniformState._inverseProjectionDirty = true;
  941. uniformState._viewProjectionDirty = true;
  942. uniformState._inverseViewProjectionDirty = true;
  943. uniformState._modelViewProjectionDirty = true;
  944. uniformState._modelViewProjectionRelativeToEyeDirty = true;
  945. }
  946. function setInfiniteProjection(uniformState, matrix) {
  947. Matrix4.clone(matrix, uniformState._infiniteProjection);
  948. uniformState._modelViewInfiniteProjectionDirty = true;
  949. }
  950. function setCamera(uniformState, camera) {
  951. Cartesian3.clone(camera.positionWC, uniformState._cameraPosition);
  952. Cartesian3.clone(camera.directionWC, uniformState._cameraDirection);
  953. Cartesian3.clone(camera.rightWC, uniformState._cameraRight);
  954. Cartesian3.clone(camera.upWC, uniformState._cameraUp);
  955. var positionCartographic = camera.positionCartographic;
  956. if (!defined(positionCartographic)) {
  957. uniformState._eyeHeight = -uniformState._ellipsoid.maximumRadius;
  958. } else {
  959. uniformState._eyeHeight = positionCartographic.height;
  960. }
  961. uniformState._encodedCameraPositionMCDirty = true;
  962. }
  963. var transformMatrix = new Matrix3();
  964. var sunCartographicScratch = new Cartographic();
  965. function setSunAndMoonDirections(uniformState, frameState) {
  966. if (
  967. !defined(
  968. Transforms.computeIcrfToFixedMatrix(frameState.time, transformMatrix)
  969. )
  970. ) {
  971. transformMatrix = Transforms.computeTemeToPseudoFixedMatrix(
  972. frameState.time,
  973. transformMatrix
  974. );
  975. }
  976. var position = Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame(
  977. frameState.time,
  978. uniformState._sunPositionWC
  979. );
  980. Matrix3.multiplyByVector(transformMatrix, position, position);
  981. Cartesian3.normalize(position, uniformState._sunDirectionWC);
  982. position = Matrix3.multiplyByVector(
  983. uniformState.viewRotation3D,
  984. position,
  985. uniformState._sunDirectionEC
  986. );
  987. Cartesian3.normalize(position, position);
  988. position = Simon1994PlanetaryPositions.computeMoonPositionInEarthInertialFrame(
  989. frameState.time,
  990. uniformState._moonDirectionEC
  991. );
  992. Matrix3.multiplyByVector(transformMatrix, position, position);
  993. Matrix3.multiplyByVector(uniformState.viewRotation3D, position, position);
  994. Cartesian3.normalize(position, position);
  995. var projection = frameState.mapProjection;
  996. var ellipsoid = projection.ellipsoid;
  997. var sunCartographic = ellipsoid.cartesianToCartographic(
  998. uniformState._sunPositionWC,
  999. sunCartographicScratch
  1000. );
  1001. projection.project(sunCartographic, uniformState._sunPositionColumbusView);
  1002. }
  1003. /**
  1004. * Synchronizes the frustum's state with the camera state. This is called
  1005. * by the {@link Scene} when rendering to ensure that automatic GLSL uniforms
  1006. * are set to the right value.
  1007. *
  1008. * @param {Object} camera The camera to synchronize with.
  1009. */
  1010. UniformState.prototype.updateCamera = function (camera) {
  1011. setView(this, camera.viewMatrix);
  1012. setInverseView(this, camera.inverseViewMatrix);
  1013. setCamera(this, camera);
  1014. this._entireFrustum.x = camera.frustum.near;
  1015. this._entireFrustum.y = camera.frustum.far;
  1016. this.updateFrustum(camera.frustum);
  1017. this._orthographicIn3D =
  1018. this._mode !== SceneMode.SCENE2D &&
  1019. camera.frustum instanceof OrthographicFrustum;
  1020. };
  1021. /**
  1022. * Synchronizes the frustum's state with the uniform state. This is called
  1023. * by the {@link Scene} when rendering to ensure that automatic GLSL uniforms
  1024. * are set to the right value.
  1025. *
  1026. * @param {Object} frustum The frustum to synchronize with.
  1027. */
  1028. UniformState.prototype.updateFrustum = function (frustum) {
  1029. setProjection(this, frustum.projectionMatrix);
  1030. if (defined(frustum.infiniteProjectionMatrix)) {
  1031. setInfiniteProjection(this, frustum.infiniteProjectionMatrix);
  1032. }
  1033. this._currentFrustum.x = frustum.near;
  1034. this._currentFrustum.y = frustum.far;
  1035. this._farDepthFromNearPlusOne = frustum.far - frustum.near + 1.0;
  1036. this._log2FarDepthFromNearPlusOne = CesiumMath.log2(
  1037. this._farDepthFromNearPlusOne
  1038. );
  1039. this._oneOverLog2FarDepthFromNearPlusOne =
  1040. 1.0 / this._log2FarDepthFromNearPlusOne;
  1041. if (defined(frustum._offCenterFrustum)) {
  1042. frustum = frustum._offCenterFrustum;
  1043. }
  1044. this._frustumPlanes.x = frustum.top;
  1045. this._frustumPlanes.y = frustum.bottom;
  1046. this._frustumPlanes.z = frustum.left;
  1047. this._frustumPlanes.w = frustum.right;
  1048. };
  1049. UniformState.prototype.updatePass = function (pass) {
  1050. this._pass = pass;
  1051. };
  1052. var EMPTY_ARRAY = [];
  1053. var defaultLight = new SunLight();
  1054. /**
  1055. * Synchronizes frame state with the uniform state. This is called
  1056. * by the {@link Scene} when rendering to ensure that automatic GLSL uniforms
  1057. * are set to the right value.
  1058. *
  1059. * @param {FrameState} frameState The frameState to synchronize with.
  1060. */
  1061. UniformState.prototype.update = function (frameState) {
  1062. this._mode = frameState.mode;
  1063. this._mapProjection = frameState.mapProjection;
  1064. this._ellipsoid = frameState.mapProjection.ellipsoid;
  1065. this._pixelRatio = frameState.pixelRatio;
  1066. var camera = frameState.camera;
  1067. this.updateCamera(camera);
  1068. if (frameState.mode === SceneMode.SCENE2D) {
  1069. this._frustum2DWidth = camera.frustum.right - camera.frustum.left;
  1070. this._eyeHeight2D.x = this._frustum2DWidth * 0.5;
  1071. this._eyeHeight2D.y = this._eyeHeight2D.x * this._eyeHeight2D.x;
  1072. } else {
  1073. this._frustum2DWidth = 0.0;
  1074. this._eyeHeight2D.x = 0.0;
  1075. this._eyeHeight2D.y = 0.0;
  1076. }
  1077. setSunAndMoonDirections(this, frameState);
  1078. var light = defaultValue(frameState.light, defaultLight);
  1079. if (light instanceof SunLight) {
  1080. this._lightDirectionWC = Cartesian3.clone(
  1081. this._sunDirectionWC,
  1082. this._lightDirectionWC
  1083. );
  1084. this._lightDirectionEC = Cartesian3.clone(
  1085. this._sunDirectionEC,
  1086. this._lightDirectionEC
  1087. );
  1088. } else {
  1089. this._lightDirectionWC = Cartesian3.normalize(
  1090. Cartesian3.negate(light.direction, this._lightDirectionWC),
  1091. this._lightDirectionWC
  1092. );
  1093. this._lightDirectionEC = Matrix3.multiplyByVector(
  1094. this.viewRotation3D,
  1095. this._lightDirectionWC,
  1096. this._lightDirectionEC
  1097. );
  1098. }
  1099. var lightColor = light.color;
  1100. var lightColorHdr = Cartesian3.fromElements(
  1101. lightColor.red,
  1102. lightColor.green,
  1103. lightColor.blue,
  1104. this._lightColorHdr
  1105. );
  1106. lightColorHdr = Cartesian3.multiplyByScalar(
  1107. lightColorHdr,
  1108. light.intensity,
  1109. lightColorHdr
  1110. );
  1111. var maximumComponent = Cartesian3.maximumComponent(lightColorHdr);
  1112. if (maximumComponent > 1.0) {
  1113. Cartesian3.divideByScalar(
  1114. lightColorHdr,
  1115. maximumComponent,
  1116. this._lightColor
  1117. );
  1118. } else {
  1119. Cartesian3.clone(lightColorHdr, this._lightColor);
  1120. }
  1121. var brdfLutGenerator = frameState.brdfLutGenerator;
  1122. var brdfLut = defined(brdfLutGenerator)
  1123. ? brdfLutGenerator.colorTexture
  1124. : undefined;
  1125. this._brdfLut = brdfLut;
  1126. this._environmentMap = defaultValue(
  1127. frameState.environmentMap,
  1128. frameState.context.defaultCubeMap
  1129. );
  1130. // IE 11 doesn't optimize out uniforms that are #ifdef'd out. So undefined values for the spherical harmonic
  1131. // coefficients and specular environment map atlas dimensions cause a crash.
  1132. this._sphericalHarmonicCoefficients = defaultValue(
  1133. frameState.sphericalHarmonicCoefficients,
  1134. EMPTY_ARRAY
  1135. );
  1136. this._specularEnvironmentMaps = frameState.specularEnvironmentMaps;
  1137. this._specularEnvironmentMapsMaximumLOD =
  1138. frameState.specularEnvironmentMapsMaximumLOD;
  1139. if (defined(this._specularEnvironmentMaps)) {
  1140. Cartesian2.clone(
  1141. this._specularEnvironmentMaps.dimensions,
  1142. this._specularEnvironmentMapsDimensions
  1143. );
  1144. }
  1145. this._fogDensity = frameState.fog.density;
  1146. this._invertClassificationColor = frameState.invertClassificationColor;
  1147. this._frameState = frameState;
  1148. this._temeToPseudoFixed = Transforms.computeTemeToPseudoFixedMatrix(
  1149. frameState.time,
  1150. this._temeToPseudoFixed
  1151. );
  1152. // Convert the relative imagerySplitPosition to absolute pixel coordinates
  1153. this._imagerySplitPosition =
  1154. frameState.imagerySplitPosition * frameState.context.drawingBufferWidth;
  1155. var fov = camera.frustum.fov;
  1156. var viewport = this._viewport;
  1157. var pixelSizePerMeter;
  1158. if (defined(fov)) {
  1159. if (viewport.height > viewport.width) {
  1160. pixelSizePerMeter = (Math.tan(0.5 * fov) * 2.0) / viewport.height;
  1161. } else {
  1162. pixelSizePerMeter = (Math.tan(0.5 * fov) * 2.0) / viewport.width;
  1163. }
  1164. } else {
  1165. pixelSizePerMeter = 1.0 / Math.max(viewport.width, viewport.height);
  1166. }
  1167. this._geometricToleranceOverMeter =
  1168. pixelSizePerMeter * frameState.maximumScreenSpaceError;
  1169. Color.clone(frameState.backgroundColor, this._backgroundColor);
  1170. this._minimumDisableDepthTestDistance =
  1171. frameState.minimumDisableDepthTestDistance;
  1172. this._minimumDisableDepthTestDistance *= this._minimumDisableDepthTestDistance;
  1173. if (this._minimumDisableDepthTestDistance === Number.POSITIVE_INFINITY) {
  1174. this._minimumDisableDepthTestDistance = -1.0;
  1175. }
  1176. };
  1177. function cleanViewport(uniformState) {
  1178. if (uniformState._viewportDirty) {
  1179. var v = uniformState._viewport;
  1180. Matrix4.computeOrthographicOffCenter(
  1181. v.x,
  1182. v.x + v.width,
  1183. v.y,
  1184. v.y + v.height,
  1185. 0.0,
  1186. 1.0,
  1187. uniformState._viewportOrthographicMatrix
  1188. );
  1189. Matrix4.computeViewportTransformation(
  1190. v,
  1191. 0.0,
  1192. 1.0,
  1193. uniformState._viewportTransformation
  1194. );
  1195. uniformState._viewportDirty = false;
  1196. }
  1197. }
  1198. function cleanInverseProjection(uniformState) {
  1199. if (uniformState._inverseProjectionDirty) {
  1200. uniformState._inverseProjectionDirty = false;
  1201. if (
  1202. uniformState._mode !== SceneMode.SCENE2D &&
  1203. uniformState._mode !== SceneMode.MORPHING &&
  1204. !uniformState._orthographicIn3D
  1205. ) {
  1206. Matrix4.inverse(
  1207. uniformState._projection,
  1208. uniformState._inverseProjection
  1209. );
  1210. } else {
  1211. Matrix4.clone(Matrix4.ZERO, uniformState._inverseProjection);
  1212. }
  1213. }
  1214. }
  1215. // Derived
  1216. function cleanModelView(uniformState) {
  1217. if (uniformState._modelViewDirty) {
  1218. uniformState._modelViewDirty = false;
  1219. Matrix4.multiplyTransformation(
  1220. uniformState._view,
  1221. uniformState._model,
  1222. uniformState._modelView
  1223. );
  1224. }
  1225. }
  1226. function cleanModelView3D(uniformState) {
  1227. if (uniformState._modelView3DDirty) {
  1228. uniformState._modelView3DDirty = false;
  1229. Matrix4.multiplyTransformation(
  1230. uniformState.view3D,
  1231. uniformState._model,
  1232. uniformState._modelView3D
  1233. );
  1234. }
  1235. }
  1236. function cleanInverseModelView(uniformState) {
  1237. if (uniformState._inverseModelViewDirty) {
  1238. uniformState._inverseModelViewDirty = false;
  1239. Matrix4.inverse(uniformState.modelView, uniformState._inverseModelView);
  1240. }
  1241. }
  1242. function cleanInverseModelView3D(uniformState) {
  1243. if (uniformState._inverseModelView3DDirty) {
  1244. uniformState._inverseModelView3DDirty = false;
  1245. Matrix4.inverse(uniformState.modelView3D, uniformState._inverseModelView3D);
  1246. }
  1247. }
  1248. function cleanViewProjection(uniformState) {
  1249. if (uniformState._viewProjectionDirty) {
  1250. uniformState._viewProjectionDirty = false;
  1251. Matrix4.multiply(
  1252. uniformState._projection,
  1253. uniformState._view,
  1254. uniformState._viewProjection
  1255. );
  1256. }
  1257. }
  1258. function cleanInverseViewProjection(uniformState) {
  1259. if (uniformState._inverseViewProjectionDirty) {
  1260. uniformState._inverseViewProjectionDirty = false;
  1261. Matrix4.inverse(
  1262. uniformState.viewProjection,
  1263. uniformState._inverseViewProjection
  1264. );
  1265. }
  1266. }
  1267. function cleanModelViewProjection(uniformState) {
  1268. if (uniformState._modelViewProjectionDirty) {
  1269. uniformState._modelViewProjectionDirty = false;
  1270. Matrix4.multiply(
  1271. uniformState._projection,
  1272. uniformState.modelView,
  1273. uniformState._modelViewProjection
  1274. );
  1275. }
  1276. }
  1277. function cleanModelViewRelativeToEye(uniformState) {
  1278. if (uniformState._modelViewRelativeToEyeDirty) {
  1279. uniformState._modelViewRelativeToEyeDirty = false;
  1280. var mv = uniformState.modelView;
  1281. var mvRte = uniformState._modelViewRelativeToEye;
  1282. mvRte[0] = mv[0];
  1283. mvRte[1] = mv[1];
  1284. mvRte[2] = mv[2];
  1285. mvRte[3] = mv[3];
  1286. mvRte[4] = mv[4];
  1287. mvRte[5] = mv[5];
  1288. mvRte[6] = mv[6];
  1289. mvRte[7] = mv[7];
  1290. mvRte[8] = mv[8];
  1291. mvRte[9] = mv[9];
  1292. mvRte[10] = mv[10];
  1293. mvRte[11] = mv[11];
  1294. mvRte[12] = 0.0;
  1295. mvRte[13] = 0.0;
  1296. mvRte[14] = 0.0;
  1297. mvRte[15] = mv[15];
  1298. }
  1299. }
  1300. function cleanInverseModelViewProjection(uniformState) {
  1301. if (uniformState._inverseModelViewProjectionDirty) {
  1302. uniformState._inverseModelViewProjectionDirty = false;
  1303. Matrix4.inverse(
  1304. uniformState.modelViewProjection,
  1305. uniformState._inverseModelViewProjection
  1306. );
  1307. }
  1308. }
  1309. function cleanModelViewProjectionRelativeToEye(uniformState) {
  1310. if (uniformState._modelViewProjectionRelativeToEyeDirty) {
  1311. uniformState._modelViewProjectionRelativeToEyeDirty = false;
  1312. Matrix4.multiply(
  1313. uniformState._projection,
  1314. uniformState.modelViewRelativeToEye,
  1315. uniformState._modelViewProjectionRelativeToEye
  1316. );
  1317. }
  1318. }
  1319. function cleanModelViewInfiniteProjection(uniformState) {
  1320. if (uniformState._modelViewInfiniteProjectionDirty) {
  1321. uniformState._modelViewInfiniteProjectionDirty = false;
  1322. Matrix4.multiply(
  1323. uniformState._infiniteProjection,
  1324. uniformState.modelView,
  1325. uniformState._modelViewInfiniteProjection
  1326. );
  1327. }
  1328. }
  1329. function cleanNormal(uniformState) {
  1330. if (uniformState._normalDirty) {
  1331. uniformState._normalDirty = false;
  1332. var m = uniformState._normal;
  1333. Matrix4.getMatrix3(uniformState.inverseModelView, m);
  1334. Matrix3.getRotation(m, m);
  1335. Matrix3.transpose(m, m);
  1336. }
  1337. }
  1338. function cleanNormal3D(uniformState) {
  1339. if (uniformState._normal3DDirty) {
  1340. uniformState._normal3DDirty = false;
  1341. var m = uniformState._normal3D;
  1342. Matrix4.getMatrix3(uniformState.inverseModelView3D, m);
  1343. Matrix3.getRotation(m, m);
  1344. Matrix3.transpose(m, m);
  1345. }
  1346. }
  1347. function cleanInverseNormal(uniformState) {
  1348. if (uniformState._inverseNormalDirty) {
  1349. uniformState._inverseNormalDirty = false;
  1350. Matrix4.getMatrix3(
  1351. uniformState.inverseModelView,
  1352. uniformState._inverseNormal
  1353. );
  1354. Matrix3.getRotation(
  1355. uniformState._inverseNormal,
  1356. uniformState._inverseNormal
  1357. );
  1358. }
  1359. }
  1360. function cleanInverseNormal3D(uniformState) {
  1361. if (uniformState._inverseNormal3DDirty) {
  1362. uniformState._inverseNormal3DDirty = false;
  1363. Matrix4.getMatrix3(
  1364. uniformState.inverseModelView3D,
  1365. uniformState._inverseNormal3D
  1366. );
  1367. Matrix3.getRotation(
  1368. uniformState._inverseNormal3D,
  1369. uniformState._inverseNormal3D
  1370. );
  1371. }
  1372. }
  1373. var cameraPositionMC = new Cartesian3();
  1374. function cleanEncodedCameraPositionMC(uniformState) {
  1375. if (uniformState._encodedCameraPositionMCDirty) {
  1376. uniformState._encodedCameraPositionMCDirty = false;
  1377. Matrix4.multiplyByPoint(
  1378. uniformState.inverseModel,
  1379. uniformState._cameraPosition,
  1380. cameraPositionMC
  1381. );
  1382. EncodedCartesian3.fromCartesian(
  1383. cameraPositionMC,
  1384. uniformState._encodedCameraPositionMC
  1385. );
  1386. }
  1387. }
  1388. var view2Dto3DPScratch = new Cartesian3();
  1389. var view2Dto3DRScratch = new Cartesian3();
  1390. var view2Dto3DUScratch = new Cartesian3();
  1391. var view2Dto3DDScratch = new Cartesian3();
  1392. var view2Dto3DCartographicScratch = new Cartographic();
  1393. var view2Dto3DCartesian3Scratch = new Cartesian3();
  1394. var view2Dto3DMatrix4Scratch = new Matrix4();
  1395. function view2Dto3D(
  1396. position2D,
  1397. direction2D,
  1398. right2D,
  1399. up2D,
  1400. frustum2DWidth,
  1401. mode,
  1402. projection,
  1403. result
  1404. ) {
  1405. // The camera position and directions are expressed in the 2D coordinate system where the Y axis is to the East,
  1406. // the Z axis is to the North, and the X axis is out of the map. Express them instead in the ENU axes where
  1407. // X is to the East, Y is to the North, and Z is out of the local horizontal plane.
  1408. var p = view2Dto3DPScratch;
  1409. p.x = position2D.y;
  1410. p.y = position2D.z;
  1411. p.z = position2D.x;
  1412. var r = view2Dto3DRScratch;
  1413. r.x = right2D.y;
  1414. r.y = right2D.z;
  1415. r.z = right2D.x;
  1416. var u = view2Dto3DUScratch;
  1417. u.x = up2D.y;
  1418. u.y = up2D.z;
  1419. u.z = up2D.x;
  1420. var d = view2Dto3DDScratch;
  1421. d.x = direction2D.y;
  1422. d.y = direction2D.z;
  1423. d.z = direction2D.x;
  1424. // In 2D, the camera height is always 12.7 million meters.
  1425. // The apparent height is equal to half the frustum width.
  1426. if (mode === SceneMode.SCENE2D) {
  1427. p.z = frustum2DWidth * 0.5;
  1428. }
  1429. // Compute the equivalent camera position in the real (3D) world.
  1430. // In 2D and Columbus View, the camera can travel outside the projection, and when it does so
  1431. // there's not really any corresponding location in the real world. So clamp the unprojected
  1432. // longitude and latitude to their valid ranges.
  1433. var cartographic = projection.unproject(p, view2Dto3DCartographicScratch);
  1434. cartographic.longitude = CesiumMath.clamp(
  1435. cartographic.longitude,
  1436. -Math.PI,
  1437. Math.PI
  1438. );
  1439. cartographic.latitude = CesiumMath.clamp(
  1440. cartographic.latitude,
  1441. -CesiumMath.PI_OVER_TWO,
  1442. CesiumMath.PI_OVER_TWO
  1443. );
  1444. var ellipsoid = projection.ellipsoid;
  1445. var position3D = ellipsoid.cartographicToCartesian(
  1446. cartographic,
  1447. view2Dto3DCartesian3Scratch
  1448. );
  1449. // Compute the rotation from the local ENU at the real world camera position to the fixed axes.
  1450. var enuToFixed = Transforms.eastNorthUpToFixedFrame(
  1451. position3D,
  1452. ellipsoid,
  1453. view2Dto3DMatrix4Scratch
  1454. );
  1455. // Transform each camera direction to the fixed axes.
  1456. Matrix4.multiplyByPointAsVector(enuToFixed, r, r);
  1457. Matrix4.multiplyByPointAsVector(enuToFixed, u, u);
  1458. Matrix4.multiplyByPointAsVector(enuToFixed, d, d);
  1459. // Compute the view matrix based on the new fixed-frame camera position and directions.
  1460. if (!defined(result)) {
  1461. result = new Matrix4();
  1462. }
  1463. result[0] = r.x;
  1464. result[1] = u.x;
  1465. result[2] = -d.x;
  1466. result[3] = 0.0;
  1467. result[4] = r.y;
  1468. result[5] = u.y;
  1469. result[6] = -d.y;
  1470. result[7] = 0.0;
  1471. result[8] = r.z;
  1472. result[9] = u.z;
  1473. result[10] = -d.z;
  1474. result[11] = 0.0;
  1475. result[12] = -Cartesian3.dot(r, position3D);
  1476. result[13] = -Cartesian3.dot(u, position3D);
  1477. result[14] = Cartesian3.dot(d, position3D);
  1478. result[15] = 1.0;
  1479. return result;
  1480. }
  1481. function updateView3D(that) {
  1482. if (that._view3DDirty) {
  1483. if (that._mode === SceneMode.SCENE3D) {
  1484. Matrix4.clone(that._view, that._view3D);
  1485. } else {
  1486. view2Dto3D(
  1487. that._cameraPosition,
  1488. that._cameraDirection,
  1489. that._cameraRight,
  1490. that._cameraUp,
  1491. that._frustum2DWidth,
  1492. that._mode,
  1493. that._mapProjection,
  1494. that._view3D
  1495. );
  1496. }
  1497. Matrix4.getMatrix3(that._view3D, that._viewRotation3D);
  1498. that._view3DDirty = false;
  1499. }
  1500. }
  1501. function updateInverseView3D(that) {
  1502. if (that._inverseView3DDirty) {
  1503. Matrix4.inverseTransformation(that.view3D, that._inverseView3D);
  1504. Matrix4.getMatrix3(that._inverseView3D, that._inverseViewRotation3D);
  1505. that._inverseView3DDirty = false;
  1506. }
  1507. }
  1508. export default UniformState;