PrimitiveCollection.js 17 KB

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