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

StaticGeometryPerMaterialBatch.js 15KB

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