CorridorGeometryUpdater.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import ApproximateTerrainHeights from '../Core/ApproximateTerrainHeights.js';
  2. import Cartesian3 from '../Core/Cartesian3.js';
  3. import Check from '../Core/Check.js';
  4. import Color from '../Core/Color.js';
  5. import ColorGeometryInstanceAttribute from '../Core/ColorGeometryInstanceAttribute.js';
  6. import CorridorGeometry from '../Core/CorridorGeometry.js';
  7. import CorridorOutlineGeometry from '../Core/CorridorOutlineGeometry.js';
  8. import defined from '../Core/defined.js';
  9. import DeveloperError from '../Core/DeveloperError.js';
  10. import DistanceDisplayConditionGeometryInstanceAttribute from '../Core/DistanceDisplayConditionGeometryInstanceAttribute.js';
  11. import GeometryInstance from '../Core/GeometryInstance.js';
  12. import Iso8601 from '../Core/Iso8601.js';
  13. import OffsetGeometryInstanceAttribute from '../Core/OffsetGeometryInstanceAttribute.js';
  14. import Rectangle from '../Core/Rectangle.js';
  15. import ShowGeometryInstanceAttribute from '../Core/ShowGeometryInstanceAttribute.js';
  16. import HeightReference from '../Scene/HeightReference.js';
  17. import MaterialAppearance from '../Scene/MaterialAppearance.js';
  18. import PerInstanceColorAppearance from '../Scene/PerInstanceColorAppearance.js';
  19. import ColorMaterialProperty from './ColorMaterialProperty.js';
  20. import DynamicGeometryUpdater from './DynamicGeometryUpdater.js';
  21. import GeometryUpdater from './GeometryUpdater.js';
  22. import GroundGeometryUpdater from './GroundGeometryUpdater.js';
  23. import Property from './Property.js';
  24. var scratchColor = new Color();
  25. var defaultOffset = Cartesian3.ZERO;
  26. var offsetScratch = new Cartesian3();
  27. var scratchRectangle = new Rectangle();
  28. function CorridorGeometryOptions(entity) {
  29. this.id = entity;
  30. this.vertexFormat = undefined;
  31. this.positions = undefined;
  32. this.width = undefined;
  33. this.cornerType = undefined;
  34. this.height = undefined;
  35. this.extrudedHeight = undefined;
  36. this.granularity = undefined;
  37. this.offsetAttribute = undefined;
  38. }
  39. /**
  40. * A {@link GeometryUpdater} for corridors.
  41. * Clients do not normally create this class directly, but instead rely on {@link DataSourceDisplay}.
  42. * @alias CorridorGeometryUpdater
  43. * @constructor
  44. *
  45. * @param {Entity} entity The entity containing the geometry to be visualized.
  46. * @param {Scene} scene The scene where visualization is taking place.
  47. */
  48. function CorridorGeometryUpdater(entity, scene) {
  49. GroundGeometryUpdater.call(this, {
  50. entity : entity,
  51. scene : scene,
  52. geometryOptions : new CorridorGeometryOptions(entity),
  53. geometryPropertyName : 'corridor',
  54. observedPropertyNames : ['availability', 'corridor']
  55. });
  56. this._onEntityPropertyChanged(entity, 'corridor', entity.corridor, undefined);
  57. }
  58. if (defined(Object.create)) {
  59. CorridorGeometryUpdater.prototype = Object.create(GroundGeometryUpdater.prototype);
  60. CorridorGeometryUpdater.prototype.constructor = CorridorGeometryUpdater;
  61. }
  62. /**
  63. * Creates the geometry instance which represents the fill of the geometry.
  64. *
  65. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  66. * @returns {GeometryInstance} The geometry instance representing the filled portion of the geometry.
  67. *
  68. * @exception {DeveloperError} This instance does not represent a filled geometry.
  69. */
  70. CorridorGeometryUpdater.prototype.createFillGeometryInstance = function(time) {
  71. //>>includeStart('debug', pragmas.debug);
  72. Check.defined('time', time);
  73. if (!this._fillEnabled) {
  74. throw new DeveloperError('This instance does not represent a filled geometry.');
  75. }
  76. //>>includeEnd('debug');
  77. var entity = this._entity;
  78. var isAvailable = entity.isAvailable(time);
  79. var attributes = {
  80. show : new ShowGeometryInstanceAttribute(isAvailable && entity.isShowing && this._showProperty.getValue(time) && this._fillProperty.getValue(time)),
  81. distanceDisplayCondition : DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(this._distanceDisplayConditionProperty.getValue(time)),
  82. offset : undefined,
  83. color : undefined
  84. };
  85. if (this._materialProperty instanceof ColorMaterialProperty) {
  86. var currentColor;
  87. if (defined(this._materialProperty.color) && (this._materialProperty.color.isConstant || isAvailable)) {
  88. currentColor = this._materialProperty.color.getValue(time, scratchColor);
  89. }
  90. if (!defined(currentColor)) {
  91. currentColor = Color.WHITE;
  92. }
  93. attributes.color = ColorGeometryInstanceAttribute.fromColor(currentColor);
  94. }
  95. if (defined(this._options.offsetAttribute)) {
  96. attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(Property.getValueOrDefault(this._terrainOffsetProperty, time, defaultOffset, offsetScratch));
  97. }
  98. return new GeometryInstance({
  99. id : entity,
  100. geometry : new CorridorGeometry(this._options),
  101. attributes : attributes
  102. });
  103. };
  104. /**
  105. * Creates the geometry instance which represents the outline of the geometry.
  106. *
  107. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  108. * @returns {GeometryInstance} The geometry instance representing the outline portion of the geometry.
  109. *
  110. * @exception {DeveloperError} This instance does not represent an outlined geometry.
  111. */
  112. CorridorGeometryUpdater.prototype.createOutlineGeometryInstance = function(time) {
  113. //>>includeStart('debug', pragmas.debug);
  114. Check.defined('time', time);
  115. if (!this._outlineEnabled) {
  116. throw new DeveloperError('This instance does not represent an outlined geometry.');
  117. }
  118. //>>includeEnd('debug');
  119. var entity = this._entity;
  120. var isAvailable = entity.isAvailable(time);
  121. var outlineColor = Property.getValueOrDefault(this._outlineColorProperty, time, Color.BLACK, scratchColor);
  122. var attributes = {
  123. show : new ShowGeometryInstanceAttribute(isAvailable && entity.isShowing && this._showProperty.getValue(time) && this._showOutlineProperty.getValue(time)),
  124. color : ColorGeometryInstanceAttribute.fromColor(outlineColor),
  125. distanceDisplayCondition : DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(this._distanceDisplayConditionProperty.getValue(time)),
  126. offset : undefined
  127. };
  128. if (defined(this._options.offsetAttribute)) {
  129. attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(Property.getValueOrDefault(this._terrainOffsetProperty, time, defaultOffset, offsetScratch));
  130. }
  131. return new GeometryInstance({
  132. id : entity,
  133. geometry : new CorridorOutlineGeometry(this._options),
  134. attributes : attributes
  135. });
  136. };
  137. CorridorGeometryUpdater.prototype._computeCenter = function(time, result) {
  138. var positions = Property.getValueOrUndefined(this._entity.corridor.positions, time);
  139. if (!defined(positions) || positions.length === 0) {
  140. return;
  141. }
  142. return Cartesian3.clone(positions[Math.floor(positions.length / 2.0)], result);
  143. };
  144. CorridorGeometryUpdater.prototype._isHidden = function(entity, corridor) {
  145. return !defined(corridor.positions) || !defined(corridor.width) || GeometryUpdater.prototype._isHidden.call(this, entity, corridor);
  146. };
  147. CorridorGeometryUpdater.prototype._isDynamic = function(entity, corridor) {
  148. return !corridor.positions.isConstant || //
  149. !Property.isConstant(corridor.height) || //
  150. !Property.isConstant(corridor.extrudedHeight) || //
  151. !Property.isConstant(corridor.granularity) || //
  152. !Property.isConstant(corridor.width) || //
  153. !Property.isConstant(corridor.outlineWidth) || //
  154. !Property.isConstant(corridor.cornerType) || //
  155. !Property.isConstant(corridor.zIndex) || //
  156. (this._onTerrain && !Property.isConstant(this._materialProperty));
  157. };
  158. CorridorGeometryUpdater.prototype._setStaticOptions = function(entity, corridor) {
  159. var heightValue = Property.getValueOrUndefined(corridor.height, Iso8601.MINIMUM_VALUE);
  160. var heightReferenceValue = Property.getValueOrDefault(corridor.heightReference, Iso8601.MINIMUM_VALUE, HeightReference.NONE);
  161. var extrudedHeightValue = Property.getValueOrUndefined(corridor.extrudedHeight, Iso8601.MINIMUM_VALUE);
  162. var extrudedHeightReferenceValue = Property.getValueOrDefault(corridor.extrudedHeightReference, Iso8601.MINIMUM_VALUE, HeightReference.NONE);
  163. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  164. heightValue = 0;
  165. }
  166. var options = this._options;
  167. options.vertexFormat = (this._materialProperty instanceof ColorMaterialProperty) ? PerInstanceColorAppearance.VERTEX_FORMAT : MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat;
  168. options.positions = corridor.positions.getValue(Iso8601.MINIMUM_VALUE, options.positions);
  169. options.width = corridor.width.getValue(Iso8601.MINIMUM_VALUE);
  170. options.granularity = Property.getValueOrUndefined(corridor.granularity, Iso8601.MINIMUM_VALUE);
  171. options.cornerType = Property.getValueOrUndefined(corridor.cornerType, Iso8601.MINIMUM_VALUE);
  172. options.offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(heightValue, heightReferenceValue, extrudedHeightValue, extrudedHeightReferenceValue);
  173. options.height = GroundGeometryUpdater.getGeometryHeight(heightValue, heightReferenceValue);
  174. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(extrudedHeightValue, extrudedHeightReferenceValue);
  175. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  176. extrudedHeightValue = ApproximateTerrainHeights.getMinimumMaximumHeights(CorridorGeometry.computeRectangle(options, scratchRectangle)).minimumTerrainHeight;
  177. }
  178. options.extrudedHeight = extrudedHeightValue;
  179. };
  180. CorridorGeometryUpdater.DynamicGeometryUpdater = DynamicCorridorGeometryUpdater;
  181. /**
  182. * @private
  183. */
  184. function DynamicCorridorGeometryUpdater(geometryUpdater, primitives, groundPrimitives) {
  185. DynamicGeometryUpdater.call(this, geometryUpdater, primitives, groundPrimitives);
  186. }
  187. if (defined(Object.create)) {
  188. DynamicCorridorGeometryUpdater.prototype = Object.create(DynamicGeometryUpdater.prototype);
  189. DynamicCorridorGeometryUpdater.prototype.constructor = DynamicCorridorGeometryUpdater;
  190. }
  191. DynamicCorridorGeometryUpdater.prototype._isHidden = function(entity, corridor, time) {
  192. var options = this._options;
  193. return !defined(options.positions) || !defined(options.width) || DynamicGeometryUpdater.prototype._isHidden.call(this, entity, corridor, time);
  194. };
  195. DynamicCorridorGeometryUpdater.prototype._setOptions = function(entity, corridor, time) {
  196. var options = this._options;
  197. var heightValue = Property.getValueOrUndefined(corridor.height, time);
  198. var heightReferenceValue = Property.getValueOrDefault(corridor.heightReference, time, HeightReference.NONE);
  199. var extrudedHeightValue = Property.getValueOrUndefined(corridor.extrudedHeight, time);
  200. var extrudedHeightReferenceValue = Property.getValueOrDefault(corridor.extrudedHeightReference, time, HeightReference.NONE);
  201. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  202. heightValue = 0;
  203. }
  204. options.positions = Property.getValueOrUndefined(corridor.positions, time);
  205. options.width = Property.getValueOrUndefined(corridor.width, time);
  206. options.granularity = Property.getValueOrUndefined(corridor.granularity, time);
  207. options.cornerType = Property.getValueOrUndefined(corridor.cornerType, time);
  208. options.offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(heightValue, heightReferenceValue, extrudedHeightValue, extrudedHeightReferenceValue);
  209. options.height = GroundGeometryUpdater.getGeometryHeight(heightValue, heightReferenceValue);
  210. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(extrudedHeightValue, extrudedHeightReferenceValue);
  211. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  212. extrudedHeightValue = ApproximateTerrainHeights.getMinimumMaximumHeights(CorridorGeometry.computeRectangle(options, scratchRectangle)).minimumTerrainHeight;
  213. }
  214. options.extrudedHeight = extrudedHeightValue;
  215. };
  216. export default CorridorGeometryUpdater;