PointVisualizer.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. var defaultColor = Color.WHITE;
  14. var defaultOutlineColor = Color.BLACK;
  15. var defaultOutlineWidth = 0.0;
  16. var defaultPixelSize = 1.0;
  17. var defaultDisableDepthTestDistance = 0.0;
  18. var colorScratch = new Color();
  19. var positionScratch = new Cartesian3();
  20. var outlineColorScratch = new Color();
  21. var scaleByDistanceScratch = new NearFarScalar();
  22. var translucencyByDistanceScratch = new NearFarScalar();
  23. var 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(PointVisualizer.prototype._onCollectionChanged, this);
  51. this._cluster = entityCluster;
  52. this._entityCollection = entityCollection;
  53. this._items = new AssociativeArray();
  54. this._onCollectionChanged(entityCollection, entityCollection.values, [], []);
  55. }
  56. /**
  57. * Updates the primitives created by this visualizer to match their
  58. * Entity counterpart at the given time.
  59. *
  60. * @param {JulianDate} time The time to update to.
  61. * @returns {Boolean} This function always returns true.
  62. */
  63. PointVisualizer.prototype.update = function(time) {
  64. //>>includeStart('debug', pragmas.debug);
  65. if (!defined(time)) {
  66. throw new DeveloperError('time is required.');
  67. }
  68. //>>includeEnd('debug');
  69. var items = this._items.values;
  70. var cluster = this._cluster;
  71. for (var i = 0, len = items.length; i < len; i++) {
  72. var item = items[i];
  73. var entity = item.entity;
  74. var pointGraphics = entity._point;
  75. var pointPrimitive = item.pointPrimitive;
  76. var billboard = item.billboard;
  77. var heightReference = Property.getValueOrDefault(pointGraphics._heightReference, time, HeightReference.NONE);
  78. var show = entity.isShowing && entity.isAvailable(time) && Property.getValueOrDefault(pointGraphics._show, time, true);
  79. var position;
  80. if (show) {
  81. position = Property.getValueOrUndefined(entity._position, time, positionScratch);
  82. show = defined(position);
  83. }
  84. if (!show) {
  85. returnPrimitive(item, entity, cluster);
  86. continue;
  87. }
  88. if (!Property.isConstant(entity._position)) {
  89. cluster._clusterDirty = true;
  90. }
  91. var needsRedraw = false;
  92. var updateClamping = false;
  93. if ((heightReference !== HeightReference.NONE) && !defined(billboard)) {
  94. if (defined(pointPrimitive)) {
  95. returnPrimitive(item, entity, cluster);
  96. pointPrimitive = undefined;
  97. }
  98. billboard = cluster.getBillboard(entity);
  99. billboard.id = entity;
  100. billboard.image = undefined;
  101. item.billboard = billboard;
  102. needsRedraw = true;
  103. // If this new billboard happens to have a position and height reference that match our new values,
  104. // billboard._updateClamping will not be called automatically. That's a problem because the clamped
  105. // height may be based on different terrain than is now loaded. So we'll manually call
  106. // _updateClamping below.
  107. updateClamping = Cartesian3.equals(billboard.position, position) && billboard.heightReference === heightReference;
  108. } else if ((heightReference === HeightReference.NONE) && !defined(pointPrimitive)) {
  109. if (defined(billboard)) {
  110. returnPrimitive(item, entity, cluster);
  111. billboard = undefined;
  112. }
  113. pointPrimitive = cluster.getPoint(entity);
  114. pointPrimitive.id = entity;
  115. item.pointPrimitive = pointPrimitive;
  116. }
  117. if (defined(pointPrimitive)) {
  118. pointPrimitive.show = true;
  119. pointPrimitive.position = position;
  120. pointPrimitive.scaleByDistance = Property.getValueOrUndefined(pointGraphics._scaleByDistance, time, scaleByDistanceScratch);
  121. pointPrimitive.translucencyByDistance = Property.getValueOrUndefined(pointGraphics._translucencyByDistance, time, translucencyByDistanceScratch);
  122. pointPrimitive.color = Property.getValueOrDefault(pointGraphics._color, time, defaultColor, colorScratch);
  123. pointPrimitive.outlineColor = Property.getValueOrDefault(pointGraphics._outlineColor, time, defaultOutlineColor, outlineColorScratch);
  124. pointPrimitive.outlineWidth = Property.getValueOrDefault(pointGraphics._outlineWidth, time, defaultOutlineWidth);
  125. pointPrimitive.pixelSize = Property.getValueOrDefault(pointGraphics._pixelSize, time, defaultPixelSize);
  126. pointPrimitive.distanceDisplayCondition = Property.getValueOrUndefined(pointGraphics._distanceDisplayCondition, time, distanceDisplayConditionScratch);
  127. pointPrimitive.disableDepthTestDistance = Property.getValueOrDefault(pointGraphics._disableDepthTestDistance, time, defaultDisableDepthTestDistance);
  128. } else if (defined(billboard)) {
  129. billboard.show = true;
  130. billboard.position = position;
  131. billboard.scaleByDistance = Property.getValueOrUndefined(pointGraphics._scaleByDistance, time, scaleByDistanceScratch);
  132. billboard.translucencyByDistance = Property.getValueOrUndefined(pointGraphics._translucencyByDistance, time, translucencyByDistanceScratch);
  133. billboard.distanceDisplayCondition = Property.getValueOrUndefined(pointGraphics._distanceDisplayCondition, time, distanceDisplayConditionScratch);
  134. billboard.disableDepthTestDistance = Property.getValueOrDefault(pointGraphics._disableDepthTestDistance, time, defaultDisableDepthTestDistance);
  135. billboard.heightReference = heightReference;
  136. var newColor = Property.getValueOrDefault(pointGraphics._color, time, defaultColor, colorScratch);
  137. var newOutlineColor = Property.getValueOrDefault(pointGraphics._outlineColor, time, defaultOutlineColor, outlineColorScratch);
  138. var newOutlineWidth = Math.round(Property.getValueOrDefault(pointGraphics._outlineWidth, time, defaultOutlineWidth));
  139. var newPixelSize = Math.max(1, Math.round(Property.getValueOrDefault(pointGraphics._pixelSize, time, defaultPixelSize)));
  140. if (newOutlineWidth > 0) {
  141. billboard.scale = 1.0;
  142. needsRedraw = needsRedraw || //
  143. newOutlineWidth !== item.outlineWidth || //
  144. newPixelSize !== item.pixelSize || //
  145. !Color.equals(newColor, item.color) || //
  146. !Color.equals(newOutlineColor, item.outlineColor);
  147. } else {
  148. billboard.scale = newPixelSize / 50.0;
  149. newPixelSize = 50.0;
  150. needsRedraw = needsRedraw || //
  151. newOutlineWidth !== item.outlineWidth || //
  152. !Color.equals(newColor, item.color) || //
  153. !Color.equals(newOutlineColor, item.outlineColor);
  154. }
  155. if (needsRedraw) {
  156. item.color = Color.clone(newColor, item.color);
  157. item.outlineColor = Color.clone(newOutlineColor, item.outlineColor);
  158. item.pixelSize = newPixelSize;
  159. item.outlineWidth = newOutlineWidth;
  160. var centerAlpha = newColor.alpha;
  161. var cssColor = newColor.toCssColorString();
  162. var cssOutlineColor = newOutlineColor.toCssColorString();
  163. var textureId = JSON.stringify([cssColor, newPixelSize, cssOutlineColor, newOutlineWidth]);
  164. billboard.setImage(textureId, createBillboardPointCallback(centerAlpha, cssColor, cssOutlineColor, newOutlineWidth, newPixelSize));
  165. }
  166. if (updateClamping) {
  167. billboard._updateClamping();
  168. }
  169. }
  170. }
  171. return true;
  172. };
  173. /**
  174. * Computes a bounding sphere which encloses the visualization produced for the specified entity.
  175. * The bounding sphere is in the fixed frame of the scene's globe.
  176. *
  177. * @param {Entity} entity The entity whose bounding sphere to compute.
  178. * @param {BoundingSphere} result The bounding sphere onto which to store the result.
  179. * @returns {BoundingSphereState} BoundingSphereState.DONE if the result contains the bounding sphere,
  180. * BoundingSphereState.PENDING if the result is still being computed, or
  181. * BoundingSphereState.FAILED if the entity has no visualization in the current scene.
  182. * @private
  183. */
  184. PointVisualizer.prototype.getBoundingSphere = function(entity, result) {
  185. //>>includeStart('debug', pragmas.debug);
  186. if (!defined(entity)) {
  187. throw new DeveloperError('entity is required.');
  188. }
  189. if (!defined(result)) {
  190. throw new DeveloperError('result is required.');
  191. }
  192. //>>includeEnd('debug');
  193. var item = this._items.get(entity.id);
  194. if (!defined(item) || !(defined(item.pointPrimitive) || defined(item.billboard))) {
  195. return BoundingSphereState.FAILED;
  196. }
  197. if (defined(item.pointPrimitive)) {
  198. result.center = Cartesian3.clone(item.pointPrimitive.position, result.center);
  199. } else {
  200. var billboard = item.billboard;
  201. if (!defined(billboard._clampedPosition)) {
  202. return BoundingSphereState.PENDING;
  203. }
  204. result.center = Cartesian3.clone(billboard._clampedPosition, result.center);
  205. }
  206. result.radius = 0;
  207. return BoundingSphereState.DONE;
  208. };
  209. /**
  210. * Returns true if this object was destroyed; otherwise, false.
  211. *
  212. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  213. */
  214. PointVisualizer.prototype.isDestroyed = function() {
  215. return false;
  216. };
  217. /**
  218. * Removes and destroys all primitives created by this instance.
  219. */
  220. PointVisualizer.prototype.destroy = function() {
  221. this._entityCollection.collectionChanged.removeEventListener(PointVisualizer.prototype._onCollectionChanged, this);
  222. var entities = this._entityCollection.values;
  223. for (var i = 0; i < entities.length; i++) {
  224. this._cluster.removePoint(entities[i]);
  225. }
  226. return destroyObject(this);
  227. };
  228. PointVisualizer.prototype._onCollectionChanged = function(entityCollection, added, removed, changed) {
  229. var i;
  230. var entity;
  231. var items = this._items;
  232. var cluster = this._cluster;
  233. for (i = added.length - 1; i > -1; i--) {
  234. entity = added[i];
  235. if (defined(entity._point) && defined(entity._position)) {
  236. items.set(entity.id, new EntityData(entity));
  237. }
  238. }
  239. for (i = changed.length - 1; i > -1; i--) {
  240. entity = changed[i];
  241. if (defined(entity._point) && defined(entity._position)) {
  242. if (!items.contains(entity.id)) {
  243. items.set(entity.id, new EntityData(entity));
  244. }
  245. } else {
  246. returnPrimitive(items.get(entity.id), entity, cluster);
  247. items.remove(entity.id);
  248. }
  249. }
  250. for (i = removed.length - 1; i > -1; i--) {
  251. entity = removed[i];
  252. returnPrimitive(items.get(entity.id), entity, cluster);
  253. items.remove(entity.id);
  254. }
  255. };
  256. function returnPrimitive(item, entity, cluster) {
  257. if (defined(item)) {
  258. var pointPrimitive = item.pointPrimitive;
  259. if (defined(pointPrimitive)) {
  260. item.pointPrimitive = undefined;
  261. cluster.removePoint(entity);
  262. return;
  263. }
  264. var billboard = item.billboard;
  265. if (defined(billboard)) {
  266. item.billboard = undefined;
  267. cluster.removeBillboard(entity);
  268. }
  269. }
  270. }
  271. export default PointVisualizer;