GroundGeometryUpdater.js 6.3 KB

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