PrimitiveCollection.js 15 KB

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