DynamicGeometryUpdater.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. const geometryUpdater = this._geometryUpdater;
  68. const onTerrain = geometryUpdater._onTerrain;
  69. const primitives = this._primitives;
  70. const 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. const entity = this._entity;
  80. const geometry = entity[this._geometryUpdater._geometryPropertyName];
  81. this._setOptions(entity, geometry, time);
  82. if (this._isHidden(entity, geometry, time)) {
  83. return;
  84. }
  85. const shadows = this._geometryUpdater.shadowsProperty.getValue(time);
  86. const options = this._options;
  87. if (!defined(geometry.fill) || geometry.fill.getValue(time)) {
  88. const fillMaterialProperty = geometryUpdater.fillMaterialProperty;
  89. const isColorAppearance =
  90. fillMaterialProperty instanceof ColorMaterialProperty;
  91. let appearance;
  92. const closed = geometryUpdater._getIsClosed(options);
  93. if (isColorAppearance) {
  94. appearance = new PerInstanceColorAppearance({
  95. closed: closed,
  96. flat:
  97. onTerrain && !geometryUpdater._supportsMaterialsforEntitiesOnTerrain,
  98. });
  99. } else {
  100. const 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. const fillInstance = this._geometryUpdater.createFillGeometryInstance(
  131. time
  132. );
  133. if (isColorAppearance) {
  134. appearance.translucent = fillInstance.attributes.color.value[3] !== 255;
  135. }
  136. this._primitive = primitives.add(
  137. new Primitive({
  138. geometryInstances: fillInstance,
  139. appearance: appearance,
  140. asynchronous: false,
  141. shadows: shadows,
  142. })
  143. );
  144. }
  145. }
  146. if (
  147. !onTerrain &&
  148. defined(geometry.outline) &&
  149. geometry.outline.getValue(time)
  150. ) {
  151. const outlineInstance = this._geometryUpdater.createOutlineGeometryInstance(
  152. time
  153. );
  154. const outlineWidth = Property.getValueOrDefault(
  155. geometry.outlineWidth,
  156. time,
  157. 1.0
  158. );
  159. this._outlinePrimitive = primitives.add(
  160. new Primitive({
  161. geometryInstances: outlineInstance,
  162. appearance: new PerInstanceColorAppearance({
  163. flat: true,
  164. translucent: outlineInstance.attributes.color.value[3] !== 255,
  165. renderState: {
  166. lineWidth: geometryUpdater._scene.clampLineWidth(outlineWidth),
  167. },
  168. }),
  169. asynchronous: false,
  170. shadows: shadows,
  171. })
  172. );
  173. }
  174. };
  175. /**
  176. * Computes a bounding sphere which encloses the visualization produced for the specified entity.
  177. * The bounding sphere is in the fixed frame of the scene's globe.
  178. * @function
  179. *
  180. * @param {BoundingSphere} result The bounding sphere onto which to store the result.
  181. * @returns {BoundingSphereState} BoundingSphereState.DONE if the result contains the bounding sphere,
  182. * BoundingSphereState.PENDING if the result is still being computed, or
  183. * BoundingSphereState.FAILED if the entity has no visualization in the current scene.
  184. * @private
  185. */
  186. DynamicGeometryUpdater.prototype.getBoundingSphere = function (result) {
  187. //>>includeStart('debug', pragmas.debug);
  188. if (!defined(result)) {
  189. throw new DeveloperError("result is required.");
  190. }
  191. //>>includeEnd('debug');
  192. const entity = this._entity;
  193. const primitive = this._primitive;
  194. const outlinePrimitive = this._outlinePrimitive;
  195. let attributes;
  196. //Outline and Fill geometries have the same bounding sphere, so just use whichever one is defined and ready
  197. if (defined(primitive) && primitive.show && primitive.ready) {
  198. attributes = primitive.getGeometryInstanceAttributes(entity);
  199. if (defined(attributes) && defined(attributes.boundingSphere)) {
  200. BoundingSphere.clone(attributes.boundingSphere, result);
  201. return BoundingSphereState.DONE;
  202. }
  203. }
  204. if (
  205. defined(outlinePrimitive) &&
  206. outlinePrimitive.show &&
  207. outlinePrimitive.ready
  208. ) {
  209. attributes = outlinePrimitive.getGeometryInstanceAttributes(entity);
  210. if (defined(attributes) && defined(attributes.boundingSphere)) {
  211. BoundingSphere.clone(attributes.boundingSphere, result);
  212. return BoundingSphereState.DONE;
  213. }
  214. }
  215. if (
  216. (defined(primitive) && !primitive.ready) ||
  217. (defined(outlinePrimitive) && !outlinePrimitive.ready)
  218. ) {
  219. return BoundingSphereState.PENDING;
  220. }
  221. return BoundingSphereState.FAILED;
  222. };
  223. /**
  224. * Returns true if this object was destroyed; otherwise, false.
  225. * @memberof DynamicGeometryUpdater
  226. * @function
  227. *
  228. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  229. */
  230. DynamicGeometryUpdater.prototype.isDestroyed = function () {
  231. return false;
  232. };
  233. /**
  234. * Destroys and resources used by the object. Once an object is destroyed, it should not be used.
  235. * @memberof DynamicGeometryUpdater
  236. * @function
  237. *
  238. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  239. */
  240. DynamicGeometryUpdater.prototype.destroy = function () {
  241. const primitives = this._primitives;
  242. const orderedGroundPrimitives = this._orderedGroundPrimitives;
  243. if (this._geometryUpdater._onTerrain) {
  244. orderedGroundPrimitives.remove(this._primitive);
  245. } else {
  246. primitives.removeAndDestroy(this._primitive);
  247. }
  248. primitives.removeAndDestroy(this._outlinePrimitive);
  249. destroyObject(this);
  250. };
  251. export default DynamicGeometryUpdater;