Cesium3DTilesetVisualizer.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. const 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. const tileset = defined(tilesetData)
  79. ? tilesetData.tilesetPrimitive
  80. : undefined;
  81. if (!defined(tilesetData) || resource.url !== tilesetData.url) {
  82. if (defined(tileset)) {
  83. primitives.removeAndDestroy(tileset);
  84. }
  85. delete tilesetHash[entity.id];
  86. createTileset(resource, tilesetHash, entity, primitives);
  87. }
  88. if (!defined(tileset)) {
  89. continue;
  90. }
  91. tileset.show = true;
  92. if (defined(modelMatrix)) {
  93. tileset.modelMatrix = modelMatrix;
  94. }
  95. tileset.maximumScreenSpaceError = Property.getValueOrDefault(
  96. tilesetGraphics.maximumScreenSpaceError,
  97. time,
  98. tileset.maximumScreenSpaceError
  99. );
  100. }
  101. return true;
  102. };
  103. /**
  104. * Returns true if this object was destroyed; otherwise, false.
  105. *
  106. * @returns {boolean} True if this object was destroyed; otherwise, false.
  107. */
  108. Cesium3DTilesetVisualizer.prototype.isDestroyed = function () {
  109. return false;
  110. };
  111. /**
  112. * Removes and destroys all primitives created by this instance.
  113. */
  114. Cesium3DTilesetVisualizer.prototype.destroy = function () {
  115. this._entityCollection.collectionChanged.removeEventListener(
  116. Cesium3DTilesetVisualizer.prototype._onCollectionChanged,
  117. this
  118. );
  119. const entities = this._entitiesToVisualize.values;
  120. const tilesetHash = this._tilesetHash;
  121. const primitives = this._primitives;
  122. for (let i = entities.length - 1; i > -1; i--) {
  123. removeTileset(this, entities[i], tilesetHash, primitives);
  124. }
  125. return destroyObject(this);
  126. };
  127. /**
  128. * Computes a bounding sphere which encloses the visualization produced for the specified entity.
  129. * The bounding sphere is in the fixed frame of the scene's globe.
  130. *
  131. * @param {Entity} entity The entity whose bounding sphere to compute.
  132. * @param {BoundingSphere} result The bounding sphere onto which to store the result.
  133. * @returns {BoundingSphereState} BoundingSphereState.DONE if the result contains the bounding sphere,
  134. * BoundingSphereState.PENDING if the result is still being computed, or
  135. * BoundingSphereState.FAILED if the entity has no visualization in the current scene.
  136. * @private
  137. */
  138. Cesium3DTilesetVisualizer.prototype.getBoundingSphere = function (
  139. entity,
  140. result
  141. ) {
  142. //>>includeStart('debug', pragmas.debug);
  143. if (!defined(entity)) {
  144. throw new DeveloperError("entity is required.");
  145. }
  146. if (!defined(result)) {
  147. throw new DeveloperError("result is required.");
  148. }
  149. //>>includeEnd('debug');
  150. const tilesetData = this._tilesetHash[entity.id];
  151. if (!defined(tilesetData) || tilesetData.loadFail) {
  152. return BoundingSphereState.FAILED;
  153. }
  154. const primitive = tilesetData.tilesetPrimitive;
  155. if (!defined(primitive)) {
  156. return BoundingSphereState.PENDING;
  157. }
  158. if (!primitive.show) {
  159. return BoundingSphereState.FAILED;
  160. }
  161. BoundingSphere.clone(primitive.boundingSphere, result);
  162. return BoundingSphereState.DONE;
  163. };
  164. /**
  165. * @private
  166. */
  167. Cesium3DTilesetVisualizer.prototype._onCollectionChanged = function (
  168. entityCollection,
  169. added,
  170. removed,
  171. changed
  172. ) {
  173. let i;
  174. let entity;
  175. const entities = this._entitiesToVisualize;
  176. const tilesetHash = this._tilesetHash;
  177. const primitives = this._primitives;
  178. for (i = added.length - 1; i > -1; i--) {
  179. entity = added[i];
  180. if (defined(entity._tileset)) {
  181. entities.set(entity.id, entity);
  182. }
  183. }
  184. for (i = changed.length - 1; i > -1; i--) {
  185. entity = changed[i];
  186. if (defined(entity._tileset)) {
  187. entities.set(entity.id, entity);
  188. } else {
  189. removeTileset(this, entity, tilesetHash, primitives);
  190. entities.remove(entity.id);
  191. }
  192. }
  193. for (i = removed.length - 1; i > -1; i--) {
  194. entity = removed[i];
  195. removeTileset(this, entity, tilesetHash, primitives);
  196. entities.remove(entity.id);
  197. }
  198. };
  199. function removeTileset(visualizer, entity, tilesetHash, primitives) {
  200. const tilesetData = tilesetHash[entity.id];
  201. if (defined(tilesetData)) {
  202. if (defined(tilesetData.tilesetPrimitive)) {
  203. primitives.removeAndDestroy(tilesetData.tilesetPrimitive);
  204. }
  205. delete tilesetHash[entity.id];
  206. }
  207. }
  208. async function createTileset(resource, tilesetHash, entity, primitives) {
  209. tilesetHash[entity.id] = {
  210. url: resource.url,
  211. loadFail: false,
  212. };
  213. try {
  214. const tileset = await Cesium3DTileset.fromUrl(resource);
  215. tileset.id = entity;
  216. primitives.add(tileset);
  217. if (!defined(tilesetHash[entity.id])) {
  218. return;
  219. }
  220. tilesetHash[entity.id].tilesetPrimitive = tileset;
  221. } catch (error) {
  222. console.error(error);
  223. tilesetHash[entity.id].loadFail = true;
  224. }
  225. }
  226. export default Cesium3DTilesetVisualizer;