智慧水务管理系统 - 精河县供水工程综合管理平台

StaticGroundGeometryPerMaterialBatch.js 15KB

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