StaticGroundPolylinePerMaterialBatch.js 14 KB

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