UniformState.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /*global define*/
  2. define([
  3. '../Core/BoundingRectangle',
  4. '../Core/Cartesian2',
  5. '../Core/Cartesian3',
  6. '../Core/Cartesian4',
  7. '../Core/Cartographic',
  8. '../Core/defined',
  9. '../Core/defineProperties',
  10. '../Core/EncodedCartesian3',
  11. '../Core/Math',
  12. '../Core/Matrix3',
  13. '../Core/Matrix4',
  14. '../Core/Simon1994PlanetaryPositions',
  15. '../Core/Transforms',
  16. '../Scene/SceneMode'
  17. ], function(
  18. BoundingRectangle,
  19. Cartesian2,
  20. Cartesian3,
  21. Cartesian4,
  22. Cartographic,
  23. defined,
  24. defineProperties,
  25. EncodedCartesian3,
  26. CesiumMath,
  27. Matrix3,
  28. Matrix4,
  29. Simon1994PlanetaryPositions,
  30. Transforms,
  31. SceneMode) {
  32. "use strict";
  33. /**
  34. * @private
  35. */
  36. var UniformState = function() {
  37. this._viewport = new BoundingRectangle();
  38. this._viewportCartesian4 = new Cartesian4();
  39. this._viewportDirty = false;
  40. this._viewportOrthographicMatrix = Matrix4.clone(Matrix4.IDENTITY);
  41. this._viewportTransformation = Matrix4.clone(Matrix4.IDENTITY);
  42. this._model = Matrix4.clone(Matrix4.IDENTITY);
  43. this._view = Matrix4.clone(Matrix4.IDENTITY);
  44. this._inverseView = Matrix4.clone(Matrix4.IDENTITY);
  45. this._projection = Matrix4.clone(Matrix4.IDENTITY);
  46. this._infiniteProjection = Matrix4.clone(Matrix4.IDENTITY);
  47. this._entireFrustum = new Cartesian2();
  48. this._currentFrustum = new Cartesian2();
  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._inverseProjectionOITDirty = true;
  67. this._inverseProjectionOIT = new Matrix4();
  68. this._modelViewDirty = true;
  69. this._modelView = new Matrix4();
  70. this._modelView3DDirty = true;
  71. this._modelView3D = new Matrix4();
  72. this._modelViewRelativeToEyeDirty = true;
  73. this._modelViewRelativeToEye = new Matrix4();
  74. this._inverseModelViewDirty = true;
  75. this._inverseModelView = new Matrix4();
  76. this._inverseModelView3DDirty = true;
  77. this._inverseModelView3D = new Matrix4();
  78. this._viewProjectionDirty = true;
  79. this._viewProjection = new Matrix4();
  80. this._inverseViewProjectionDirty = true;
  81. this._inverseViewProjection = new Matrix4();
  82. this._modelViewProjectionDirty = true;
  83. this._modelViewProjection = new Matrix4();
  84. this._inverseModelViewProjectionDirty = true;
  85. this._inverseModelViewProjection = new Matrix4();
  86. this._modelViewProjectionRelativeToEyeDirty = true;
  87. this._modelViewProjectionRelativeToEye = new Matrix4();
  88. this._modelViewInfiniteProjectionDirty = true;
  89. this._modelViewInfiniteProjection = new Matrix4();
  90. this._normalDirty = true;
  91. this._normal = new Matrix3();
  92. this._normal3DDirty = true;
  93. this._normal3D = new Matrix3();
  94. this._inverseNormalDirty = true;
  95. this._inverseNormal = new Matrix3();
  96. this._inverseNormal3DDirty = true;
  97. this._inverseNormal3D = new Matrix3();
  98. this._encodedCameraPositionMCDirty = true;
  99. this._encodedCameraPositionMC = new EncodedCartesian3();
  100. this._cameraPosition = new Cartesian3();
  101. this._sunPositionWC = new Cartesian3();
  102. this._sunPositionColumbusView = new Cartesian3();
  103. this._sunDirectionWC = new Cartesian3();
  104. this._sunDirectionEC = new Cartesian3();
  105. this._moonDirectionEC = new Cartesian3();
  106. this._mode = undefined;
  107. this._mapProjection = undefined;
  108. this._cameraDirection = new Cartesian3();
  109. this._cameraRight = new Cartesian3();
  110. this._cameraUp = new Cartesian3();
  111. this._frustum2DWidth = 0.0;
  112. this._eyeHeight2D = new Cartesian2();
  113. this._resolutionScale = 1.0;
  114. };
  115. defineProperties(UniformState.prototype, {
  116. /**
  117. * @memberof UniformState.prototype
  118. * @type {FrameState}
  119. * @readonly
  120. */
  121. frameState : {
  122. get : function() {
  123. return this._frameState;
  124. }
  125. },
  126. /**
  127. * @memberof UniformState.prototype
  128. * @type {BoundingRectangle}
  129. */
  130. viewport : {
  131. get : function() {
  132. return this._viewport;
  133. },
  134. set : function(viewport) {
  135. if (!BoundingRectangle.equals(viewport, this._viewport)) {
  136. BoundingRectangle.clone(viewport, this._viewport);
  137. var v = this._viewport;
  138. var vc = this._viewportCartesian4;
  139. vc.x = v.x;
  140. vc.y = v.y;
  141. vc.z = v.width;
  142. vc.w = v.height;
  143. this._viewportDirty = true;
  144. }
  145. }
  146. },
  147. /**
  148. * @memberof UniformState.prototype
  149. * @private
  150. */
  151. viewportCartesian4 : {
  152. get : function() {
  153. return this._viewportCartesian4;
  154. }
  155. },
  156. viewportOrthographic : {
  157. get : function() {
  158. cleanViewport(this);
  159. return this._viewportOrthographicMatrix;
  160. }
  161. },
  162. viewportTransformation : {
  163. get : function() {
  164. cleanViewport(this);
  165. return this._viewportTransformation;
  166. }
  167. },
  168. /**
  169. * @memberof UniformState.prototype
  170. * @type {Matrix4}
  171. */
  172. model : {
  173. get : function() {
  174. return this._model;
  175. },
  176. set : function(matrix) {
  177. Matrix4.clone(matrix, this._model);
  178. this._modelView3DDirty = true;
  179. this._inverseModelView3DDirty = true;
  180. this._inverseModelDirty = true;
  181. this._inverseTransposeModelDirty = true;
  182. this._modelViewDirty = true;
  183. this._inverseModelViewDirty = true;
  184. this._viewProjectionDirty = true;
  185. this._inverseViewProjectionDirty = true;
  186. this._modelViewRelativeToEyeDirty = true;
  187. this._inverseModelViewDirty = true;
  188. this._modelViewProjectionDirty = true;
  189. this._inverseModelViewProjectionDirty = true;
  190. this._modelViewProjectionRelativeToEyeDirty = true;
  191. this._modelViewInfiniteProjectionDirty = true;
  192. this._normalDirty = true;
  193. this._inverseNormalDirty = true;
  194. this._normal3DDirty = true;
  195. this._inverseNormal3DDirty = true;
  196. this._encodedCameraPositionMCDirty = true;
  197. }
  198. },
  199. /**
  200. * @memberof UniformState.prototype
  201. * @type {Matrix4}
  202. */
  203. inverseModel : {
  204. get : function() {
  205. if (this._inverseModelDirty) {
  206. this._inverseModelDirty = false;
  207. Matrix4.inverse(this._model, this._inverseModel);
  208. }
  209. return this._inverseModel;
  210. }
  211. },
  212. /**
  213. * @memberof UniformState.prototype
  214. * @private
  215. */
  216. inverseTranposeModel : {
  217. get : function() {
  218. var m = this._inverseTransposeModel;
  219. if (this._inverseTransposeModelDirty) {
  220. this._inverseTransposeModelDirty = false;
  221. Matrix4.getRotation(this.inverseModel, m);
  222. Matrix3.transpose(m, m);
  223. }
  224. return m;
  225. }
  226. },
  227. /**
  228. * @memberof UniformState.prototype
  229. * @type {Matrix4}
  230. */
  231. view : {
  232. get : function() {
  233. return this._view;
  234. }
  235. },
  236. /**
  237. * The 3D view matrix. In 3D mode, this is identical to {@link UniformState#view},
  238. * but in 2D and Columbus View it is a synthetic matrix based on the equivalent position
  239. * of the camera in the 3D world.
  240. * @memberof UniformState.prototype
  241. * @type {Matrix4}
  242. */
  243. view3D : {
  244. get : function() {
  245. if (this._view3DDirty) {
  246. if (this._mode === SceneMode.SCENE3D) {
  247. Matrix4.clone(this._view, this._view3D);
  248. } else {
  249. view2Dto3D(this._cameraPosition, this._cameraDirection, this._cameraRight, this._cameraUp, this._frustum2DWidth, this._mode, this._mapProjection, this._view3D);
  250. }
  251. Matrix4.getRotation(this._view3D, this._viewRotation3D);
  252. this._view3DDirty = false;
  253. }
  254. return this._view3D;
  255. }
  256. },
  257. /**
  258. * The 3x3 rotation matrix of the current view matrix ({@link UniformState#view}).
  259. * @memberof UniformState.prototype
  260. * @type {Matrix3}
  261. */
  262. viewRotation : {
  263. get : function() {
  264. return this._viewRotation;
  265. }
  266. },
  267. /**
  268. * @memberof UniformState.prototype
  269. * @type {Matrix3}
  270. */
  271. viewRotation3D : {
  272. get : function() {
  273. var view3D = this.view3D;
  274. return this._viewRotation3D;
  275. }
  276. },
  277. /**
  278. * @memberof UniformState.prototype
  279. * @type {Matrix4}
  280. */
  281. inverseView : {
  282. get : function() {
  283. return this._inverseView;
  284. }
  285. },
  286. /**
  287. * the 4x4 inverse-view matrix that transforms from eye to 3D world coordinates. In 3D mode, this is
  288. * identical to {@link UniformState#inverseView}, but in 2D and Columbus View it is a synthetic matrix
  289. * based on the equivalent position of the camera in the 3D world.
  290. * @memberof UniformState.prototype
  291. * @type {Matrix4}
  292. */
  293. inverseView3D : {
  294. get : function() {
  295. if (this._inverseView3DDirty) {
  296. Matrix4.inverseTransformation(this.view3D, this._inverseView3D);
  297. Matrix4.getRotation(this._inverseView3D, this._inverseViewRotation3D);
  298. this._inverseView3DDirty = false;
  299. }
  300. return this._inverseView3D;
  301. }
  302. },
  303. /**
  304. * @memberof UniformState.prototype
  305. * @type {Matrix3}
  306. */
  307. inverseViewRotation : {
  308. get : function() {
  309. return this._inverseViewRotation;
  310. }
  311. },
  312. /**
  313. * The 3x3 rotation matrix of the current 3D inverse-view matrix ({@link UniformState#inverseView3D}).
  314. * @memberof UniformState.prototype
  315. * @type {Matrix3}
  316. */
  317. inverseViewRotation3D : {
  318. get : function() {
  319. var inverseView = this.inverseView3D;
  320. return this._inverseViewRotation3D;
  321. }
  322. },
  323. /**
  324. * @memberof UniformState.prototype
  325. * @type {Matrix4}
  326. */
  327. projection : {
  328. get : function() {
  329. return this._projection;
  330. }
  331. },
  332. /**
  333. * @memberof UniformState.prototype
  334. * @type {Matrix4}
  335. */
  336. inverseProjection : {
  337. get : function() {
  338. cleanInverseProjection(this);
  339. return this._inverseProjection;
  340. }
  341. },
  342. /**
  343. * @memberof UniformState.prototype
  344. * @private
  345. */
  346. inverseProjectionOIT : {
  347. get : function() {
  348. cleanInverseProjectionOIT(this);
  349. return this._inverseProjectionOIT;
  350. }
  351. },
  352. /**
  353. * @memberof UniformState.prototype
  354. * @type {Matrix4}
  355. */
  356. infiniteProjection : {
  357. get : function() {
  358. return this._infiniteProjection;
  359. }
  360. },
  361. /**
  362. * @memberof UniformState.prototype
  363. * @type {Matrix4}
  364. */
  365. modelView : {
  366. get : function() {
  367. cleanModelView(this);
  368. return this._modelView;
  369. }
  370. },
  371. /**
  372. * The 3D model-view matrix. In 3D mode, this is equivalent to {@link UniformState#modelView}. In 2D and
  373. * Columbus View, however, it is a synthetic matrix based on the equivalent position of the camera in the 3D world.
  374. * @memberof UniformState.prototype
  375. * @type {Matrix4}
  376. */
  377. modelView3D : {
  378. get : function() {
  379. cleanModelView3D(this);
  380. return this._modelView3D;
  381. }
  382. },
  383. /**
  384. * Model-view relative to eye matrix.
  385. *
  386. * @memberof UniformState.prototype
  387. * @type {Matrix4}
  388. */
  389. modelViewRelativeToEye : {
  390. get : function() {
  391. cleanModelViewRelativeToEye(this);
  392. return this._modelViewRelativeToEye;
  393. }
  394. },
  395. /**
  396. * @memberof UniformState.prototype
  397. * @type {Matrix4}
  398. */
  399. inverseModelView : {
  400. get : function() {
  401. cleanInverseModelView(this);
  402. return this._inverseModelView;
  403. }
  404. },
  405. /**
  406. * The inverse of the 3D model-view matrix. In 3D mode, this is equivalent to {@link UniformState#inverseModelView}.
  407. * In 2D and Columbus View, however, it is a synthetic matrix based on the equivalent position of the camera in the 3D world.
  408. * @memberof UniformState.prototype
  409. * @type {Matrix4}
  410. */
  411. inverseModelView3D : {
  412. get : function() {
  413. cleanInverseModelView3D(this);
  414. return this._inverseModelView3D;
  415. }
  416. },
  417. /**
  418. * @memberof UniformState.prototype
  419. * @type {Matrix4}
  420. */
  421. viewProjection : {
  422. get : function() {
  423. cleanViewProjection(this);
  424. return this._viewProjection;
  425. }
  426. },
  427. /**
  428. * @memberof UniformState.prototype
  429. * @type {Matrix4}
  430. */
  431. inverseViewProjection : {
  432. get : function() {
  433. cleanInverseViewProjection(this);
  434. return this._inverseViewProjection;
  435. }
  436. },
  437. /**
  438. * @memberof UniformState.prototype
  439. * @type {Matrix4}
  440. */
  441. modelViewProjection : {
  442. get : function() {
  443. cleanModelViewProjection(this);
  444. return this._modelViewProjection;
  445. }
  446. },
  447. /**
  448. * @memberof UniformState.prototype
  449. * @type {Matrix4}
  450. */
  451. inverseModelViewProjection : {
  452. get : function() {
  453. cleanInverseModelViewProjection(this);
  454. return this._inverseModelViewProjection;
  455. }
  456. },
  457. /**
  458. * Model-view-projection relative to eye matrix.
  459. *
  460. * @memberof UniformState.prototype
  461. * @type {Matrix4}
  462. */
  463. modelViewProjectionRelativeToEye : {
  464. get : function() {
  465. cleanModelViewProjectionRelativeToEye(this);
  466. return this._modelViewProjectionRelativeToEye;
  467. }
  468. },
  469. /**
  470. * @memberof UniformState.prototype
  471. * @type {Matrix4}
  472. */
  473. modelViewInfiniteProjection : {
  474. get : function() {
  475. cleanModelViewInfiniteProjection(this);
  476. return this._modelViewInfiniteProjection;
  477. }
  478. },
  479. /**
  480. * A 3x3 normal transformation matrix that transforms normal vectors in model coordinates to
  481. * eye coordinates.
  482. * @memberof UniformState.prototype
  483. * @type {Matrix3}
  484. */
  485. normal : {
  486. get : function() {
  487. cleanNormal(this);
  488. return this._normal;
  489. }
  490. },
  491. /**
  492. * A 3x3 normal transformation matrix that transforms normal vectors in 3D model
  493. * coordinates to eye coordinates. In 3D mode, this is identical to
  494. * {@link UniformState#normal}, but in 2D and Columbus View it represents the normal transformation
  495. * matrix as if the camera were at an equivalent location in 3D mode.
  496. * @memberof UniformState.prototype
  497. * @type {Matrix3}
  498. */
  499. normal3D : {
  500. get : function() {
  501. cleanNormal3D(this);
  502. return this._normal3D;
  503. }
  504. },
  505. /**
  506. * An inverse 3x3 normal transformation matrix that transforms normal vectors in model coordinates
  507. * to eye coordinates.
  508. * @memberof UniformState.prototype
  509. * @type {Matrix3}
  510. */
  511. inverseNormal : {
  512. get : function() {
  513. cleanInverseNormal(this);
  514. return this._inverseNormal;
  515. }
  516. },
  517. /**
  518. * An inverse 3x3 normal transformation matrix that transforms normal vectors in eye coordinates
  519. * to 3D model coordinates. In 3D mode, this is identical to
  520. * {@link UniformState#inverseNormal}, but in 2D and Columbus View it represents the normal transformation
  521. * matrix as if the camera were at an equivalent location in 3D mode.
  522. * @memberof UniformState.prototype
  523. * @type {Matrix3}
  524. */
  525. inverseNormal3D : {
  526. get : function() {
  527. cleanInverseNormal3D(this);
  528. return this._inverseNormal3D;
  529. }
  530. },
  531. /**
  532. * The near distance (<code>x</code>) and the far distance (<code>y</code>) of the frustum defined by the camera.
  533. * This is the largest possible frustum, not an individual frustum used for multi-frustum rendering.
  534. * @memberof UniformState.prototype
  535. * @type {Cartesian2}
  536. */
  537. entireFrustum : {
  538. get : function() {
  539. return this._entireFrustum;
  540. }
  541. },
  542. /**
  543. * The near distance (<code>x</code>) and the far distance (<code>y</code>) of the frustum defined by the camera.
  544. * This is the individual frustum used for multi-frustum rendering.
  545. * @memberof UniformState.prototype
  546. * @type {Cartesian2}
  547. */
  548. currentFrustum : {
  549. get : function() {
  550. return this._currentFrustum;
  551. }
  552. },
  553. /**
  554. * The the height (<code>x</code>) and the height squared (<code>y</code>)
  555. * in meters of the camera above the 2D world plane. This uniform is only valid
  556. * when the {@link SceneMode} equal to <code>SCENE2D</code>.
  557. * @memberof UniformState.prototype
  558. * @type {Cartesian2}
  559. */
  560. eyeHeight2D : {
  561. get : function() {
  562. return this._eyeHeight2D;
  563. }
  564. },
  565. /**
  566. * The sun position in 3D world coordinates at the current scene time.
  567. * @memberof UniformState.prototype
  568. * @type {Cartesian3}
  569. */
  570. sunPositionWC : {
  571. get : function() {
  572. return this._sunPositionWC;
  573. }
  574. },
  575. /**
  576. * The sun position in 2D world coordinates at the current scene time.
  577. * @memberof UniformState.prototype
  578. * @type {Cartesian3}
  579. */
  580. sunPositionColumbusView : {
  581. get : function(){
  582. return this._sunPositionColumbusView;
  583. }
  584. },
  585. /**
  586. * A normalized vector to the sun in 3D world coordinates at the current scene time. Even in 2D or
  587. * Columbus View mode, this returns the position of the sun in the 3D scene.
  588. * @memberof UniformState.prototype
  589. * @type {Cartesian3}
  590. */
  591. sunDirectionWC : {
  592. get : function() {
  593. return this._sunDirectionWC;
  594. }
  595. },
  596. /**
  597. * A normalized vector to the sun in eye coordinates at the current scene time. In 3D mode, this
  598. * returns the actual vector from the camera position to the sun position. In 2D and Columbus View, it returns
  599. * the vector from the equivalent 3D camera position to the position of the sun in the 3D scene.
  600. * @memberof UniformState.prototype
  601. * @type {Cartesian3}
  602. */
  603. sunDirectionEC : {
  604. get : function() {
  605. return this._sunDirectionEC;
  606. }
  607. },
  608. /**
  609. * A normalized vector to the moon in eye coordinates at the current scene time. In 3D mode, this
  610. * returns the actual vector from the camera position to the moon position. In 2D and Columbus View, it returns
  611. * the vector from the equivalent 3D camera position to the position of the moon in the 3D scene.
  612. * @memberof UniformState.prototype
  613. * @type {Cartesian3}
  614. */
  615. moonDirectionEC : {
  616. get : function() {
  617. return this._moonDirectionEC;
  618. }
  619. },
  620. /**
  621. * The high bits of the camera position.
  622. * @memberof UniformState.prototype
  623. * @type {Cartesian3}
  624. */
  625. encodedCameraPositionMCHigh : {
  626. get : function() {
  627. cleanEncodedCameraPositionMC(this);
  628. return this._encodedCameraPositionMC.high;
  629. }
  630. },
  631. /**
  632. * The low bits of the camera position.
  633. * @memberof UniformState.prototype
  634. * @type {Cartesian3}
  635. */
  636. encodedCameraPositionMCLow : {
  637. get : function() {
  638. cleanEncodedCameraPositionMC(this);
  639. return this._encodedCameraPositionMC.low;
  640. }
  641. },
  642. /**
  643. * A 3x3 matrix that transforms from True Equator Mean Equinox (TEME) axes to the
  644. * pseudo-fixed axes at the Scene's current time.
  645. * @memberof UniformState.prototype
  646. * @type {Matrix3}
  647. */
  648. temeToPseudoFixedMatrix : {
  649. get : function() {
  650. return this._temeToPseudoFixed;
  651. }
  652. },
  653. /**
  654. * Gets the scaling factor for transforming from the canvas
  655. * pixel space to canvas coordinate space.
  656. * @memberof UniformState.prototype
  657. * @type {Number}
  658. */
  659. resolutionScale : {
  660. get : function() {
  661. return this._resolutionScale;
  662. }
  663. }
  664. });
  665. function setView(uniformState, matrix) {
  666. Matrix4.clone(matrix, uniformState._view);
  667. Matrix4.getRotation(matrix, uniformState._viewRotation);
  668. uniformState._view3DDirty = true;
  669. uniformState._inverseView3DDirty = true;
  670. uniformState._modelViewDirty = true;
  671. uniformState._modelView3DDirty = true;
  672. uniformState._modelViewRelativeToEyeDirty = true;
  673. uniformState._inverseModelViewDirty = true;
  674. uniformState._inverseModelView3DDirty = true;
  675. uniformState._viewProjectionDirty = true;
  676. uniformState._modelViewProjectionDirty = true;
  677. uniformState._modelViewProjectionRelativeToEyeDirty = true;
  678. uniformState._modelViewInfiniteProjectionDirty = true;
  679. uniformState._normalDirty = true;
  680. uniformState._inverseNormalDirty = true;
  681. uniformState._normal3DDirty = true;
  682. uniformState._inverseNormal3DDirty = true;
  683. }
  684. function setInverseView(uniformState, matrix) {
  685. Matrix4.clone(matrix, uniformState._inverseView);
  686. Matrix4.getRotation(matrix, uniformState._inverseViewRotation);
  687. }
  688. function setProjection(uniformState, matrix) {
  689. Matrix4.clone(matrix, uniformState._projection);
  690. uniformState._inverseProjectionDirty = true;
  691. uniformState._inverseProjectionOITDirty = true;
  692. uniformState._viewProjectionDirty = true;
  693. uniformState._modelViewProjectionDirty = true;
  694. uniformState._modelViewProjectionRelativeToEyeDirty = true;
  695. }
  696. function setInfiniteProjection(uniformState, matrix) {
  697. Matrix4.clone(matrix, uniformState._infiniteProjection);
  698. uniformState._modelViewInfiniteProjectionDirty = true;
  699. }
  700. function setCamera(uniformState, camera) {
  701. Cartesian3.clone(camera.positionWC, uniformState._cameraPosition);
  702. Cartesian3.clone(camera.directionWC, uniformState._cameraDirection);
  703. Cartesian3.clone(camera.rightWC, uniformState._cameraRight);
  704. Cartesian3.clone(camera.upWC, uniformState._cameraUp);
  705. uniformState._encodedCameraPositionMCDirty = true;
  706. }
  707. var transformMatrix = new Matrix3();
  708. var sunCartographicScratch = new Cartographic();
  709. function setSunAndMoonDirections(uniformState, frameState) {
  710. if (!defined(Transforms.computeIcrfToFixedMatrix(frameState.time, transformMatrix))) {
  711. transformMatrix = Transforms.computeTemeToPseudoFixedMatrix(frameState.time, transformMatrix);
  712. }
  713. var position = Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame(frameState.time, uniformState._sunPositionWC);
  714. Matrix3.multiplyByVector(transformMatrix, position, position);
  715. Cartesian3.normalize(position, uniformState._sunDirectionWC);
  716. position = Matrix3.multiplyByVector(uniformState.viewRotation3D, position, uniformState._sunDirectionEC);
  717. Cartesian3.normalize(position, position);
  718. position = Simon1994PlanetaryPositions.computeMoonPositionInEarthInertialFrame(frameState.time, uniformState._moonDirectionEC);
  719. Matrix3.multiplyByVector(transformMatrix, position, position);
  720. Matrix3.multiplyByVector(uniformState.viewRotation3D, position, position);
  721. Cartesian3.normalize(position, position);
  722. var projection = frameState.mapProjection;
  723. var ellipsoid = projection.ellipsoid;
  724. var sunCartographic = ellipsoid.cartesianToCartographic(uniformState._sunPositionWC, sunCartographicScratch);
  725. projection.project(sunCartographic, uniformState._sunPositionColumbusView);
  726. }
  727. /**
  728. * Synchronizes the frustum's state with the uniform state. This is called
  729. * by the {@link Scene} when rendering to ensure that automatic GLSL uniforms
  730. * are set to the right value.
  731. *
  732. * @param {Object} frustum The frustum to synchronize with.
  733. */
  734. UniformState.prototype.updateFrustum = function(frustum) {
  735. setProjection(this, frustum.projectionMatrix);
  736. if (defined(frustum.infiniteProjectionMatrix)) {
  737. setInfiniteProjection(this, frustum.infiniteProjectionMatrix);
  738. }
  739. this._currentFrustum.x = frustum.near;
  740. this._currentFrustum.y = frustum.far;
  741. };
  742. /**
  743. * Synchronizes frame state with the uniform state. This is called
  744. * by the {@link Scene} when rendering to ensure that automatic GLSL uniforms
  745. * are set to the right value.
  746. *
  747. * @param {FrameState} frameState The frameState to synchronize with.
  748. */
  749. UniformState.prototype.update = function(context, frameState) {
  750. this._mode = frameState.mode;
  751. this._mapProjection = frameState.mapProjection;
  752. var canvas = context._canvas;
  753. this._resolutionScale = canvas.width / canvas.clientWidth;
  754. var camera = frameState.camera;
  755. setView(this, camera.viewMatrix);
  756. setInverseView(this, camera.inverseViewMatrix);
  757. setCamera(this, camera);
  758. if (frameState.mode === SceneMode.SCENE2D) {
  759. this._frustum2DWidth = camera.frustum.right - camera.frustum.left;
  760. this._eyeHeight2D.x = this._frustum2DWidth * 0.5;
  761. this._eyeHeight2D.y = this._eyeHeight2D.x * this._eyeHeight2D.x;
  762. } else {
  763. this._frustum2DWidth = 0.0;
  764. this._eyeHeight2D.x = 0.0;
  765. this._eyeHeight2D.y = 0.0;
  766. }
  767. setSunAndMoonDirections(this, frameState);
  768. this._entireFrustum.x = camera.frustum.near;
  769. this._entireFrustum.y = camera.frustum.far;
  770. this.updateFrustum(camera.frustum);
  771. this._frameState = frameState;
  772. this._temeToPseudoFixed = Transforms.computeTemeToPseudoFixedMatrix(frameState.time, this._temeToPseudoFixed);
  773. };
  774. function cleanViewport(uniformState) {
  775. if (uniformState._viewportDirty) {
  776. var v = uniformState._viewport;
  777. Matrix4.computeOrthographicOffCenter(v.x, v.x + v.width, v.y, v.y + v.height, 0.0, 1.0, uniformState._viewportOrthographicMatrix);
  778. Matrix4.computeViewportTransformation(v, 0.0, 1.0, uniformState._viewportTransformation);
  779. uniformState._viewportDirty = false;
  780. }
  781. }
  782. function cleanInverseProjection(uniformState) {
  783. if (uniformState._inverseProjectionDirty) {
  784. uniformState._inverseProjectionDirty = false;
  785. Matrix4.inverse(uniformState._projection, uniformState._inverseProjection);
  786. }
  787. }
  788. function cleanInverseProjectionOIT(uniformState) {
  789. if (uniformState._inverseProjectionOITDirty) {
  790. uniformState._inverseProjectionOITDirty = false;
  791. if (uniformState._mode !== SceneMode.SCENE2D && uniformState._mode !== SceneMode.MORPHING) {
  792. Matrix4.inverse(uniformState._projection, uniformState._inverseProjectionOIT);
  793. } else {
  794. Matrix4.clone(Matrix4.IDENTITY, uniformState._inverseProjectionOIT);
  795. }
  796. }
  797. }
  798. // Derived
  799. function cleanModelView(uniformState) {
  800. if (uniformState._modelViewDirty) {
  801. uniformState._modelViewDirty = false;
  802. Matrix4.multiplyTransformation(uniformState._view, uniformState._model, uniformState._modelView);
  803. }
  804. }
  805. function cleanModelView3D(uniformState) {
  806. if (uniformState._modelView3DDirty) {
  807. uniformState._modelView3DDirty = false;
  808. Matrix4.multiplyTransformation(uniformState.view3D, uniformState._model, uniformState._modelView3D);
  809. }
  810. }
  811. function cleanInverseModelView(uniformState) {
  812. if (uniformState._inverseModelViewDirty) {
  813. uniformState._inverseModelViewDirty = false;
  814. Matrix4.inverse(uniformState.modelView, uniformState._inverseModelView);
  815. }
  816. }
  817. function cleanInverseModelView3D(uniformState) {
  818. if (uniformState._inverseModelView3DDirty) {
  819. uniformState._inverseModelView3DDirty = false;
  820. Matrix4.inverse(uniformState.modelView3D, uniformState._inverseModelView3D);
  821. }
  822. }
  823. function cleanViewProjection(uniformState) {
  824. if (uniformState._viewProjectionDirty) {
  825. uniformState._viewProjectionDirty = false;
  826. Matrix4.multiply(uniformState._projection, uniformState._view, uniformState._viewProjection);
  827. }
  828. }
  829. function cleanInverseViewProjection(uniformState) {
  830. if (uniformState._inverseViewProjectionDirty) {
  831. uniformState._inverseViewProjectionDirty = false;
  832. Matrix4.inverse(uniformState.viewProjection, uniformState._inverseViewProjection);
  833. }
  834. }
  835. function cleanModelViewProjection(uniformState) {
  836. if (uniformState._modelViewProjectionDirty) {
  837. uniformState._modelViewProjectionDirty = false;
  838. Matrix4.multiply(uniformState._projection, uniformState.modelView, uniformState._modelViewProjection);
  839. }
  840. }
  841. function cleanModelViewRelativeToEye(uniformState) {
  842. if (uniformState._modelViewRelativeToEyeDirty) {
  843. uniformState._modelViewRelativeToEyeDirty = false;
  844. var mv = uniformState.modelView;
  845. var mvRte = uniformState._modelViewRelativeToEye;
  846. mvRte[0] = mv[0];
  847. mvRte[1] = mv[1];
  848. mvRte[2] = mv[2];
  849. mvRte[3] = mv[3];
  850. mvRte[4] = mv[4];
  851. mvRte[5] = mv[5];
  852. mvRte[6] = mv[6];
  853. mvRte[7] = mv[7];
  854. mvRte[8] = mv[8];
  855. mvRte[9] = mv[9];
  856. mvRte[10] = mv[10];
  857. mvRte[11] = mv[11];
  858. mvRte[12] = 0.0;
  859. mvRte[13] = 0.0;
  860. mvRte[14] = 0.0;
  861. mvRte[15] = mv[15];
  862. }
  863. }
  864. function cleanInverseModelViewProjection(uniformState) {
  865. if (uniformState._inverseModelViewProjectionDirty) {
  866. uniformState._inverseModelViewProjectionDirty = false;
  867. Matrix4.inverse(uniformState.modelViewProjection, uniformState._inverseModelViewProjection);
  868. }
  869. }
  870. function cleanModelViewProjectionRelativeToEye(uniformState) {
  871. if (uniformState._modelViewProjectionRelativeToEyeDirty) {
  872. uniformState._modelViewProjectionRelativeToEyeDirty = false;
  873. Matrix4.multiply(uniformState._projection, uniformState.modelViewRelativeToEye, uniformState._modelViewProjectionRelativeToEye);
  874. }
  875. }
  876. function cleanModelViewInfiniteProjection(uniformState) {
  877. if (uniformState._modelViewInfiniteProjectionDirty) {
  878. uniformState._modelViewInfiniteProjectionDirty = false;
  879. Matrix4.multiply(uniformState._infiniteProjection, uniformState.modelView, uniformState._modelViewInfiniteProjection);
  880. }
  881. }
  882. function cleanNormal(uniformState) {
  883. if (uniformState._normalDirty) {
  884. uniformState._normalDirty = false;
  885. var m = uniformState._normal;
  886. Matrix4.getRotation(uniformState.inverseModelView, m);
  887. Matrix3.transpose(m, m);
  888. }
  889. }
  890. function cleanNormal3D(uniformState) {
  891. if (uniformState._normal3DDirty) {
  892. uniformState._normal3DDirty = false;
  893. var m = uniformState._normal3D;
  894. Matrix4.getRotation(uniformState.inverseModelView3D, m);
  895. Matrix3.transpose(m, m);
  896. }
  897. }
  898. function cleanInverseNormal(uniformState) {
  899. if (uniformState._inverseNormalDirty) {
  900. uniformState._inverseNormalDirty = false;
  901. Matrix4.getRotation(uniformState.inverseModelView, uniformState._inverseNormal);
  902. }
  903. }
  904. function cleanInverseNormal3D(uniformState) {
  905. if (uniformState._inverseNormal3DDirty) {
  906. uniformState._inverseNormal3DDirty = false;
  907. Matrix4.getRotation(uniformState.inverseModelView3D, uniformState._inverseNormal3D);
  908. }
  909. }
  910. var cameraPositionMC = new Cartesian3();
  911. function cleanEncodedCameraPositionMC(uniformState) {
  912. if (uniformState._encodedCameraPositionMCDirty) {
  913. uniformState._encodedCameraPositionMCDirty = false;
  914. Matrix4.multiplyByPoint(uniformState.inverseModel, uniformState._cameraPosition, cameraPositionMC);
  915. EncodedCartesian3.fromCartesian(cameraPositionMC, uniformState._encodedCameraPositionMC);
  916. }
  917. }
  918. var view2Dto3DPScratch = new Cartesian3();
  919. var view2Dto3DRScratch = new Cartesian3();
  920. var view2Dto3DUScratch = new Cartesian3();
  921. var view2Dto3DDScratch = new Cartesian3();
  922. var view2Dto3DCartographicScratch = new Cartographic();
  923. var view2Dto3DCartesian3Scratch = new Cartesian3();
  924. var view2Dto3DMatrix4Scratch = new Matrix4();
  925. function view2Dto3D(position2D, direction2D, right2D, up2D, frustum2DWidth, mode, projection, result) {
  926. // The camera position and directions are expressed in the 2D coordinate system where the Y axis is to the East,
  927. // the Z axis is to the North, and the X axis is out of the map. Express them instead in the ENU axes where
  928. // X is to the East, Y is to the North, and Z is out of the local horizontal plane.
  929. var p = view2Dto3DPScratch;
  930. p.x = position2D.y;
  931. p.y = position2D.z;
  932. p.z = position2D.x;
  933. var r = view2Dto3DRScratch;
  934. r.x = right2D.y;
  935. r.y = right2D.z;
  936. r.z = right2D.x;
  937. var u = view2Dto3DUScratch;
  938. u.x = up2D.y;
  939. u.y = up2D.z;
  940. u.z = up2D.x;
  941. var d = view2Dto3DDScratch;
  942. d.x = direction2D.y;
  943. d.y = direction2D.z;
  944. d.z = direction2D.x;
  945. // In 2D, the camera height is always 12.7 million meters.
  946. // The apparent height is equal to half the frustum width.
  947. if (mode === SceneMode.SCENE2D) {
  948. p.z = frustum2DWidth * 0.5;
  949. }
  950. // Compute the equivalent camera position in the real (3D) world.
  951. // In 2D and Columbus View, the camera can travel outside the projection, and when it does so
  952. // there's not really any corresponding location in the real world. So clamp the unprojected
  953. // longitude and latitude to their valid ranges.
  954. var cartographic = projection.unproject(p, view2Dto3DCartographicScratch);
  955. cartographic.longitude = CesiumMath.clamp(cartographic.longitude, -Math.PI, Math.PI);
  956. cartographic.latitude = CesiumMath.clamp(cartographic.latitude, -CesiumMath.PI_OVER_TWO, CesiumMath.PI_OVER_TWO);
  957. var ellipsoid = projection.ellipsoid;
  958. var position3D = ellipsoid.cartographicToCartesian(cartographic, view2Dto3DCartesian3Scratch);
  959. // Compute the rotation from the local ENU at the real world camera position to the fixed axes.
  960. var enuToFixed = Transforms.eastNorthUpToFixedFrame(position3D, ellipsoid, view2Dto3DMatrix4Scratch);
  961. // Transform each camera direction to the fixed axes.
  962. Matrix4.multiplyByPointAsVector(enuToFixed, r, r);
  963. Matrix4.multiplyByPointAsVector(enuToFixed, u, u);
  964. Matrix4.multiplyByPointAsVector(enuToFixed, d, d);
  965. // Compute the view matrix based on the new fixed-frame camera position and directions.
  966. if (!defined(result)) {
  967. result = new Matrix4();
  968. }
  969. result[0] = r.x;
  970. result[1] = u.x;
  971. result[2] = -d.x;
  972. result[3] = 0.0;
  973. result[4] = r.y;
  974. result[5] = u.y;
  975. result[6] = -d.y;
  976. result[7] = 0.0;
  977. result[8] = r.z;
  978. result[9] = u.z;
  979. result[10] = -d.z;
  980. result[11] = 0.0;
  981. result[12] = -Cartesian3.dot(r, position3D);
  982. result[13] = -Cartesian3.dot(u, position3D);
  983. result[14] = Cartesian3.dot(d, position3D);
  984. result[15] = 1.0;
  985. return result;
  986. }
  987. return UniformState;
  988. });