StaticOutlineGeometryBatch.js 16 KB

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