RectangleGeometryUpdater.js 13 KB

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