智慧水务管理系统 - 精河县供水工程综合管理平台

DynamicGeometryUpdater.js 8.7KB

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