RectangleGeometryUpdater.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. var scratchColor = new Color();
  27. var defaultOffset = Cartesian3.ZERO;
  28. var offsetScratch = new Cartesian3();
  29. var scratchRectangle = new Rectangle();
  30. var scratchCenterRect = new Rectangle();
  31. var 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. var entity = this._entity;
  93. var isAvailable = entity.isAvailable(time);
  94. var 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. var 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. var entity = this._entity;
  156. var isAvailable = entity.isAvailable(time);
  157. var outlineColor = Property.getValueOrDefault(
  158. this._outlineColorProperty,
  159. time,
  160. Color.BLACK,
  161. scratchColor
  162. );
  163. var distanceDisplayCondition = this._distanceDisplayConditionProperty.getValue(
  164. time
  165. );
  166. var 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. var rect = Property.getValueOrUndefined(
  197. this._entity.rectangle.coordinates,
  198. time,
  199. scratchCenterRect
  200. );
  201. if (!defined(rect)) {
  202. return;
  203. }
  204. var 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. var isColorMaterial = this._materialProperty instanceof ColorMaterialProperty;
  233. var heightValue = Property.getValueOrUndefined(
  234. rectangle.height,
  235. Iso8601.MINIMUM_VALUE
  236. );
  237. var heightReferenceValue = Property.getValueOrDefault(
  238. rectangle.heightReference,
  239. Iso8601.MINIMUM_VALUE,
  240. HeightReference.NONE
  241. );
  242. var extrudedHeightValue = Property.getValueOrUndefined(
  243. rectangle.extrudedHeight,
  244. Iso8601.MINIMUM_VALUE
  245. );
  246. var extrudedHeightReferenceValue = Property.getValueOrDefault(
  247. rectangle.extrudedHeightReference,
  248. Iso8601.MINIMUM_VALUE,
  249. HeightReference.NONE
  250. );
  251. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  252. heightValue = 0;
  253. }
  254. var options = this._options;
  255. options.vertexFormat = isColorMaterial
  256. ? PerInstanceColorAppearance.VERTEX_FORMAT
  257. : MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat;
  258. options.rectangle = rectangle.coordinates.getValue(
  259. Iso8601.MINIMUM_VALUE,
  260. options.rectangle
  261. );
  262. options.granularity = Property.getValueOrUndefined(
  263. rectangle.granularity,
  264. Iso8601.MINIMUM_VALUE
  265. );
  266. options.stRotation = Property.getValueOrUndefined(
  267. rectangle.stRotation,
  268. Iso8601.MINIMUM_VALUE
  269. );
  270. options.rotation = Property.getValueOrUndefined(
  271. rectangle.rotation,
  272. Iso8601.MINIMUM_VALUE
  273. );
  274. options.offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(
  275. heightValue,
  276. heightReferenceValue,
  277. extrudedHeightValue,
  278. extrudedHeightReferenceValue
  279. );
  280. options.height = GroundGeometryUpdater.getGeometryHeight(
  281. heightValue,
  282. heightReferenceValue
  283. );
  284. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(
  285. extrudedHeightValue,
  286. extrudedHeightReferenceValue
  287. );
  288. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  289. extrudedHeightValue = ApproximateTerrainHeights.getMinimumMaximumHeights(
  290. RectangleGeometry.computeRectangle(options, scratchRectangle)
  291. ).minimumTerrainHeight;
  292. }
  293. options.extrudedHeight = extrudedHeightValue;
  294. };
  295. RectangleGeometryUpdater.DynamicGeometryUpdater = DynamicRectangleGeometryUpdater;
  296. /**
  297. * @private
  298. */
  299. function DynamicRectangleGeometryUpdater(
  300. geometryUpdater,
  301. primitives,
  302. groundPrimitives
  303. ) {
  304. DynamicGeometryUpdater.call(
  305. this,
  306. geometryUpdater,
  307. primitives,
  308. groundPrimitives
  309. );
  310. }
  311. if (defined(Object.create)) {
  312. DynamicRectangleGeometryUpdater.prototype = Object.create(
  313. DynamicGeometryUpdater.prototype
  314. );
  315. DynamicRectangleGeometryUpdater.prototype.constructor = DynamicRectangleGeometryUpdater;
  316. }
  317. DynamicRectangleGeometryUpdater.prototype._isHidden = function (
  318. entity,
  319. rectangle,
  320. time
  321. ) {
  322. return (
  323. !defined(this._options.rectangle) ||
  324. DynamicGeometryUpdater.prototype._isHidden.call(
  325. this,
  326. entity,
  327. rectangle,
  328. time
  329. )
  330. );
  331. };
  332. DynamicRectangleGeometryUpdater.prototype._setOptions = function (
  333. entity,
  334. rectangle,
  335. time
  336. ) {
  337. var options = this._options;
  338. var heightValue = Property.getValueOrUndefined(rectangle.height, time);
  339. var heightReferenceValue = Property.getValueOrDefault(
  340. rectangle.heightReference,
  341. time,
  342. HeightReference.NONE
  343. );
  344. var extrudedHeightValue = Property.getValueOrUndefined(
  345. rectangle.extrudedHeight,
  346. time
  347. );
  348. var extrudedHeightReferenceValue = Property.getValueOrDefault(
  349. rectangle.extrudedHeightReference,
  350. time,
  351. HeightReference.NONE
  352. );
  353. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  354. heightValue = 0;
  355. }
  356. options.rectangle = Property.getValueOrUndefined(
  357. rectangle.coordinates,
  358. time,
  359. options.rectangle
  360. );
  361. options.granularity = Property.getValueOrUndefined(
  362. rectangle.granularity,
  363. time
  364. );
  365. options.stRotation = Property.getValueOrUndefined(rectangle.stRotation, time);
  366. options.rotation = Property.getValueOrUndefined(rectangle.rotation, time);
  367. options.offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(
  368. heightValue,
  369. heightReferenceValue,
  370. extrudedHeightValue,
  371. extrudedHeightReferenceValue
  372. );
  373. options.height = GroundGeometryUpdater.getGeometryHeight(
  374. heightValue,
  375. heightReferenceValue
  376. );
  377. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(
  378. extrudedHeightValue,
  379. extrudedHeightReferenceValue
  380. );
  381. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  382. extrudedHeightValue = ApproximateTerrainHeights.getMinimumMaximumHeights(
  383. RectangleGeometry.computeRectangle(options, scratchRectangle)
  384. ).minimumTerrainHeight;
  385. }
  386. options.extrudedHeight = extrudedHeightValue;
  387. };
  388. export default RectangleGeometryUpdater;