StaticGeometryColorBatch.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. import AssociativeArray from '../Core/AssociativeArray.js';
  2. import Cartesian3 from '../Core/Cartesian3.js';
  3. import Color from '../Core/Color.js';
  4. import ColorGeometryInstanceAttribute from '../Core/ColorGeometryInstanceAttribute.js';
  5. import defined from '../Core/defined.js';
  6. import DistanceDisplayCondition from '../Core/DistanceDisplayCondition.js';
  7. import DistanceDisplayConditionGeometryInstanceAttribute from '../Core/DistanceDisplayConditionGeometryInstanceAttribute.js';
  8. import OffsetGeometryInstanceAttribute from '../Core/OffsetGeometryInstanceAttribute.js';
  9. import ShowGeometryInstanceAttribute from '../Core/ShowGeometryInstanceAttribute.js';
  10. import Primitive from '../Scene/Primitive.js';
  11. import BoundingSphereState from './BoundingSphereState.js';
  12. import ColorMaterialProperty from './ColorMaterialProperty.js';
  13. import MaterialProperty from './MaterialProperty.js';
  14. import Property from './Property.js';
  15. var colorScratch = new Color();
  16. var distanceDisplayConditionScratch = new DistanceDisplayCondition();
  17. var defaultDistanceDisplayCondition = new DistanceDisplayCondition();
  18. var defaultOffset = Cartesian3.ZERO;
  19. var offsetScratch = new Cartesian3();
  20. function Batch(primitives, translucent, appearanceType, depthFailAppearanceType, depthFailMaterialProperty, closed, shadows) {
  21. this.translucent = translucent;
  22. this.appearanceType = appearanceType;
  23. this.depthFailAppearanceType = depthFailAppearanceType;
  24. this.depthFailMaterialProperty = depthFailMaterialProperty;
  25. this.depthFailMaterial = undefined;
  26. this.closed = closed;
  27. this.shadows = shadows;
  28. this.primitives = primitives;
  29. this.createPrimitive = false;
  30. this.waitingOnCreate = false;
  31. this.primitive = undefined;
  32. this.oldPrimitive = undefined;
  33. this.geometry = new AssociativeArray();
  34. this.updaters = new AssociativeArray();
  35. this.updatersWithAttributes = new AssociativeArray();
  36. this.attributes = new AssociativeArray();
  37. this.subscriptions = new AssociativeArray();
  38. this.showsUpdated = new AssociativeArray();
  39. this.itemsToRemove = [];
  40. this.invalidated = false;
  41. var removeMaterialSubscription;
  42. if (defined(depthFailMaterialProperty)) {
  43. removeMaterialSubscription = depthFailMaterialProperty.definitionChanged.addEventListener(Batch.prototype.onMaterialChanged, this);
  44. }
  45. this.removeMaterialSubscription = removeMaterialSubscription;
  46. }
  47. Batch.prototype.onMaterialChanged = function() {
  48. this.invalidated = true;
  49. };
  50. Batch.prototype.isMaterial = function(updater) {
  51. var material = this.depthFailMaterialProperty;
  52. var updaterMaterial = updater.depthFailMaterialProperty;
  53. if (updaterMaterial === material) {
  54. return true;
  55. }
  56. if (defined(material)) {
  57. return material.equals(updaterMaterial);
  58. }
  59. return false;
  60. };
  61. Batch.prototype.add = function(updater, instance) {
  62. var id = updater.id;
  63. this.createPrimitive = true;
  64. this.geometry.set(id, instance);
  65. this.updaters.set(id, updater);
  66. if (!updater.hasConstantFill || !updater.fillMaterialProperty.isConstant || !Property.isConstant(updater.distanceDisplayConditionProperty) || !Property.isConstant(updater.terrainOffsetProperty)) {
  67. this.updatersWithAttributes.set(id, updater);
  68. } else {
  69. var that = this;
  70. this.subscriptions.set(id, updater.entity.definitionChanged.addEventListener(function(entity, propertyName, newValue, oldValue) {
  71. if (propertyName === 'isShowing') {
  72. that.showsUpdated.set(updater.id, updater);
  73. }
  74. }));
  75. }
  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. this.showsUpdated.remove(id);
  87. }
  88. return true;
  89. }
  90. return false;
  91. };
  92. Batch.prototype.update = function(time) {
  93. var isUpdated = true;
  94. var removedCount = 0;
  95. var primitive = this.primitive;
  96. var primitives = this.primitives;
  97. var i;
  98. if (this.createPrimitive) {
  99. var geometries = this.geometry.values;
  100. var geometriesLength = geometries.length;
  101. if (geometriesLength > 0) {
  102. if (defined(primitive)) {
  103. if (!defined(this.oldPrimitive)) {
  104. this.oldPrimitive = primitive;
  105. } else {
  106. primitives.remove(primitive);
  107. }
  108. }
  109. var depthFailAppearance;
  110. if (defined(this.depthFailAppearanceType)) {
  111. if (defined(this.depthFailMaterialProperty)) {
  112. this.depthFailMaterial = MaterialProperty.getValue(time, this.depthFailMaterialProperty, this.depthFailMaterial);
  113. }
  114. depthFailAppearance = new this.depthFailAppearanceType({
  115. material : this.depthFailMaterial,
  116. translucent : this.translucent,
  117. closed : this.closed
  118. });
  119. }
  120. primitive = new Primitive({
  121. show : false,
  122. asynchronous : true,
  123. geometryInstances : geometries,
  124. appearance : new this.appearanceType({
  125. translucent : this.translucent,
  126. closed : this.closed
  127. }),
  128. depthFailAppearance : depthFailAppearance,
  129. shadows : this.shadows
  130. });
  131. primitives.add(primitive);
  132. isUpdated = false;
  133. } else {
  134. if (defined(primitive)) {
  135. primitives.remove(primitive);
  136. primitive = undefined;
  137. }
  138. var oldPrimitive = this.oldPrimitive;
  139. if (defined(oldPrimitive)) {
  140. primitives.remove(oldPrimitive);
  141. this.oldPrimitive = undefined;
  142. }
  143. }
  144. this.attributes.removeAll();
  145. this.primitive = primitive;
  146. this.createPrimitive = false;
  147. this.waitingOnCreate = true;
  148. } else if (defined(primitive) && primitive.ready) {
  149. primitive.show = true;
  150. if (defined(this.oldPrimitive)) {
  151. primitives.remove(this.oldPrimitive);
  152. this.oldPrimitive = undefined;
  153. }
  154. if (defined(this.depthFailAppearanceType) && !(this.depthFailMaterialProperty instanceof ColorMaterialProperty)) {
  155. this.depthFailMaterial = MaterialProperty.getValue(time, this.depthFailMaterialProperty, this.depthFailMaterial);
  156. this.primitive.depthFailAppearance.material = this.depthFailMaterial;
  157. }
  158. var updatersWithAttributes = this.updatersWithAttributes.values;
  159. var length = updatersWithAttributes.length;
  160. var waitingOnCreate = this.waitingOnCreate;
  161. for (i = 0; i < length; i++) {
  162. var updater = updatersWithAttributes[i];
  163. var instance = this.geometry.get(updater.id);
  164. var attributes = this.attributes.get(instance.id.id);
  165. if (!defined(attributes)) {
  166. attributes = primitive.getGeometryInstanceAttributes(instance.id);
  167. this.attributes.set(instance.id.id, attributes);
  168. }
  169. if (!updater.fillMaterialProperty.isConstant || waitingOnCreate) {
  170. var colorProperty = updater.fillMaterialProperty.color;
  171. var resultColor = Property.getValueOrDefault(colorProperty, time, Color.WHITE, colorScratch);
  172. if (!Color.equals(attributes._lastColor, resultColor)) {
  173. attributes._lastColor = Color.clone(resultColor, attributes._lastColor);
  174. attributes.color = ColorGeometryInstanceAttribute.toValue(resultColor, attributes.color);
  175. if ((this.translucent && attributes.color[3] === 255) || (!this.translucent && attributes.color[3] !== 255)) {
  176. this.itemsToRemove[removedCount++] = updater;
  177. }
  178. }
  179. }
  180. if (defined(this.depthFailAppearanceType) && updater.depthFailMaterialProperty instanceof ColorMaterialProperty && (!updater.depthFailMaterialProperty.isConstant || waitingOnCreate)) {
  181. var depthFailColorProperty = updater.depthFailMaterialProperty.color;
  182. var depthColor = Property.getValueOrDefault(depthFailColorProperty, time, Color.WHITE, colorScratch);
  183. if (!Color.equals(attributes._lastDepthFailColor, depthColor)) {
  184. attributes._lastDepthFailColor = Color.clone(depthColor, attributes._lastDepthFailColor);
  185. attributes.depthFailColor = ColorGeometryInstanceAttribute.toValue(depthColor, attributes.depthFailColor);
  186. }
  187. }
  188. var show = updater.entity.isShowing && (updater.hasConstantFill || updater.isFilled(time));
  189. var currentShow = attributes.show[0] === 1;
  190. if (show !== currentShow) {
  191. attributes.show = ShowGeometryInstanceAttribute.toValue(show, attributes.show);
  192. }
  193. var distanceDisplayConditionProperty = updater.distanceDisplayConditionProperty;
  194. if (!Property.isConstant(distanceDisplayConditionProperty)) {
  195. var distanceDisplayCondition = Property.getValueOrDefault(distanceDisplayConditionProperty, time, defaultDistanceDisplayCondition, distanceDisplayConditionScratch);
  196. if (!DistanceDisplayCondition.equals(distanceDisplayCondition, attributes._lastDistanceDisplayCondition)) {
  197. attributes._lastDistanceDisplayCondition = DistanceDisplayCondition.clone(distanceDisplayCondition, attributes._lastDistanceDisplayCondition);
  198. attributes.distanceDisplayCondition = DistanceDisplayConditionGeometryInstanceAttribute.toValue(distanceDisplayCondition, attributes.distanceDisplayCondition);
  199. }
  200. }
  201. var offsetProperty = updater.terrainOffsetProperty;
  202. if (!Property.isConstant(offsetProperty)) {
  203. var offset = Property.getValueOrDefault(offsetProperty, time, defaultOffset, offsetScratch);
  204. if (!Cartesian3.equals(offset, attributes._lastOffset)) {
  205. attributes._lastOffset = Cartesian3.clone(offset, attributes._lastOffset);
  206. attributes.offset = OffsetGeometryInstanceAttribute.toValue(offset, attributes.offset);
  207. }
  208. }
  209. }
  210. this.updateShows(primitive);
  211. this.waitingOnCreate = false;
  212. } else if (defined(primitive) && !primitive.ready) {
  213. isUpdated = false;
  214. }
  215. this.itemsToRemove.length = removedCount;
  216. return isUpdated;
  217. };
  218. Batch.prototype.updateShows = function(primitive) {
  219. var showsUpdated = this.showsUpdated.values;
  220. var length = showsUpdated.length;
  221. for (var i = 0; i < length; i++) {
  222. var updater = showsUpdated[i];
  223. var instance = this.geometry.get(updater.id);
  224. var attributes = this.attributes.get(instance.id.id);
  225. if (!defined(attributes)) {
  226. attributes = primitive.getGeometryInstanceAttributes(instance.id);
  227. this.attributes.set(instance.id.id, attributes);
  228. }
  229. var show = updater.entity.isShowing;
  230. var currentShow = attributes.show[0] === 1;
  231. if (show !== currentShow) {
  232. attributes.show = ShowGeometryInstanceAttribute.toValue(show, attributes.show);
  233. instance.attributes.show.value[0] = attributes.show[0];
  234. }
  235. }
  236. this.showsUpdated.removeAll();
  237. };
  238. Batch.prototype.contains = function(updater) {
  239. return this.updaters.contains(updater.id);
  240. };
  241. Batch.prototype.getBoundingSphere = function(updater, result) {
  242. var primitive = this.primitive;
  243. if (!primitive.ready) {
  244. return BoundingSphereState.PENDING;
  245. }
  246. var attributes = primitive.getGeometryInstanceAttributes(updater.entity);
  247. if (!defined(attributes) || !defined(attributes.boundingSphere) ||//
  248. (defined(attributes.show) && attributes.show[0] === 0)) {
  249. return BoundingSphereState.FAILED;
  250. }
  251. attributes.boundingSphere.clone(result);
  252. return BoundingSphereState.DONE;
  253. };
  254. Batch.prototype.destroy = function() {
  255. var primitive = this.primitive;
  256. var primitives = this.primitives;
  257. if (defined(primitive)) {
  258. primitives.remove(primitive);
  259. }
  260. var oldPrimitive = this.oldPrimitive;
  261. if (defined(oldPrimitive)) {
  262. primitives.remove(oldPrimitive);
  263. }
  264. if (defined(this.removeMaterialSubscription)) {
  265. this.removeMaterialSubscription();
  266. }
  267. };
  268. /**
  269. * @private
  270. */
  271. function StaticGeometryColorBatch(primitives, appearanceType, depthFailAppearanceType, closed, shadows) {
  272. this._solidItems = [];
  273. this._translucentItems = [];
  274. this._primitives = primitives;
  275. this._appearanceType = appearanceType;
  276. this._depthFailAppearanceType = depthFailAppearanceType;
  277. this._closed = closed;
  278. this._shadows = shadows;
  279. }
  280. StaticGeometryColorBatch.prototype.add = function(time, updater) {
  281. var items;
  282. var translucent;
  283. var instance = updater.createFillGeometryInstance(time);
  284. if (instance.attributes.color.value[3] === 255) {
  285. items = this._solidItems;
  286. translucent = false;
  287. } else {
  288. items = this._translucentItems;
  289. translucent = true;
  290. }
  291. var length = items.length;
  292. for (var i = 0; i < length; i++) {
  293. var item = items[i];
  294. if (item.isMaterial(updater)) {
  295. item.add(updater, instance);
  296. return;
  297. }
  298. }
  299. var batch = new Batch(this._primitives, translucent, this._appearanceType, this._depthFailAppearanceType, updater.depthFailMaterialProperty, this._closed, this._shadows);
  300. batch.add(updater, instance);
  301. items.push(batch);
  302. };
  303. function removeItem(items, updater) {
  304. var length = items.length;
  305. for (var i = length - 1; i >= 0; i--) {
  306. var item = items[i];
  307. if (item.remove(updater)) {
  308. if (item.updaters.length === 0) {
  309. items.splice(i, 1);
  310. item.destroy();
  311. }
  312. return true;
  313. }
  314. }
  315. return false;
  316. }
  317. StaticGeometryColorBatch.prototype.remove = function(updater) {
  318. if (!removeItem(this._solidItems, updater)) {
  319. removeItem(this._translucentItems, updater);
  320. }
  321. };
  322. function moveItems(batch, items, time) {
  323. var itemsMoved = false;
  324. var length = items.length;
  325. for (var i = 0; i < length; ++i) {
  326. var item = items[i];
  327. var itemsToRemove = item.itemsToRemove;
  328. var itemsToMoveLength = itemsToRemove.length;
  329. if (itemsToMoveLength > 0) {
  330. for (i = 0; i < itemsToMoveLength; i++) {
  331. var updater = itemsToRemove[i];
  332. item.remove(updater);
  333. batch.add(time, updater);
  334. itemsMoved = true;
  335. }
  336. }
  337. }
  338. return itemsMoved;
  339. }
  340. function updateItems(batch, items, time, isUpdated) {
  341. var length = items.length;
  342. var i;
  343. for (i = length - 1; i >= 0; i--) {
  344. var item = items[i];
  345. if (item.invalidated) {
  346. items.splice(i, 1);
  347. var updaters = item.updaters.values;
  348. var updatersLength = updaters.length;
  349. for (var h = 0; h < updatersLength; h++) {
  350. batch.add(time, updaters[h]);
  351. }
  352. item.destroy();
  353. }
  354. }
  355. length = items.length;
  356. for (i = 0; i < length; ++i) {
  357. isUpdated = items[i].update(time) && isUpdated;
  358. }
  359. return isUpdated;
  360. }
  361. StaticGeometryColorBatch.prototype.update = function(time) {
  362. //Perform initial update
  363. var isUpdated = updateItems(this, this._solidItems, time, true);
  364. isUpdated = updateItems(this, this._translucentItems, time, isUpdated) && isUpdated;
  365. //If any items swapped between solid/translucent, we need to
  366. //move them between batches
  367. var solidsMoved = moveItems(this, this._solidItems, time);
  368. var translucentsMoved = moveItems(this, this._translucentItems, time);
  369. //If we moved anything around, we need to re-build the primitive
  370. if (solidsMoved || translucentsMoved) {
  371. isUpdated = updateItems(this, this._solidItems, time, isUpdated) && isUpdated;
  372. isUpdated = updateItems(this, this._translucentItems, time, isUpdated)&& isUpdated;
  373. }
  374. return isUpdated;
  375. };
  376. function getBoundingSphere(items, updater, result) {
  377. var length = items.length;
  378. for (var i = 0; i < length; i++) {
  379. var item = items[i];
  380. if (item.contains(updater)){
  381. return item.getBoundingSphere(updater, result);
  382. }
  383. }
  384. return BoundingSphereState.FAILED;
  385. }
  386. StaticGeometryColorBatch.prototype.getBoundingSphere = function(updater, result) {
  387. var boundingSphere = getBoundingSphere(this._solidItems, updater, result);
  388. if (boundingSphere === BoundingSphereState.FAILED) {
  389. return getBoundingSphere(this._translucentItems, updater, result);
  390. }
  391. return boundingSphere;
  392. };
  393. function removeAllPrimitives(items) {
  394. var length = items.length;
  395. for (var i = 0; i < length; i++) {
  396. items[i].destroy();
  397. }
  398. items.length = 0;
  399. }
  400. StaticGeometryColorBatch.prototype.removeAllPrimitives = function() {
  401. removeAllPrimitives(this._solidItems);
  402. removeAllPrimitives(this._translucentItems);
  403. };
  404. export default StaticGeometryColorBatch;