PointVisualizer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. import AssociativeArray from "../Core/AssociativeArray.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Color from "../Core/Color.js";
  4. import defined from "../Core/defined.js";
  5. import destroyObject from "../Core/destroyObject.js";
  6. import DeveloperError from "../Core/DeveloperError.js";
  7. import DistanceDisplayCondition from "../Core/DistanceDisplayCondition.js";
  8. import NearFarScalar from "../Core/NearFarScalar.js";
  9. import createBillboardPointCallback from "../Scene/createBillboardPointCallback.js";
  10. import HeightReference from "../Scene/HeightReference.js";
  11. import BoundingSphereState from "./BoundingSphereState.js";
  12. import Property from "./Property.js";
  13. const defaultColor = Color.WHITE;
  14. const defaultOutlineColor = Color.BLACK;
  15. const defaultOutlineWidth = 0.0;
  16. const defaultPixelSize = 1.0;
  17. const defaultDisableDepthTestDistance = 0.0;
  18. const colorScratch = new Color();
  19. const positionScratch = new Cartesian3();
  20. const outlineColorScratch = new Color();
  21. const scaleByDistanceScratch = new NearFarScalar();
  22. const translucencyByDistanceScratch = new NearFarScalar();
  23. const distanceDisplayConditionScratch = new DistanceDisplayCondition();
  24. function EntityData(entity) {
  25. this.entity = entity;
  26. this.pointPrimitive = undefined;
  27. this.billboard = undefined;
  28. this.color = undefined;
  29. this.outlineColor = undefined;
  30. this.pixelSize = undefined;
  31. this.outlineWidth = undefined;
  32. }
  33. /**
  34. * A {@link Visualizer} which maps {@link Entity#point} to a {@link PointPrimitive}.
  35. * @alias PointVisualizer
  36. * @constructor
  37. *
  38. * @param {EntityCluster} entityCluster The entity cluster to manage the collection of billboards and optionally cluster with other entities.
  39. * @param {EntityCollection} entityCollection The entityCollection to visualize.
  40. */
  41. function PointVisualizer(entityCluster, entityCollection) {
  42. //>>includeStart('debug', pragmas.debug);
  43. if (!defined(entityCluster)) {
  44. throw new DeveloperError("entityCluster is required.");
  45. }
  46. if (!defined(entityCollection)) {
  47. throw new DeveloperError("entityCollection is required.");
  48. }
  49. //>>includeEnd('debug');
  50. entityCollection.collectionChanged.addEventListener(
  51. PointVisualizer.prototype._onCollectionChanged,
  52. this
  53. );
  54. this._cluster = entityCluster;
  55. this._entityCollection = entityCollection;
  56. this._items = new AssociativeArray();
  57. this._onCollectionChanged(entityCollection, entityCollection.values, [], []);
  58. }
  59. /**
  60. * Updates the primitives created by this visualizer to match their
  61. * Entity counterpart at the given time.
  62. *
  63. * @param {JulianDate} time The time to update to.
  64. * @returns {boolean} This function always returns true.
  65. */
  66. PointVisualizer.prototype.update = function (time) {
  67. //>>includeStart('debug', pragmas.debug);
  68. if (!defined(time)) {
  69. throw new DeveloperError("time is required.");
  70. }
  71. //>>includeEnd('debug');
  72. const items = this._items.values;
  73. const cluster = this._cluster;
  74. for (let i = 0, len = items.length; i < len; i++) {
  75. const item = items[i];
  76. const entity = item.entity;
  77. const pointGraphics = entity._point;
  78. let pointPrimitive = item.pointPrimitive;
  79. let billboard = item.billboard;
  80. const heightReference = Property.getValueOrDefault(
  81. pointGraphics._heightReference,
  82. time,
  83. HeightReference.NONE
  84. );
  85. let show =
  86. entity.isShowing &&
  87. entity.isAvailable(time) &&
  88. Property.getValueOrDefault(pointGraphics._show, time, true);
  89. let position;
  90. if (show) {
  91. position = Property.getValueOrUndefined(
  92. entity._position,
  93. time,
  94. positionScratch
  95. );
  96. show = defined(position);
  97. }
  98. if (!show) {
  99. returnPrimitive(item, entity, cluster);
  100. continue;
  101. }
  102. if (!Property.isConstant(entity._position)) {
  103. cluster._clusterDirty = true;
  104. }
  105. let needsRedraw = false;
  106. let updateClamping = false;
  107. if (heightReference !== HeightReference.NONE && !defined(billboard)) {
  108. if (defined(pointPrimitive)) {
  109. returnPrimitive(item, entity, cluster);
  110. pointPrimitive = undefined;
  111. }
  112. billboard = cluster.getBillboard(entity);
  113. billboard.id = entity;
  114. billboard.image = undefined;
  115. item.billboard = billboard;
  116. needsRedraw = true;
  117. // If this new billboard happens to have a position and height reference that match our new values,
  118. // billboard._updateClamping will not be called automatically. That's a problem because the clamped
  119. // height may be based on different terrain than is now loaded. So we'll manually call
  120. // _updateClamping below.
  121. updateClamping =
  122. Cartesian3.equals(billboard.position, position) &&
  123. billboard.heightReference === heightReference;
  124. } else if (
  125. heightReference === HeightReference.NONE &&
  126. !defined(pointPrimitive)
  127. ) {
  128. if (defined(billboard)) {
  129. returnPrimitive(item, entity, cluster);
  130. billboard = undefined;
  131. }
  132. pointPrimitive = cluster.getPoint(entity);
  133. pointPrimitive.id = entity;
  134. item.pointPrimitive = pointPrimitive;
  135. }
  136. if (defined(pointPrimitive)) {
  137. pointPrimitive.show = true;
  138. pointPrimitive.position = position;
  139. pointPrimitive.scaleByDistance = Property.getValueOrUndefined(
  140. pointGraphics._scaleByDistance,
  141. time,
  142. scaleByDistanceScratch
  143. );
  144. pointPrimitive.translucencyByDistance = Property.getValueOrUndefined(
  145. pointGraphics._translucencyByDistance,
  146. time,
  147. translucencyByDistanceScratch
  148. );
  149. pointPrimitive.color = Property.getValueOrDefault(
  150. pointGraphics._color,
  151. time,
  152. defaultColor,
  153. colorScratch
  154. );
  155. pointPrimitive.outlineColor = Property.getValueOrDefault(
  156. pointGraphics._outlineColor,
  157. time,
  158. defaultOutlineColor,
  159. outlineColorScratch
  160. );
  161. pointPrimitive.outlineWidth = Property.getValueOrDefault(
  162. pointGraphics._outlineWidth,
  163. time,
  164. defaultOutlineWidth
  165. );
  166. pointPrimitive.pixelSize = Property.getValueOrDefault(
  167. pointGraphics._pixelSize,
  168. time,
  169. defaultPixelSize
  170. );
  171. pointPrimitive.distanceDisplayCondition = Property.getValueOrUndefined(
  172. pointGraphics._distanceDisplayCondition,
  173. time,
  174. distanceDisplayConditionScratch
  175. );
  176. pointPrimitive.disableDepthTestDistance = Property.getValueOrDefault(
  177. pointGraphics._disableDepthTestDistance,
  178. time,
  179. defaultDisableDepthTestDistance
  180. );
  181. } else if (defined(billboard)) {
  182. billboard.show = true;
  183. billboard.position = position;
  184. billboard.scaleByDistance = Property.getValueOrUndefined(
  185. pointGraphics._scaleByDistance,
  186. time,
  187. scaleByDistanceScratch
  188. );
  189. billboard.translucencyByDistance = Property.getValueOrUndefined(
  190. pointGraphics._translucencyByDistance,
  191. time,
  192. translucencyByDistanceScratch
  193. );
  194. billboard.distanceDisplayCondition = Property.getValueOrUndefined(
  195. pointGraphics._distanceDisplayCondition,
  196. time,
  197. distanceDisplayConditionScratch
  198. );
  199. billboard.disableDepthTestDistance = Property.getValueOrDefault(
  200. pointGraphics._disableDepthTestDistance,
  201. time,
  202. defaultDisableDepthTestDistance
  203. );
  204. billboard.heightReference = heightReference;
  205. const newColor = Property.getValueOrDefault(
  206. pointGraphics._color,
  207. time,
  208. defaultColor,
  209. colorScratch
  210. );
  211. const newOutlineColor = Property.getValueOrDefault(
  212. pointGraphics._outlineColor,
  213. time,
  214. defaultOutlineColor,
  215. outlineColorScratch
  216. );
  217. const newOutlineWidth = Math.round(
  218. Property.getValueOrDefault(
  219. pointGraphics._outlineWidth,
  220. time,
  221. defaultOutlineWidth
  222. )
  223. );
  224. let newPixelSize = Math.max(
  225. 1,
  226. Math.round(
  227. Property.getValueOrDefault(
  228. pointGraphics._pixelSize,
  229. time,
  230. defaultPixelSize
  231. )
  232. )
  233. );
  234. if (newOutlineWidth > 0) {
  235. billboard.scale = 1.0;
  236. needsRedraw =
  237. needsRedraw || //
  238. newOutlineWidth !== item.outlineWidth || //
  239. newPixelSize !== item.pixelSize || //
  240. !Color.equals(newColor, item.color) || //
  241. !Color.equals(newOutlineColor, item.outlineColor);
  242. } else {
  243. billboard.scale = newPixelSize / 50.0;
  244. newPixelSize = 50.0;
  245. needsRedraw =
  246. needsRedraw || //
  247. newOutlineWidth !== item.outlineWidth || //
  248. !Color.equals(newColor, item.color) || //
  249. !Color.equals(newOutlineColor, item.outlineColor);
  250. }
  251. if (needsRedraw) {
  252. item.color = Color.clone(newColor, item.color);
  253. item.outlineColor = Color.clone(newOutlineColor, item.outlineColor);
  254. item.pixelSize = newPixelSize;
  255. item.outlineWidth = newOutlineWidth;
  256. const centerAlpha = newColor.alpha;
  257. const cssColor = newColor.toCssColorString();
  258. const cssOutlineColor = newOutlineColor.toCssColorString();
  259. const textureId = JSON.stringify([
  260. cssColor,
  261. newPixelSize,
  262. cssOutlineColor,
  263. newOutlineWidth,
  264. ]);
  265. billboard.setImage(
  266. textureId,
  267. createBillboardPointCallback(
  268. centerAlpha,
  269. cssColor,
  270. cssOutlineColor,
  271. newOutlineWidth,
  272. newPixelSize
  273. )
  274. );
  275. }
  276. if (updateClamping) {
  277. billboard._updateClamping();
  278. }
  279. }
  280. }
  281. return true;
  282. };
  283. /**
  284. * Computes a bounding sphere which encloses the visualization produced for the specified entity.
  285. * The bounding sphere is in the fixed frame of the scene's globe.
  286. *
  287. * @param {Entity} entity The entity whose bounding sphere to compute.
  288. * @param {BoundingSphere} result The bounding sphere onto which to store the result.
  289. * @returns {BoundingSphereState} BoundingSphereState.DONE if the result contains the bounding sphere,
  290. * BoundingSphereState.PENDING if the result is still being computed, or
  291. * BoundingSphereState.FAILED if the entity has no visualization in the current scene.
  292. * @private
  293. */
  294. PointVisualizer.prototype.getBoundingSphere = function (entity, result) {
  295. //>>includeStart('debug', pragmas.debug);
  296. if (!defined(entity)) {
  297. throw new DeveloperError("entity is required.");
  298. }
  299. if (!defined(result)) {
  300. throw new DeveloperError("result is required.");
  301. }
  302. //>>includeEnd('debug');
  303. const item = this._items.get(entity.id);
  304. if (
  305. !defined(item) ||
  306. !(defined(item.pointPrimitive) || defined(item.billboard))
  307. ) {
  308. return BoundingSphereState.FAILED;
  309. }
  310. if (defined(item.pointPrimitive)) {
  311. result.center = Cartesian3.clone(
  312. item.pointPrimitive.position,
  313. result.center
  314. );
  315. } else {
  316. const billboard = item.billboard;
  317. if (!defined(billboard._clampedPosition)) {
  318. return BoundingSphereState.PENDING;
  319. }
  320. result.center = Cartesian3.clone(billboard._clampedPosition, result.center);
  321. }
  322. result.radius = 0;
  323. return BoundingSphereState.DONE;
  324. };
  325. /**
  326. * Returns true if this object was destroyed; otherwise, false.
  327. *
  328. * @returns {boolean} True if this object was destroyed; otherwise, false.
  329. */
  330. PointVisualizer.prototype.isDestroyed = function () {
  331. return false;
  332. };
  333. /**
  334. * Removes and destroys all primitives created by this instance.
  335. */
  336. PointVisualizer.prototype.destroy = function () {
  337. this._entityCollection.collectionChanged.removeEventListener(
  338. PointVisualizer.prototype._onCollectionChanged,
  339. this
  340. );
  341. const entities = this._entityCollection.values;
  342. for (let i = 0; i < entities.length; i++) {
  343. this._cluster.removePoint(entities[i]);
  344. }
  345. return destroyObject(this);
  346. };
  347. PointVisualizer.prototype._onCollectionChanged = function (
  348. entityCollection,
  349. added,
  350. removed,
  351. changed
  352. ) {
  353. let i;
  354. let entity;
  355. const items = this._items;
  356. const cluster = this._cluster;
  357. for (i = added.length - 1; i > -1; i--) {
  358. entity = added[i];
  359. if (defined(entity._point) && defined(entity._position)) {
  360. items.set(entity.id, new EntityData(entity));
  361. }
  362. }
  363. for (i = changed.length - 1; i > -1; i--) {
  364. entity = changed[i];
  365. if (defined(entity._point) && defined(entity._position)) {
  366. if (!items.contains(entity.id)) {
  367. items.set(entity.id, new EntityData(entity));
  368. }
  369. } else {
  370. returnPrimitive(items.get(entity.id), entity, cluster);
  371. items.remove(entity.id);
  372. }
  373. }
  374. for (i = removed.length - 1; i > -1; i--) {
  375. entity = removed[i];
  376. returnPrimitive(items.get(entity.id), entity, cluster);
  377. items.remove(entity.id);
  378. }
  379. };
  380. function returnPrimitive(item, entity, cluster) {
  381. if (defined(item)) {
  382. const pointPrimitive = item.pointPrimitive;
  383. if (defined(pointPrimitive)) {
  384. item.pointPrimitive = undefined;
  385. cluster.removePoint(entity);
  386. return;
  387. }
  388. const billboard = item.billboard;
  389. if (defined(billboard)) {
  390. item.billboard = undefined;
  391. cluster.removeBillboard(entity);
  392. }
  393. }
  394. }
  395. export default PointVisualizer;