StaticOutlineGeometryBatch.js 14 KB

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