RectangleGeometryUpdater.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. const scratchColor = new Color();
  27. const defaultOffset = Cartesian3.ZERO;
  28. const offsetScratch = new Cartesian3();
  29. const scratchRectangle = new Rectangle();
  30. const scratchCenterRect = new Rectangle();
  31. const 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(
  61. entity,
  62. "rectangle",
  63. entity.rectangle,
  64. undefined
  65. );
  66. }
  67. if (defined(Object.create)) {
  68. RectangleGeometryUpdater.prototype = Object.create(
  69. GroundGeometryUpdater.prototype
  70. );
  71. RectangleGeometryUpdater.prototype.constructor = RectangleGeometryUpdater;
  72. }
  73. /**
  74. * Creates the geometry instance which represents the fill of the geometry.
  75. *
  76. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  77. * @returns {GeometryInstance} The geometry instance representing the filled portion of the geometry.
  78. *
  79. * @exception {DeveloperError} This instance does not represent a filled geometry.
  80. */
  81. RectangleGeometryUpdater.prototype.createFillGeometryInstance = function (
  82. time
  83. ) {
  84. //>>includeStart('debug', pragmas.debug);
  85. Check.defined("time", time);
  86. if (!this._fillEnabled) {
  87. throw new DeveloperError(
  88. "This instance does not represent a filled geometry."
  89. );
  90. }
  91. //>>includeEnd('debug');
  92. const entity = this._entity;
  93. const isAvailable = entity.isAvailable(time);
  94. const attributes = {
  95. show: new ShowGeometryInstanceAttribute(
  96. isAvailable &&
  97. entity.isShowing &&
  98. this._showProperty.getValue(time) &&
  99. this._fillProperty.getValue(time)
  100. ),
  101. distanceDisplayCondition: DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(
  102. this._distanceDisplayConditionProperty.getValue(time)
  103. ),
  104. offset: undefined,
  105. color: undefined,
  106. };
  107. if (this._materialProperty instanceof ColorMaterialProperty) {
  108. let currentColor;
  109. if (
  110. defined(this._materialProperty.color) &&
  111. (this._materialProperty.color.isConstant || isAvailable)
  112. ) {
  113. currentColor = this._materialProperty.color.getValue(time, scratchColor);
  114. }
  115. if (!defined(currentColor)) {
  116. currentColor = Color.WHITE;
  117. }
  118. attributes.color = ColorGeometryInstanceAttribute.fromColor(currentColor);
  119. }
  120. if (defined(this._options.offsetAttribute)) {
  121. attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(
  122. Property.getValueOrDefault(
  123. this._terrainOffsetProperty,
  124. time,
  125. defaultOffset,
  126. offsetScratch
  127. )
  128. );
  129. }
  130. return new GeometryInstance({
  131. id: entity,
  132. geometry: new RectangleGeometry(this._options),
  133. attributes: attributes,
  134. });
  135. };
  136. /**
  137. * Creates the geometry instance which represents the outline of the geometry.
  138. *
  139. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  140. * @returns {GeometryInstance} The geometry instance representing the outline portion of the geometry.
  141. *
  142. * @exception {DeveloperError} This instance does not represent an outlined geometry.
  143. */
  144. RectangleGeometryUpdater.prototype.createOutlineGeometryInstance = function (
  145. time
  146. ) {
  147. //>>includeStart('debug', pragmas.debug);
  148. Check.defined("time", time);
  149. if (!this._outlineEnabled) {
  150. throw new DeveloperError(
  151. "This instance does not represent an outlined geometry."
  152. );
  153. }
  154. //>>includeEnd('debug');
  155. const entity = this._entity;
  156. const isAvailable = entity.isAvailable(time);
  157. const outlineColor = Property.getValueOrDefault(
  158. this._outlineColorProperty,
  159. time,
  160. Color.BLACK,
  161. scratchColor
  162. );
  163. const distanceDisplayCondition = this._distanceDisplayConditionProperty.getValue(
  164. time
  165. );
  166. const attributes = {
  167. show: new ShowGeometryInstanceAttribute(
  168. isAvailable &&
  169. entity.isShowing &&
  170. this._showProperty.getValue(time) &&
  171. this._showOutlineProperty.getValue(time)
  172. ),
  173. color: ColorGeometryInstanceAttribute.fromColor(outlineColor),
  174. distanceDisplayCondition: DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(
  175. distanceDisplayCondition
  176. ),
  177. offset: undefined,
  178. };
  179. if (defined(this._options.offsetAttribute)) {
  180. attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(
  181. Property.getValueOrDefault(
  182. this._terrainOffsetProperty,
  183. time,
  184. defaultOffset,
  185. offsetScratch
  186. )
  187. );
  188. }
  189. return new GeometryInstance({
  190. id: entity,
  191. geometry: new RectangleOutlineGeometry(this._options),
  192. attributes: attributes,
  193. });
  194. };
  195. RectangleGeometryUpdater.prototype._computeCenter = function (time, result) {
  196. const rect = Property.getValueOrUndefined(
  197. this._entity.rectangle.coordinates,
  198. time,
  199. scratchCenterRect
  200. );
  201. if (!defined(rect)) {
  202. return;
  203. }
  204. const center = Rectangle.center(rect, scratchCarto);
  205. return Cartographic.toCartesian(center, Ellipsoid.WGS84, result);
  206. };
  207. RectangleGeometryUpdater.prototype._isHidden = function (entity, rectangle) {
  208. return (
  209. !defined(rectangle.coordinates) ||
  210. GeometryUpdater.prototype._isHidden.call(this, entity, rectangle)
  211. );
  212. };
  213. RectangleGeometryUpdater.prototype._isDynamic = function (entity, rectangle) {
  214. return (
  215. !rectangle.coordinates.isConstant || //
  216. !Property.isConstant(rectangle.height) || //
  217. !Property.isConstant(rectangle.extrudedHeight) || //
  218. !Property.isConstant(rectangle.granularity) || //
  219. !Property.isConstant(rectangle.stRotation) || //
  220. !Property.isConstant(rectangle.rotation) || //
  221. !Property.isConstant(rectangle.outlineWidth) || //
  222. !Property.isConstant(rectangle.zIndex) || //
  223. (this._onTerrain &&
  224. !Property.isConstant(this._materialProperty) &&
  225. !(this._materialProperty instanceof ColorMaterialProperty))
  226. );
  227. };
  228. RectangleGeometryUpdater.prototype._setStaticOptions = function (
  229. entity,
  230. rectangle
  231. ) {
  232. const isColorMaterial =
  233. this._materialProperty instanceof ColorMaterialProperty;
  234. let heightValue = Property.getValueOrUndefined(
  235. rectangle.height,
  236. Iso8601.MINIMUM_VALUE
  237. );
  238. const heightReferenceValue = Property.getValueOrDefault(
  239. rectangle.heightReference,
  240. Iso8601.MINIMUM_VALUE,
  241. HeightReference.NONE
  242. );
  243. let extrudedHeightValue = Property.getValueOrUndefined(
  244. rectangle.extrudedHeight,
  245. Iso8601.MINIMUM_VALUE
  246. );
  247. const extrudedHeightReferenceValue = Property.getValueOrDefault(
  248. rectangle.extrudedHeightReference,
  249. Iso8601.MINIMUM_VALUE,
  250. HeightReference.NONE
  251. );
  252. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  253. heightValue = 0;
  254. }
  255. const options = this._options;
  256. options.vertexFormat = isColorMaterial
  257. ? PerInstanceColorAppearance.VERTEX_FORMAT
  258. : MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat;
  259. options.rectangle = rectangle.coordinates.getValue(
  260. Iso8601.MINIMUM_VALUE,
  261. options.rectangle
  262. );
  263. options.granularity = Property.getValueOrUndefined(
  264. rectangle.granularity,
  265. Iso8601.MINIMUM_VALUE
  266. );
  267. options.stRotation = Property.getValueOrUndefined(
  268. rectangle.stRotation,
  269. Iso8601.MINIMUM_VALUE
  270. );
  271. options.rotation = Property.getValueOrUndefined(
  272. rectangle.rotation,
  273. Iso8601.MINIMUM_VALUE
  274. );
  275. options.offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(
  276. heightValue,
  277. heightReferenceValue,
  278. extrudedHeightValue,
  279. extrudedHeightReferenceValue
  280. );
  281. options.height = GroundGeometryUpdater.getGeometryHeight(
  282. heightValue,
  283. heightReferenceValue
  284. );
  285. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(
  286. extrudedHeightValue,
  287. extrudedHeightReferenceValue
  288. );
  289. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  290. extrudedHeightValue = ApproximateTerrainHeights.getMinimumMaximumHeights(
  291. RectangleGeometry.computeRectangle(options, scratchRectangle)
  292. ).minimumTerrainHeight;
  293. }
  294. options.extrudedHeight = extrudedHeightValue;
  295. };
  296. RectangleGeometryUpdater.DynamicGeometryUpdater = DynamicRectangleGeometryUpdater;
  297. /**
  298. * @private
  299. */
  300. function DynamicRectangleGeometryUpdater(
  301. geometryUpdater,
  302. primitives,
  303. groundPrimitives
  304. ) {
  305. DynamicGeometryUpdater.call(
  306. this,
  307. geometryUpdater,
  308. primitives,
  309. groundPrimitives
  310. );
  311. }
  312. if (defined(Object.create)) {
  313. DynamicRectangleGeometryUpdater.prototype = Object.create(
  314. DynamicGeometryUpdater.prototype
  315. );
  316. DynamicRectangleGeometryUpdater.prototype.constructor = DynamicRectangleGeometryUpdater;
  317. }
  318. DynamicRectangleGeometryUpdater.prototype._isHidden = function (
  319. entity,
  320. rectangle,
  321. time
  322. ) {
  323. return (
  324. !defined(this._options.rectangle) ||
  325. DynamicGeometryUpdater.prototype._isHidden.call(
  326. this,
  327. entity,
  328. rectangle,
  329. time
  330. )
  331. );
  332. };
  333. DynamicRectangleGeometryUpdater.prototype._setOptions = function (
  334. entity,
  335. rectangle,
  336. time
  337. ) {
  338. const options = this._options;
  339. let heightValue = Property.getValueOrUndefined(rectangle.height, time);
  340. const heightReferenceValue = Property.getValueOrDefault(
  341. rectangle.heightReference,
  342. time,
  343. HeightReference.NONE
  344. );
  345. let extrudedHeightValue = Property.getValueOrUndefined(
  346. rectangle.extrudedHeight,
  347. time
  348. );
  349. const extrudedHeightReferenceValue = Property.getValueOrDefault(
  350. rectangle.extrudedHeightReference,
  351. time,
  352. HeightReference.NONE
  353. );
  354. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  355. heightValue = 0;
  356. }
  357. options.rectangle = Property.getValueOrUndefined(
  358. rectangle.coordinates,
  359. time,
  360. options.rectangle
  361. );
  362. options.granularity = Property.getValueOrUndefined(
  363. rectangle.granularity,
  364. time
  365. );
  366. options.stRotation = Property.getValueOrUndefined(rectangle.stRotation, time);
  367. options.rotation = Property.getValueOrUndefined(rectangle.rotation, time);
  368. options.offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(
  369. heightValue,
  370. heightReferenceValue,
  371. extrudedHeightValue,
  372. extrudedHeightReferenceValue
  373. );
  374. options.height = GroundGeometryUpdater.getGeometryHeight(
  375. heightValue,
  376. heightReferenceValue
  377. );
  378. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(
  379. extrudedHeightValue,
  380. extrudedHeightReferenceValue
  381. );
  382. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  383. extrudedHeightValue = ApproximateTerrainHeights.getMinimumMaximumHeights(
  384. RectangleGeometry.computeRectangle(options, scratchRectangle)
  385. ).minimumTerrainHeight;
  386. }
  387. options.extrudedHeight = extrudedHeightValue;
  388. };
  389. export default RectangleGeometryUpdater;