PointVisualizer.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*global define*/
  2. define([
  3. '../Core/AssociativeArray',
  4. '../Core/Cartesian3',
  5. '../Core/Color',
  6. '../Core/defined',
  7. '../Core/destroyObject',
  8. '../Core/DeveloperError',
  9. '../Core/NearFarScalar',
  10. '../Scene/BillboardCollection',
  11. './Property'
  12. ], function(
  13. AssociativeArray,
  14. Cartesian3,
  15. Color,
  16. defined,
  17. destroyObject,
  18. DeveloperError,
  19. NearFarScalar,
  20. BillboardCollection,
  21. Property) {
  22. "use strict";
  23. var defaultColor = Color.WHITE;
  24. var defaultOutlineColor = Color.BLACK;
  25. var defaultOutlineWidth = 0.0;
  26. var defaultPixelSize = 1.0;
  27. var color = new Color();
  28. var position = new Cartesian3();
  29. var outlineColor = new Color();
  30. var scaleByDistance = new NearFarScalar();
  31. var EntityData = function(entity) {
  32. this.entity = entity;
  33. this.billboard = undefined;
  34. this.color = undefined;
  35. this.outlineColor = undefined;
  36. this.pixelSize = undefined;
  37. this.outlineWidth = undefined;
  38. };
  39. /**
  40. * A {@link Visualizer} which maps {@link Entity#point} to a {@link Billboard}.
  41. * @alias PointVisualizer
  42. * @constructor
  43. *
  44. * @param {Scene} scene The scene the primitives will be rendered in.
  45. * @param {EntityCollection} entityCollection The entityCollection to visualize.
  46. */
  47. var PointVisualizer = function(scene, entityCollection) {
  48. //>>includeStart('debug', pragmas.debug);
  49. if (!defined(scene)) {
  50. throw new DeveloperError('scene is required.');
  51. }
  52. if (!defined(entityCollection)) {
  53. throw new DeveloperError('entityCollection is required.');
  54. }
  55. //>>includeEnd('debug');
  56. entityCollection.collectionChanged.addEventListener(PointVisualizer.prototype._onCollectionChanged, this);
  57. this._scene = scene;
  58. this._unusedIndexes = [];
  59. this._entityCollection = entityCollection;
  60. this._billboardCollection = undefined;
  61. this._items = new AssociativeArray();
  62. this._onCollectionChanged(entityCollection, entityCollection.entities, [], []);
  63. };
  64. /**
  65. * Updates the primitives created by this visualizer to match their
  66. * Entity counterpart at the given time.
  67. *
  68. * @param {JulianDate} time The time to update to.
  69. * @returns {Boolean} This function always returns true.
  70. */
  71. PointVisualizer.prototype.update = function(time) {
  72. //>>includeStart('debug', pragmas.debug);
  73. if (!defined(time)) {
  74. throw new DeveloperError('time is required.');
  75. }
  76. //>>includeEnd('debug');
  77. var items = this._items.values;
  78. var unusedIndexes = this._unusedIndexes;
  79. for (var i = 0, len = items.length; i < len; i++) {
  80. var item = items[i];
  81. var entity = item.entity;
  82. var pointGraphics = entity._point;
  83. var billboard = item.billboard;
  84. var show = entity.isAvailable(time) && Property.getValueOrDefault(pointGraphics._show, time, true);
  85. if (show) {
  86. position = Property.getValueOrUndefined(entity._position, time, position);
  87. show = defined(position);
  88. }
  89. if (!show) {
  90. returnBillboard(item, unusedIndexes);
  91. continue;
  92. }
  93. var init = false;
  94. var needRedraw = false;
  95. if (!defined(billboard)) {
  96. init = true;
  97. var billboardCollection = this._billboardCollection;
  98. if (!defined(billboardCollection)) {
  99. billboardCollection = new BillboardCollection();
  100. this._billboardCollection = billboardCollection;
  101. this._scene.primitives.add(billboardCollection);
  102. }
  103. var length = unusedIndexes.length;
  104. if (length > 0) {
  105. billboard = billboardCollection.get(unusedIndexes.pop());
  106. } else {
  107. billboard = billboardCollection.add();
  108. }
  109. billboard.id = entity;
  110. billboard.image = undefined;
  111. item.billboard = billboard;
  112. needRedraw = true;
  113. }
  114. billboard.show = true;
  115. billboard.position = position;
  116. billboard.scaleByDistance = Property.getValueOrUndefined(pointGraphics._scaleByDistance, time, scaleByDistance);
  117. var colorProperty = pointGraphics._color;
  118. var outlineColorProperty = pointGraphics._outlineColor;
  119. var newColor = init || !Property.isConstant(colorProperty) ? Property.getValueOrDefault(colorProperty, time, defaultColor, color) : item.color;
  120. var newOutlineColor = init || !Property.isConstant(outlineColorProperty) ? Property.getValueOrDefault(outlineColorProperty, time, defaultOutlineColor, outlineColor) : item.outlineColor;
  121. var newOutlineWidth = Math.round(Property.getValueOrDefault(pointGraphics._outlineWidth, time, defaultOutlineWidth));
  122. var newPixelSize = Math.max(1, Math.round(Property.getValueOrDefault(pointGraphics._pixelSize, time, defaultPixelSize)));
  123. if (newOutlineWidth > 0) {
  124. billboard.scale = 1.0;
  125. needRedraw = needRedraw || //
  126. newOutlineWidth !== item.outlineWidth || //
  127. newPixelSize !== item.pixelSize || //
  128. !Color.equals(newColor, item.color) || //
  129. !Color.equals(newOutlineColor, item.outlineColor);
  130. } else {
  131. billboard.scale = newPixelSize / 50.0;
  132. newPixelSize = 50.0;
  133. needRedraw = needRedraw || //
  134. newOutlineWidth !== item.outlineWidth || //
  135. !Color.equals(newColor, item.color) || //
  136. !Color.equals(newOutlineColor, item.outlineColor);
  137. }
  138. if (needRedraw) {
  139. item.color = Color.clone(newColor, item.color);
  140. item.outlineColor = Color.clone(newOutlineColor, item.outlineColor);
  141. item.pixelSize = newPixelSize;
  142. item.outlineWidth = newOutlineWidth;
  143. var centerAlpha = newColor.alpha;
  144. var cssColor = newColor.toCssColorString();
  145. var cssOutlineColor = newOutlineColor.toCssColorString();
  146. var textureId = JSON.stringify([cssColor, newPixelSize, cssOutlineColor, newOutlineWidth]);
  147. billboard.setImage(textureId, createCallback(centerAlpha, cssColor, cssOutlineColor, newOutlineWidth, newPixelSize));
  148. }
  149. }
  150. return true;
  151. };
  152. /**
  153. * Returns true if this object was destroyed; otherwise, false.
  154. *
  155. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  156. */
  157. PointVisualizer.prototype.isDestroyed = function() {
  158. return false;
  159. };
  160. /**
  161. * Removes and destroys all primitives created by this instance.
  162. */
  163. PointVisualizer.prototype.destroy = function() {
  164. this._entityCollection.collectionChanged.removeEventListener(PointVisualizer.prototype._onCollectionChanged, this);
  165. if (defined(this._billboardCollection)) {
  166. this._scene.primitives.remove(this._billboardCollection);
  167. }
  168. return destroyObject(this);
  169. };
  170. PointVisualizer.prototype._onCollectionChanged = function(entityCollection, added, removed, changed) {
  171. var i;
  172. var entity;
  173. var unusedIndexes = this._unusedIndexes;
  174. var items = this._items;
  175. for (i = added.length - 1; i > -1; i--) {
  176. entity = added[i];
  177. if (defined(entity._point) && defined(entity._position)) {
  178. items.set(entity.id, new EntityData(entity));
  179. }
  180. }
  181. for (i = changed.length - 1; i > -1; i--) {
  182. entity = changed[i];
  183. if (defined(entity._point) && defined(entity._position)) {
  184. if (!items.contains(entity.id)) {
  185. items.set(entity.id, new EntityData(entity));
  186. }
  187. } else {
  188. returnBillboard(items.get(entity.id), unusedIndexes);
  189. items.remove(entity.id);
  190. }
  191. }
  192. for (i = removed.length - 1; i > -1; i--) {
  193. entity = removed[i];
  194. returnBillboard(items.get(entity.id), unusedIndexes);
  195. items.remove(entity.id);
  196. }
  197. };
  198. function returnBillboard(item, unusedIndexes) {
  199. if (defined(item)) {
  200. var billboard = item.billboard;
  201. if (defined(billboard)) {
  202. item.billboard = undefined;
  203. billboard.show = false;
  204. billboard.image = undefined;
  205. unusedIndexes.push(billboard._index);
  206. }
  207. }
  208. }
  209. function createCallback(centerAlpha, cssColor, cssOutlineColor, cssOutlineWidth, newPixelSize) {
  210. return function(id) {
  211. var canvas = document.createElement('canvas');
  212. var length = newPixelSize + (2 * cssOutlineWidth);
  213. canvas.height = canvas.width = length;
  214. var context2D = canvas.getContext('2d');
  215. context2D.clearRect(0, 0, length, length);
  216. if (cssOutlineWidth !== 0) {
  217. context2D.beginPath();
  218. context2D.arc(length / 2, length / 2, length / 2, 0, 2 * Math.PI, true);
  219. context2D.closePath();
  220. context2D.fillStyle = cssOutlineColor;
  221. context2D.fill();
  222. // Punch a hole in the center if needed.
  223. if (centerAlpha < 1.0) {
  224. context2D.save();
  225. context2D.globalCompositeOperation = 'destination-out';
  226. context2D.beginPath();
  227. context2D.arc(length / 2, length / 2, newPixelSize / 2, 0, 2 * Math.PI, true);
  228. context2D.closePath();
  229. context2D.fillStyle = 'black';
  230. context2D.fill();
  231. context2D.restore();
  232. }
  233. }
  234. context2D.beginPath();
  235. context2D.arc(length / 2, length / 2, newPixelSize / 2, 0, 2 * Math.PI, true);
  236. context2D.closePath();
  237. context2D.fillStyle = cssColor;
  238. context2D.fill();
  239. return canvas;
  240. };
  241. }
  242. return PointVisualizer;
  243. });