StaticGeometryPerMaterialBatch.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 distanceDisplayConditionScratch = new DistanceDisplayCondition();
  16. var defaultDistanceDisplayCondition = new DistanceDisplayCondition();
  17. var defaultOffset = Cartesian3.ZERO;
  18. var offsetScratch = new Cartesian3();
  19. function Batch(primitives, appearanceType, materialProperty, depthFailAppearanceType, depthFailMaterialProperty, closed, shadows) {
  20. this.primitives = primitives;
  21. this.appearanceType = appearanceType;
  22. this.materialProperty = materialProperty;
  23. this.depthFailAppearanceType = depthFailAppearanceType;
  24. this.depthFailMaterialProperty = depthFailMaterialProperty;
  25. this.closed = closed;
  26. this.shadows = shadows;
  27. this.updaters = new AssociativeArray();
  28. this.createPrimitive = true;
  29. this.primitive = undefined;
  30. this.oldPrimitive = undefined;
  31. this.geometry = new AssociativeArray();
  32. this.material = undefined;
  33. this.depthFailMaterial = undefined;
  34. this.updatersWithAttributes = new AssociativeArray();
  35. this.attributes = new AssociativeArray();
  36. this.invalidated = false;
  37. this.removeMaterialSubscription = materialProperty.definitionChanged.addEventListener(Batch.prototype.onMaterialChanged, this);
  38. this.subscriptions = new AssociativeArray();
  39. this.showsUpdated = new AssociativeArray();
  40. }
  41. Batch.prototype.onMaterialChanged = function() {
  42. this.invalidated = true;
  43. };
  44. Batch.prototype.isMaterial = function(updater) {
  45. var material = this.materialProperty;
  46. var updaterMaterial = updater.fillMaterialProperty;
  47. var depthFailMaterial = this.depthFailMaterialProperty;
  48. var updaterDepthFailMaterial = updater.depthFailMaterialProperty;
  49. if (updaterMaterial === material && updaterDepthFailMaterial === depthFailMaterial) {
  50. return true;
  51. }
  52. var equals = defined(material) && material.equals(updaterMaterial);
  53. equals = ((!defined(depthFailMaterial) && !defined(updaterDepthFailMaterial)) || (defined(depthFailMaterial) && depthFailMaterial.equals(updaterDepthFailMaterial))) && equals;
  54. return equals;
  55. };
  56. Batch.prototype.add = function(time, updater) {
  57. var id = updater.id;
  58. this.updaters.set(id, updater);
  59. this.geometry.set(id, updater.createFillGeometryInstance(time));
  60. if (!updater.hasConstantFill || !updater.fillMaterialProperty.isConstant || !Property.isConstant(updater.distanceDisplayConditionProperty) || !Property.isConstant(updater.terrainOffsetProperty)) {
  61. this.updatersWithAttributes.set(id, updater);
  62. } else {
  63. var that = this;
  64. this.subscriptions.set(id, updater.entity.definitionChanged.addEventListener(function(entity, propertyName, newValue, oldValue) {
  65. if (propertyName === 'isShowing') {
  66. that.showsUpdated.set(updater.id, updater);
  67. }
  68. }));
  69. }
  70. this.createPrimitive = true;
  71. };
  72. Batch.prototype.remove = function(updater) {
  73. var id = updater.id;
  74. this.createPrimitive = this.geometry.remove(id) || this.createPrimitive;
  75. if (this.updaters.remove(id)) {
  76. this.updatersWithAttributes.remove(id);
  77. var unsubscribe = this.subscriptions.get(id);
  78. if (defined(unsubscribe)) {
  79. unsubscribe();
  80. this.subscriptions.remove(id);
  81. this.showsUpdated.remove(id);
  82. }
  83. return true;
  84. }
  85. return false;
  86. };
  87. var colorScratch = new Color();
  88. Batch.prototype.update = function(time) {
  89. var isUpdated = true;
  90. var primitive = this.primitive;
  91. var primitives = this.primitives;
  92. var geometries = this.geometry.values;
  93. var i;
  94. if (this.createPrimitive) {
  95. var geometriesLength = geometries.length;
  96. if (geometriesLength > 0) {
  97. if (defined(primitive)) {
  98. if (!defined(this.oldPrimitive)) {
  99. this.oldPrimitive = primitive;
  100. } else {
  101. primitives.remove(primitive);
  102. }
  103. }
  104. this.material = MaterialProperty.getValue(time, this.materialProperty, this.material);
  105. var depthFailAppearance;
  106. if (defined(this.depthFailMaterialProperty)) {
  107. this.depthFailMaterial = MaterialProperty.getValue(time, this.depthFailMaterialProperty, this.depthFailMaterial);
  108. depthFailAppearance = new this.depthFailAppearanceType({
  109. material : this.depthFailMaterial,
  110. translucent : this.depthFailMaterial.isTranslucent(),
  111. closed : this.closed
  112. });
  113. }
  114. primitive = new Primitive({
  115. show : false,
  116. asynchronous : true,
  117. geometryInstances : geometries,
  118. appearance : new this.appearanceType({
  119. material : this.material,
  120. translucent : this.material.isTranslucent(),
  121. closed : this.closed
  122. }),
  123. depthFailAppearance : depthFailAppearance,
  124. shadows : this.shadows
  125. });
  126. primitives.add(primitive);
  127. isUpdated = false;
  128. } else {
  129. if (defined(primitive)) {
  130. primitives.remove(primitive);
  131. primitive = undefined;
  132. }
  133. var oldPrimitive = this.oldPrimitive;
  134. if (defined(oldPrimitive)) {
  135. primitives.remove(oldPrimitive);
  136. this.oldPrimitive = undefined;
  137. }
  138. }
  139. this.attributes.removeAll();
  140. this.primitive = primitive;
  141. this.createPrimitive = false;
  142. } else if (defined(primitive) && primitive.ready) {
  143. primitive.show = true;
  144. if (defined(this.oldPrimitive)) {
  145. primitives.remove(this.oldPrimitive);
  146. this.oldPrimitive = undefined;
  147. }
  148. this.material = MaterialProperty.getValue(time, this.materialProperty, this.material);
  149. this.primitive.appearance.material = this.material;
  150. if (defined(this.depthFailAppearanceType) && !(this.depthFailMaterialProperty instanceof ColorMaterialProperty)) {
  151. this.depthFailMaterial = MaterialProperty.getValue(time, this.depthFailMaterialProperty, this.depthFailMaterial);
  152. this.primitive.depthFailAppearance.material = this.depthFailMaterial;
  153. }
  154. var updatersWithAttributes = this.updatersWithAttributes.values;
  155. var length = updatersWithAttributes.length;
  156. for (i = 0; i < length; i++) {
  157. var updater = updatersWithAttributes[i];
  158. var entity = updater.entity;
  159. var instance = this.geometry.get(updater.id);
  160. var attributes = this.attributes.get(instance.id.id);
  161. if (!defined(attributes)) {
  162. attributes = primitive.getGeometryInstanceAttributes(instance.id);
  163. this.attributes.set(instance.id.id, attributes);
  164. }
  165. if (defined(this.depthFailAppearanceType) && this.depthFailMaterialProperty instanceof ColorMaterialProperty && !updater.depthFailMaterialProperty.isConstant) {
  166. var depthFailColorProperty = updater.depthFailMaterialProperty.color;
  167. var depthFailColor = Property.getValueOrDefault(depthFailColorProperty, time, Color.WHITE, colorScratch);
  168. if (!Color.equals(attributes._lastDepthFailColor, depthFailColor)) {
  169. attributes._lastDepthFailColor = Color.clone(depthFailColor, attributes._lastDepthFailColor);
  170. attributes.depthFailColor = ColorGeometryInstanceAttribute.toValue(depthFailColor, attributes.depthFailColor);
  171. }
  172. }
  173. var show = entity.isShowing && (updater.hasConstantFill || updater.isFilled(time));
  174. var currentShow = attributes.show[0] === 1;
  175. if (show !== currentShow) {
  176. attributes.show = ShowGeometryInstanceAttribute.toValue(show, attributes.show);
  177. }
  178. var distanceDisplayConditionProperty = updater.distanceDisplayConditionProperty;
  179. if (!Property.isConstant(distanceDisplayConditionProperty)) {
  180. var distanceDisplayCondition = Property.getValueOrDefault(distanceDisplayConditionProperty, time, defaultDistanceDisplayCondition, distanceDisplayConditionScratch);
  181. if (!DistanceDisplayCondition.equals(distanceDisplayCondition, attributes._lastDistanceDisplayCondition)) {
  182. attributes._lastDistanceDisplayCondition = DistanceDisplayCondition.clone(distanceDisplayCondition, attributes._lastDistanceDisplayCondition);
  183. attributes.distanceDisplayCondition = DistanceDisplayConditionGeometryInstanceAttribute.toValue(distanceDisplayCondition, attributes.distanceDisplayCondition);
  184. }
  185. }
  186. var offsetProperty = updater.terrainOffsetProperty;
  187. if (!Property.isConstant(offsetProperty)) {
  188. var offset = Property.getValueOrDefault(offsetProperty, time, defaultOffset, offsetScratch);
  189. if (!Cartesian3.equals(offset, attributes._lastOffset)) {
  190. attributes._lastOffset = Cartesian3.clone(offset, attributes._lastOffset);
  191. attributes.offset = OffsetGeometryInstanceAttribute.toValue(offset, attributes.offset);
  192. }
  193. }
  194. }
  195. this.updateShows(primitive);
  196. } else if (defined(primitive) && !primitive.ready) {
  197. isUpdated = false;
  198. }
  199. return isUpdated;
  200. };
  201. Batch.prototype.updateShows = function(primitive) {
  202. var showsUpdated = this.showsUpdated.values;
  203. var length = showsUpdated.length;
  204. for (var i = 0; i < length; i++) {
  205. var updater = showsUpdated[i];
  206. var entity = updater.entity;
  207. var instance = this.geometry.get(updater.id);
  208. var attributes = this.attributes.get(instance.id.id);
  209. if (!defined(attributes)) {
  210. attributes = primitive.getGeometryInstanceAttributes(instance.id);
  211. this.attributes.set(instance.id.id, attributes);
  212. }
  213. var show = entity.isShowing;
  214. var currentShow = attributes.show[0] === 1;
  215. if (show !== currentShow) {
  216. attributes.show = ShowGeometryInstanceAttribute.toValue(show, attributes.show);
  217. instance.attributes.show.value[0] = attributes.show[0];
  218. }
  219. }
  220. this.showsUpdated.removeAll();
  221. };
  222. Batch.prototype.contains = function(updater) {
  223. return this.updaters.contains(updater.id);
  224. };
  225. Batch.prototype.getBoundingSphere = function(updater, result) {
  226. var primitive = this.primitive;
  227. if (!primitive.ready) {
  228. return BoundingSphereState.PENDING;
  229. }
  230. var attributes = primitive.getGeometryInstanceAttributes(updater.entity);
  231. if (!defined(attributes) || !defined(attributes.boundingSphere) ||
  232. (defined(attributes.show) && attributes.show[0] === 0)) {
  233. return BoundingSphereState.FAILED;
  234. }
  235. attributes.boundingSphere.clone(result);
  236. return BoundingSphereState.DONE;
  237. };
  238. Batch.prototype.destroy = function() {
  239. var primitive = this.primitive;
  240. var primitives = this.primitives;
  241. if (defined(primitive)) {
  242. primitives.remove(primitive);
  243. }
  244. var oldPrimitive = this.oldPrimitive;
  245. if (defined(oldPrimitive)) {
  246. primitives.remove(oldPrimitive);
  247. }
  248. this.removeMaterialSubscription();
  249. };
  250. /**
  251. * @private
  252. */
  253. function StaticGeometryPerMaterialBatch(primitives, appearanceType, depthFailAppearanceType, closed, shadows) {
  254. this._items = [];
  255. this._primitives = primitives;
  256. this._appearanceType = appearanceType;
  257. this._depthFailAppearanceType = depthFailAppearanceType;
  258. this._closed = closed;
  259. this._shadows = shadows;
  260. }
  261. StaticGeometryPerMaterialBatch.prototype.add = function(time, updater) {
  262. var items = this._items;
  263. var length = items.length;
  264. for (var i = 0; i < length; i++) {
  265. var item = items[i];
  266. if (item.isMaterial(updater)) {
  267. item.add(time, updater);
  268. return;
  269. }
  270. }
  271. var batch = new Batch(this._primitives, this._appearanceType, updater.fillMaterialProperty, this._depthFailAppearanceType, updater.depthFailMaterialProperty, this._closed, this._shadows);
  272. batch.add(time, updater);
  273. items.push(batch);
  274. };
  275. StaticGeometryPerMaterialBatch.prototype.remove = function(updater) {
  276. var items = this._items;
  277. var length = items.length;
  278. for (var i = length - 1; i >= 0; i--) {
  279. var item = items[i];
  280. if (item.remove(updater)) {
  281. if (item.updaters.length === 0) {
  282. items.splice(i, 1);
  283. item.destroy();
  284. }
  285. break;
  286. }
  287. }
  288. };
  289. StaticGeometryPerMaterialBatch.prototype.update = function(time) {
  290. var i;
  291. var items = this._items;
  292. var length = items.length;
  293. for (i = length - 1; i >= 0; i--) {
  294. var item = items[i];
  295. if (item.invalidated) {
  296. items.splice(i, 1);
  297. var updaters = item.updaters.values;
  298. var updatersLength = updaters.length;
  299. for (var h = 0; h < updatersLength; h++) {
  300. this.add(time, updaters[h]);
  301. }
  302. item.destroy();
  303. }
  304. }
  305. var isUpdated = true;
  306. for (i = 0; i < items.length; i++) {
  307. isUpdated = items[i].update(time) && isUpdated;
  308. }
  309. return isUpdated;
  310. };
  311. StaticGeometryPerMaterialBatch.prototype.getBoundingSphere = function(updater, result) {
  312. var items = this._items;
  313. var length = items.length;
  314. for (var i = 0; i < length; i++) {
  315. var item = items[i];
  316. if (item.contains(updater)){
  317. return item.getBoundingSphere(updater, result);
  318. }
  319. }
  320. return BoundingSphereState.FAILED;
  321. };
  322. StaticGeometryPerMaterialBatch.prototype.removeAllPrimitives = function() {
  323. var items = this._items;
  324. var length = items.length;
  325. for (var i = 0; i < length; i++) {
  326. items[i].destroy();
  327. }
  328. this._items.length = 0;
  329. };
  330. export default StaticGeometryPerMaterialBatch;