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

StaticOutlineGeometryBatch.js 14KB

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