StaticGroundGeometryPerMaterialBatch.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. const distanceDisplayConditionScratch = new DistanceDisplayCondition();
  14. const defaultDistanceDisplayCondition = new DistanceDisplayCondition();
  15. // Encapsulates a Primitive and all the entities that it represents.
  16. function Batch(
  17. primitives,
  18. classificationType,
  19. appearanceType,
  20. materialProperty,
  21. usingSphericalTextureCoordinates,
  22. zIndex
  23. ) {
  24. this.primitives = primitives; // scene level primitive collection
  25. this.classificationType = classificationType;
  26. this.appearanceType = appearanceType;
  27. this.materialProperty = materialProperty;
  28. this.updaters = new AssociativeArray();
  29. this.createPrimitive = true;
  30. this.primitive = undefined; // a GroundPrimitive encapsulating all the entities
  31. this.oldPrimitive = undefined;
  32. this.geometry = new AssociativeArray();
  33. this.material = undefined;
  34. this.updatersWithAttributes = new AssociativeArray();
  35. this.attributes = new AssociativeArray();
  36. this.invalidated = false;
  37. this.removeMaterialSubscription = materialProperty.definitionChanged.addEventListener(
  38. Batch.prototype.onMaterialChanged,
  39. this
  40. );
  41. this.subscriptions = new AssociativeArray();
  42. this.showsUpdated = new AssociativeArray();
  43. this.usingSphericalTextureCoordinates = usingSphericalTextureCoordinates;
  44. this.zIndex = zIndex;
  45. this.rectangleCollisionCheck = new RectangleCollisionChecker();
  46. }
  47. Batch.prototype.onMaterialChanged = function () {
  48. this.invalidated = true;
  49. };
  50. Batch.prototype.overlapping = function (rectangle) {
  51. return this.rectangleCollisionCheck.collides(rectangle);
  52. };
  53. // Check if the given updater's material is compatible with this batch
  54. Batch.prototype.isMaterial = function (updater) {
  55. const material = this.materialProperty;
  56. const updaterMaterial = updater.fillMaterialProperty;
  57. if (
  58. updaterMaterial === material ||
  59. (updaterMaterial instanceof ColorMaterialProperty &&
  60. material instanceof ColorMaterialProperty)
  61. ) {
  62. return true;
  63. }
  64. return defined(material) && material.equals(updaterMaterial);
  65. };
  66. Batch.prototype.add = function (time, updater, geometryInstance) {
  67. const id = updater.id;
  68. this.updaters.set(id, updater);
  69. this.geometry.set(id, geometryInstance);
  70. this.rectangleCollisionCheck.insert(id, geometryInstance.geometry.rectangle);
  71. // Updaters with dynamic attributes must be tracked separately, may exit the batch
  72. if (
  73. !updater.hasConstantFill ||
  74. !updater.fillMaterialProperty.isConstant ||
  75. !Property.isConstant(updater.distanceDisplayConditionProperty)
  76. ) {
  77. this.updatersWithAttributes.set(id, updater);
  78. } else {
  79. const that = this;
  80. // Listen for show changes. These will be synchronized in updateShows.
  81. this.subscriptions.set(
  82. id,
  83. updater.entity.definitionChanged.addEventListener(function (
  84. entity,
  85. propertyName,
  86. newValue,
  87. oldValue
  88. ) {
  89. if (propertyName === "isShowing") {
  90. that.showsUpdated.set(updater.id, updater);
  91. }
  92. })
  93. );
  94. }
  95. this.createPrimitive = true;
  96. };
  97. Batch.prototype.remove = function (updater) {
  98. const id = updater.id;
  99. const geometryInstance = this.geometry.get(id);
  100. this.createPrimitive = this.geometry.remove(id) || this.createPrimitive;
  101. if (this.updaters.remove(id)) {
  102. this.rectangleCollisionCheck.remove(
  103. id,
  104. geometryInstance.geometry.rectangle
  105. );
  106. this.updatersWithAttributes.remove(id);
  107. const unsubscribe = this.subscriptions.get(id);
  108. if (defined(unsubscribe)) {
  109. unsubscribe();
  110. this.subscriptions.remove(id);
  111. }
  112. return true;
  113. }
  114. return false;
  115. };
  116. Batch.prototype.update = function (time) {
  117. let isUpdated = true;
  118. let primitive = this.primitive;
  119. const primitives = this.primitives;
  120. const geometries = this.geometry.values;
  121. let i;
  122. if (this.createPrimitive) {
  123. const geometriesLength = geometries.length;
  124. if (geometriesLength > 0) {
  125. if (defined(primitive)) {
  126. // Keep a handle to the old primitive so it can be removed when the updated version is ready.
  127. if (!defined(this.oldPrimitive)) {
  128. this.oldPrimitive = primitive;
  129. } else {
  130. // For if the new primitive changes again before it is ready.
  131. primitives.remove(primitive);
  132. }
  133. }
  134. this.material = MaterialProperty.getValue(
  135. time,
  136. this.materialProperty,
  137. this.material
  138. );
  139. primitive = new GroundPrimitive({
  140. show: false,
  141. asynchronous: true,
  142. geometryInstances: geometries.slice(),
  143. appearance: new this.appearanceType({
  144. material: this.material,
  145. // translucent and closed properties overridden
  146. }),
  147. classificationType: this.classificationType,
  148. });
  149. primitives.add(primitive, this.zIndex);
  150. isUpdated = false;
  151. } else {
  152. if (defined(primitive)) {
  153. primitives.remove(primitive);
  154. primitive = undefined;
  155. }
  156. const oldPrimitive = this.oldPrimitive;
  157. if (defined(oldPrimitive)) {
  158. primitives.remove(oldPrimitive);
  159. this.oldPrimitive = undefined;
  160. }
  161. }
  162. this.attributes.removeAll();
  163. this.primitive = primitive;
  164. this.createPrimitive = false;
  165. } else if (defined(primitive) && primitive.ready) {
  166. primitive.show = true;
  167. if (defined(this.oldPrimitive)) {
  168. primitives.remove(this.oldPrimitive);
  169. this.oldPrimitive = undefined;
  170. }
  171. this.material = MaterialProperty.getValue(
  172. time,
  173. this.materialProperty,
  174. this.material
  175. );
  176. this.primitive.appearance.material = this.material;
  177. const updatersWithAttributes = this.updatersWithAttributes.values;
  178. const length = updatersWithAttributes.length;
  179. for (i = 0; i < length; i++) {
  180. const updater = updatersWithAttributes[i];
  181. const entity = updater.entity;
  182. const instance = this.geometry.get(updater.id);
  183. let attributes = this.attributes.get(instance.id.id);
  184. if (!defined(attributes)) {
  185. attributes = primitive.getGeometryInstanceAttributes(instance.id);
  186. this.attributes.set(instance.id.id, attributes);
  187. }
  188. const show =
  189. entity.isShowing && (updater.hasConstantFill || updater.isFilled(time));
  190. const currentShow = attributes.show[0] === 1;
  191. if (show !== currentShow) {
  192. attributes.show = ShowGeometryInstanceAttribute.toValue(
  193. show,
  194. attributes.show
  195. );
  196. }
  197. const distanceDisplayConditionProperty =
  198. updater.distanceDisplayConditionProperty;
  199. if (!Property.isConstant(distanceDisplayConditionProperty)) {
  200. const distanceDisplayCondition = Property.getValueOrDefault(
  201. distanceDisplayConditionProperty,
  202. time,
  203. defaultDistanceDisplayCondition,
  204. distanceDisplayConditionScratch
  205. );
  206. if (
  207. !DistanceDisplayCondition.equals(
  208. distanceDisplayCondition,
  209. attributes._lastDistanceDisplayCondition
  210. )
  211. ) {
  212. attributes._lastDistanceDisplayCondition = DistanceDisplayCondition.clone(
  213. distanceDisplayCondition,
  214. attributes._lastDistanceDisplayCondition
  215. );
  216. attributes.distanceDisplayCondition = DistanceDisplayConditionGeometryInstanceAttribute.toValue(
  217. distanceDisplayCondition,
  218. attributes.distanceDisplayCondition
  219. );
  220. }
  221. }
  222. }
  223. this.updateShows(primitive);
  224. } else if (defined(primitive) && !primitive.ready) {
  225. isUpdated = false;
  226. }
  227. return isUpdated;
  228. };
  229. Batch.prototype.updateShows = function (primitive) {
  230. const showsUpdated = this.showsUpdated.values;
  231. const length = showsUpdated.length;
  232. for (let i = 0; i < length; i++) {
  233. const updater = showsUpdated[i];
  234. const entity = updater.entity;
  235. const instance = this.geometry.get(updater.id);
  236. let attributes = this.attributes.get(instance.id.id);
  237. if (!defined(attributes)) {
  238. attributes = primitive.getGeometryInstanceAttributes(instance.id);
  239. this.attributes.set(instance.id.id, attributes);
  240. }
  241. const show = entity.isShowing;
  242. const currentShow = attributes.show[0] === 1;
  243. if (show !== currentShow) {
  244. attributes.show = ShowGeometryInstanceAttribute.toValue(
  245. show,
  246. attributes.show
  247. );
  248. instance.attributes.show.value[0] = attributes.show[0];
  249. }
  250. }
  251. this.showsUpdated.removeAll();
  252. };
  253. Batch.prototype.contains = function (updater) {
  254. return this.updaters.contains(updater.id);
  255. };
  256. Batch.prototype.getBoundingSphere = function (updater, result) {
  257. const primitive = this.primitive;
  258. if (!primitive.ready) {
  259. return BoundingSphereState.PENDING;
  260. }
  261. const attributes = primitive.getGeometryInstanceAttributes(updater.entity);
  262. if (
  263. !defined(attributes) ||
  264. !defined(attributes.boundingSphere) ||
  265. (defined(attributes.show) && attributes.show[0] === 0)
  266. ) {
  267. return BoundingSphereState.FAILED;
  268. }
  269. attributes.boundingSphere.clone(result);
  270. return BoundingSphereState.DONE;
  271. };
  272. Batch.prototype.destroy = function () {
  273. const primitive = this.primitive;
  274. const primitives = this.primitives;
  275. if (defined(primitive)) {
  276. primitives.remove(primitive);
  277. }
  278. const oldPrimitive = this.oldPrimitive;
  279. if (defined(oldPrimitive)) {
  280. primitives.remove(oldPrimitive);
  281. }
  282. this.removeMaterialSubscription();
  283. };
  284. /**
  285. * @private
  286. */
  287. function StaticGroundGeometryPerMaterialBatch(
  288. primitives,
  289. classificationType,
  290. appearanceType
  291. ) {
  292. this._items = [];
  293. this._primitives = primitives;
  294. this._classificationType = classificationType;
  295. this._appearanceType = appearanceType;
  296. }
  297. StaticGroundGeometryPerMaterialBatch.prototype.add = function (time, updater) {
  298. const items = this._items;
  299. const length = items.length;
  300. const geometryInstance = updater.createFillGeometryInstance(time);
  301. const usingSphericalTextureCoordinates = ShadowVolumeAppearance.shouldUseSphericalCoordinates(
  302. geometryInstance.geometry.rectangle
  303. );
  304. const zIndex = Property.getValueOrDefault(updater.zIndex, 0);
  305. // Check if the Entity represented by the updater can be placed in an existing batch. Requirements:
  306. // * compatible material (same material or same color)
  307. // * same type of texture coordinates (spherical vs. planar)
  308. // * conservatively non-overlapping with any entities in the existing batch
  309. for (let i = 0; i < length; ++i) {
  310. const item = items[i];
  311. if (
  312. item.isMaterial(updater) &&
  313. item.usingSphericalTextureCoordinates ===
  314. usingSphericalTextureCoordinates &&
  315. item.zIndex === zIndex &&
  316. !item.overlapping(geometryInstance.geometry.rectangle)
  317. ) {
  318. item.add(time, updater, geometryInstance);
  319. return;
  320. }
  321. }
  322. // If a compatible batch wasn't found, create a new batch.
  323. const batch = new Batch(
  324. this._primitives,
  325. this._classificationType,
  326. this._appearanceType,
  327. updater.fillMaterialProperty,
  328. usingSphericalTextureCoordinates,
  329. zIndex
  330. );
  331. batch.add(time, updater, geometryInstance);
  332. items.push(batch);
  333. };
  334. StaticGroundGeometryPerMaterialBatch.prototype.remove = function (updater) {
  335. const items = this._items;
  336. const length = items.length;
  337. for (let i = length - 1; i >= 0; i--) {
  338. const item = items[i];
  339. if (item.remove(updater)) {
  340. if (item.updaters.length === 0) {
  341. items.splice(i, 1);
  342. item.destroy();
  343. }
  344. break;
  345. }
  346. }
  347. };
  348. StaticGroundGeometryPerMaterialBatch.prototype.update = function (time) {
  349. let i;
  350. const items = this._items;
  351. const length = items.length;
  352. for (i = length - 1; i >= 0; i--) {
  353. const item = items[i];
  354. if (item.invalidated) {
  355. items.splice(i, 1);
  356. const updaters = item.updaters.values;
  357. const updatersLength = updaters.length;
  358. for (let h = 0; h < updatersLength; h++) {
  359. this.add(time, updaters[h]);
  360. }
  361. item.destroy();
  362. }
  363. }
  364. let isUpdated = true;
  365. for (i = 0; i < items.length; i++) {
  366. isUpdated = items[i].update(time) && isUpdated;
  367. }
  368. return isUpdated;
  369. };
  370. StaticGroundGeometryPerMaterialBatch.prototype.getBoundingSphere = function (
  371. updater,
  372. result
  373. ) {
  374. const items = this._items;
  375. const length = items.length;
  376. for (let i = 0; i < length; i++) {
  377. const item = items[i];
  378. if (item.contains(updater)) {
  379. return item.getBoundingSphere(updater, result);
  380. }
  381. }
  382. return BoundingSphereState.FAILED;
  383. };
  384. StaticGroundGeometryPerMaterialBatch.prototype.removeAllPrimitives = function () {
  385. const items = this._items;
  386. const length = items.length;
  387. for (let i = 0; i < length; i++) {
  388. items[i].destroy();
  389. }
  390. this._items.length = 0;
  391. };
  392. export default StaticGroundGeometryPerMaterialBatch;