CloudCollection.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. import BlendingState from "./BlendingState.js";
  2. import Buffer from "../Renderer/Buffer.js";
  3. import BufferUsage from "../Renderer/BufferUsage.js";
  4. import Cartesian3 from "../Core/Cartesian3.js";
  5. import Check from "../Core/Check.js";
  6. import Color from "../Core/Color.js";
  7. import ComputeCommand from "../Renderer/ComputeCommand.js";
  8. import CloudType from "./CloudType.js";
  9. import CloudCollectionFS from "../Shaders/CloudCollectionFS.js";
  10. import CloudCollectionVS from "../Shaders/CloudCollectionVS.js";
  11. import CloudNoiseFS from "../Shaders/CloudNoiseFS.js";
  12. import CloudNoiseVS from "../Shaders/CloudNoiseVS.js";
  13. import ComponentDatatype from "../Core/ComponentDatatype.js";
  14. import CumulusCloud from "./CumulusCloud.js";
  15. import defaultValue from "../Core/defaultValue.js";
  16. import defined from "../Core/defined.js";
  17. import destroyObject from "../Core/destroyObject.js";
  18. import DeveloperError from "../Core/DeveloperError.js";
  19. import DrawCommand from "../Renderer/DrawCommand.js";
  20. import EncodedCartesian3 from "../Core/EncodedCartesian3.js";
  21. import IndexDatatype from "../Core/IndexDatatype.js";
  22. import Pass from "../Renderer/Pass.js";
  23. import PixelDatatype from "../Renderer/PixelDatatype.js";
  24. import PixelFormat from "../Core/PixelFormat.js";
  25. import RenderState from "../Renderer/RenderState.js";
  26. import Sampler from "../Renderer/Sampler.js";
  27. import ShaderSource from "../Renderer/ShaderSource.js";
  28. import ShaderProgram from "../Renderer/ShaderProgram.js";
  29. import Texture from "../Renderer/Texture.js";
  30. import TextureMagnificationFilter from "../Renderer/TextureMagnificationFilter.js";
  31. import TextureMinificationFilter from "../Renderer/TextureMinificationFilter.js";
  32. import TextureWrap from "../Renderer/TextureWrap.js";
  33. import VertexArray from "../Renderer/VertexArray.js";
  34. import VertexArrayFacade from "../Renderer/VertexArrayFacade.js";
  35. import WebGLConstants from "../Core/WebGLConstants.js";
  36. let attributeLocations;
  37. const scratchTextureDimensions = new Cartesian3();
  38. const attributeLocationsBatched = {
  39. positionHighAndScaleX: 0,
  40. positionLowAndScaleY: 1,
  41. packedAttribute0: 2, // show, brightness, direction
  42. packedAttribute1: 3, // cloudSize, slice
  43. color: 4,
  44. };
  45. const attributeLocationsInstanced = {
  46. direction: 0,
  47. positionHighAndScaleX: 1,
  48. positionLowAndScaleY: 2,
  49. packedAttribute0: 3, // show, brightness
  50. packedAttribute1: 4, // cloudSize, slice
  51. color: 5,
  52. };
  53. const SHOW_INDEX = CumulusCloud.SHOW_INDEX;
  54. const POSITION_INDEX = CumulusCloud.POSITION_INDEX;
  55. const SCALE_INDEX = CumulusCloud.SCALE_INDEX;
  56. const MAXIMUM_SIZE_INDEX = CumulusCloud.MAXIMUM_SIZE_INDEX;
  57. const SLICE_INDEX = CumulusCloud.SLICE_INDEX;
  58. const BRIGHTNESS_INDEX = CumulusCloud.BRIGHTNESS_INDEX;
  59. const NUMBER_OF_PROPERTIES = CumulusCloud.NUMBER_OF_PROPERTIES;
  60. const COLOR_INDEX = CumulusCloud.COLOR_INDEX;
  61. /**
  62. * A renderable collection of clouds in the 3D scene.
  63. * <br /><br />
  64. * <div align='center'>
  65. * <img src='Images/CumulusCloud.png' width='400' height='300' /><br />
  66. * Example cumulus clouds
  67. * </div>
  68. * <br /><br />
  69. * Clouds are added and removed from the collection using {@link CloudCollection#add}
  70. * and {@link CloudCollection#remove}.
  71. * @alias CloudCollection
  72. * @constructor
  73. *
  74. * @param {Object} [options] Object with the following properties:
  75. * @param {Boolean} [options.show=true] Whether to display the clouds.
  76. * @param {Number} [options.noiseDetail=16.0] Desired amount of detail in the noise texture.
  77. * @param {Number} [options.noiseOffset=Cartesian3.ZERO] Desired translation of data in noise texture.
  78. * @param {Boolean} [options.debugBillboards=false] For debugging only. Determines if the billboards are rendered with an opaque color.
  79. * @param {Boolean} [options.debugEllipsoids=false] For debugging only. Determines if the clouds will be rendered as opaque ellipsoids.
  80. * @see CloudCollection#add
  81. * @see CloudCollection#remove
  82. * @see CumulusCloud
  83. *
  84. * @demo {@link https://sandcastle.cesium.com/index.html?src=Clouds.html|Cesium Sandcastle Clouds Demo}
  85. * @demo {@link https://sandcastle.cesium.com/index.html?src=Cloud%20Parameters.html|Cesium Sandcastle Cloud Parameters Demo}
  86. *
  87. * @example
  88. * // Create a cloud collection with two cumulus clouds
  89. * const clouds = scene.primitives.add(new Cesium.CloudCollection());
  90. * clouds.add({
  91. * position : new Cesium.Cartesian3(1.0, 2.0, 3.0),
  92. * maximumSize: new Cesium.Cartesian3(20.0, 12.0, 8.0)
  93. * });
  94. * clouds.add({
  95. * position : new Cesium.Cartesian3(4.0, 5.0, 6.0),
  96. * maximumSize: new Cesium.Cartesian3(15.0, 9.0, 9.0),
  97. * slice: 0.5
  98. * });
  99. *
  100. */
  101. function CloudCollection(options) {
  102. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  103. this._clouds = [];
  104. this._cloudsToUpdate = [];
  105. this._cloudsToUpdateIndex = 0;
  106. this._cloudsRemoved = false;
  107. this._createVertexArray = false;
  108. this._propertiesChanged = new Uint32Array(NUMBER_OF_PROPERTIES);
  109. this._noiseTexture = undefined;
  110. this._textureSliceWidth = 128;
  111. this._noiseTextureRows = 4;
  112. /**
  113. * <p>
  114. * Controls the amount of detail captured in the precomputed noise texture
  115. * used to render the cumulus clouds. In order for the texture to be tileable,
  116. * this must be a power of two. For best results, set this to be a power of two
  117. * between <code>8.0</code> and <code>32.0</code> (inclusive).
  118. * </p>
  119. *
  120. * <div align='center'>
  121. * <table border='0' cellpadding='5'><tr>
  122. * <td align='center'>
  123. * <code>clouds.noiseDetail = 8.0;</code><br/>
  124. * <img src='Images/CloudCollection.noiseDetail8.png' width='250' height='158' />
  125. * </td>
  126. * <td align='center'>
  127. * <code>clouds.noiseDetail = 32.0;</code><br/>
  128. * <img src='Images/CloudCollection.noiseDetail32.png' width='250' height='158' />
  129. * </td>
  130. * </tr></table>
  131. * </div>
  132. *
  133. * @type {Number}
  134. *
  135. * @default 16.0
  136. */
  137. this.noiseDetail = defaultValue(options.noiseDetail, 16.0);
  138. /**
  139. * <p>
  140. * Applies a translation to noise texture coordinates to generate different data.
  141. * This can be modified if the default noise does not generate good-looking clouds.
  142. * </p>
  143. *
  144. * <div align='center'>
  145. * <table border='0' cellpadding='5'><tr>
  146. * <td align='center'>
  147. * <code>default</code><br/>
  148. * <img src='Images/CloudCollection.noiseOffsetdefault.png' width='250' height='158' />
  149. * </td>
  150. * <td align='center'>
  151. * <code>clouds.noiseOffset = new Cesium.Cartesian3(10, 20, 10);</code><br/>
  152. * <img src='Images/CloudCollection.noiseOffsetx10y20z10.png' width='250' height='158' />
  153. * </td>
  154. * </tr></table>
  155. * </div>
  156. * @type {Cartesian3}
  157. *
  158. * @default Cartesian3.ZERO
  159. */
  160. this.noiseOffset = Cartesian3.clone(
  161. defaultValue(options.noiseOffset, Cartesian3.ZERO)
  162. );
  163. this._loading = false;
  164. this._ready = false;
  165. const that = this;
  166. this._uniforms = {
  167. u_noiseTexture: function () {
  168. return that._noiseTexture;
  169. },
  170. u_noiseTextureDimensions: getNoiseTextureDimensions(that),
  171. u_noiseDetail: function () {
  172. return that.noiseDetail;
  173. },
  174. };
  175. this._vaNoise = undefined;
  176. this._spNoise = undefined;
  177. this._spCreated = false;
  178. this._sp = undefined;
  179. this._rs = undefined;
  180. /**
  181. * Determines if billboards in this collection will be shown.
  182. *
  183. * @type {Boolean}
  184. * @default true
  185. */
  186. this.show = defaultValue(options.show, true);
  187. this._colorCommands = [];
  188. /**
  189. * This property is for debugging only; it is not for production use nor is it optimized.
  190. * <p>
  191. * Renders the billboards with one opaque color for the sake of debugging.
  192. * </p>
  193. *
  194. * @type {Boolean}
  195. *
  196. * @default false
  197. */
  198. this.debugBillboards = defaultValue(options.debugBillboards, false);
  199. this._compiledDebugBillboards = false;
  200. /**
  201. * This property is for debugging only; it is not for production use nor is it optimized.
  202. * <p>
  203. * Draws the clouds as opaque, monochrome ellipsoids for the sake of debugging.
  204. * If <code>debugBillboards</code> is also true, then the ellipsoids will draw on top of the billboards.
  205. * </p>
  206. *
  207. * @type {Boolean}
  208. *
  209. * @default false
  210. */
  211. this.debugEllipsoids = defaultValue(options.debugEllipsoids, false);
  212. this._compiledDebugEllipsoids = false;
  213. }
  214. // Wraps useful texture metrics into a single vec3 for less overhead.
  215. function getNoiseTextureDimensions(collection) {
  216. return function () {
  217. scratchTextureDimensions.x = collection._textureSliceWidth;
  218. scratchTextureDimensions.y = collection._noiseTextureRows;
  219. scratchTextureDimensions.z = 1.0 / collection._noiseTextureRows;
  220. return scratchTextureDimensions;
  221. };
  222. }
  223. Object.defineProperties(CloudCollection.prototype, {
  224. /**
  225. * Returns the number of clouds in this collection.
  226. * @memberof CloudCollection.prototype
  227. * @type {Number}
  228. */
  229. length: {
  230. get: function () {
  231. removeClouds(this);
  232. return this._clouds.length;
  233. },
  234. },
  235. });
  236. function destroyClouds(clouds) {
  237. const length = clouds.length;
  238. for (let i = 0; i < length; ++i) {
  239. if (clouds[i]) {
  240. clouds[i]._destroy();
  241. }
  242. }
  243. }
  244. /**
  245. * Creates and adds a cloud with the specified initial properties to the collection.
  246. * The added cloud is returned so it can be modified or removed from the collection later.
  247. *
  248. * @param {Object}[options] A template describing the cloud's properties as shown in Example 1.
  249. * @returns {CumulusCloud} The cloud that was added to the collection.
  250. *
  251. * @performance Calling <code>add</code> is expected constant time. However, the collection's vertex buffer
  252. * is rewritten - an <code>O(n)</code> operation that also incurs CPU to GPU overhead. For
  253. * best performance, add as many clouds as possible before calling <code>update</code>.
  254. *
  255. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  256. *
  257. *
  258. * @example
  259. * // Example 1: Add a cumulus cloud, specifying all the default values.
  260. * const c = clouds.add({
  261. * show : true,
  262. * position : Cesium.Cartesian3.ZERO,
  263. * scale : new Cesium.Cartesian2(20.0, 12.0),
  264. * maximumSize: new Cesium.Cartesian3(20.0, 12.0, 12.0),
  265. * slice: -1.0,
  266. * cloudType : CloudType.CUMULUS
  267. * });
  268. *
  269. * @example
  270. * // Example 2: Specify only the cloud's cartographic position.
  271. * const c = clouds.add({
  272. * position : Cesium.Cartesian3.fromDegrees(longitude, latitude, height)
  273. * });
  274. *
  275. * @see CloudCollection#remove
  276. * @see CloudCollection#removeAll
  277. */
  278. CloudCollection.prototype.add = function (options) {
  279. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  280. const cloudType = defaultValue(options.cloudType, CloudType.CUMULUS);
  281. //>>includeStart('debug', pragmas.debug);
  282. if (!CloudType.validate(cloudType)) {
  283. throw new DeveloperError("invalid cloud type");
  284. }
  285. //>>includeEnd('debug');
  286. let cloud;
  287. if (cloudType === CloudType.CUMULUS) {
  288. cloud = new CumulusCloud(options, this);
  289. cloud._index = this._clouds.length;
  290. this._clouds.push(cloud);
  291. this._createVertexArray = true;
  292. }
  293. return cloud;
  294. };
  295. /**
  296. * Removes a cloud from the collection.
  297. *
  298. * @param {CumulusCloud} cloud The cloud to remove.
  299. * @returns {Boolean} <code>true</code> if the cloud was removed; <code>false</code> if the cloud was not found in the collection.
  300. *
  301. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  302. *
  303. *
  304. * @example
  305. * const c = clouds.add(...);
  306. * clouds.remove(c); // Returns true
  307. *
  308. * @see CloudCollection#add
  309. * @see CloudCollection#removeAll
  310. * @see CumulusCloud#show
  311. */
  312. CloudCollection.prototype.remove = function (cloud) {
  313. if (this.contains(cloud)) {
  314. this._clouds[cloud._index] = undefined; // Removed later in removeClouds()
  315. this._cloudsRemoved = true;
  316. this._createVertexArray = true;
  317. cloud._destroy();
  318. return true;
  319. }
  320. return false;
  321. };
  322. /**
  323. * Removes all clouds from the collection.
  324. *
  325. * @performance <code>O(n)</code>. It is more efficient to remove all the clouds
  326. * from a collection and then add new ones than to create a new collection entirely.
  327. *
  328. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  329. *
  330. * @example
  331. * clouds.add(...);
  332. * clouds.add(...);
  333. * clouds.removeAll();
  334. *
  335. * @see CloudCollection#add
  336. * @see CloudCollection#remove
  337. */
  338. CloudCollection.prototype.removeAll = function () {
  339. destroyClouds(this._clouds);
  340. this._clouds = [];
  341. this._cloudsToUpdate = [];
  342. this._cloudsToUpdateIndex = 0;
  343. this._cloudsRemoved = false;
  344. this._createVertexArray = true;
  345. };
  346. function removeClouds(cloudCollection) {
  347. if (cloudCollection._cloudsRemoved) {
  348. cloudCollection._cloudsRemoved = false;
  349. const newClouds = [];
  350. const clouds = cloudCollection._clouds;
  351. const length = clouds.length;
  352. for (let i = 0, j = 0; i < length; ++i) {
  353. const cloud = clouds[i];
  354. if (defined(cloud)) {
  355. clouds._index = j++;
  356. newClouds.push(cloud);
  357. }
  358. }
  359. cloudCollection._clouds = newClouds;
  360. }
  361. }
  362. CloudCollection.prototype._updateCloud = function (cloud, propertyChanged) {
  363. if (!cloud._dirty) {
  364. this._cloudsToUpdate[this._cloudsToUpdateIndex++] = cloud;
  365. }
  366. ++this._propertiesChanged[propertyChanged];
  367. };
  368. /**
  369. * Check whether this collection contains a given cloud.
  370. *
  371. * @param {CumulusCloud} [cloud] The cloud to check for.
  372. * @returns {Boolean} true if this collection contains the cloud, false otherwise.
  373. *
  374. * @see CloudCollection#get
  375. */
  376. CloudCollection.prototype.contains = function (cloud) {
  377. return defined(cloud) && cloud._cloudCollection === this;
  378. };
  379. /**
  380. * Returns the cloud in the collection at the specified index. Indices are zero-based
  381. * and increase as clouds are added. Removing a cloud shifts all clouds after
  382. * it to the left, changing their indices. This function is commonly used with
  383. * {@link CloudCollection#length} to iterate over all the clouds in the collection.
  384. *
  385. * @param {Number} index The zero-based index of the cloud.
  386. * @returns {CumulusCloud} The cloud at the specified index.
  387. *
  388. * @performance Expected constant time. If clouds were removed from the collection and
  389. * {@link CloudCollection#update} was not called, an implicit <code>O(n)</code>
  390. * operation is performed.
  391. *
  392. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  393. *
  394. *
  395. * @example
  396. * // Toggle the show property of every cloud in the collection
  397. * const len = clouds.length;
  398. * for (let i = 0; i < len; ++i) {
  399. * const c = clouds.get(i);
  400. * c.show = !c.show;
  401. * }
  402. *
  403. * @see CloudCollection#length
  404. */
  405. CloudCollection.prototype.get = function (index) {
  406. //>>includeStart('debug', pragmas.debug);
  407. Check.typeOf.number("index", index);
  408. //>>includeEnd('debug');
  409. removeClouds(this);
  410. return this._clouds[index];
  411. };
  412. const texturePositions = new Float32Array([
  413. -1.0,
  414. -1.0,
  415. 1.0,
  416. -1.0,
  417. 1.0,
  418. 1.0,
  419. -1.0,
  420. 1.0,
  421. ]);
  422. const textureIndices = new Uint16Array([0, 1, 2, 0, 2, 3]);
  423. function createTextureVA(context) {
  424. const positionBuffer = Buffer.createVertexBuffer({
  425. context: context,
  426. typedArray: texturePositions,
  427. usage: BufferUsage.STATIC_DRAW,
  428. });
  429. const indexBuffer = Buffer.createIndexBuffer({
  430. context: context,
  431. typedArray: textureIndices,
  432. usage: BufferUsage.STATIC_DRAW,
  433. indexDatatype: IndexDatatype.UNSIGNED_SHORT,
  434. });
  435. const attributes = [
  436. {
  437. index: 0,
  438. vertexBuffer: positionBuffer,
  439. componentsPerAttribute: 2,
  440. componentDatatype: ComponentDatatype.FLOAT,
  441. },
  442. ];
  443. return new VertexArray({
  444. context: context,
  445. attributes: attributes,
  446. indexBuffer: indexBuffer,
  447. });
  448. }
  449. let getIndexBuffer;
  450. function getIndexBufferBatched(context) {
  451. const sixteenK = 16 * 1024;
  452. let indexBuffer = context.cache.cloudCollection_indexBufferBatched;
  453. if (defined(indexBuffer)) {
  454. return indexBuffer;
  455. }
  456. // Subtract 6 because the last index is reserved for primitive restart.
  457. // https://www.khronos.org/registry/webgl/specs/latest/2.0/#5.18
  458. const length = sixteenK * 6 - 6;
  459. const indices = new Uint16Array(length);
  460. for (let i = 0, j = 0; i < length; i += 6, j += 4) {
  461. indices[i] = j;
  462. indices[i + 1] = j + 1;
  463. indices[i + 2] = j + 2;
  464. indices[i + 3] = j;
  465. indices[i + 4] = j + 2;
  466. indices[i + 5] = j + 3;
  467. }
  468. indexBuffer = Buffer.createIndexBuffer({
  469. context: context,
  470. typedArray: indices,
  471. usage: BufferUsage.STATIC_DRAW,
  472. indexDatatype: IndexDatatype.UNSIGNED_SHORT,
  473. });
  474. indexBuffer.vertexArrayDestroyable = false;
  475. context.cache.cloudCollection_indexBufferBatched = indexBuffer;
  476. return indexBuffer;
  477. }
  478. function getIndexBufferInstanced(context) {
  479. let indexBuffer = context.cache.cloudCollection_indexBufferInstanced;
  480. if (defined(indexBuffer)) {
  481. return indexBuffer;
  482. }
  483. indexBuffer = Buffer.createIndexBuffer({
  484. context: context,
  485. typedArray: new Uint16Array([0, 1, 2, 0, 2, 3]),
  486. usage: BufferUsage.STATIC_DRAW,
  487. indexDatatype: IndexDatatype.UNSIGNED_SHORT,
  488. });
  489. indexBuffer.vertexArrayDestroyable = false;
  490. context.cache.cloudCollection_indexBufferInstanced = indexBuffer;
  491. return indexBuffer;
  492. }
  493. function getVertexBufferInstanced(context) {
  494. let vertexBuffer = context.cache.cloudCollection_vertexBufferInstanced;
  495. if (defined(vertexBuffer)) {
  496. return vertexBuffer;
  497. }
  498. vertexBuffer = Buffer.createVertexBuffer({
  499. context: context,
  500. typedArray: new Float32Array([0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]),
  501. usage: BufferUsage.STATIC_DRAW,
  502. });
  503. vertexBuffer.vertexArrayDestroyable = false;
  504. context.cache.cloudCollection_vertexBufferInstanced = vertexBuffer;
  505. return vertexBuffer;
  506. }
  507. function createVAF(context, numberOfClouds, instanced) {
  508. const attributes = [
  509. {
  510. index: attributeLocations.positionHighAndScaleX,
  511. componentsPerAttribute: 4,
  512. componentDatatype: ComponentDatatype.FLOAT,
  513. usage: BufferUsage.STATIC_DRAW,
  514. },
  515. {
  516. index: attributeLocations.positionLowAndScaleY,
  517. componentsPerAttribute: 4,
  518. componentDatatype: ComponentDatatype.FLOAT,
  519. usage: BufferUsage.STATIC_DRAW,
  520. },
  521. {
  522. index: attributeLocations.packedAttribute0,
  523. componentsPerAttribute: 4,
  524. componentDatatype: ComponentDatatype.FLOAT,
  525. usage: BufferUsage.STATIC_DRAW,
  526. },
  527. {
  528. index: attributeLocations.packedAttribute1,
  529. componentsPerAttribute: 4,
  530. componentDatatype: ComponentDatatype.FLOAT,
  531. usage: BufferUsage.STATIC_DRAW,
  532. },
  533. {
  534. index: attributeLocations.color,
  535. componentsPerAttribute: 4,
  536. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  537. normalize: true,
  538. usage: BufferUsage.STATIC_DRAW,
  539. },
  540. ];
  541. if (instanced) {
  542. attributes.push({
  543. index: attributeLocations.direction,
  544. componentsPerAttribute: 2,
  545. componentDatatype: ComponentDatatype.FLOAT,
  546. vertexBuffer: getVertexBufferInstanced(context),
  547. });
  548. }
  549. const sizeInVertices = instanced ? numberOfClouds : 4 * numberOfClouds;
  550. return new VertexArrayFacade(context, attributes, sizeInVertices, instanced);
  551. }
  552. const writePositionScratch = new EncodedCartesian3();
  553. function writePositionAndScale(cloudCollection, frameState, vafWriters, cloud) {
  554. let i;
  555. const positionHighWriter =
  556. vafWriters[attributeLocations.positionHighAndScaleX];
  557. const positionLowWriter = vafWriters[attributeLocations.positionLowAndScaleY];
  558. const position = cloud.position;
  559. EncodedCartesian3.fromCartesian(position, writePositionScratch);
  560. const scale = cloud.scale;
  561. const high = writePositionScratch.high;
  562. const low = writePositionScratch.low;
  563. if (cloudCollection._instanced) {
  564. i = cloud._index;
  565. positionHighWriter(i, high.x, high.y, high.z, scale.x);
  566. positionLowWriter(i, low.x, low.y, low.z, scale.y);
  567. } else {
  568. i = cloud._index * 4;
  569. positionHighWriter(i + 0, high.x, high.y, high.z, scale.x);
  570. positionHighWriter(i + 1, high.x, high.y, high.z, scale.x);
  571. positionHighWriter(i + 2, high.x, high.y, high.z, scale.x);
  572. positionHighWriter(i + 3, high.x, high.y, high.z, scale.x);
  573. positionLowWriter(i + 0, low.x, low.y, low.z, scale.y);
  574. positionLowWriter(i + 1, low.x, low.y, low.z, scale.y);
  575. positionLowWriter(i + 2, low.x, low.y, low.z, scale.y);
  576. positionLowWriter(i + 3, low.x, low.y, low.z, scale.y);
  577. }
  578. }
  579. function writePackedAttribute0(cloudCollection, frameState, vafWriters, cloud) {
  580. let i;
  581. const writer = vafWriters[attributeLocations.packedAttribute0];
  582. const show = cloud.show;
  583. const brightness = cloud.brightness;
  584. if (cloudCollection._instanced) {
  585. i = cloud._index;
  586. writer(i, show, brightness, 0.0, 0.0);
  587. } else {
  588. i = cloud._index * 4;
  589. writer(i + 0, show, brightness, 0.0, 0.0);
  590. writer(i + 1, show, brightness, 1.0, 0.0);
  591. writer(i + 2, show, brightness, 1.0, 1.0);
  592. writer(i + 3, show, brightness, 0.0, 1.0);
  593. }
  594. }
  595. function writePackedAttribute1(cloudCollection, frameState, vafWriters, cloud) {
  596. let i;
  597. const writer = vafWriters[attributeLocations.packedAttribute1];
  598. const maximumSize = cloud.maximumSize;
  599. const slice = cloud.slice;
  600. if (cloudCollection._instanced) {
  601. i = cloud._index;
  602. writer(i, maximumSize.x, maximumSize.y, maximumSize.z, slice);
  603. } else {
  604. i = cloud._index * 4;
  605. writer(i + 0, maximumSize.x, maximumSize.y, maximumSize.z, slice);
  606. writer(i + 1, maximumSize.x, maximumSize.y, maximumSize.z, slice);
  607. writer(i + 2, maximumSize.x, maximumSize.y, maximumSize.z, slice);
  608. writer(i + 3, maximumSize.x, maximumSize.y, maximumSize.z, slice);
  609. }
  610. }
  611. function writeColor(cloudCollection, frameState, vafWriters, cloud) {
  612. let i;
  613. const writer = vafWriters[attributeLocations.color];
  614. const color = cloud.color;
  615. const red = Color.floatToByte(color.red);
  616. const green = Color.floatToByte(color.green);
  617. const blue = Color.floatToByte(color.blue);
  618. const alpha = Color.floatToByte(color.alpha);
  619. if (cloudCollection._instanced) {
  620. i = cloud._index;
  621. writer(i, red, green, blue, alpha);
  622. } else {
  623. i = cloud._index * 4;
  624. writer(i + 0, red, green, blue, alpha);
  625. writer(i + 1, red, green, blue, alpha);
  626. writer(i + 2, red, green, blue, alpha);
  627. writer(i + 3, red, green, blue, alpha);
  628. }
  629. }
  630. function writeCloud(cloudCollection, frameState, vafWriters, cloud) {
  631. writePositionAndScale(cloudCollection, frameState, vafWriters, cloud);
  632. writePackedAttribute0(cloudCollection, frameState, vafWriters, cloud);
  633. writePackedAttribute1(cloudCollection, frameState, vafWriters, cloud);
  634. writeColor(cloudCollection, frameState, vafWriters, cloud);
  635. }
  636. function createNoiseTexture(cloudCollection, frameState, vsSource, fsSource) {
  637. const that = cloudCollection;
  638. const textureSliceWidth = that._textureSliceWidth;
  639. const noiseTextureRows = that._noiseTextureRows;
  640. //>>includeStart('debug', pragmas.debug);
  641. if (
  642. textureSliceWidth / noiseTextureRows < 1 ||
  643. textureSliceWidth % noiseTextureRows !== 0
  644. ) {
  645. throw new DeveloperError(
  646. "noiseTextureRows must evenly divide textureSliceWidth"
  647. );
  648. }
  649. //>>includeEnd('debug');
  650. const context = frameState.context;
  651. that._vaNoise = createTextureVA(context);
  652. that._spNoise = ShaderProgram.fromCache({
  653. context: context,
  654. vertexShaderSource: vsSource,
  655. fragmentShaderSource: fsSource,
  656. attributeLocations: {
  657. position: 0,
  658. },
  659. });
  660. const noiseDetail = that.noiseDetail;
  661. const noiseOffset = that.noiseOffset;
  662. that._noiseTexture = new Texture({
  663. context: context,
  664. width: (textureSliceWidth * textureSliceWidth) / noiseTextureRows,
  665. height: textureSliceWidth * noiseTextureRows,
  666. pixelDatatype: PixelDatatype.UNSIGNED_BYTE,
  667. pixelFormat: PixelFormat.RGBA,
  668. sampler: new Sampler({
  669. wrapS: TextureWrap.REPEAT,
  670. wrapT: TextureWrap.REPEAT,
  671. minificationFilter: TextureMinificationFilter.NEAREST,
  672. magnificationFilter: TextureMagnificationFilter.NEAREST,
  673. }),
  674. });
  675. const textureCommand = new ComputeCommand({
  676. vertexArray: that._vaNoise,
  677. shaderProgram: that._spNoise,
  678. outputTexture: that._noiseTexture,
  679. uniformMap: {
  680. u_noiseTextureDimensions: getNoiseTextureDimensions(that),
  681. u_noiseDetail: function () {
  682. return noiseDetail;
  683. },
  684. u_noiseOffset: function () {
  685. return noiseOffset;
  686. },
  687. },
  688. persists: false,
  689. owner: cloudCollection,
  690. postExecute: function (texture) {
  691. that._ready = true;
  692. that._loading = false;
  693. },
  694. });
  695. frameState.commandList.push(textureCommand);
  696. that._loading = true;
  697. }
  698. function createVertexArray(cloudCollection, frameState) {
  699. const that = cloudCollection;
  700. const context = frameState.context;
  701. that._createVertexArray = false;
  702. that._vaf = that._vaf && that._vaf.destroy();
  703. const clouds = cloudCollection._clouds;
  704. const cloudsLength = clouds.length;
  705. if (cloudsLength > 0) {
  706. that._vaf = createVAF(context, cloudsLength, that._instanced);
  707. const vafWriters = that._vaf.writers;
  708. let i;
  709. // Rewrite entire buffer if clouds were added or removed.
  710. for (i = 0; i < cloudsLength; ++i) {
  711. const cloud = clouds[i];
  712. writeCloud(cloudCollection, frameState, vafWriters, cloud);
  713. }
  714. // Different cloud collections share the same index buffer.
  715. that._vaf.commit(getIndexBuffer(context));
  716. }
  717. }
  718. const scratchWriterArray = [];
  719. function updateClouds(cloudCollection, frameState) {
  720. const context = frameState.context;
  721. const that = cloudCollection;
  722. const clouds = that._clouds;
  723. const cloudsLength = clouds.length;
  724. const cloudsToUpdate = that._cloudsToUpdate;
  725. const cloudsToUpdateLength = that._cloudsToUpdateIndex;
  726. const properties = that._propertiesChanged;
  727. const writers = scratchWriterArray;
  728. writers.length = 0;
  729. if (properties[POSITION_INDEX] || properties[SCALE_INDEX]) {
  730. writers.push(writePositionAndScale);
  731. }
  732. if (properties[SHOW_INDEX] || properties[BRIGHTNESS_INDEX]) {
  733. writers.push(writePackedAttribute0);
  734. }
  735. if (properties[MAXIMUM_SIZE_INDEX] || properties[SLICE_INDEX]) {
  736. writers.push(writePackedAttribute1);
  737. }
  738. if (properties[COLOR_INDEX]) {
  739. writers.push(writeColor);
  740. }
  741. const numWriters = writers.length;
  742. const vafWriters = that._vaf.writers;
  743. let i, c, w;
  744. if (cloudsToUpdateLength / cloudsLength > 0.1) {
  745. // Like BillboardCollection, if more than 10% of clouds change,
  746. // rewrite the entire buffer.
  747. for (i = 0; i < cloudsToUpdateLength; ++i) {
  748. c = cloudsToUpdate[i];
  749. c._dirty = false;
  750. for (w = 0; w < numWriters; ++w) {
  751. writers[w](cloudCollection, frameState, vafWriters, c);
  752. }
  753. }
  754. that._vaf.commit(getIndexBuffer(context));
  755. } else {
  756. for (i = 0; i < cloudsToUpdateLength; ++i) {
  757. c = cloudsToUpdate[i];
  758. c._dirty = false;
  759. for (w = 0; w < numWriters; ++w) {
  760. writers[w](cloudCollection, frameState, vafWriters, c);
  761. }
  762. if (that._instanced) {
  763. that._vaf.subCommit(c._index, 1);
  764. } else {
  765. that._vaf.subCommit(c._index * 4, 4);
  766. }
  767. }
  768. that._vaf.endSubCommits();
  769. }
  770. that._cloudsToUpdateIndex = 0;
  771. }
  772. function createShaderProgram(cloudCollection, frameState, vsSource, fsSource) {
  773. const context = frameState.context;
  774. const that = cloudCollection;
  775. const vs = new ShaderSource({
  776. defines: [],
  777. sources: [vsSource],
  778. });
  779. if (that._instanced) {
  780. vs.defines.push("INSTANCED");
  781. }
  782. const fs = new ShaderSource({
  783. defines: [],
  784. sources: [fsSource],
  785. });
  786. if (that.debugBillboards) {
  787. fs.defines.push("DEBUG_BILLBOARDS");
  788. }
  789. if (that.debugEllipsoids) {
  790. fs.defines.push("DEBUG_ELLIPSOIDS");
  791. }
  792. that._sp = ShaderProgram.replaceCache({
  793. context: context,
  794. shaderProgram: that._sp,
  795. vertexShaderSource: vs,
  796. fragmentShaderSource: fs,
  797. attributeLocations: attributeLocations,
  798. });
  799. that._rs = RenderState.fromCache({
  800. depthTest: {
  801. enabled: true,
  802. func: WebGLConstants.LESS,
  803. },
  804. depthMask: false,
  805. blending: BlendingState.ALPHA_BLEND,
  806. });
  807. that._spCreated = true;
  808. that._compiledDebugBillboards = that.debugBillboards;
  809. that._compiledDebugEllipsoids = that.debugEllipsoids;
  810. }
  811. function createDrawCommands(cloudCollection, frameState) {
  812. const that = cloudCollection;
  813. const pass = frameState.passes;
  814. const uniforms = that._uniforms;
  815. const commandList = frameState.commandList;
  816. if (pass.render) {
  817. const colorList = that._colorCommands;
  818. const va = that._vaf.va;
  819. const vaLength = va.length;
  820. colorList.length = vaLength;
  821. for (let i = 0; i < vaLength; i++) {
  822. let command = colorList[i];
  823. if (!defined(command)) {
  824. command = colorList[i] = new DrawCommand();
  825. }
  826. command.pass = Pass.TRANSLUCENT;
  827. command.owner = cloudCollection;
  828. command.uniformMap = uniforms;
  829. command.count = va[i].indicesCount;
  830. command.vertexArray = va[i].va;
  831. command.shaderProgram = that._sp;
  832. command.renderState = that._rs;
  833. if (that._instanced) {
  834. command.count = 6;
  835. command.instanceCount = that._clouds.length;
  836. }
  837. commandList.push(command);
  838. }
  839. }
  840. }
  841. /**
  842. * @private
  843. */
  844. CloudCollection.prototype.update = function (frameState) {
  845. removeClouds(this);
  846. if (!this.show) {
  847. return;
  848. }
  849. const debugging = this.debugBillboards || this.debugEllipsoids;
  850. this._ready = debugging ? true : defined(this._noiseTexture);
  851. if (!this._ready && !this._loading && !debugging) {
  852. createNoiseTexture(this, frameState, CloudNoiseVS, CloudNoiseFS);
  853. }
  854. this._instanced = frameState.context.instancedArrays;
  855. attributeLocations = this._instanced
  856. ? attributeLocationsInstanced
  857. : attributeLocationsBatched;
  858. getIndexBuffer = this._instanced
  859. ? getIndexBufferInstanced
  860. : getIndexBufferBatched;
  861. const clouds = this._clouds;
  862. const cloudsLength = clouds.length;
  863. const cloudsToUpdate = this._cloudsToUpdate;
  864. const cloudsToUpdateLength = this._cloudsToUpdateIndex;
  865. if (this._createVertexArray) {
  866. createVertexArray(this, frameState);
  867. } else if (cloudsToUpdateLength > 0) {
  868. // Clouds were modified, but none were added or removed.
  869. updateClouds(this, frameState);
  870. }
  871. // If the number of total clouds ever shrinks considerably,
  872. // truncate cloudsToUpdate so that we free memory that
  873. // we are no longer using.
  874. if (cloudsToUpdateLength > cloudsLength * 1.5) {
  875. cloudsToUpdate.length = cloudsLength;
  876. }
  877. if (
  878. !defined(this._vaf) ||
  879. !defined(this._vaf.va) ||
  880. !this._ready & !debugging
  881. ) {
  882. return;
  883. }
  884. if (
  885. !this._spCreated ||
  886. this.debugBillboards !== this._compiledDebugBillboards ||
  887. this.debugEllipsoids !== this._compiledDebugEllipsoids
  888. ) {
  889. createShaderProgram(this, frameState, CloudCollectionVS, CloudCollectionFS);
  890. }
  891. createDrawCommands(this, frameState);
  892. };
  893. /**
  894. * Returns true if this object was destroyed; otherwise, false.
  895. * <br /><br />
  896. * If this object was destroyed, it should not be used; calling any function other than
  897. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  898. *
  899. * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  900. *
  901. * @see CloudCollection#destroy
  902. */
  903. CloudCollection.prototype.isDestroyed = function () {
  904. return false;
  905. };
  906. /**
  907. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  908. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  909. * <br /><br />
  910. * Once an object is destroyed, it should not be used; calling any function other than
  911. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  912. * assign the return value (<code>undefined</code>) to the object as done in the example.
  913. *
  914. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  915. *
  916. *
  917. * @example
  918. * clouds = clouds && clouds.destroy();
  919. *
  920. * @see CloudCollection#isDestroyed
  921. */
  922. CloudCollection.prototype.destroy = function () {
  923. this._noiseTexture = this._noiseTexture && this._noiseTexture.destroy();
  924. this._sp = this._sp && this._sp.destroy();
  925. this._vaf = this._vaf && this._vaf.destroy();
  926. destroyClouds(this._clouds);
  927. return destroyObject(this);
  928. };
  929. export default CloudCollection;