StaticGeometryPerMaterialBatch.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 = materialProperty.definitionChanged.addEventListener(
  46. Batch.prototype.onMaterialChanged,
  47. this
  48. );
  49. this.subscriptions = new AssociativeArray();
  50. this.showsUpdated = new AssociativeArray();
  51. }
  52. Batch.prototype.onMaterialChanged = function () {
  53. this.invalidated = true;
  54. };
  55. Batch.prototype.isMaterial = function (updater) {
  56. const material = this.materialProperty;
  57. const updaterMaterial = updater.fillMaterialProperty;
  58. const depthFailMaterial = this.depthFailMaterialProperty;
  59. const updaterDepthFailMaterial = updater.depthFailMaterialProperty;
  60. if (
  61. updaterMaterial === material &&
  62. updaterDepthFailMaterial === depthFailMaterial
  63. ) {
  64. return true;
  65. }
  66. let equals = defined(material) && material.equals(updaterMaterial);
  67. equals =
  68. ((!defined(depthFailMaterial) && !defined(updaterDepthFailMaterial)) ||
  69. (defined(depthFailMaterial) &&
  70. depthFailMaterial.equals(updaterDepthFailMaterial))) &&
  71. equals;
  72. return equals;
  73. };
  74. Batch.prototype.add = function (time, updater) {
  75. const id = updater.id;
  76. this.updaters.set(id, updater);
  77. this.geometry.set(id, updater.createFillGeometryInstance(time));
  78. if (
  79. !updater.hasConstantFill ||
  80. !updater.fillMaterialProperty.isConstant ||
  81. !Property.isConstant(updater.distanceDisplayConditionProperty) ||
  82. !Property.isConstant(updater.terrainOffsetProperty)
  83. ) {
  84. this.updatersWithAttributes.set(id, updater);
  85. } else {
  86. const that = this;
  87. this.subscriptions.set(
  88. id,
  89. updater.entity.definitionChanged.addEventListener(function (
  90. entity,
  91. propertyName,
  92. newValue,
  93. oldValue
  94. ) {
  95. if (propertyName === "isShowing") {
  96. that.showsUpdated.set(updater.id, updater);
  97. }
  98. })
  99. );
  100. }
  101. this.createPrimitive = true;
  102. };
  103. Batch.prototype.remove = function (updater) {
  104. const id = updater.id;
  105. this.createPrimitive = this.geometry.remove(id) || this.createPrimitive;
  106. if (this.updaters.remove(id)) {
  107. this.updatersWithAttributes.remove(id);
  108. const unsubscribe = this.subscriptions.get(id);
  109. if (defined(unsubscribe)) {
  110. unsubscribe();
  111. this.subscriptions.remove(id);
  112. this.showsUpdated.remove(id);
  113. }
  114. return true;
  115. }
  116. return false;
  117. };
  118. const colorScratch = new Color();
  119. Batch.prototype.update = function (time) {
  120. let isUpdated = true;
  121. let primitive = this.primitive;
  122. const primitives = this.primitives;
  123. const geometries = this.geometry.values;
  124. let i;
  125. if (this.createPrimitive) {
  126. const geometriesLength = geometries.length;
  127. if (geometriesLength > 0) {
  128. if (defined(primitive)) {
  129. if (!defined(this.oldPrimitive)) {
  130. this.oldPrimitive = primitive;
  131. } else {
  132. primitives.remove(primitive);
  133. }
  134. }
  135. this.material = MaterialProperty.getValue(
  136. time,
  137. this.materialProperty,
  138. this.material
  139. );
  140. let depthFailAppearance;
  141. if (defined(this.depthFailMaterialProperty)) {
  142. this.depthFailMaterial = MaterialProperty.getValue(
  143. time,
  144. this.depthFailMaterialProperty,
  145. this.depthFailMaterial
  146. );
  147. depthFailAppearance = new this.depthFailAppearanceType({
  148. material: this.depthFailMaterial,
  149. translucent: this.depthFailMaterial.isTranslucent(),
  150. closed: this.closed,
  151. });
  152. }
  153. primitive = new Primitive({
  154. show: false,
  155. asynchronous: true,
  156. geometryInstances: geometries.slice(),
  157. appearance: new this.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 = DistanceDisplayCondition.clone(
  263. distanceDisplayCondition,
  264. attributes._lastDistanceDisplayCondition
  265. );
  266. attributes.distanceDisplayCondition = DistanceDisplayConditionGeometryInstanceAttribute.toValue(
  267. distanceDisplayCondition,
  268. attributes.distanceDisplayCondition
  269. );
  270. }
  271. }
  272. const offsetProperty = updater.terrainOffsetProperty;
  273. if (!Property.isConstant(offsetProperty)) {
  274. const offset = Property.getValueOrDefault(
  275. offsetProperty,
  276. time,
  277. defaultOffset,
  278. offsetScratch
  279. );
  280. if (!Cartesian3.equals(offset, attributes._lastOffset)) {
  281. attributes._lastOffset = Cartesian3.clone(
  282. offset,
  283. attributes._lastOffset
  284. );
  285. attributes.offset = OffsetGeometryInstanceAttribute.toValue(
  286. offset,
  287. attributes.offset
  288. );
  289. }
  290. }
  291. }
  292. this.updateShows(primitive);
  293. } else if (defined(primitive) && !primitive.ready) {
  294. isUpdated = false;
  295. }
  296. return isUpdated;
  297. };
  298. Batch.prototype.updateShows = function (primitive) {
  299. const showsUpdated = this.showsUpdated.values;
  300. const length = showsUpdated.length;
  301. for (let i = 0; i < length; i++) {
  302. const updater = showsUpdated[i];
  303. const entity = updater.entity;
  304. const instance = this.geometry.get(updater.id);
  305. let attributes = this.attributes.get(instance.id.id);
  306. if (!defined(attributes)) {
  307. attributes = primitive.getGeometryInstanceAttributes(instance.id);
  308. this.attributes.set(instance.id.id, attributes);
  309. }
  310. const show = entity.isShowing;
  311. const currentShow = attributes.show[0] === 1;
  312. if (show !== currentShow) {
  313. attributes.show = ShowGeometryInstanceAttribute.toValue(
  314. show,
  315. attributes.show
  316. );
  317. instance.attributes.show.value[0] = attributes.show[0];
  318. }
  319. }
  320. this.showsUpdated.removeAll();
  321. };
  322. Batch.prototype.contains = function (updater) {
  323. return this.updaters.contains(updater.id);
  324. };
  325. Batch.prototype.getBoundingSphere = function (updater, result) {
  326. const primitive = this.primitive;
  327. if (!primitive.ready) {
  328. return BoundingSphereState.PENDING;
  329. }
  330. const attributes = primitive.getGeometryInstanceAttributes(updater.entity);
  331. if (
  332. !defined(attributes) ||
  333. !defined(attributes.boundingSphere) ||
  334. (defined(attributes.show) && attributes.show[0] === 0)
  335. ) {
  336. return BoundingSphereState.FAILED;
  337. }
  338. attributes.boundingSphere.clone(result);
  339. return BoundingSphereState.DONE;
  340. };
  341. Batch.prototype.destroy = function () {
  342. const primitive = this.primitive;
  343. const primitives = this.primitives;
  344. if (defined(primitive)) {
  345. primitives.remove(primitive);
  346. }
  347. const oldPrimitive = this.oldPrimitive;
  348. if (defined(oldPrimitive)) {
  349. primitives.remove(oldPrimitive);
  350. }
  351. this.removeMaterialSubscription();
  352. };
  353. /**
  354. * @private
  355. */
  356. function StaticGeometryPerMaterialBatch(
  357. primitives,
  358. appearanceType,
  359. depthFailAppearanceType,
  360. closed,
  361. shadows
  362. ) {
  363. this._items = [];
  364. this._primitives = primitives;
  365. this._appearanceType = appearanceType;
  366. this._depthFailAppearanceType = depthFailAppearanceType;
  367. this._closed = closed;
  368. this._shadows = shadows;
  369. }
  370. StaticGeometryPerMaterialBatch.prototype.add = function (time, updater) {
  371. const items = this._items;
  372. const length = items.length;
  373. for (let i = 0; i < length; i++) {
  374. const item = items[i];
  375. if (item.isMaterial(updater)) {
  376. item.add(time, updater);
  377. return;
  378. }
  379. }
  380. const batch = new Batch(
  381. this._primitives,
  382. this._appearanceType,
  383. updater.fillMaterialProperty,
  384. this._depthFailAppearanceType,
  385. updater.depthFailMaterialProperty,
  386. this._closed,
  387. this._shadows
  388. );
  389. batch.add(time, updater);
  390. items.push(batch);
  391. };
  392. StaticGeometryPerMaterialBatch.prototype.remove = function (updater) {
  393. const items = this._items;
  394. const length = items.length;
  395. for (let i = length - 1; i >= 0; i--) {
  396. const item = items[i];
  397. if (item.remove(updater)) {
  398. if (item.updaters.length === 0) {
  399. items.splice(i, 1);
  400. item.destroy();
  401. }
  402. break;
  403. }
  404. }
  405. };
  406. StaticGeometryPerMaterialBatch.prototype.update = function (time) {
  407. let i;
  408. const items = this._items;
  409. const length = items.length;
  410. for (i = length - 1; i >= 0; i--) {
  411. const item = items[i];
  412. if (item.invalidated) {
  413. items.splice(i, 1);
  414. const updaters = item.updaters.values;
  415. const updatersLength = updaters.length;
  416. for (let h = 0; h < updatersLength; h++) {
  417. this.add(time, updaters[h]);
  418. }
  419. item.destroy();
  420. }
  421. }
  422. let isUpdated = true;
  423. for (i = 0; i < items.length; i++) {
  424. isUpdated = items[i].update(time) && isUpdated;
  425. }
  426. return isUpdated;
  427. };
  428. StaticGeometryPerMaterialBatch.prototype.getBoundingSphere = function (
  429. updater,
  430. result
  431. ) {
  432. const items = this._items;
  433. const length = items.length;
  434. for (let i = 0; i < length; i++) {
  435. const item = items[i];
  436. if (item.contains(updater)) {
  437. return item.getBoundingSphere(updater, result);
  438. }
  439. }
  440. return BoundingSphereState.FAILED;
  441. };
  442. StaticGeometryPerMaterialBatch.prototype.removeAllPrimitives = function () {
  443. const items = this._items;
  444. const length = items.length;
  445. for (let i = 0; i < length; i++) {
  446. items[i].destroy();
  447. }
  448. this._items.length = 0;
  449. };
  450. export default StaticGeometryPerMaterialBatch;