GroundGeometryUpdater.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import Check from '../Core/Check.js';
  2. import defaultValue from '../Core/defaultValue.js';
  3. import defined from '../Core/defined.js';
  4. import defineProperties from '../Core/defineProperties.js';
  5. import DeveloperError from '../Core/DeveloperError.js';
  6. import GeometryOffsetAttribute from '../Core/GeometryOffsetAttribute.js';
  7. import oneTimeWarning from '../Core/oneTimeWarning.js';
  8. import GroundPrimitive from '../Scene/GroundPrimitive.js';
  9. import HeightReference from '../Scene/HeightReference.js';
  10. import CallbackProperty from './CallbackProperty.js';
  11. import ConstantProperty from './ConstantProperty.js';
  12. import GeometryUpdater from './GeometryUpdater.js';
  13. import TerrainOffsetProperty from './TerrainOffsetProperty.js';
  14. var defaultZIndex = new ConstantProperty(0);
  15. /**
  16. * An abstract class for updating ground geometry entities.
  17. * @constructor
  18. * @alias GroundGeometryUpdater
  19. * @param {Object} options An object with the following properties:
  20. * @param {Entity} options.entity The entity containing the geometry to be visualized.
  21. * @param {Scene} options.scene The scene where visualization is taking place.
  22. * @param {Object} options.geometryOptions Options for the geometry
  23. * @param {String} options.geometryPropertyName The geometry property name
  24. * @param {String[]} options.observedPropertyNames The entity properties this geometry cares about
  25. */
  26. function GroundGeometryUpdater(options) {
  27. GeometryUpdater.call(this, options);
  28. this._zIndex = 0;
  29. this._terrainOffsetProperty = undefined;
  30. }
  31. if (defined(Object.create)) {
  32. GroundGeometryUpdater.prototype = Object.create(GeometryUpdater.prototype);
  33. GroundGeometryUpdater.prototype.constructor = GroundGeometryUpdater;
  34. }
  35. defineProperties(GroundGeometryUpdater.prototype, {
  36. /**
  37. * Gets the zindex
  38. * @type {Number}
  39. * @memberof GroundGeometryUpdater.prototype
  40. * @readonly
  41. */
  42. zIndex: {
  43. get: function() {
  44. return this._zIndex;
  45. }
  46. },
  47. /**
  48. * Gets the terrain offset property
  49. * @type {TerrainOffsetProperty}
  50. * @memberof GroundGeometryUpdater.prototype
  51. * @readonly
  52. */
  53. terrainOffsetProperty: {
  54. get: function() {
  55. return this._terrainOffsetProperty;
  56. }
  57. }
  58. });
  59. GroundGeometryUpdater.prototype._isOnTerrain = function(entity, geometry) {
  60. return this._fillEnabled && !defined(geometry.height) && !defined(geometry.extrudedHeight) && GroundPrimitive.isSupported(this._scene);
  61. };
  62. GroundGeometryUpdater.prototype._getIsClosed = function(options) {
  63. var height = options.height;
  64. var extrudedHeight = options.extrudedHeight;
  65. return height === 0 || (defined(extrudedHeight) && extrudedHeight !== height);
  66. };
  67. GroundGeometryUpdater.prototype._computeCenter = DeveloperError.throwInstantiationError;
  68. GroundGeometryUpdater.prototype._onEntityPropertyChanged = function(entity, propertyName, newValue, oldValue) {
  69. GeometryUpdater.prototype._onEntityPropertyChanged.call(this, entity, propertyName, newValue, oldValue);
  70. if (this._observedPropertyNames.indexOf(propertyName) === -1) {
  71. return;
  72. }
  73. var geometry = this._entity[this._geometryPropertyName];
  74. if (!defined(geometry)) {
  75. return;
  76. }
  77. if (defined(geometry.zIndex) && (defined(geometry.height) || defined(geometry.extrudedHeight))) {
  78. oneTimeWarning(oneTimeWarning.geometryZIndex);
  79. }
  80. this._zIndex = defaultValue(geometry.zIndex, defaultZIndex);
  81. if (defined(this._terrainOffsetProperty)) {
  82. this._terrainOffsetProperty.destroy();
  83. this._terrainOffsetProperty = undefined;
  84. }
  85. var heightReferenceProperty = geometry.heightReference;
  86. var extrudedHeightReferenceProperty = geometry.extrudedHeightReference;
  87. if (defined(heightReferenceProperty) || defined(extrudedHeightReferenceProperty)) {
  88. var centerPosition = new CallbackProperty(this._computeCenter.bind(this), !this._dynamic);
  89. this._terrainOffsetProperty = new TerrainOffsetProperty(this._scene, centerPosition, heightReferenceProperty, extrudedHeightReferenceProperty);
  90. }
  91. };
  92. /**
  93. * Destroys and resources used by the object. Once an object is destroyed, it should not be used.
  94. *
  95. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  96. */
  97. GroundGeometryUpdater.prototype.destroy = function() {
  98. if (defined(this._terrainOffsetProperty)) {
  99. this._terrainOffsetProperty.destroy();
  100. this._terrainOffsetProperty = undefined;
  101. }
  102. GeometryUpdater.prototype.destroy.call(this);
  103. };
  104. /**
  105. * @private
  106. */
  107. GroundGeometryUpdater.getGeometryHeight = function(height, heightReference) {
  108. //>>includeStart('debug', pragmas.debug);
  109. Check.defined('heightReference', heightReference);
  110. //>>includeEnd('debug');
  111. if (!defined(height)) {
  112. if (heightReference !== HeightReference.NONE) {
  113. oneTimeWarning(oneTimeWarning.geometryHeightReference);
  114. }
  115. return;
  116. }
  117. if (heightReference !== HeightReference.CLAMP_TO_GROUND) {
  118. return height;
  119. }
  120. return 0.0;
  121. };
  122. /**
  123. * @private
  124. */
  125. GroundGeometryUpdater.getGeometryExtrudedHeight = function(extrudedHeight, extrudedHeightReference) {
  126. //>>includeStart('debug', pragmas.debug);
  127. Check.defined('extrudedHeightReference', extrudedHeightReference);
  128. //>>includeEnd('debug');
  129. if (!defined(extrudedHeight)) {
  130. if (extrudedHeightReference !== HeightReference.NONE) {
  131. oneTimeWarning(oneTimeWarning.geometryExtrudedHeightReference);
  132. }
  133. return;
  134. }
  135. if (extrudedHeightReference !== HeightReference.CLAMP_TO_GROUND) {
  136. return extrudedHeight;
  137. }
  138. return GroundGeometryUpdater.CLAMP_TO_GROUND;
  139. };
  140. /**
  141. * @private
  142. */
  143. GroundGeometryUpdater.CLAMP_TO_GROUND = 'clamp';
  144. /**
  145. * @private
  146. */
  147. GroundGeometryUpdater.computeGeometryOffsetAttribute = function(height, heightReference, extrudedHeight, extrudedHeightReference) {
  148. if (!defined(height) || !defined(heightReference)) {
  149. heightReference = HeightReference.NONE;
  150. }
  151. if (!defined(extrudedHeight) || !defined(extrudedHeightReference)) {
  152. extrudedHeightReference = HeightReference.NONE;
  153. }
  154. var n = 0;
  155. if (heightReference !== HeightReference.NONE) {
  156. n++;
  157. }
  158. if (extrudedHeightReference === HeightReference.RELATIVE_TO_GROUND) {
  159. n++;
  160. }
  161. if (n === 2) {
  162. return GeometryOffsetAttribute.ALL;
  163. }
  164. if (n === 1) {
  165. return GeometryOffsetAttribute.TOP;
  166. }
  167. return undefined;
  168. };
  169. export default GroundGeometryUpdater;