StaticGroundGeometryColorBatch.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import AssociativeArray from '../Core/AssociativeArray.js';
  2. import Color from '../Core/Color.js';
  3. import defined from '../Core/defined.js';
  4. import DistanceDisplayCondition from '../Core/DistanceDisplayCondition.js';
  5. import DistanceDisplayConditionGeometryInstanceAttribute from '../Core/DistanceDisplayConditionGeometryInstanceAttribute.js';
  6. import ShowGeometryInstanceAttribute from '../Core/ShowGeometryInstanceAttribute.js';
  7. import GroundPrimitive from '../Scene/GroundPrimitive.js';
  8. import BoundingSphereState from './BoundingSphereState.js';
  9. import Property from './Property.js';
  10. var colorScratch = new Color();
  11. var distanceDisplayConditionScratch = new DistanceDisplayCondition();
  12. var defaultDistanceDisplayCondition = new DistanceDisplayCondition();
  13. function Batch(primitives, classificationType, color, key, zIndex) {
  14. this.primitives = primitives;
  15. this.zIndex = zIndex;
  16. this.classificationType = classificationType;
  17. this.color = color;
  18. this.key = key;
  19. this.createPrimitive = false;
  20. this.waitingOnCreate = false;
  21. this.primitive = undefined;
  22. this.oldPrimitive = undefined;
  23. this.geometry = new AssociativeArray();
  24. this.updaters = new AssociativeArray();
  25. this.updatersWithAttributes = new AssociativeArray();
  26. this.attributes = new AssociativeArray();
  27. this.subscriptions = new AssociativeArray();
  28. this.showsUpdated = new AssociativeArray();
  29. this.itemsToRemove = [];
  30. this.isDirty = false;
  31. }
  32. Batch.prototype.add = function(updater, instance) {
  33. var id = updater.id;
  34. this.createPrimitive = true;
  35. this.geometry.set(id, instance);
  36. this.updaters.set(id, updater);
  37. if (!updater.hasConstantFill || !updater.fillMaterialProperty.isConstant || !Property.isConstant(updater.distanceDisplayConditionProperty)) {
  38. this.updatersWithAttributes.set(id, updater);
  39. } else {
  40. var that = this;
  41. this.subscriptions.set(id, updater.entity.definitionChanged.addEventListener(function(entity, propertyName, newValue, oldValue) {
  42. if (propertyName === 'isShowing') {
  43. that.showsUpdated.set(updater.id, updater);
  44. }
  45. }));
  46. }
  47. };
  48. Batch.prototype.remove = function(updater) {
  49. var id = updater.id;
  50. this.createPrimitive = this.geometry.remove(id) || this.createPrimitive;
  51. if (this.updaters.remove(id)) {
  52. this.updatersWithAttributes.remove(id);
  53. var unsubscribe = this.subscriptions.get(id);
  54. if (defined(unsubscribe)) {
  55. unsubscribe();
  56. this.subscriptions.remove(id);
  57. this.showsUpdated.remove(id);
  58. }
  59. return true;
  60. }
  61. return false;
  62. };
  63. var scratchArray = new Array(4);
  64. Batch.prototype.update = function(time) {
  65. var isUpdated = true;
  66. var removedCount = 0;
  67. var primitive = this.primitive;
  68. var primitives = this.primitives;
  69. var i;
  70. if (this.createPrimitive) {
  71. var geometries = this.geometry.values;
  72. var geometriesLength = geometries.length;
  73. if (geometriesLength > 0) {
  74. if (defined(primitive)) {
  75. if (!defined(this.oldPrimitive)) {
  76. this.oldPrimitive = primitive;
  77. } else {
  78. primitives.remove(primitive);
  79. }
  80. }
  81. primitive = new GroundPrimitive({
  82. show : false,
  83. asynchronous : true,
  84. geometryInstances : geometries,
  85. classificationType : this.classificationType
  86. });
  87. primitives.add(primitive, this.zIndex);
  88. isUpdated = false;
  89. } else {
  90. if (defined(primitive)) {
  91. primitives.remove(primitive);
  92. primitive = undefined;
  93. }
  94. var oldPrimitive = this.oldPrimitive;
  95. if (defined(oldPrimitive)) {
  96. primitives.remove(oldPrimitive);
  97. this.oldPrimitive = undefined;
  98. }
  99. }
  100. this.attributes.removeAll();
  101. this.primitive = primitive;
  102. this.createPrimitive = false;
  103. this.waitingOnCreate = true;
  104. } else if (defined(primitive) && primitive.ready) {
  105. primitive.show = true;
  106. if (defined(this.oldPrimitive)) {
  107. primitives.remove(this.oldPrimitive);
  108. this.oldPrimitive = undefined;
  109. }
  110. var updatersWithAttributes = this.updatersWithAttributes.values;
  111. var length = updatersWithAttributes.length;
  112. var waitingOnCreate = this.waitingOnCreate;
  113. for (i = 0; i < length; i++) {
  114. var updater = updatersWithAttributes[i];
  115. var instance = this.geometry.get(updater.id);
  116. var attributes = this.attributes.get(instance.id.id);
  117. if (!defined(attributes)) {
  118. attributes = primitive.getGeometryInstanceAttributes(instance.id);
  119. this.attributes.set(instance.id.id, attributes);
  120. }
  121. if (!updater.fillMaterialProperty.isConstant || waitingOnCreate) {
  122. var colorProperty = updater.fillMaterialProperty.color;
  123. var fillColor = Property.getValueOrDefault(colorProperty, time, Color.WHITE, colorScratch);
  124. if (!Color.equals(attributes._lastColor, fillColor)) {
  125. attributes._lastColor = Color.clone(fillColor, attributes._lastColor);
  126. var color = this.color;
  127. var newColor = fillColor.toBytes(scratchArray);
  128. if (color[0] !== newColor[0] || color[1] !== newColor[1] ||
  129. color[2] !== newColor[2] || color[3] !== newColor[3]) {
  130. this.itemsToRemove[removedCount++] = updater;
  131. }
  132. }
  133. }
  134. var show = updater.entity.isShowing && (updater.hasConstantFill || updater.isFilled(time));
  135. var currentShow = attributes.show[0] === 1;
  136. if (show !== currentShow) {
  137. attributes.show = ShowGeometryInstanceAttribute.toValue(show, attributes.show);
  138. }
  139. var distanceDisplayConditionProperty = updater.distanceDisplayConditionProperty;
  140. if (!Property.isConstant(distanceDisplayConditionProperty)) {
  141. var distanceDisplayCondition = Property.getValueOrDefault(distanceDisplayConditionProperty, time, defaultDistanceDisplayCondition, distanceDisplayConditionScratch);
  142. if (!DistanceDisplayCondition.equals(distanceDisplayCondition, attributes._lastDistanceDisplayCondition)) {
  143. attributes._lastDistanceDisplayCondition = DistanceDisplayCondition.clone(distanceDisplayCondition, attributes._lastDistanceDisplayCondition);
  144. attributes.distanceDisplayCondition = DistanceDisplayConditionGeometryInstanceAttribute.toValue(distanceDisplayCondition, attributes.distanceDisplayCondition);
  145. }
  146. }
  147. }
  148. this.updateShows(primitive);
  149. this.waitingOnCreate = false;
  150. } else if (defined(primitive) && !primitive.ready) {
  151. isUpdated = false;
  152. }
  153. this.itemsToRemove.length = removedCount;
  154. return isUpdated;
  155. };
  156. Batch.prototype.updateShows = function(primitive) {
  157. var showsUpdated = this.showsUpdated.values;
  158. var length = showsUpdated.length;
  159. for (var i = 0; i < length; i++) {
  160. var updater = showsUpdated[i];
  161. var instance = this.geometry.get(updater.id);
  162. var attributes = this.attributes.get(instance.id.id);
  163. if (!defined(attributes)) {
  164. attributes = primitive.getGeometryInstanceAttributes(instance.id);
  165. this.attributes.set(instance.id.id, attributes);
  166. }
  167. var show = updater.entity.isShowing;
  168. var currentShow = attributes.show[0] === 1;
  169. if (show !== currentShow) {
  170. attributes.show = ShowGeometryInstanceAttribute.toValue(show, attributes.show);
  171. instance.attributes.show.value[0] = attributes.show[0];
  172. }
  173. }
  174. this.showsUpdated.removeAll();
  175. };
  176. Batch.prototype.contains = function(updater) {
  177. return this.updaters.contains(updater.id);
  178. };
  179. Batch.prototype.getBoundingSphere = function(updater, result) {
  180. var primitive = this.primitive;
  181. if (!primitive.ready) {
  182. return BoundingSphereState.PENDING;
  183. }
  184. var bs = primitive.getBoundingSphere(updater.entity);
  185. if (!defined(bs)) {
  186. return BoundingSphereState.FAILED;
  187. }
  188. bs.clone(result);
  189. return BoundingSphereState.DONE;
  190. };
  191. Batch.prototype.removeAllPrimitives = function() {
  192. var primitives = this.primitives;
  193. var primitive = this.primitive;
  194. if (defined(primitive)) {
  195. primitives.remove(primitive);
  196. this.primitive = undefined;
  197. this.geometry.removeAll();
  198. this.updaters.removeAll();
  199. }
  200. var oldPrimitive = this.oldPrimitive;
  201. if (defined(oldPrimitive)) {
  202. primitives.remove(oldPrimitive);
  203. this.oldPrimitive = undefined;
  204. }
  205. };
  206. /**
  207. * @private
  208. */
  209. function StaticGroundGeometryColorBatch(primitives, classificationType) {
  210. this._batches = new AssociativeArray();
  211. this._primitives = primitives;
  212. this._classificationType = classificationType;
  213. }
  214. StaticGroundGeometryColorBatch.prototype.add = function(time, updater) {
  215. var instance = updater.createFillGeometryInstance(time);
  216. var batches = this._batches;
  217. // color and zIndex are batch breakers, so we'll use that for the key
  218. var zIndex = Property.getValueOrDefault(updater.zIndex, 0);
  219. var batchKey = new Uint32Array(instance.attributes.color.value.buffer)[0] + ':' + zIndex;
  220. var batch;
  221. if (batches.contains(batchKey)) {
  222. batch = batches.get(batchKey);
  223. } else {
  224. batch = new Batch(this._primitives, this._classificationType, instance.attributes.color.value, batchKey, zIndex);
  225. batches.set(batchKey, batch);
  226. }
  227. batch.add(updater, instance);
  228. return batch;
  229. };
  230. StaticGroundGeometryColorBatch.prototype.remove = function(updater) {
  231. var batchesArray = this._batches.values;
  232. var count = batchesArray.length;
  233. for (var i = 0; i < count; ++i) {
  234. if (batchesArray[i].remove(updater)) {
  235. return;
  236. }
  237. }
  238. };
  239. StaticGroundGeometryColorBatch.prototype.update = function(time) {
  240. var i;
  241. var updater;
  242. //Perform initial update
  243. var isUpdated = true;
  244. var batches = this._batches;
  245. var batchesArray = batches.values;
  246. var batchCount = batchesArray.length;
  247. for (i = 0; i < batchCount; ++i) {
  248. isUpdated = batchesArray[i].update(time) && isUpdated;
  249. }
  250. //If any items swapped between batches we need to move them
  251. for (i = 0; i < batchCount; ++i) {
  252. var oldBatch = batchesArray[i];
  253. var itemsToRemove = oldBatch.itemsToRemove;
  254. var itemsToMoveLength = itemsToRemove.length;
  255. for (var j = 0; j < itemsToMoveLength; j++) {
  256. updater = itemsToRemove[j];
  257. oldBatch.remove(updater);
  258. var newBatch = this.add(time, updater);
  259. oldBatch.isDirty = true;
  260. newBatch.isDirty = true;
  261. }
  262. }
  263. //If we moved anything around, we need to re-build the primitive and remove empty batches
  264. var batchesArrayCopy = batchesArray.slice();
  265. var batchesCopyCount = batchesArrayCopy.length;
  266. for (i = 0; i < batchesCopyCount; ++i) {
  267. var batch = batchesArrayCopy[i];
  268. if (batch.isDirty) {
  269. isUpdated = batchesArrayCopy[i].update(time) && isUpdated;
  270. batch.isDirty = false;
  271. }
  272. if (batch.geometry.length === 0) {
  273. batches.remove(batch.key);
  274. }
  275. }
  276. return isUpdated;
  277. };
  278. StaticGroundGeometryColorBatch.prototype.getBoundingSphere = function(updater, result) {
  279. var batchesArray = this._batches.values;
  280. var batchCount = batchesArray.length;
  281. for (var i = 0; i < batchCount; ++i) {
  282. var batch = batchesArray[i];
  283. if (batch.contains(updater)) {
  284. return batch.getBoundingSphere(updater, result);
  285. }
  286. }
  287. return BoundingSphereState.FAILED;
  288. };
  289. StaticGroundGeometryColorBatch.prototype.removeAllPrimitives = function() {
  290. var batchesArray = this._batches.values;
  291. var batchCount = batchesArray.length;
  292. for (var i = 0; i < batchCount; ++i) {
  293. batchesArray[i].removeAllPrimitives();
  294. }
  295. };
  296. export default StaticGroundGeometryColorBatch;