CylinderGeometryUpdater.js 12 KB

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