Cesium3DTilesetVisualizer.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import AssociativeArray from "../Core/AssociativeArray.js";
  2. import BoundingSphere from "../Core/BoundingSphere.js";
  3. import defined from "../Core/defined.js";
  4. import destroyObject from "../Core/destroyObject.js";
  5. import DeveloperError from "../Core/DeveloperError.js";
  6. import Matrix4 from "../Core/Matrix4.js";
  7. import Resource from "../Core/Resource.js";
  8. import Cesium3DTileset from "../Scene/Cesium3DTileset.js";
  9. import BoundingSphereState from "./BoundingSphereState.js";
  10. import Property from "./Property.js";
  11. const modelMatrixScratch = new Matrix4();
  12. /**
  13. * A {@link Visualizer} which maps {@link Entity#tileset} to a {@link Cesium3DTileset}.
  14. * @alias Cesium3DTilesetVisualizer
  15. * @constructor
  16. *
  17. * @param {Scene} scene The scene the primitives will be rendered in.
  18. * @param {EntityCollection} entityCollection The entityCollection to visualize.
  19. */
  20. function Cesium3DTilesetVisualizer(scene, entityCollection) {
  21. //>>includeStart('debug', pragmas.debug);
  22. if (!defined(scene)) {
  23. throw new DeveloperError("scene is required.");
  24. }
  25. if (!defined(entityCollection)) {
  26. throw new DeveloperError("entityCollection is required.");
  27. }
  28. //>>includeEnd('debug');
  29. entityCollection.collectionChanged.addEventListener(
  30. Cesium3DTilesetVisualizer.prototype._onCollectionChanged,
  31. this
  32. );
  33. this._scene = scene;
  34. this._primitives = scene.primitives;
  35. this._entityCollection = entityCollection;
  36. this._tilesetHash = {};
  37. this._entitiesToVisualize = new AssociativeArray();
  38. this._onCollectionChanged(entityCollection, entityCollection.values, [], []);
  39. }
  40. /**
  41. * Updates models created this visualizer to match their
  42. * Entity counterpart at the given time.
  43. *
  44. * @param {JulianDate} time The time to update to.
  45. * @returns {Boolean} This function always returns true.
  46. */
  47. Cesium3DTilesetVisualizer.prototype.update = function (time) {
  48. //>>includeStart('debug', pragmas.debug);
  49. if (!defined(time)) {
  50. throw new DeveloperError("time is required.");
  51. }
  52. //>>includeEnd('debug');
  53. const entities = this._entitiesToVisualize.values;
  54. const tilesetHash = this._tilesetHash;
  55. const primitives = this._primitives;
  56. for (let i = 0, len = entities.length; i < len; i++) {
  57. const entity = entities[i];
  58. const tilesetGraphics = entity._tileset;
  59. let resource;
  60. let tilesetData = tilesetHash[entity.id];
  61. const show =
  62. entity.isShowing &&
  63. entity.isAvailable(time) &&
  64. Property.getValueOrDefault(tilesetGraphics._show, time, true);
  65. let modelMatrix;
  66. if (show) {
  67. modelMatrix = entity.computeModelMatrix(time, modelMatrixScratch);
  68. resource = Resource.createIfNeeded(
  69. Property.getValueOrUndefined(tilesetGraphics._uri, time)
  70. );
  71. }
  72. if (!show) {
  73. if (defined(tilesetData)) {
  74. tilesetData.tilesetPrimitive.show = false;
  75. }
  76. continue;
  77. }
  78. let tileset = defined(tilesetData)
  79. ? tilesetData.tilesetPrimitive
  80. : undefined;
  81. if (!defined(tileset) || resource.url !== tilesetData.url) {
  82. if (defined(tileset)) {
  83. primitives.removeAndDestroy(tileset);
  84. delete tilesetHash[entity.id];
  85. }
  86. tileset = new Cesium3DTileset({
  87. url: resource,
  88. });
  89. tileset.id = entity;
  90. primitives.add(tileset);
  91. tilesetData = {
  92. tilesetPrimitive: tileset,
  93. url: resource.url,
  94. loadFail: false,
  95. };
  96. tilesetHash[entity.id] = tilesetData;
  97. checkLoad(tileset, entity, tilesetHash);
  98. }
  99. tileset.show = true;
  100. if (defined(modelMatrix)) {
  101. tileset.modelMatrix = modelMatrix;
  102. }
  103. tileset.maximumScreenSpaceError = Property.getValueOrDefault(
  104. tilesetGraphics.maximumScreenSpaceError,
  105. time,
  106. tileset.maximumScreenSpaceError
  107. );
  108. }
  109. return true;
  110. };
  111. /**
  112. * Returns true if this object was destroyed; otherwise, false.
  113. *
  114. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  115. */
  116. Cesium3DTilesetVisualizer.prototype.isDestroyed = function () {
  117. return false;
  118. };
  119. /**
  120. * Removes and destroys all primitives created by this instance.
  121. */
  122. Cesium3DTilesetVisualizer.prototype.destroy = function () {
  123. this._entityCollection.collectionChanged.removeEventListener(
  124. Cesium3DTilesetVisualizer.prototype._onCollectionChanged,
  125. this
  126. );
  127. const entities = this._entitiesToVisualize.values;
  128. const tilesetHash = this._tilesetHash;
  129. const primitives = this._primitives;
  130. for (let i = entities.length - 1; i > -1; i--) {
  131. removeTileset(this, entities[i], tilesetHash, primitives);
  132. }
  133. return destroyObject(this);
  134. };
  135. /**
  136. * Computes a bounding sphere which encloses the visualization produced for the specified entity.
  137. * The bounding sphere is in the fixed frame of the scene's globe.
  138. *
  139. * @param {Entity} entity The entity whose bounding sphere to compute.
  140. * @param {BoundingSphere} result The bounding sphere onto which to store the result.
  141. * @returns {BoundingSphereState} BoundingSphereState.DONE if the result contains the bounding sphere,
  142. * BoundingSphereState.PENDING if the result is still being computed, or
  143. * BoundingSphereState.FAILED if the entity has no visualization in the current scene.
  144. * @private
  145. */
  146. Cesium3DTilesetVisualizer.prototype.getBoundingSphere = function (
  147. entity,
  148. result
  149. ) {
  150. //>>includeStart('debug', pragmas.debug);
  151. if (!defined(entity)) {
  152. throw new DeveloperError("entity is required.");
  153. }
  154. if (!defined(result)) {
  155. throw new DeveloperError("result is required.");
  156. }
  157. //>>includeEnd('debug');
  158. const tilesetData = this._tilesetHash[entity.id];
  159. if (!defined(tilesetData) || tilesetData.loadFail) {
  160. return BoundingSphereState.FAILED;
  161. }
  162. const primitive = tilesetData.tilesetPrimitive;
  163. if (!defined(primitive) || !primitive.show) {
  164. return BoundingSphereState.FAILED;
  165. }
  166. if (!primitive.ready) {
  167. return BoundingSphereState.PENDING;
  168. }
  169. BoundingSphere.clone(primitive.boundingSphere, result);
  170. return BoundingSphereState.DONE;
  171. };
  172. /**
  173. * @private
  174. */
  175. Cesium3DTilesetVisualizer.prototype._onCollectionChanged = function (
  176. entityCollection,
  177. added,
  178. removed,
  179. changed
  180. ) {
  181. let i;
  182. let entity;
  183. const entities = this._entitiesToVisualize;
  184. const tilesetHash = this._tilesetHash;
  185. const primitives = this._primitives;
  186. for (i = added.length - 1; i > -1; i--) {
  187. entity = added[i];
  188. if (defined(entity._tileset)) {
  189. entities.set(entity.id, entity);
  190. }
  191. }
  192. for (i = changed.length - 1; i > -1; i--) {
  193. entity = changed[i];
  194. if (defined(entity._tileset)) {
  195. entities.set(entity.id, entity);
  196. } else {
  197. removeTileset(this, entity, tilesetHash, primitives);
  198. entities.remove(entity.id);
  199. }
  200. }
  201. for (i = removed.length - 1; i > -1; i--) {
  202. entity = removed[i];
  203. removeTileset(this, entity, tilesetHash, primitives);
  204. entities.remove(entity.id);
  205. }
  206. };
  207. function removeTileset(visualizer, entity, tilesetHash, primitives) {
  208. const tilesetData = tilesetHash[entity.id];
  209. if (defined(tilesetData)) {
  210. primitives.removeAndDestroy(tilesetData.tilesetPrimitive);
  211. delete tilesetHash[entity.id];
  212. }
  213. }
  214. function checkLoad(primitive, entity, tilesetHash) {
  215. primitive.readyPromise.catch(function (error) {
  216. console.error(error);
  217. tilesetHash[entity.id].loadFail = true;
  218. });
  219. }
  220. export default Cesium3DTilesetVisualizer;