BoxGeometryUpdater.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import BoxGeometry from '../Core/BoxGeometry.js';
  2. import BoxOutlineGeometry from '../Core/BoxOutlineGeometry.js';
  3. import Cartesian3 from '../Core/Cartesian3.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 defineProperties from '../Core/defineProperties.js';
  9. import DeveloperError from '../Core/DeveloperError.js';
  10. import DistanceDisplayConditionGeometryInstanceAttribute from '../Core/DistanceDisplayConditionGeometryInstanceAttribute.js';
  11. import GeometryInstance from '../Core/GeometryInstance.js';
  12. import GeometryOffsetAttribute from '../Core/GeometryOffsetAttribute.js';
  13. import Iso8601 from '../Core/Iso8601.js';
  14. import OffsetGeometryInstanceAttribute from '../Core/OffsetGeometryInstanceAttribute.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 heightReferenceOnEntityPropertyChanged from './heightReferenceOnEntityPropertyChanged.js';
  23. import Property from './Property.js';
  24. var defaultOffset = Cartesian3.ZERO;
  25. var offsetScratch = new Cartesian3();
  26. var positionScratch = new Cartesian3();
  27. var scratchColor = new Color();
  28. function BoxGeometryOptions(entity) {
  29. this.id = entity;
  30. this.vertexFormat = undefined;
  31. this.dimensions = undefined;
  32. this.offsetAttribute = undefined;
  33. }
  34. /**
  35. * A {@link GeometryUpdater} for boxes.
  36. * Clients do not normally create this class directly, but instead rely on {@link DataSourceDisplay}.
  37. * @alias BoxGeometryUpdater
  38. * @constructor
  39. *
  40. * @param {Entity} entity The entity containing the geometry to be visualized.
  41. * @param {Scene} scene The scene where visualization is taking place.
  42. */
  43. function BoxGeometryUpdater(entity, scene) {
  44. GeometryUpdater.call(this, {
  45. entity : entity,
  46. scene : scene,
  47. geometryOptions : new BoxGeometryOptions(entity),
  48. geometryPropertyName : 'box',
  49. observedPropertyNames : ['availability', 'position', 'orientation', 'box']
  50. });
  51. this._onEntityPropertyChanged(entity, 'box', entity.box, undefined);
  52. }
  53. if (defined(Object.create)) {
  54. BoxGeometryUpdater.prototype = Object.create(GeometryUpdater.prototype);
  55. BoxGeometryUpdater.prototype.constructor = BoxGeometryUpdater;
  56. }
  57. defineProperties(BoxGeometryUpdater.prototype, {
  58. /**
  59. * Gets the terrain offset property
  60. * @type {TerrainOffsetProperty}
  61. * @memberof BoxGeometryUpdater.prototype
  62. * @readonly
  63. */
  64. terrainOffsetProperty: {
  65. get: function() {
  66. return this._terrainOffsetProperty;
  67. }
  68. }
  69. });
  70. /**
  71. * Creates the geometry instance which represents the fill of the geometry.
  72. *
  73. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  74. * @returns {GeometryInstance} The geometry instance representing the filled portion of the geometry.
  75. *
  76. * @exception {DeveloperError} This instance does not represent a filled geometry.
  77. */
  78. BoxGeometryUpdater.prototype.createFillGeometryInstance = function(time) {
  79. //>>includeStart('debug', pragmas.debug);
  80. Check.defined('time', time);
  81. if (!this._fillEnabled) {
  82. throw new DeveloperError('This instance does not represent a filled geometry.');
  83. }
  84. //>>includeEnd('debug');
  85. var entity = this._entity;
  86. var isAvailable = entity.isAvailable(time);
  87. var show = new ShowGeometryInstanceAttribute(isAvailable && entity.isShowing && this._showProperty.getValue(time) && this._fillProperty.getValue(time));
  88. var distanceDisplayCondition = this._distanceDisplayConditionProperty.getValue(time);
  89. var distanceDisplayConditionAttribute = DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(distanceDisplayCondition);
  90. var attributes = {
  91. show : show,
  92. distanceDisplayCondition : distanceDisplayConditionAttribute,
  93. color : undefined,
  94. offset: undefined
  95. };
  96. if (this._materialProperty instanceof ColorMaterialProperty) {
  97. var currentColor;
  98. if (defined(this._materialProperty.color) && (this._materialProperty.color.isConstant || isAvailable)) {
  99. currentColor = this._materialProperty.color.getValue(time, scratchColor);
  100. }
  101. if (!defined(currentColor)) {
  102. currentColor = Color.WHITE;
  103. }
  104. attributes.color = ColorGeometryInstanceAttribute.fromColor(currentColor);
  105. }
  106. if (defined(this._options.offsetAttribute)) {
  107. attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(Property.getValueOrDefault(this._terrainOffsetProperty, time, defaultOffset, offsetScratch));
  108. }
  109. return new GeometryInstance({
  110. id : entity,
  111. geometry : BoxGeometry.fromDimensions(this._options),
  112. modelMatrix : entity.computeModelMatrixForHeightReference(time, entity.box.heightReference, this._options.dimensions.z * 0.5, this._scene.mapProjection.ellipsoid),
  113. attributes : attributes
  114. });
  115. };
  116. /**
  117. * Creates the geometry instance which represents the outline of the geometry.
  118. *
  119. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  120. * @returns {GeometryInstance} The geometry instance representing the outline portion of the geometry.
  121. *
  122. * @exception {DeveloperError} This instance does not represent an outlined geometry.
  123. */
  124. BoxGeometryUpdater.prototype.createOutlineGeometryInstance = function(time) {
  125. //>>includeStart('debug', pragmas.debug);
  126. Check.defined('time', time);
  127. if (!this._outlineEnabled) {
  128. throw new DeveloperError('This instance does not represent an outlined geometry.');
  129. }
  130. //>>includeEnd('debug');
  131. var entity = this._entity;
  132. var isAvailable = entity.isAvailable(time);
  133. var outlineColor = Property.getValueOrDefault(this._outlineColorProperty, time, Color.BLACK, scratchColor);
  134. var distanceDisplayCondition = this._distanceDisplayConditionProperty.getValue(time);
  135. var attributes = {
  136. show : new ShowGeometryInstanceAttribute(isAvailable && entity.isShowing && this._showProperty.getValue(time) && this._showOutlineProperty.getValue(time)),
  137. color : ColorGeometryInstanceAttribute.fromColor(outlineColor),
  138. distanceDisplayCondition : DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(distanceDisplayCondition),
  139. offset : undefined
  140. };
  141. if (defined(this._options.offsetAttribute)) {
  142. attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(Property.getValueOrDefault(this._terrainOffsetProperty, time, defaultOffset, offsetScratch));
  143. }
  144. return new GeometryInstance({
  145. id : entity,
  146. geometry : BoxOutlineGeometry.fromDimensions(this._options),
  147. modelMatrix : entity.computeModelMatrixForHeightReference(time, entity.box.heightReference, this._options.dimensions.z * 0.5, this._scene.mapProjection.ellipsoid),
  148. attributes : attributes
  149. });
  150. };
  151. BoxGeometryUpdater.prototype._computeCenter = function(time, result) {
  152. return Property.getValueOrUndefined(this._entity.position, time, result);
  153. };
  154. BoxGeometryUpdater.prototype._isHidden = function(entity, box) {
  155. return !defined(box.dimensions) || !defined(entity.position) || GeometryUpdater.prototype._isHidden.call(this, entity, box);
  156. };
  157. BoxGeometryUpdater.prototype._isDynamic = function(entity, box) {
  158. return !entity.position.isConstant || !Property.isConstant(entity.orientation) || !box.dimensions.isConstant || !Property.isConstant(box.outlineWidth);
  159. };
  160. BoxGeometryUpdater.prototype._setStaticOptions = function(entity, box) {
  161. var heightReference = Property.getValueOrDefault(box.heightReference, Iso8601.MINIMUM_VALUE, HeightReference.NONE);
  162. var options = this._options;
  163. options.vertexFormat = this._materialProperty instanceof ColorMaterialProperty ? PerInstanceColorAppearance.VERTEX_FORMAT : MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat;
  164. options.dimensions = box.dimensions.getValue(Iso8601.MINIMUM_VALUE, options.dimensions);
  165. options.offsetAttribute = heightReference !== HeightReference.NONE ? GeometryOffsetAttribute.ALL : undefined;
  166. };
  167. BoxGeometryUpdater.prototype._onEntityPropertyChanged = heightReferenceOnEntityPropertyChanged;
  168. BoxGeometryUpdater.DynamicGeometryUpdater = DynamicBoxGeometryUpdater;
  169. /**
  170. * @private
  171. */
  172. function DynamicBoxGeometryUpdater(geometryUpdater, primitives, groundPrimitives) {
  173. DynamicGeometryUpdater.call(this, geometryUpdater, primitives, groundPrimitives);
  174. }
  175. if (defined(Object.create)) {
  176. DynamicBoxGeometryUpdater.prototype = Object.create(DynamicGeometryUpdater.prototype);
  177. DynamicBoxGeometryUpdater.prototype.constructor = DynamicBoxGeometryUpdater;
  178. }
  179. DynamicBoxGeometryUpdater.prototype._isHidden = function(entity, box, time) {
  180. var position = Property.getValueOrUndefined(entity.position, time, positionScratch);
  181. var dimensions = this._options.dimensions;
  182. return !defined(position) || !defined(dimensions) || DynamicGeometryUpdater.prototype._isHidden.call(this, entity, box, time);
  183. };
  184. DynamicBoxGeometryUpdater.prototype._setOptions = function(entity, box, time) {
  185. var heightReference = Property.getValueOrDefault(box.heightReference, time, HeightReference.NONE);
  186. var options = this._options;
  187. options.dimensions = Property.getValueOrUndefined(box.dimensions, time, options.dimensions);
  188. options.offsetAttribute = heightReference !== HeightReference.NONE ? GeometryOffsetAttribute.ALL : undefined;
  189. };
  190. export default BoxGeometryUpdater;