StaticGroundGeometryPerMaterialBatch.js 15 KB

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