DynamicGeometryUpdater.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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(geometryUpdater, primitives, orderedGroundPrimitives) {
  29. //>>includeStart('debug', pragmas.debug);
  30. Check.defined('geometryUpdater', geometryUpdater);
  31. Check.defined('primitives', primitives);
  32. Check.defined('orderedGroundPrimitives', orderedGroundPrimitives);
  33. //>>includeEnd('debug');
  34. this._primitives = primitives;
  35. this._orderedGroundPrimitives = orderedGroundPrimitives;
  36. this._primitive = undefined;
  37. this._outlinePrimitive = undefined;
  38. this._geometryUpdater = geometryUpdater;
  39. this._options = geometryUpdater._options;
  40. this._entity = geometryUpdater._entity;
  41. this._material = undefined;
  42. }
  43. DynamicGeometryUpdater.prototype._isHidden = function(entity, geometry, time) {
  44. return !entity.isShowing || !entity.isAvailable(time) || !Property.getValueOrDefault(geometry.show, time, true);
  45. };
  46. DynamicGeometryUpdater.prototype._setOptions = DeveloperError.throwInstantiationError;
  47. /**
  48. * Updates the geometry to the specified time.
  49. * @memberof DynamicGeometryUpdater
  50. * @function
  51. *
  52. * @param {JulianDate} time The current time.
  53. */
  54. DynamicGeometryUpdater.prototype.update = function(time) {
  55. //>>includeStart('debug', pragmas.debug);
  56. Check.defined('time', time);
  57. //>>includeEnd('debug');
  58. var geometryUpdater = this._geometryUpdater;
  59. var onTerrain = geometryUpdater._onTerrain;
  60. var primitives = this._primitives;
  61. var orderedGroundPrimitives = this._orderedGroundPrimitives;
  62. if (onTerrain) {
  63. orderedGroundPrimitives.remove(this._primitive);
  64. } else {
  65. primitives.removeAndDestroy(this._primitive);
  66. primitives.removeAndDestroy(this._outlinePrimitive);
  67. this._outlinePrimitive = undefined;
  68. }
  69. this._primitive = undefined;
  70. var entity = this._entity;
  71. var geometry = entity[this._geometryUpdater._geometryPropertyName];
  72. this._setOptions(entity, geometry, time);
  73. if (this._isHidden(entity, geometry, time)) {
  74. return;
  75. }
  76. var shadows = this._geometryUpdater.shadowsProperty.getValue(time);
  77. var options = this._options;
  78. if (!defined(geometry.fill) || geometry.fill.getValue(time)) {
  79. var fillMaterialProperty = geometryUpdater.fillMaterialProperty;
  80. var isColorAppearance = fillMaterialProperty instanceof ColorMaterialProperty;
  81. var appearance;
  82. var closed = geometryUpdater._getIsClosed(options);
  83. if (isColorAppearance) {
  84. appearance = new PerInstanceColorAppearance({
  85. closed: closed,
  86. flat : onTerrain && !geometryUpdater._supportsMaterialsforEntitiesOnTerrain
  87. });
  88. } else {
  89. var material = MaterialProperty.getValue(time, fillMaterialProperty, this._material);
  90. this._material = material;
  91. appearance = new MaterialAppearance({
  92. material : material,
  93. translucent : material.isTranslucent(),
  94. closed : closed
  95. });
  96. }
  97. if (onTerrain) {
  98. options.vertexFormat = PerInstanceColorAppearance.VERTEX_FORMAT;
  99. this._primitive = orderedGroundPrimitives.add(new GroundPrimitive({
  100. geometryInstances : this._geometryUpdater.createFillGeometryInstance(time),
  101. appearance : appearance,
  102. asynchronous : false,
  103. shadows : shadows,
  104. classificationType : this._geometryUpdater.classificationTypeProperty.getValue(time)
  105. }), Property.getValueOrUndefined(this._geometryUpdater.zIndex, time));
  106. } else {
  107. options.vertexFormat = appearance.vertexFormat;
  108. var fillInstance = this._geometryUpdater.createFillGeometryInstance(time);
  109. if (isColorAppearance) {
  110. appearance.translucent = fillInstance.attributes.color.value[3] !== 255;
  111. }
  112. this._primitive = primitives.add(new Primitive({
  113. geometryInstances : fillInstance,
  114. appearance : appearance,
  115. asynchronous : false,
  116. shadows : shadows
  117. }));
  118. }
  119. }
  120. if (!onTerrain && defined(geometry.outline) && geometry.outline.getValue(time)) {
  121. var outlineInstance = this._geometryUpdater.createOutlineGeometryInstance(time);
  122. var outlineWidth = Property.getValueOrDefault(geometry.outlineWidth, time, 1.0);
  123. this._outlinePrimitive = primitives.add(new Primitive({
  124. geometryInstances : outlineInstance,
  125. appearance : new PerInstanceColorAppearance({
  126. flat : true,
  127. translucent : outlineInstance.attributes.color.value[3] !== 255,
  128. renderState : {
  129. lineWidth : geometryUpdater._scene.clampLineWidth(outlineWidth)
  130. }
  131. }),
  132. asynchronous : false,
  133. shadows : shadows
  134. }));
  135. }
  136. };
  137. /**
  138. * Computes a bounding sphere which encloses the visualization produced for the specified entity.
  139. * The bounding sphere is in the fixed frame of the scene's globe.
  140. * @function
  141. *
  142. * @param {BoundingSphere} result The bounding sphere onto which to store the result.
  143. * @returns {BoundingSphereState} BoundingSphereState.DONE if the result contains the bounding sphere,
  144. * BoundingSphereState.PENDING if the result is still being computed, or
  145. * BoundingSphereState.FAILED if the entity has no visualization in the current scene.
  146. * @private
  147. */
  148. DynamicGeometryUpdater.prototype.getBoundingSphere = function(result) {
  149. //>>includeStart('debug', pragmas.debug);
  150. if (!defined(result)) {
  151. throw new DeveloperError('result is required.');
  152. }
  153. //>>includeEnd('debug');
  154. var entity = this._entity;
  155. var primitive = this._primitive;
  156. var outlinePrimitive = this._outlinePrimitive;
  157. var attributes;
  158. //Outline and Fill geometries have the same bounding sphere, so just use whichever one is defined and ready
  159. if (defined(primitive) && primitive.show && primitive.ready) {
  160. attributes = primitive.getGeometryInstanceAttributes(entity);
  161. if (defined(attributes) && defined(attributes.boundingSphere)) {
  162. BoundingSphere.clone(attributes.boundingSphere, result);
  163. return BoundingSphereState.DONE;
  164. }
  165. }
  166. if (defined(outlinePrimitive) && outlinePrimitive.show && outlinePrimitive.ready) {
  167. attributes = outlinePrimitive.getGeometryInstanceAttributes(entity);
  168. if (defined(attributes) && defined(attributes.boundingSphere)) {
  169. BoundingSphere.clone(attributes.boundingSphere, result);
  170. return BoundingSphereState.DONE;
  171. }
  172. }
  173. if ((defined(primitive) && !primitive.ready) || (defined(outlinePrimitive) && !outlinePrimitive.ready)) {
  174. return BoundingSphereState.PENDING;
  175. }
  176. return BoundingSphereState.FAILED;
  177. };
  178. /**
  179. * Returns true if this object was destroyed; otherwise, false.
  180. * @memberof DynamicGeometryUpdater
  181. * @function
  182. *
  183. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  184. */
  185. DynamicGeometryUpdater.prototype.isDestroyed = function() {
  186. return false;
  187. };
  188. /**
  189. * Destroys and resources used by the object. Once an object is destroyed, it should not be used.
  190. * @memberof DynamicGeometryUpdater
  191. * @function
  192. *
  193. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  194. */
  195. DynamicGeometryUpdater.prototype.destroy = function() {
  196. var primitives = this._primitives;
  197. var orderedGroundPrimitives = this._orderedGroundPrimitives;
  198. if (this._geometryUpdater._onTerrain) {
  199. orderedGroundPrimitives.remove(this._primitive);
  200. } else {
  201. primitives.removeAndDestroy(this._primitive);
  202. }
  203. primitives.removeAndDestroy(this._outlinePrimitive);
  204. destroyObject(this);
  205. };
  206. export default DynamicGeometryUpdater;