DynamicGeometryUpdater.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import BoundingSphere from "../Core/BoundingSphere.js";
  2. import Check from "../Core/Check.js";
  3. import defined from "../Core/defined.js";
  4. import destroyObject from "../Core/destroyObject.js";
  5. import DeveloperError from "../Core/DeveloperError.js";
  6. import GroundPrimitive from "../Scene/GroundPrimitive.js";
  7. import MaterialAppearance from "../Scene/MaterialAppearance.js";
  8. import PerInstanceColorAppearance from "../Scene/PerInstanceColorAppearance.js";
  9. import Primitive from "../Scene/Primitive.js";
  10. import BoundingSphereState from "./BoundingSphereState.js";
  11. import ColorMaterialProperty from "./ColorMaterialProperty.js";
  12. import MaterialProperty from "./MaterialProperty.js";
  13. import Property from "./Property.js";
  14. /**
  15. * Defines the interface for a dynamic geometry updater. A DynamicGeometryUpdater
  16. * is responsible for handling visualization of a specific type of geometry
  17. * that needs to be recomputed based on simulation time.
  18. * This object is never used directly by client code, but is instead created by
  19. * {@link GeometryUpdater} implementations which contain dynamic geometry.
  20. *
  21. * This type defines an interface and cannot be instantiated directly.
  22. *
  23. * @alias DynamicGeometryUpdater
  24. * @constructor
  25. * @private
  26. * @abstract
  27. */
  28. function DynamicGeometryUpdater(
  29. geometryUpdater,
  30. primitives,
  31. orderedGroundPrimitives
  32. ) {
  33. //>>includeStart('debug', pragmas.debug);
  34. Check.defined("geometryUpdater", geometryUpdater);
  35. Check.defined("primitives", primitives);
  36. Check.defined("orderedGroundPrimitives", orderedGroundPrimitives);
  37. //>>includeEnd('debug');
  38. this._primitives = primitives;
  39. this._orderedGroundPrimitives = orderedGroundPrimitives;
  40. this._primitive = undefined;
  41. this._outlinePrimitive = undefined;
  42. this._geometryUpdater = geometryUpdater;
  43. this._options = geometryUpdater._options;
  44. this._entity = geometryUpdater._entity;
  45. this._material = undefined;
  46. }
  47. DynamicGeometryUpdater.prototype._isHidden = function (entity, geometry, time) {
  48. return (
  49. !entity.isShowing ||
  50. !entity.isAvailable(time) ||
  51. !Property.getValueOrDefault(geometry.show, time, true)
  52. );
  53. };
  54. DynamicGeometryUpdater.prototype._setOptions =
  55. DeveloperError.throwInstantiationError;
  56. /**
  57. * Updates the geometry to the specified time.
  58. * @memberof DynamicGeometryUpdater
  59. * @function
  60. *
  61. * @param {JulianDate} time The current time.
  62. */
  63. DynamicGeometryUpdater.prototype.update = function (time) {
  64. //>>includeStart('debug', pragmas.debug);
  65. Check.defined("time", time);
  66. //>>includeEnd('debug');
  67. var geometryUpdater = this._geometryUpdater;
  68. var onTerrain = geometryUpdater._onTerrain;
  69. var primitives = this._primitives;
  70. var orderedGroundPrimitives = this._orderedGroundPrimitives;
  71. if (onTerrain) {
  72. orderedGroundPrimitives.remove(this._primitive);
  73. } else {
  74. primitives.removeAndDestroy(this._primitive);
  75. primitives.removeAndDestroy(this._outlinePrimitive);
  76. this._outlinePrimitive = undefined;
  77. }
  78. this._primitive = undefined;
  79. var entity = this._entity;
  80. var geometry = entity[this._geometryUpdater._geometryPropertyName];
  81. this._setOptions(entity, geometry, time);
  82. if (this._isHidden(entity, geometry, time)) {
  83. return;
  84. }
  85. var shadows = this._geometryUpdater.shadowsProperty.getValue(time);
  86. var options = this._options;
  87. if (!defined(geometry.fill) || geometry.fill.getValue(time)) {
  88. var fillMaterialProperty = geometryUpdater.fillMaterialProperty;
  89. var isColorAppearance =
  90. fillMaterialProperty instanceof ColorMaterialProperty;
  91. var appearance;
  92. var closed = geometryUpdater._getIsClosed(options);
  93. if (isColorAppearance) {
  94. appearance = new PerInstanceColorAppearance({
  95. closed: closed,
  96. flat:
  97. onTerrain && !geometryUpdater._supportsMaterialsforEntitiesOnTerrain,
  98. });
  99. } else {
  100. var material = MaterialProperty.getValue(
  101. time,
  102. fillMaterialProperty,
  103. this._material
  104. );
  105. this._material = material;
  106. appearance = new MaterialAppearance({
  107. material: material,
  108. translucent: material.isTranslucent(),
  109. closed: closed,
  110. });
  111. }
  112. if (onTerrain) {
  113. options.vertexFormat = PerInstanceColorAppearance.VERTEX_FORMAT;
  114. this._primitive = orderedGroundPrimitives.add(
  115. new GroundPrimitive({
  116. geometryInstances: this._geometryUpdater.createFillGeometryInstance(
  117. time
  118. ),
  119. appearance: appearance,
  120. asynchronous: false,
  121. shadows: shadows,
  122. classificationType: this._geometryUpdater.classificationTypeProperty.getValue(
  123. time
  124. ),
  125. }),
  126. Property.getValueOrUndefined(this._geometryUpdater.zIndex, time)
  127. );
  128. } else {
  129. options.vertexFormat = appearance.vertexFormat;
  130. var fillInstance = this._geometryUpdater.createFillGeometryInstance(time);
  131. if (isColorAppearance) {
  132. appearance.translucent = fillInstance.attributes.color.value[3] !== 255;
  133. }
  134. this._primitive = primitives.add(
  135. new Primitive({
  136. geometryInstances: fillInstance,
  137. appearance: appearance,
  138. asynchronous: false,
  139. shadows: shadows,
  140. })
  141. );
  142. }
  143. }
  144. if (
  145. !onTerrain &&
  146. defined(geometry.outline) &&
  147. geometry.outline.getValue(time)
  148. ) {
  149. var outlineInstance = this._geometryUpdater.createOutlineGeometryInstance(
  150. time
  151. );
  152. var outlineWidth = Property.getValueOrDefault(
  153. geometry.outlineWidth,
  154. time,
  155. 1.0
  156. );
  157. this._outlinePrimitive = primitives.add(
  158. new Primitive({
  159. geometryInstances: outlineInstance,
  160. appearance: new PerInstanceColorAppearance({
  161. flat: true,
  162. translucent: outlineInstance.attributes.color.value[3] !== 255,
  163. renderState: {
  164. lineWidth: geometryUpdater._scene.clampLineWidth(outlineWidth),
  165. },
  166. }),
  167. asynchronous: false,
  168. shadows: shadows,
  169. })
  170. );
  171. }
  172. };
  173. /**
  174. * Computes a bounding sphere which encloses the visualization produced for the specified entity.
  175. * The bounding sphere is in the fixed frame of the scene's globe.
  176. * @function
  177. *
  178. * @param {BoundingSphere} result The bounding sphere onto which to store the result.
  179. * @returns {BoundingSphereState} BoundingSphereState.DONE if the result contains the bounding sphere,
  180. * BoundingSphereState.PENDING if the result is still being computed, or
  181. * BoundingSphereState.FAILED if the entity has no visualization in the current scene.
  182. * @private
  183. */
  184. DynamicGeometryUpdater.prototype.getBoundingSphere = function (result) {
  185. //>>includeStart('debug', pragmas.debug);
  186. if (!defined(result)) {
  187. throw new DeveloperError("result is required.");
  188. }
  189. //>>includeEnd('debug');
  190. var entity = this._entity;
  191. var primitive = this._primitive;
  192. var outlinePrimitive = this._outlinePrimitive;
  193. var attributes;
  194. //Outline and Fill geometries have the same bounding sphere, so just use whichever one is defined and ready
  195. if (defined(primitive) && primitive.show && primitive.ready) {
  196. attributes = primitive.getGeometryInstanceAttributes(entity);
  197. if (defined(attributes) && defined(attributes.boundingSphere)) {
  198. BoundingSphere.clone(attributes.boundingSphere, result);
  199. return BoundingSphereState.DONE;
  200. }
  201. }
  202. if (
  203. defined(outlinePrimitive) &&
  204. outlinePrimitive.show &&
  205. outlinePrimitive.ready
  206. ) {
  207. attributes = outlinePrimitive.getGeometryInstanceAttributes(entity);
  208. if (defined(attributes) && defined(attributes.boundingSphere)) {
  209. BoundingSphere.clone(attributes.boundingSphere, result);
  210. return BoundingSphereState.DONE;
  211. }
  212. }
  213. if (
  214. (defined(primitive) && !primitive.ready) ||
  215. (defined(outlinePrimitive) && !outlinePrimitive.ready)
  216. ) {
  217. return BoundingSphereState.PENDING;
  218. }
  219. return BoundingSphereState.FAILED;
  220. };
  221. /**
  222. * Returns true if this object was destroyed; otherwise, false.
  223. * @memberof DynamicGeometryUpdater
  224. * @function
  225. *
  226. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  227. */
  228. DynamicGeometryUpdater.prototype.isDestroyed = function () {
  229. return false;
  230. };
  231. /**
  232. * Destroys and resources used by the object. Once an object is destroyed, it should not be used.
  233. * @memberof DynamicGeometryUpdater
  234. * @function
  235. *
  236. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  237. */
  238. DynamicGeometryUpdater.prototype.destroy = function () {
  239. var primitives = this._primitives;
  240. var orderedGroundPrimitives = this._orderedGroundPrimitives;
  241. if (this._geometryUpdater._onTerrain) {
  242. orderedGroundPrimitives.remove(this._primitive);
  243. } else {
  244. primitives.removeAndDestroy(this._primitive);
  245. }
  246. primitives.removeAndDestroy(this._outlinePrimitive);
  247. destroyObject(this);
  248. };
  249. export default DynamicGeometryUpdater;