StaticGroundPolylinePerMaterialBatch.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. import AssociativeArray from '../Core/AssociativeArray.js';
  2. import Color from '../Core/Color.js';
  3. import ColorGeometryInstanceAttribute from '../Core/ColorGeometryInstanceAttribute.js';
  4. import defaultValue from '../Core/defaultValue.js';
  5. import defined from '../Core/defined.js';
  6. import DistanceDisplayCondition from '../Core/DistanceDisplayCondition.js';
  7. import DistanceDisplayConditionGeometryInstanceAttribute from '../Core/DistanceDisplayConditionGeometryInstanceAttribute.js';
  8. import ShowGeometryInstanceAttribute from '../Core/ShowGeometryInstanceAttribute.js';
  9. import GroundPolylinePrimitive from '../Scene/GroundPolylinePrimitive.js';
  10. import PolylineColorAppearance from '../Scene/PolylineColorAppearance.js';
  11. import PolylineMaterialAppearance from '../Scene/PolylineMaterialAppearance.js';
  12. import BoundingSphereState from './BoundingSphereState.js';
  13. import ColorMaterialProperty from './ColorMaterialProperty.js';
  14. import MaterialProperty from './MaterialProperty.js';
  15. import Property from './Property.js';
  16. var scratchColor = new Color();
  17. var distanceDisplayConditionScratch = new DistanceDisplayCondition();
  18. var defaultDistanceDisplayCondition = new DistanceDisplayCondition();
  19. // Encapsulates a Primitive and all the entities that it represents.
  20. function Batch(orderedGroundPrimitives, classificationType, materialProperty, zIndex, asynchronous) {
  21. var appearanceType;
  22. if (materialProperty instanceof ColorMaterialProperty) {
  23. appearanceType = PolylineColorAppearance;
  24. } else {
  25. appearanceType = PolylineMaterialAppearance;
  26. }
  27. this.orderedGroundPrimitives = orderedGroundPrimitives; // scene level primitive collection
  28. this.classificationType = classificationType;
  29. this.appearanceType = appearanceType;
  30. this.materialProperty = materialProperty;
  31. this.updaters = new AssociativeArray();
  32. this.createPrimitive = true;
  33. this.primitive = undefined; // a GroundPolylinePrimitive encapsulating all the entities
  34. this.oldPrimitive = undefined;
  35. this.geometry = new AssociativeArray();
  36. this.material = undefined;
  37. this.updatersWithAttributes = new AssociativeArray();
  38. this.attributes = new AssociativeArray();
  39. this.invalidated = false;
  40. this.removeMaterialSubscription = materialProperty.definitionChanged.addEventListener(Batch.prototype.onMaterialChanged, this);
  41. this.subscriptions = new AssociativeArray();
  42. this.showsUpdated = new AssociativeArray();
  43. this.zIndex = zIndex;
  44. this._asynchronous = asynchronous;
  45. }
  46. Batch.prototype.onMaterialChanged = function() {
  47. this.invalidated = true;
  48. };
  49. // Check if the given updater's material is compatible with this batch
  50. Batch.prototype.isMaterial = function(updater) {
  51. var material = this.materialProperty;
  52. var updaterMaterial = updater.fillMaterialProperty;
  53. if (updaterMaterial === material ||
  54. (updaterMaterial instanceof ColorMaterialProperty && material instanceof ColorMaterialProperty)) {
  55. return true;
  56. }
  57. return defined(material) && material.equals(updaterMaterial);
  58. };
  59. Batch.prototype.add = function(time, updater, geometryInstance) {
  60. var id = updater.id;
  61. this.updaters.set(id, updater);
  62. this.geometry.set(id, geometryInstance);
  63. // Updaters with dynamic attributes must be tracked separately, may exit the batch
  64. if (!updater.hasConstantFill || !updater.fillMaterialProperty.isConstant || !Property.isConstant(updater.distanceDisplayConditionProperty)) {
  65. this.updatersWithAttributes.set(id, updater);
  66. } else {
  67. var that = this;
  68. // Listen for show changes. These will be synchronized in updateShows.
  69. this.subscriptions.set(id, updater.entity.definitionChanged.addEventListener(function(entity, propertyName, newValue, oldValue) {
  70. if (propertyName === 'isShowing') {
  71. that.showsUpdated.set(updater.id, updater);
  72. }
  73. }));
  74. }
  75. this.createPrimitive = true;
  76. };
  77. Batch.prototype.remove = function(updater) {
  78. var id = updater.id;
  79. this.createPrimitive = this.geometry.remove(id) || this.createPrimitive;
  80. if (this.updaters.remove(id)) {
  81. this.updatersWithAttributes.remove(id);
  82. var unsubscribe = this.subscriptions.get(id);
  83. if (defined(unsubscribe)) {
  84. unsubscribe();
  85. this.subscriptions.remove(id);
  86. }
  87. return true;
  88. }
  89. return false;
  90. };
  91. Batch.prototype.update = function(time) {
  92. var isUpdated = true;
  93. var primitive = this.primitive;
  94. var orderedGroundPrimitives = this.orderedGroundPrimitives;
  95. var geometries = this.geometry.values;
  96. var i;
  97. if (this.createPrimitive) {
  98. var geometriesLength = geometries.length;
  99. if (geometriesLength > 0) {
  100. if (defined(primitive)) {
  101. // Keep a handle to the old primitive so it can be removed when the updated version is ready.
  102. if (!defined(this.oldPrimitive)) {
  103. this.oldPrimitive = primitive;
  104. } else {
  105. // For if the new primitive changes again before it is ready.
  106. orderedGroundPrimitives.remove(primitive);
  107. }
  108. }
  109. primitive = new GroundPolylinePrimitive({
  110. show : false,
  111. asynchronous : this._asynchronous,
  112. geometryInstances : geometries,
  113. appearance : new this.appearanceType(),
  114. classificationType : this.classificationType
  115. });
  116. if (this.appearanceType === PolylineMaterialAppearance) {
  117. this.material = MaterialProperty.getValue(time, this.materialProperty, this.material);
  118. primitive.appearance.material = this.material;
  119. }
  120. orderedGroundPrimitives.add(primitive, this.zIndex);
  121. isUpdated = false;
  122. } else {
  123. if (defined(primitive)) {
  124. orderedGroundPrimitives.remove(primitive);
  125. primitive = undefined;
  126. }
  127. var oldPrimitive = this.oldPrimitive;
  128. if (defined(oldPrimitive)) {
  129. orderedGroundPrimitives.remove(oldPrimitive);
  130. this.oldPrimitive = undefined;
  131. }
  132. }
  133. this.attributes.removeAll();
  134. this.primitive = primitive;
  135. this.createPrimitive = false;
  136. } else if (defined(primitive) && primitive.ready) {
  137. primitive.show = true;
  138. if (defined(this.oldPrimitive)) {
  139. orderedGroundPrimitives.remove(this.oldPrimitive);
  140. this.oldPrimitive = undefined;
  141. }
  142. if (this.appearanceType === PolylineMaterialAppearance) {
  143. this.material = MaterialProperty.getValue(time, this.materialProperty, this.material);
  144. this.primitive.appearance.material = this.material;
  145. }
  146. var updatersWithAttributes = this.updatersWithAttributes.values;
  147. var length = updatersWithAttributes.length;
  148. for (i = 0; i < length; i++) {
  149. var updater = updatersWithAttributes[i];
  150. var entity = updater.entity;
  151. var instance = this.geometry.get(updater.id);
  152. var attributes = this.attributes.get(instance.id.id);
  153. if (!defined(attributes)) {
  154. attributes = primitive.getGeometryInstanceAttributes(instance.id);
  155. this.attributes.set(instance.id.id, attributes);
  156. }
  157. if (!updater.fillMaterialProperty.isConstant) {
  158. var colorProperty = updater.fillMaterialProperty.color;
  159. var resultColor = Property.getValueOrDefault(colorProperty, time, Color.WHITE, scratchColor);
  160. if (!Color.equals(attributes._lastColor, resultColor)) {
  161. attributes._lastColor = Color.clone(resultColor, attributes._lastColor);
  162. attributes.color = ColorGeometryInstanceAttribute.toValue(resultColor, attributes.color);
  163. }
  164. }
  165. var show = entity.isShowing && (updater.hasConstantFill || updater.isFilled(time));
  166. var currentShow = attributes.show[0] === 1;
  167. if (show !== currentShow) {
  168. attributes.show = ShowGeometryInstanceAttribute.toValue(show, attributes.show);
  169. }
  170. var distanceDisplayConditionProperty = updater.distanceDisplayConditionProperty;
  171. if (!Property.isConstant(distanceDisplayConditionProperty)) {
  172. var distanceDisplayCondition = Property.getValueOrDefault(distanceDisplayConditionProperty, time, defaultDistanceDisplayCondition, distanceDisplayConditionScratch);
  173. if (!DistanceDisplayCondition.equals(distanceDisplayCondition, attributes._lastDistanceDisplayCondition)) {
  174. attributes._lastDistanceDisplayCondition = DistanceDisplayCondition.clone(distanceDisplayCondition, attributes._lastDistanceDisplayCondition);
  175. attributes.distanceDisplayCondition = DistanceDisplayConditionGeometryInstanceAttribute.toValue(distanceDisplayCondition, attributes.distanceDisplayCondition);
  176. }
  177. }
  178. }
  179. this.updateShows(primitive);
  180. } else if (defined(primitive) && !primitive.ready) {
  181. isUpdated = false;
  182. }
  183. return isUpdated;
  184. };
  185. Batch.prototype.updateShows = function(primitive) {
  186. var showsUpdated = this.showsUpdated.values;
  187. var length = showsUpdated.length;
  188. for (var i = 0; i < length; i++) {
  189. var updater = showsUpdated[i];
  190. var entity = updater.entity;
  191. var instance = this.geometry.get(updater.id);
  192. var attributes = this.attributes.get(instance.id.id);
  193. if (!defined(attributes)) {
  194. attributes = primitive.getGeometryInstanceAttributes(instance.id);
  195. this.attributes.set(instance.id.id, attributes);
  196. }
  197. var show = entity.isShowing;
  198. var currentShow = attributes.show[0] === 1;
  199. if (show !== currentShow) {
  200. attributes.show = ShowGeometryInstanceAttribute.toValue(show, attributes.show);
  201. instance.attributes.show.value[0] = attributes.show[0];
  202. }
  203. }
  204. this.showsUpdated.removeAll();
  205. };
  206. Batch.prototype.contains = function(updater) {
  207. return this.updaters.contains(updater.id);
  208. };
  209. Batch.prototype.getBoundingSphere = function(updater, result) {
  210. var primitive = this.primitive;
  211. if (!primitive.ready) {
  212. return BoundingSphereState.PENDING;
  213. }
  214. var attributes = primitive.getGeometryInstanceAttributes(updater.entity);
  215. if (!defined(attributes) || !defined(attributes.boundingSphere) ||
  216. (defined(attributes.show) && attributes.show[0] === 0)) {
  217. return BoundingSphereState.FAILED;
  218. }
  219. attributes.boundingSphere.clone(result);
  220. return BoundingSphereState.DONE;
  221. };
  222. Batch.prototype.destroy = function() {
  223. var primitive = this.primitive;
  224. var orderedGroundPrimitives = this.orderedGroundPrimitives;
  225. if (defined(primitive)) {
  226. orderedGroundPrimitives.remove(primitive);
  227. }
  228. var oldPrimitive = this.oldPrimitive;
  229. if (defined(oldPrimitive)) {
  230. orderedGroundPrimitives.remove(oldPrimitive);
  231. }
  232. this.removeMaterialSubscription();
  233. };
  234. /**
  235. * @private
  236. */
  237. function StaticGroundPolylinePerMaterialBatch(orderedGroundPrimitives, classificationType, asynchronous) {
  238. this._items = [];
  239. this._orderedGroundPrimitives = orderedGroundPrimitives;
  240. this._classificationType = classificationType;
  241. this._asynchronous = defaultValue(asynchronous, true);
  242. }
  243. StaticGroundPolylinePerMaterialBatch.prototype.add = function(time, updater) {
  244. var items = this._items;
  245. var length = items.length;
  246. var geometryInstance = updater.createFillGeometryInstance(time);
  247. var zIndex = Property.getValueOrDefault(updater.zIndex, 0);
  248. // Check if the Entity represented by the updater has the same material or a material representable with per-instance color.
  249. for (var i = 0; i < length; ++i) {
  250. var item = items[i];
  251. if (item.isMaterial(updater) &&
  252. item.zIndex === zIndex) {
  253. item.add(time, updater, geometryInstance);
  254. return;
  255. }
  256. }
  257. // If a compatible batch wasn't found, create a new batch.
  258. var batch = new Batch(this._orderedGroundPrimitives, this._classificationType, updater.fillMaterialProperty, zIndex, this._asynchronous);
  259. batch.add(time, updater, geometryInstance);
  260. items.push(batch);
  261. };
  262. StaticGroundPolylinePerMaterialBatch.prototype.remove = function(updater) {
  263. var items = this._items;
  264. var length = items.length;
  265. for (var i = length - 1; i >= 0; i--) {
  266. var item = items[i];
  267. if (item.remove(updater)) {
  268. if (item.updaters.length === 0) {
  269. items.splice(i, 1);
  270. item.destroy();
  271. }
  272. break;
  273. }
  274. }
  275. };
  276. StaticGroundPolylinePerMaterialBatch.prototype.update = function(time) {
  277. var i;
  278. var items = this._items;
  279. var length = items.length;
  280. for (i = length - 1; i >= 0; i--) {
  281. var item = items[i];
  282. if (item.invalidated) {
  283. items.splice(i, 1);
  284. var updaters = item.updaters.values;
  285. var updatersLength = updaters.length;
  286. for (var h = 0; h < updatersLength; h++) {
  287. this.add(time, updaters[h]);
  288. }
  289. item.destroy();
  290. }
  291. }
  292. var isUpdated = true;
  293. for (i = 0; i < items.length; i++) {
  294. isUpdated = items[i].update(time) && isUpdated;
  295. }
  296. return isUpdated;
  297. };
  298. StaticGroundPolylinePerMaterialBatch.prototype.getBoundingSphere = function(updater, result) {
  299. var items = this._items;
  300. var length = items.length;
  301. for (var i = 0; i < length; i++) {
  302. var item = items[i];
  303. if (item.contains(updater)){
  304. return item.getBoundingSphere(updater, result);
  305. }
  306. }
  307. return BoundingSphereState.FAILED;
  308. };
  309. StaticGroundPolylinePerMaterialBatch.prototype.removeAllPrimitives = function() {
  310. var items = this._items;
  311. var length = items.length;
  312. for (var i = 0; i < length; i++) {
  313. items[i].destroy();
  314. }
  315. this._items.length = 0;
  316. };
  317. export default StaticGroundPolylinePerMaterialBatch;