Source: DataSources/BillboardVisualizer.js

  1. /*global define*/
  2. define([
  3. '../Core/AssociativeArray',
  4. '../Core/BoundingRectangle',
  5. '../Core/Cartesian2',
  6. '../Core/Cartesian3',
  7. '../Core/Color',
  8. '../Core/defaultValue',
  9. '../Core/defined',
  10. '../Core/destroyObject',
  11. '../Core/DeveloperError',
  12. '../Core/DistanceDisplayCondition',
  13. '../Core/NearFarScalar',
  14. '../Scene/HeightReference',
  15. '../Scene/HorizontalOrigin',
  16. '../Scene/VerticalOrigin',
  17. './BoundingSphereState',
  18. './EntityCluster',
  19. './Property'
  20. ], function(
  21. AssociativeArray,
  22. BoundingRectangle,
  23. Cartesian2,
  24. Cartesian3,
  25. Color,
  26. defaultValue,
  27. defined,
  28. destroyObject,
  29. DeveloperError,
  30. DistanceDisplayCondition,
  31. NearFarScalar,
  32. HeightReference,
  33. HorizontalOrigin,
  34. VerticalOrigin,
  35. BoundingSphereState,
  36. EntityCluster,
  37. Property) {
  38. 'use strict';
  39. var defaultColor = Color.WHITE;
  40. var defaultEyeOffset = Cartesian3.ZERO;
  41. var defaultHeightReference = HeightReference.NONE;
  42. var defaultPixelOffset = Cartesian2.ZERO;
  43. var defaultScale = 1.0;
  44. var defaultRotation = 0.0;
  45. var defaultAlignedAxis = Cartesian3.ZERO;
  46. var defaultHorizontalOrigin = HorizontalOrigin.CENTER;
  47. var defaultVerticalOrigin = VerticalOrigin.CENTER;
  48. var defaultSizeInMeters = false;
  49. var position = new Cartesian3();
  50. var color = new Color();
  51. var eyeOffset = new Cartesian3();
  52. var pixelOffset = new Cartesian2();
  53. var scaleByDistance = new NearFarScalar();
  54. var translucencyByDistance = new NearFarScalar();
  55. var pixelOffsetScaleByDistance = new NearFarScalar();
  56. var boundingRectangle = new BoundingRectangle();
  57. var distanceDisplayCondition = new DistanceDisplayCondition();
  58. function EntityData(entity) {
  59. this.entity = entity;
  60. this.billboard = undefined;
  61. this.textureValue = undefined;
  62. }
  63. /**
  64. * A {@link Visualizer} which maps {@link Entity#billboard} to a {@link Billboard}.
  65. * @alias BillboardVisualizer
  66. * @constructor
  67. *
  68. * @param {EntityCluster} entityCluster The entity cluster to manage the collection of billboards and optionally cluster with other entities.
  69. * @param {EntityCollection} entityCollection The entityCollection to visualize.
  70. */
  71. function BillboardVisualizer(entityCluster, entityCollection) {
  72. //>>includeStart('debug', pragmas.debug);
  73. if (!defined(entityCluster)) {
  74. throw new DeveloperError('entityCluster is required.');
  75. }
  76. if (!defined(entityCollection)) {
  77. throw new DeveloperError('entityCollection is required.');
  78. }
  79. //>>includeEnd('debug');
  80. entityCollection.collectionChanged.addEventListener(BillboardVisualizer.prototype._onCollectionChanged, this);
  81. this._cluster = entityCluster;
  82. this._entityCollection = entityCollection;
  83. this._items = new AssociativeArray();
  84. this._onCollectionChanged(entityCollection, entityCollection.values, [], []);
  85. }
  86. /**
  87. * Updates the primitives created by this visualizer to match their
  88. * Entity counterpart at the given time.
  89. *
  90. * @param {JulianDate} time The time to update to.
  91. * @returns {Boolean} This function always returns true.
  92. */
  93. BillboardVisualizer.prototype.update = function(time) {
  94. //>>includeStart('debug', pragmas.debug);
  95. if (!defined(time)) {
  96. throw new DeveloperError('time is required.');
  97. }
  98. //>>includeEnd('debug');
  99. var items = this._items.values;
  100. var cluster = this._cluster;
  101. for (var i = 0, len = items.length; i < len; i++) {
  102. var item = items[i];
  103. var entity = item.entity;
  104. var billboardGraphics = entity._billboard;
  105. var textureValue;
  106. var billboard = item.billboard;
  107. var show = entity.isShowing && entity.isAvailable(time) && Property.getValueOrDefault(billboardGraphics._show, time, true);
  108. if (show) {
  109. position = Property.getValueOrUndefined(entity._position, time, position);
  110. textureValue = Property.getValueOrUndefined(billboardGraphics._image, time);
  111. show = defined(position) && defined(textureValue);
  112. }
  113. if (!show) {
  114. //don't bother creating or updating anything else
  115. returnPrimitive(item, entity, cluster);
  116. continue;
  117. }
  118. if (!Property.isConstant(entity._position)) {
  119. cluster._clusterDirty = true;
  120. }
  121. if (!defined(billboard)) {
  122. billboard = cluster.getBillboard(entity);
  123. billboard.id = entity;
  124. billboard.image = undefined;
  125. item.billboard = billboard;
  126. }
  127. billboard.show = show;
  128. if (!defined(billboard.image) || item.textureValue !== textureValue) {
  129. billboard.image = textureValue;
  130. item.textureValue = textureValue;
  131. }
  132. billboard.position = position;
  133. billboard.color = Property.getValueOrDefault(billboardGraphics._color, time, defaultColor, color);
  134. billboard.eyeOffset = Property.getValueOrDefault(billboardGraphics._eyeOffset, time, defaultEyeOffset, eyeOffset);
  135. billboard.heightReference = Property.getValueOrDefault(billboardGraphics._heightReference, time, defaultHeightReference);
  136. billboard.pixelOffset = Property.getValueOrDefault(billboardGraphics._pixelOffset, time, defaultPixelOffset, pixelOffset);
  137. billboard.scale = Property.getValueOrDefault(billboardGraphics._scale, time, defaultScale);
  138. billboard.rotation = Property.getValueOrDefault(billboardGraphics._rotation, time, defaultRotation);
  139. billboard.alignedAxis = Property.getValueOrDefault(billboardGraphics._alignedAxis, time, defaultAlignedAxis);
  140. billboard.horizontalOrigin = Property.getValueOrDefault(billboardGraphics._horizontalOrigin, time, defaultHorizontalOrigin);
  141. billboard.verticalOrigin = Property.getValueOrDefault(billboardGraphics._verticalOrigin, time, defaultVerticalOrigin);
  142. billboard.width = Property.getValueOrUndefined(billboardGraphics._width, time);
  143. billboard.height = Property.getValueOrUndefined(billboardGraphics._height, time);
  144. billboard.scaleByDistance = Property.getValueOrUndefined(billboardGraphics._scaleByDistance, time, scaleByDistance);
  145. billboard.translucencyByDistance = Property.getValueOrUndefined(billboardGraphics._translucencyByDistance, time, translucencyByDistance);
  146. billboard.pixelOffsetScaleByDistance = Property.getValueOrUndefined(billboardGraphics._pixelOffsetScaleByDistance, time, pixelOffsetScaleByDistance);
  147. billboard.sizeInMeters = Property.getValueOrDefault(billboardGraphics._sizeInMeters, defaultSizeInMeters);
  148. billboard.distanceDisplayCondition = Property.getValueOrUndefined(billboardGraphics._distanceDisplayCondition, time, distanceDisplayCondition);
  149. var subRegion = Property.getValueOrUndefined(billboardGraphics._imageSubRegion, time, boundingRectangle);
  150. if (defined(subRegion)) {
  151. billboard.setImageSubRegion(billboard._imageId, subRegion);
  152. }
  153. }
  154. return true;
  155. };
  156. /**
  157. * Computes a bounding sphere which encloses the visualization produced for the specified entity.
  158. * The bounding sphere is in the fixed frame of the scene's globe.
  159. *
  160. * @param {Entity} entity The entity whose bounding sphere to compute.
  161. * @param {BoundingSphere} result The bounding sphere onto which to store the result.
  162. * @returns {BoundingSphereState} BoundingSphereState.DONE if the result contains the bounding sphere,
  163. * BoundingSphereState.PENDING if the result is still being computed, or
  164. * BoundingSphereState.FAILED if the entity has no visualization in the current scene.
  165. * @private
  166. */
  167. BillboardVisualizer.prototype.getBoundingSphere = function(entity, result) {
  168. //>>includeStart('debug', pragmas.debug);
  169. if (!defined(entity)) {
  170. throw new DeveloperError('entity is required.');
  171. }
  172. if (!defined(result)) {
  173. throw new DeveloperError('result is required.');
  174. }
  175. //>>includeEnd('debug');
  176. var item = this._items.get(entity.id);
  177. if (!defined(item) || !defined(item.billboard)) {
  178. return BoundingSphereState.FAILED;
  179. }
  180. var billboard = item.billboard;
  181. if (billboard.heightReference === HeightReference.NONE) {
  182. result.center = Cartesian3.clone(billboard.position, result.center);
  183. } else {
  184. if (!defined(billboard._clampedPosition)) {
  185. return BoundingSphereState.PENDING;
  186. }
  187. result.center = Cartesian3.clone(billboard._clampedPosition, result.center);
  188. }
  189. result.radius = 0;
  190. return BoundingSphereState.DONE;
  191. };
  192. /**
  193. * Returns true if this object was destroyed; otherwise, false.
  194. *
  195. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  196. */
  197. BillboardVisualizer.prototype.isDestroyed = function() {
  198. return false;
  199. };
  200. /**
  201. * Removes and destroys all primitives created by this instance.
  202. */
  203. BillboardVisualizer.prototype.destroy = function() {
  204. this._entityCollection.collectionChanged.removeEventListener(BillboardVisualizer.prototype._onCollectionChanged, this);
  205. var entities = this._entityCollection.values;
  206. for (var i = 0; i < entities.length; i++) {
  207. this._cluster.removeBillboard(entities[i]);
  208. }
  209. return destroyObject(this);
  210. };
  211. BillboardVisualizer.prototype._onCollectionChanged = function(entityCollection, added, removed, changed) {
  212. var i;
  213. var entity;
  214. var items = this._items;
  215. var cluster = this._cluster;
  216. for (i = added.length - 1; i > -1; i--) {
  217. entity = added[i];
  218. if (defined(entity._billboard) && defined(entity._position)) {
  219. items.set(entity.id, new EntityData(entity));
  220. }
  221. }
  222. for (i = changed.length - 1; i > -1; i--) {
  223. entity = changed[i];
  224. if (defined(entity._billboard) && defined(entity._position)) {
  225. if (!items.contains(entity.id)) {
  226. items.set(entity.id, new EntityData(entity));
  227. }
  228. } else {
  229. returnPrimitive(items.get(entity.id), entity, cluster);
  230. items.remove(entity.id);
  231. }
  232. }
  233. for (i = removed.length - 1; i > -1; i--) {
  234. entity = removed[i];
  235. returnPrimitive(items.get(entity.id), entity, cluster);
  236. items.remove(entity.id);
  237. }
  238. };
  239. function returnPrimitive(item, entity, cluster) {
  240. if (defined(item)) {
  241. item.billboard = undefined;
  242. cluster.removeBillboard(entity);
  243. }
  244. }
  245. return BillboardVisualizer;
  246. });