PrimitiveCollection.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import createGuid from "../Core/createGuid.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defined from "../Core/defined.js";
  4. import destroyObject from "../Core/destroyObject.js";
  5. import DeveloperError from "../Core/DeveloperError.js";
  6. /**
  7. * A collection of primitives. This is most often used with {@link Scene#primitives},
  8. * but <code>PrimitiveCollection</code> is also a primitive itself so collections can
  9. * be added to collections forming a hierarchy.
  10. *
  11. * @alias PrimitiveCollection
  12. * @constructor
  13. *
  14. * @param {Object} [options] Object with the following properties:
  15. * @param {Boolean} [options.show=true] Determines if the primitives in the collection will be shown.
  16. * @param {Boolean} [options.destroyPrimitives=true] Determines if primitives in the collection are destroyed when they are removed.
  17. *
  18. * @example
  19. * const billboards = new Cesium.BillboardCollection();
  20. * const labels = new Cesium.LabelCollection();
  21. *
  22. * const collection = new Cesium.PrimitiveCollection();
  23. * collection.add(billboards);
  24. *
  25. * scene.primitives.add(collection); // Add collection
  26. * scene.primitives.add(labels); // Add regular primitive
  27. */
  28. function PrimitiveCollection(options) {
  29. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  30. this._primitives = [];
  31. this._guid = createGuid();
  32. // Used by the OrderedGroundPrimitiveCollection
  33. this._zIndex = undefined;
  34. /**
  35. * Determines if primitives in this collection will be shown.
  36. *
  37. * @type {Boolean}
  38. * @default true
  39. */
  40. this.show = defaultValue(options.show, true);
  41. /**
  42. * Determines if primitives in the collection are destroyed when they are removed by
  43. * {@link PrimitiveCollection#destroy} or {@link PrimitiveCollection#remove} or implicitly
  44. * by {@link PrimitiveCollection#removeAll}.
  45. *
  46. * @type {Boolean}
  47. * @default true
  48. *
  49. * @example
  50. * // Example 1. Primitives are destroyed by default.
  51. * const primitives = new Cesium.PrimitiveCollection();
  52. * const labels = primitives.add(new Cesium.LabelCollection());
  53. * primitives = primitives.destroy();
  54. * const b = labels.isDestroyed(); // true
  55. *
  56. * @example
  57. * // Example 2. Do not destroy primitives in a collection.
  58. * const primitives = new Cesium.PrimitiveCollection();
  59. * primitives.destroyPrimitives = false;
  60. * const labels = primitives.add(new Cesium.LabelCollection());
  61. * primitives = primitives.destroy();
  62. * const b = labels.isDestroyed(); // false
  63. * labels = labels.destroy(); // explicitly destroy
  64. */
  65. this.destroyPrimitives = defaultValue(options.destroyPrimitives, true);
  66. }
  67. Object.defineProperties(PrimitiveCollection.prototype, {
  68. /**
  69. * Gets the number of primitives in the collection.
  70. *
  71. * @memberof PrimitiveCollection.prototype
  72. *
  73. * @type {Number}
  74. * @readonly
  75. */
  76. length: {
  77. get: function () {
  78. return this._primitives.length;
  79. },
  80. },
  81. });
  82. /**
  83. * Adds a primitive to the collection.
  84. *
  85. * @param {Object} primitive The primitive to add.
  86. * @param {Number} [index] The index to add the layer at. If omitted, the primitive will be added at the bottom of all existing primitives.
  87. * @returns {Object} The primitive added to the collection.
  88. *
  89. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  90. *
  91. * @example
  92. * const billboards = scene.primitives.add(new Cesium.BillboardCollection());
  93. */
  94. PrimitiveCollection.prototype.add = function (primitive, index) {
  95. const hasIndex = defined(index);
  96. //>>includeStart('debug', pragmas.debug);
  97. if (!defined(primitive)) {
  98. throw new DeveloperError("primitive is required.");
  99. }
  100. if (hasIndex) {
  101. if (index < 0) {
  102. throw new DeveloperError("index must be greater than or equal to zero.");
  103. } else if (index > this._primitives.length) {
  104. throw new DeveloperError(
  105. "index must be less than or equal to the number of primitives."
  106. );
  107. }
  108. }
  109. //>>includeEnd('debug');
  110. const external = (primitive._external = primitive._external || {});
  111. const composites = (external._composites = external._composites || {});
  112. composites[this._guid] = {
  113. collection: this,
  114. };
  115. if (!hasIndex) {
  116. this._primitives.push(primitive);
  117. } else {
  118. this._primitives.splice(index, 0, primitive);
  119. }
  120. return primitive;
  121. };
  122. /**
  123. * Removes a primitive from the collection.
  124. *
  125. * @param {Object} [primitive] The primitive to remove.
  126. * @returns {Boolean} <code>true</code> if the primitive was removed; <code>false</code> if the primitive is <code>undefined</code> or was not found in the collection.
  127. *
  128. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  129. *
  130. *
  131. * @example
  132. * const billboards = scene.primitives.add(new Cesium.BillboardCollection());
  133. * scene.primitives.remove(billboards); // Returns true
  134. *
  135. * @see PrimitiveCollection#destroyPrimitives
  136. */
  137. PrimitiveCollection.prototype.remove = function (primitive) {
  138. // PERFORMANCE_IDEA: We can obviously make this a lot faster.
  139. if (this.contains(primitive)) {
  140. const index = this._primitives.indexOf(primitive);
  141. if (index !== -1) {
  142. this._primitives.splice(index, 1);
  143. delete primitive._external._composites[this._guid];
  144. if (this.destroyPrimitives) {
  145. primitive.destroy();
  146. }
  147. return true;
  148. }
  149. // else ... this is not possible, I swear.
  150. }
  151. return false;
  152. };
  153. /**
  154. * Removes and destroys a primitive, regardless of destroyPrimitives setting.
  155. * @private
  156. */
  157. PrimitiveCollection.prototype.removeAndDestroy = function (primitive) {
  158. const removed = this.remove(primitive);
  159. if (removed && !this.destroyPrimitives) {
  160. primitive.destroy();
  161. }
  162. return removed;
  163. };
  164. /**
  165. * Removes all primitives in the collection.
  166. *
  167. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  168. *
  169. * @see PrimitiveCollection#destroyPrimitives
  170. */
  171. PrimitiveCollection.prototype.removeAll = function () {
  172. const primitives = this._primitives;
  173. const length = primitives.length;
  174. for (let i = 0; i < length; ++i) {
  175. delete primitives[i]._external._composites[this._guid];
  176. if (this.destroyPrimitives) {
  177. primitives[i].destroy();
  178. }
  179. }
  180. this._primitives = [];
  181. };
  182. /**
  183. * Determines if this collection contains a primitive.
  184. *
  185. * @param {Object} [primitive] The primitive to check for.
  186. * @returns {Boolean} <code>true</code> if the primitive is in the collection; <code>false</code> if the primitive is <code>undefined</code> or was not found in the collection.
  187. *
  188. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  189. *
  190. * @see PrimitiveCollection#get
  191. */
  192. PrimitiveCollection.prototype.contains = function (primitive) {
  193. return !!(
  194. defined(primitive) &&
  195. primitive._external &&
  196. primitive._external._composites &&
  197. primitive._external._composites[this._guid]
  198. );
  199. };
  200. function getPrimitiveIndex(compositePrimitive, primitive) {
  201. //>>includeStart('debug', pragmas.debug);
  202. if (!compositePrimitive.contains(primitive)) {
  203. throw new DeveloperError("primitive is not in this collection.");
  204. }
  205. //>>includeEnd('debug');
  206. return compositePrimitive._primitives.indexOf(primitive);
  207. }
  208. /**
  209. * Raises a primitive "up one" in the collection. If all primitives in the collection are drawn
  210. * on the globe surface, this visually moves the primitive up one.
  211. *
  212. * @param {Object} [primitive] The primitive to raise.
  213. *
  214. * @exception {DeveloperError} primitive is not in this collection.
  215. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  216. *
  217. * @see PrimitiveCollection#raiseToTop
  218. * @see PrimitiveCollection#lower
  219. * @see PrimitiveCollection#lowerToBottom
  220. */
  221. PrimitiveCollection.prototype.raise = function (primitive) {
  222. if (defined(primitive)) {
  223. const index = getPrimitiveIndex(this, primitive);
  224. const primitives = this._primitives;
  225. if (index !== primitives.length - 1) {
  226. const p = primitives[index];
  227. primitives[index] = primitives[index + 1];
  228. primitives[index + 1] = p;
  229. }
  230. }
  231. };
  232. /**
  233. * Raises a primitive to the "top" of the collection. If all primitives in the collection are drawn
  234. * on the globe surface, this visually moves the primitive to the top.
  235. *
  236. * @param {Object} [primitive] The primitive to raise the top.
  237. *
  238. * @exception {DeveloperError} primitive is not in this collection.
  239. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  240. *
  241. * @see PrimitiveCollection#raise
  242. * @see PrimitiveCollection#lower
  243. * @see PrimitiveCollection#lowerToBottom
  244. */
  245. PrimitiveCollection.prototype.raiseToTop = function (primitive) {
  246. if (defined(primitive)) {
  247. const index = getPrimitiveIndex(this, primitive);
  248. const primitives = this._primitives;
  249. if (index !== primitives.length - 1) {
  250. // PERFORMANCE_IDEA: Could be faster
  251. primitives.splice(index, 1);
  252. primitives.push(primitive);
  253. }
  254. }
  255. };
  256. /**
  257. * Lowers a primitive "down one" in the collection. If all primitives in the collection are drawn
  258. * on the globe surface, this visually moves the primitive down one.
  259. *
  260. * @param {Object} [primitive] The primitive to lower.
  261. *
  262. * @exception {DeveloperError} primitive is not in this collection.
  263. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  264. *
  265. * @see PrimitiveCollection#lowerToBottom
  266. * @see PrimitiveCollection#raise
  267. * @see PrimitiveCollection#raiseToTop
  268. */
  269. PrimitiveCollection.prototype.lower = function (primitive) {
  270. if (defined(primitive)) {
  271. const index = getPrimitiveIndex(this, primitive);
  272. const primitives = this._primitives;
  273. if (index !== 0) {
  274. const p = primitives[index];
  275. primitives[index] = primitives[index - 1];
  276. primitives[index - 1] = p;
  277. }
  278. }
  279. };
  280. /**
  281. * Lowers a primitive to the "bottom" of the collection. If all primitives in the collection are drawn
  282. * on the globe surface, this visually moves the primitive to the bottom.
  283. *
  284. * @param {Object} [primitive] The primitive to lower to the bottom.
  285. *
  286. * @exception {DeveloperError} primitive is not in this collection.
  287. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  288. *
  289. * @see PrimitiveCollection#lower
  290. * @see PrimitiveCollection#raise
  291. * @see PrimitiveCollection#raiseToTop
  292. */
  293. PrimitiveCollection.prototype.lowerToBottom = function (primitive) {
  294. if (defined(primitive)) {
  295. const index = getPrimitiveIndex(this, primitive);
  296. const primitives = this._primitives;
  297. if (index !== 0) {
  298. // PERFORMANCE_IDEA: Could be faster
  299. primitives.splice(index, 1);
  300. primitives.unshift(primitive);
  301. }
  302. }
  303. };
  304. /**
  305. * Returns the primitive in the collection at the specified index.
  306. *
  307. * @param {Number} index The zero-based index of the primitive to return.
  308. * @returns {Object} The primitive at the <code>index</code>.
  309. *
  310. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  311. *
  312. *
  313. * @example
  314. * // Toggle the show property of every primitive in the collection.
  315. * const primitives = scene.primitives;
  316. * const length = primitives.length;
  317. * for (let i = 0; i < length; ++i) {
  318. * const p = primitives.get(i);
  319. * p.show = !p.show;
  320. * }
  321. *
  322. * @see PrimitiveCollection#length
  323. */
  324. PrimitiveCollection.prototype.get = function (index) {
  325. //>>includeStart('debug', pragmas.debug);
  326. if (!defined(index)) {
  327. throw new DeveloperError("index is required.");
  328. }
  329. //>>includeEnd('debug');
  330. return this._primitives[index];
  331. };
  332. /**
  333. * @private
  334. */
  335. PrimitiveCollection.prototype.update = function (frameState) {
  336. if (!this.show) {
  337. return;
  338. }
  339. const primitives = this._primitives;
  340. // Using primitives.length in the loop is a temporary workaround
  341. // to allow quadtree updates to add and remove primitives in
  342. // update(). This will be changed to manage added and removed lists.
  343. for (let i = 0; i < primitives.length; ++i) {
  344. primitives[i].update(frameState);
  345. }
  346. };
  347. /**
  348. * @private
  349. */
  350. PrimitiveCollection.prototype.prePassesUpdate = function (frameState) {
  351. const primitives = this._primitives;
  352. // Using primitives.length in the loop is a temporary workaround
  353. // to allow quadtree updates to add and remove primitives in
  354. // update(). This will be changed to manage added and removed lists.
  355. for (let i = 0; i < primitives.length; ++i) {
  356. const primitive = primitives[i];
  357. if (defined(primitive.prePassesUpdate)) {
  358. primitive.prePassesUpdate(frameState);
  359. }
  360. }
  361. };
  362. /**
  363. * @private
  364. */
  365. PrimitiveCollection.prototype.updateForPass = function (frameState, passState) {
  366. const primitives = this._primitives;
  367. // Using primitives.length in the loop is a temporary workaround
  368. // to allow quadtree updates to add and remove primitives in
  369. // update(). This will be changed to manage added and removed lists.
  370. for (let i = 0; i < primitives.length; ++i) {
  371. const primitive = primitives[i];
  372. if (defined(primitive.updateForPass)) {
  373. primitive.updateForPass(frameState, passState);
  374. }
  375. }
  376. };
  377. /**
  378. * @private
  379. */
  380. PrimitiveCollection.prototype.postPassesUpdate = function (frameState) {
  381. const primitives = this._primitives;
  382. // Using primitives.length in the loop is a temporary workaround
  383. // to allow quadtree updates to add and remove primitives in
  384. // update(). This will be changed to manage added and removed lists.
  385. for (let i = 0; i < primitives.length; ++i) {
  386. const primitive = primitives[i];
  387. if (defined(primitive.postPassesUpdate)) {
  388. primitive.postPassesUpdate(frameState);
  389. }
  390. }
  391. };
  392. /**
  393. * Returns true if this object was destroyed; otherwise, false.
  394. * <br /><br />
  395. * If this object was destroyed, it should not be used; calling any function other than
  396. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  397. *
  398. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  399. *
  400. * @see PrimitiveCollection#destroy
  401. */
  402. PrimitiveCollection.prototype.isDestroyed = function () {
  403. return false;
  404. };
  405. /**
  406. * Destroys the WebGL resources held by each primitive in this collection. Explicitly destroying this
  407. * collection allows for deterministic release of WebGL resources, instead of relying on the garbage
  408. * collector to destroy this collection.
  409. * <br /><br />
  410. * Since destroying a collection destroys all the contained primitives, only destroy a collection
  411. * when you are sure no other code is still using any of the contained primitives.
  412. * <br /><br />
  413. * Once this collection is destroyed, it should not be used; calling any function other than
  414. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  415. * assign the return value (<code>undefined</code>) to the object as done in the example.
  416. *
  417. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  418. *
  419. *
  420. * @example
  421. * primitives = primitives && primitives.destroy();
  422. *
  423. * @see PrimitiveCollection#isDestroyed
  424. */
  425. PrimitiveCollection.prototype.destroy = function () {
  426. this.removeAll();
  427. return destroyObject(this);
  428. };
  429. export default PrimitiveCollection;