Sun.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import BoundingSphere from "../Core/BoundingSphere.js";
  2. import Cartesian2 from "../Core/Cartesian2.js";
  3. import Cartesian3 from "../Core/Cartesian3.js";
  4. import Cartesian4 from "../Core/Cartesian4.js";
  5. import ComponentDatatype from "../Core/ComponentDatatype.js";
  6. import defined from "../Core/defined.js";
  7. import destroyObject from "../Core/destroyObject.js";
  8. import IndexDatatype from "../Core/IndexDatatype.js";
  9. import CesiumMath from "../Core/Math.js";
  10. import Matrix4 from "../Core/Matrix4.js";
  11. import PixelFormat from "../Core/PixelFormat.js";
  12. import PrimitiveType from "../Core/PrimitiveType.js";
  13. import Buffer from "../Renderer/Buffer.js";
  14. import BufferUsage from "../Renderer/BufferUsage.js";
  15. import ComputeCommand from "../Renderer/ComputeCommand.js";
  16. import DrawCommand from "../Renderer/DrawCommand.js";
  17. import PixelDatatype from "../Renderer/PixelDatatype.js";
  18. import RenderState from "../Renderer/RenderState.js";
  19. import ShaderProgram from "../Renderer/ShaderProgram.js";
  20. import Texture from "../Renderer/Texture.js";
  21. import VertexArray from "../Renderer/VertexArray.js";
  22. import SunFS from "../Shaders/SunFS.js";
  23. import SunTextureFS from "../Shaders/SunTextureFS.js";
  24. import SunVS from "../Shaders/SunVS.js";
  25. import BlendingState from "./BlendingState.js";
  26. import SceneMode from "./SceneMode.js";
  27. import SceneTransforms from "./SceneTransforms.js";
  28. /**
  29. * Draws a sun billboard.
  30. * <p>This is only supported in 3D and Columbus view.</p>
  31. *
  32. * @alias Sun
  33. * @constructor
  34. *
  35. *
  36. * @example
  37. * scene.sun = new Cesium.Sun();
  38. *
  39. * @see Scene#sun
  40. */
  41. function Sun() {
  42. /**
  43. * Determines if the sun will be shown.
  44. *
  45. * @type {boolean}
  46. * @default true
  47. */
  48. this.show = true;
  49. this._drawCommand = new DrawCommand({
  50. primitiveType: PrimitiveType.TRIANGLES,
  51. boundingVolume: new BoundingSphere(),
  52. owner: this,
  53. });
  54. this._commands = {
  55. drawCommand: this._drawCommand,
  56. computeCommand: undefined,
  57. };
  58. this._boundingVolume = new BoundingSphere();
  59. this._boundingVolume2D = new BoundingSphere();
  60. this._texture = undefined;
  61. this._drawingBufferWidth = undefined;
  62. this._drawingBufferHeight = undefined;
  63. this._radiusTS = undefined;
  64. this._size = undefined;
  65. this.glowFactor = 1.0;
  66. this._glowFactorDirty = false;
  67. this._useHdr = undefined;
  68. const that = this;
  69. this._uniformMap = {
  70. u_texture: function () {
  71. return that._texture;
  72. },
  73. u_size: function () {
  74. return that._size;
  75. },
  76. };
  77. }
  78. Object.defineProperties(Sun.prototype, {
  79. /**
  80. * Gets or sets a number that controls how "bright" the Sun's lens flare appears
  81. * to be. Zero shows just the Sun's disc without any flare.
  82. * Use larger values for a more pronounced flare around the Sun.
  83. *
  84. * @memberof Sun.prototype
  85. * @type {number}
  86. * @default 1.0
  87. */
  88. glowFactor: {
  89. get: function () {
  90. return this._glowFactor;
  91. },
  92. set: function (glowFactor) {
  93. glowFactor = Math.max(glowFactor, 0.0);
  94. this._glowFactor = glowFactor;
  95. this._glowFactorDirty = true;
  96. },
  97. },
  98. });
  99. const scratchPositionWC = new Cartesian2();
  100. const scratchLimbWC = new Cartesian2();
  101. const scratchPositionEC = new Cartesian4();
  102. const scratchCartesian4 = new Cartesian4();
  103. /**
  104. * @private
  105. */
  106. Sun.prototype.update = function (frameState, passState, useHdr) {
  107. if (!this.show) {
  108. return undefined;
  109. }
  110. const mode = frameState.mode;
  111. if (mode === SceneMode.SCENE2D || mode === SceneMode.MORPHING) {
  112. return undefined;
  113. }
  114. if (!frameState.passes.render) {
  115. return undefined;
  116. }
  117. const context = frameState.context;
  118. const drawingBufferWidth = passState.viewport.width;
  119. const drawingBufferHeight = passState.viewport.height;
  120. if (
  121. !defined(this._texture) ||
  122. drawingBufferWidth !== this._drawingBufferWidth ||
  123. drawingBufferHeight !== this._drawingBufferHeight ||
  124. this._glowFactorDirty ||
  125. useHdr !== this._useHdr
  126. ) {
  127. this._texture = this._texture && this._texture.destroy();
  128. this._drawingBufferWidth = drawingBufferWidth;
  129. this._drawingBufferHeight = drawingBufferHeight;
  130. this._glowFactorDirty = false;
  131. this._useHdr = useHdr;
  132. let size = Math.max(drawingBufferWidth, drawingBufferHeight);
  133. size = Math.pow(2.0, Math.ceil(Math.log(size) / Math.log(2.0)) - 2.0);
  134. // The size computed above can be less than 1.0 if size < 4.0. This will probably
  135. // never happen in practice, but does in the tests. Clamp to 1.0 to prevent WebGL
  136. // errors in the tests.
  137. size = Math.max(1.0, size);
  138. const pixelDatatype = useHdr
  139. ? context.halfFloatingPointTexture
  140. ? PixelDatatype.HALF_FLOAT
  141. : PixelDatatype.FLOAT
  142. : PixelDatatype.UNSIGNED_BYTE;
  143. this._texture = new Texture({
  144. context: context,
  145. width: size,
  146. height: size,
  147. pixelFormat: PixelFormat.RGBA,
  148. pixelDatatype: pixelDatatype,
  149. });
  150. this._glowLengthTS = this._glowFactor * 5.0;
  151. this._radiusTS = (1.0 / (1.0 + 2.0 * this._glowLengthTS)) * 0.5;
  152. const that = this;
  153. const uniformMap = {
  154. u_radiusTS: function () {
  155. return that._radiusTS;
  156. },
  157. };
  158. this._commands.computeCommand = new ComputeCommand({
  159. fragmentShaderSource: SunTextureFS,
  160. outputTexture: this._texture,
  161. uniformMap: uniformMap,
  162. persists: false,
  163. owner: this,
  164. postExecute: function () {
  165. that._commands.computeCommand = undefined;
  166. },
  167. });
  168. }
  169. const drawCommand = this._drawCommand;
  170. if (!defined(drawCommand.vertexArray)) {
  171. const attributeLocations = {
  172. direction: 0,
  173. };
  174. const directions = new Uint8Array(4 * 2);
  175. directions[0] = 0;
  176. directions[1] = 0;
  177. directions[2] = 255;
  178. directions[3] = 0.0;
  179. directions[4] = 255;
  180. directions[5] = 255;
  181. directions[6] = 0.0;
  182. directions[7] = 255;
  183. const vertexBuffer = Buffer.createVertexBuffer({
  184. context: context,
  185. typedArray: directions,
  186. usage: BufferUsage.STATIC_DRAW,
  187. });
  188. const attributes = [
  189. {
  190. index: attributeLocations.direction,
  191. vertexBuffer: vertexBuffer,
  192. componentsPerAttribute: 2,
  193. normalize: true,
  194. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  195. },
  196. ];
  197. // Workaround Internet Explorer 11.0.8 lack of TRIANGLE_FAN
  198. const indexBuffer = Buffer.createIndexBuffer({
  199. context: context,
  200. typedArray: new Uint16Array([0, 1, 2, 0, 2, 3]),
  201. usage: BufferUsage.STATIC_DRAW,
  202. indexDatatype: IndexDatatype.UNSIGNED_SHORT,
  203. });
  204. drawCommand.vertexArray = new VertexArray({
  205. context: context,
  206. attributes: attributes,
  207. indexBuffer: indexBuffer,
  208. });
  209. drawCommand.shaderProgram = ShaderProgram.fromCache({
  210. context: context,
  211. vertexShaderSource: SunVS,
  212. fragmentShaderSource: SunFS,
  213. attributeLocations: attributeLocations,
  214. });
  215. drawCommand.renderState = RenderState.fromCache({
  216. blending: BlendingState.ALPHA_BLEND,
  217. });
  218. drawCommand.uniformMap = this._uniformMap;
  219. }
  220. const sunPosition = context.uniformState.sunPositionWC;
  221. const sunPositionCV = context.uniformState.sunPositionColumbusView;
  222. const boundingVolume = this._boundingVolume;
  223. const boundingVolume2D = this._boundingVolume2D;
  224. Cartesian3.clone(sunPosition, boundingVolume.center);
  225. boundingVolume2D.center.x = sunPositionCV.z;
  226. boundingVolume2D.center.y = sunPositionCV.x;
  227. boundingVolume2D.center.z = sunPositionCV.y;
  228. boundingVolume.radius =
  229. CesiumMath.SOLAR_RADIUS + CesiumMath.SOLAR_RADIUS * this._glowLengthTS;
  230. boundingVolume2D.radius = boundingVolume.radius;
  231. if (mode === SceneMode.SCENE3D) {
  232. BoundingSphere.clone(boundingVolume, drawCommand.boundingVolume);
  233. } else if (mode === SceneMode.COLUMBUS_VIEW) {
  234. BoundingSphere.clone(boundingVolume2D, drawCommand.boundingVolume);
  235. }
  236. const position = SceneTransforms.computeActualWgs84Position(
  237. frameState,
  238. sunPosition,
  239. scratchCartesian4
  240. );
  241. const dist = Cartesian3.magnitude(
  242. Cartesian3.subtract(position, frameState.camera.position, scratchCartesian4)
  243. );
  244. const projMatrix = context.uniformState.projection;
  245. const positionEC = scratchPositionEC;
  246. positionEC.x = 0;
  247. positionEC.y = 0;
  248. positionEC.z = -dist;
  249. positionEC.w = 1;
  250. const positionCC = Matrix4.multiplyByVector(
  251. projMatrix,
  252. positionEC,
  253. scratchCartesian4
  254. );
  255. const positionWC = SceneTransforms.clipToGLWindowCoordinates(
  256. passState.viewport,
  257. positionCC,
  258. scratchPositionWC
  259. );
  260. positionEC.x = CesiumMath.SOLAR_RADIUS;
  261. const limbCC = Matrix4.multiplyByVector(
  262. projMatrix,
  263. positionEC,
  264. scratchCartesian4
  265. );
  266. const limbWC = SceneTransforms.clipToGLWindowCoordinates(
  267. passState.viewport,
  268. limbCC,
  269. scratchLimbWC
  270. );
  271. this._size = Cartesian2.magnitude(
  272. Cartesian2.subtract(limbWC, positionWC, scratchCartesian4)
  273. );
  274. this._size = 2.0 * this._size * (1.0 + 2.0 * this._glowLengthTS);
  275. this._size = Math.ceil(this._size);
  276. return this._commands;
  277. };
  278. /**
  279. * Returns true if this object was destroyed; otherwise, false.
  280. * <br /><br />
  281. * If this object was destroyed, it should not be used; calling any function other than
  282. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  283. *
  284. * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  285. *
  286. * @see Sun#destroy
  287. */
  288. Sun.prototype.isDestroyed = function () {
  289. return false;
  290. };
  291. /**
  292. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  293. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  294. * <br /><br />
  295. * Once an object is destroyed, it should not be used; calling any function other than
  296. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  297. * assign the return value (<code>undefined</code>) to the object as done in the example.
  298. *
  299. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  300. *
  301. *
  302. * @example
  303. * sun = sun && sun.destroy();
  304. *
  305. * @see Sun#isDestroyed
  306. */
  307. Sun.prototype.destroy = function () {
  308. const command = this._drawCommand;
  309. command.vertexArray = command.vertexArray && command.vertexArray.destroy();
  310. command.shaderProgram =
  311. command.shaderProgram && command.shaderProgram.destroy();
  312. this._texture = this._texture && this._texture.destroy();
  313. return destroyObject(this);
  314. };
  315. export default Sun;