PolygonGraphics.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import Event from "../Core/Event.js";
  5. import PolygonHierarchy from "../Core/PolygonHierarchy.js";
  6. import ConstantProperty from "./ConstantProperty.js";
  7. import createMaterialPropertyDescriptor from "./createMaterialPropertyDescriptor.js";
  8. import createPropertyDescriptor from "./createPropertyDescriptor.js";
  9. function createPolygonHierarchyProperty(value) {
  10. if (Array.isArray(value)) {
  11. // convert array of positions to PolygonHierarchy object
  12. value = new PolygonHierarchy(value);
  13. }
  14. return new ConstantProperty(value);
  15. }
  16. /**
  17. * @typedef {object} PolygonGraphics.ConstructorOptions
  18. *
  19. * Initialization options for the PolygonGraphics constructor
  20. *
  21. * @property {Property | boolean} [show=true] A boolean Property specifying the visibility of the polygon.
  22. * @property {Property | PolygonHierarchy | Cartesian3[]} [hierarchy] A Property specifying the {@link PolygonHierarchy}.
  23. * @property {Property | number} [height=0] A numeric Property specifying the altitude of the polygon relative to the ellipsoid surface.
  24. * @property {Property | HeightReference} [heightReference=HeightReference.NONE] A Property specifying what the height is relative to.
  25. * @property {Property | number} [extrudedHeight] A numeric Property specifying the altitude of the polygon's extruded face relative to the ellipsoid surface.
  26. * @property {Property | HeightReference} [extrudedHeightReference=HeightReference.NONE] A Property specifying what the extrudedHeight is relative to.
  27. * @property {Property | number} [stRotation=0.0] A numeric property specifying the rotation of the polygon texture counter-clockwise from north. Only has an effect if textureCoordinates is not defined.
  28. * @property {Property | number} [granularity=Cesium.Math.RADIANS_PER_DEGREE] A numeric Property specifying the angular distance between each latitude and longitude point.
  29. * @property {Property | boolean} [fill=true] A boolean Property specifying whether the polygon is filled with the provided material.
  30. * @property {MaterialProperty | Color} [material=Color.WHITE] A Property specifying the material used to fill the polygon.
  31. * @property {Property | boolean} [outline=false] A boolean Property specifying whether the polygon is outlined.
  32. * @property {Property | Color} [outlineColor=Color.BLACK] A Property specifying the {@link Color} of the outline.
  33. * @property {Property | number} [outlineWidth=1.0] A numeric Property specifying the width of the outline.
  34. * @property {Property | boolean} [perPositionHeight=false] A boolean specifying whether or not the height of each position is used.
  35. * @property {boolean | boolean} [closeTop=true] When false, leaves off the top of an extruded polygon open.
  36. * @property {boolean | boolean} [closeBottom=true] When false, leaves off the bottom of an extruded polygon open.
  37. * @property {Property | ArcType} [arcType=ArcType.GEODESIC] The type of line the polygon edges must follow.
  38. * @property {Property | ShadowMode} [shadows=ShadowMode.DISABLED] An enum Property specifying whether the polygon casts or receives shadows from light sources.
  39. * @property {Property | DistanceDisplayCondition} [distanceDisplayCondition] A Property specifying at what distance from the camera that this polygon will be displayed.
  40. * @property {Property | ClassificationType} [classificationType=ClassificationType.BOTH] An enum Property specifying whether this polygon will classify terrain, 3D Tiles, or both when on the ground.
  41. * @property {ConstantProperty | number} [zIndex=0] A property specifying the zIndex used for ordering ground geometry. Only has an effect if the polygon is constant and neither height or extrudedHeight are specified.
  42. * @property {Property | PolygonHierarchy} [textureCoordinates] A Property specifying texture coordinates as a {@link PolygonHierarchy} of {@link Cartesian2} points. Has no effect for ground primitives.
  43. */
  44. /**
  45. * Describes a polygon defined by an hierarchy of linear rings which make up the outer shape and any nested holes.
  46. * The polygon conforms to the curvature of the globe and can be placed on the surface or
  47. * at altitude and can optionally be extruded into a volume.
  48. *
  49. * @alias PolygonGraphics
  50. * @constructor
  51. *
  52. * @param {PolygonGraphics.ConstructorOptions} [options] Object describing initialization options
  53. *
  54. * @see Entity
  55. * @demo {@link https://sandcastle.cesium.com/index.html?src=Polygon.html|Cesium Sandcastle Polygon Demo}
  56. */
  57. function PolygonGraphics(options) {
  58. this._definitionChanged = new Event();
  59. this._show = undefined;
  60. this._showSubscription = undefined;
  61. this._hierarchy = undefined;
  62. this._hierarchySubscription = undefined;
  63. this._height = undefined;
  64. this._heightSubscription = undefined;
  65. this._heightReference = undefined;
  66. this._heightReferenceSubscription = undefined;
  67. this._extrudedHeight = undefined;
  68. this._extrudedHeightSubscription = undefined;
  69. this._extrudedHeightReference = undefined;
  70. this._extrudedHeightReferenceSubscription = undefined;
  71. this._stRotation = undefined;
  72. this._stRotationSubscription = undefined;
  73. this._granularity = undefined;
  74. this._granularitySubscription = undefined;
  75. this._fill = undefined;
  76. this._fillSubscription = undefined;
  77. this._material = undefined;
  78. this._materialSubscription = undefined;
  79. this._outline = undefined;
  80. this._outlineSubscription = undefined;
  81. this._outlineColor = undefined;
  82. this._outlineColorSubscription = undefined;
  83. this._outlineWidth = undefined;
  84. this._outlineWidthSubscription = undefined;
  85. this._perPositionHeight = undefined;
  86. this._perPositionHeightSubscription = undefined;
  87. this._closeTop = undefined;
  88. this._closeTopSubscription = undefined;
  89. this._closeBottom = undefined;
  90. this._closeBottomSubscription = undefined;
  91. this._arcType = undefined;
  92. this._arcTypeSubscription = undefined;
  93. this._shadows = undefined;
  94. this._shadowsSubscription = undefined;
  95. this._distanceDisplayCondition = undefined;
  96. this._distanceDisplayConditionSubscription = undefined;
  97. this._classificationType = undefined;
  98. this._classificationTypeSubscription = undefined;
  99. this._zIndex = undefined;
  100. this._zIndexSubscription = undefined;
  101. this._textureCoordinates = undefined;
  102. this._textureCoordinatesSubscription = undefined;
  103. this.merge(defaultValue(options, defaultValue.EMPTY_OBJECT));
  104. }
  105. Object.defineProperties(PolygonGraphics.prototype, {
  106. /**
  107. * Gets the event that is raised whenever a property or sub-property is changed or modified.
  108. * @memberof PolygonGraphics.prototype
  109. *
  110. * @type {Event}
  111. * @readonly
  112. */
  113. definitionChanged: {
  114. get: function () {
  115. return this._definitionChanged;
  116. },
  117. },
  118. /**
  119. * Gets or sets the boolean Property specifying the visibility of the polygon.
  120. * @memberof PolygonGraphics.prototype
  121. * @type {Property|undefined}
  122. * @default true
  123. */
  124. show: createPropertyDescriptor("show"),
  125. /**
  126. * Gets or sets the Property specifying the {@link PolygonHierarchy}.
  127. * @memberof PolygonGraphics.prototype
  128. * @type {Property|undefined}
  129. */
  130. hierarchy: createPropertyDescriptor(
  131. "hierarchy",
  132. undefined,
  133. createPolygonHierarchyProperty
  134. ),
  135. /**
  136. * Gets or sets the numeric Property specifying the constant altitude of the polygon.
  137. * @memberof PolygonGraphics.prototype
  138. * @type {Property|undefined}
  139. * @default 0.0
  140. */
  141. height: createPropertyDescriptor("height"),
  142. /**
  143. * Gets or sets the Property specifying the {@link HeightReference}.
  144. * @memberof PolygonGraphics.prototype
  145. * @type {Property|undefined}
  146. * @default HeightReference.NONE
  147. */
  148. heightReference: createPropertyDescriptor("heightReference"),
  149. /**
  150. * Gets or sets the numeric Property specifying the altitude of the polygon extrusion.
  151. * If {@link PolygonGraphics#perPositionHeight} is false, the volume starts at {@link PolygonGraphics#height} and ends at this altitude.
  152. * If {@link PolygonGraphics#perPositionHeight} is true, the volume starts at the height of each {@link PolygonGraphics#hierarchy} position and ends at this altitude.
  153. * @memberof PolygonGraphics.prototype
  154. * @type {Property|undefined}
  155. */
  156. extrudedHeight: createPropertyDescriptor("extrudedHeight"),
  157. /**
  158. * Gets or sets the Property specifying the extruded {@link HeightReference}.
  159. * @memberof PolygonGraphics.prototype
  160. * @type {Property|undefined}
  161. * @default HeightReference.NONE
  162. */
  163. extrudedHeightReference: createPropertyDescriptor("extrudedHeightReference"),
  164. /**
  165. * Gets or sets the numeric property specifying the rotation of the polygon texture counter-clockwise from north. Only has an effect if textureCoordinates is not defined.
  166. * @memberof PolygonGraphics.prototype
  167. * @type {Property|undefined}
  168. * @default 0
  169. */
  170. stRotation: createPropertyDescriptor("stRotation"),
  171. /**
  172. * Gets or sets the numeric Property specifying the angular distance between points on the polygon.
  173. * @memberof PolygonGraphics.prototype
  174. * @type {Property|undefined}
  175. * @default {CesiumMath.RADIANS_PER_DEGREE}
  176. */
  177. granularity: createPropertyDescriptor("granularity"),
  178. /**
  179. * Gets or sets the boolean Property specifying whether the polygon is filled with the provided material.
  180. * @memberof PolygonGraphics.prototype
  181. * @type {Property|undefined}
  182. * @default true
  183. */
  184. fill: createPropertyDescriptor("fill"),
  185. /**
  186. * Gets or sets the Property specifying the material used to fill the polygon.
  187. * @memberof PolygonGraphics.prototype
  188. * @type {MaterialProperty}
  189. * @default Color.WHITE
  190. */
  191. material: createMaterialPropertyDescriptor("material"),
  192. /**
  193. * Gets or sets the Property specifying whether the polygon is outlined.
  194. * @memberof PolygonGraphics.prototype
  195. * @type {Property|undefined}
  196. * @default false
  197. */
  198. outline: createPropertyDescriptor("outline"),
  199. /**
  200. * Gets or sets the Property specifying the {@link Color} of the outline.
  201. * @memberof PolygonGraphics.prototype
  202. * @type {Property|undefined}
  203. * @default Color.BLACK
  204. */
  205. outlineColor: createPropertyDescriptor("outlineColor"),
  206. /**
  207. * Gets or sets the numeric Property specifying the width of the outline.
  208. * <p>
  209. * Note: This property will be ignored on all major browsers on Windows platforms. For details, see (@link https://github.com/CesiumGS/cesium/issues/40}.
  210. * </p>
  211. * @memberof PolygonGraphics.prototype
  212. * @type {Property|undefined}
  213. * @default 1.0
  214. */
  215. outlineWidth: createPropertyDescriptor("outlineWidth"),
  216. /**
  217. * Gets or sets the boolean specifying whether or not the the height of each position is used.
  218. * If true, the shape will have non-uniform altitude defined by the height of each {@link PolygonGraphics#hierarchy} position.
  219. * If false, the shape will have a constant altitude as specified by {@link PolygonGraphics#height}.
  220. * @memberof PolygonGraphics.prototype
  221. * @type {Property|undefined}
  222. */
  223. perPositionHeight: createPropertyDescriptor("perPositionHeight"),
  224. /**
  225. * Gets or sets a boolean specifying whether or not the top of an extruded polygon is included.
  226. * @memberof PolygonGraphics.prototype
  227. * @type {Property|undefined}
  228. */
  229. closeTop: createPropertyDescriptor("closeTop"),
  230. /**
  231. * Gets or sets a boolean specifying whether or not the bottom of an extruded polygon is included.
  232. * @memberof PolygonGraphics.prototype
  233. * @type {Property|undefined}
  234. */
  235. closeBottom: createPropertyDescriptor("closeBottom"),
  236. /**
  237. * Gets or sets the {@link ArcType} Property specifying the type of lines the polygon edges use.
  238. * @memberof PolygonGraphics.prototype
  239. * @type {Property|undefined}
  240. * @default ArcType.GEODESIC
  241. */
  242. arcType: createPropertyDescriptor("arcType"),
  243. /**
  244. * Get or sets the enum Property specifying whether the polygon
  245. * casts or receives shadows from light sources.
  246. * @memberof PolygonGraphics.prototype
  247. * @type {Property|undefined}
  248. * @default ShadowMode.DISABLED
  249. */
  250. shadows: createPropertyDescriptor("shadows"),
  251. /**
  252. * Gets or sets the {@link DistanceDisplayCondition} Property specifying at what distance from the camera that this polygon will be displayed.
  253. * @memberof PolygonGraphics.prototype
  254. * @type {Property|undefined}
  255. */
  256. distanceDisplayCondition: createPropertyDescriptor(
  257. "distanceDisplayCondition"
  258. ),
  259. /**
  260. * Gets or sets the {@link ClassificationType} Property specifying whether this polygon will classify terrain, 3D Tiles, or both when on the ground.
  261. * @memberof PolygonGraphics.prototype
  262. * @type {Property|undefined}
  263. * @default ClassificationType.BOTH
  264. */
  265. classificationType: createPropertyDescriptor("classificationType"),
  266. /**
  267. * Gets or sets the zIndex Prperty specifying the ordering of ground geometry. Only has an effect if the polygon is constant and neither height or extrudedHeight are specified.
  268. * @memberof PolygonGraphics.prototype
  269. * @type {ConstantProperty|undefined}
  270. * @default 0
  271. */
  272. zIndex: createPropertyDescriptor("zIndex"),
  273. /**
  274. * A Property specifying texture coordinates as a {@link PolygonHierarchy} of {@link Cartesian2} points. Has no effect for ground primitives.
  275. * @memberof PolygonGraphics.prototype
  276. * @type {Property|undefined}
  277. */
  278. textureCoordinates: createPropertyDescriptor("textureCoordinates"),
  279. });
  280. /**
  281. * Duplicates this instance.
  282. *
  283. * @param {PolygonGraphics} [result] The object onto which to store the result.
  284. * @returns {PolygonGraphics} The modified result parameter or a new instance if one was not provided.
  285. */
  286. PolygonGraphics.prototype.clone = function (result) {
  287. if (!defined(result)) {
  288. return new PolygonGraphics(this);
  289. }
  290. result.show = this.show;
  291. result.hierarchy = this.hierarchy;
  292. result.height = this.height;
  293. result.heightReference = this.heightReference;
  294. result.extrudedHeight = this.extrudedHeight;
  295. result.extrudedHeightReference = this.extrudedHeightReference;
  296. result.stRotation = this.stRotation;
  297. result.granularity = this.granularity;
  298. result.fill = this.fill;
  299. result.material = this.material;
  300. result.outline = this.outline;
  301. result.outlineColor = this.outlineColor;
  302. result.outlineWidth = this.outlineWidth;
  303. result.perPositionHeight = this.perPositionHeight;
  304. result.closeTop = this.closeTop;
  305. result.closeBottom = this.closeBottom;
  306. result.arcType = this.arcType;
  307. result.shadows = this.shadows;
  308. result.distanceDisplayCondition = this.distanceDisplayCondition;
  309. result.classificationType = this.classificationType;
  310. result.zIndex = this.zIndex;
  311. result.textureCoordinates = this.textureCoordinates;
  312. return result;
  313. };
  314. /**
  315. * Assigns each unassigned property on this object to the value
  316. * of the same property on the provided source object.
  317. *
  318. * @param {PolygonGraphics} source The object to be merged into this object.
  319. */
  320. PolygonGraphics.prototype.merge = function (source) {
  321. //>>includeStart('debug', pragmas.debug);
  322. if (!defined(source)) {
  323. throw new DeveloperError("source is required.");
  324. }
  325. //>>includeEnd('debug');
  326. this.show = defaultValue(this.show, source.show);
  327. this.hierarchy = defaultValue(this.hierarchy, source.hierarchy);
  328. this.height = defaultValue(this.height, source.height);
  329. this.heightReference = defaultValue(
  330. this.heightReference,
  331. source.heightReference
  332. );
  333. this.extrudedHeight = defaultValue(
  334. this.extrudedHeight,
  335. source.extrudedHeight
  336. );
  337. this.extrudedHeightReference = defaultValue(
  338. this.extrudedHeightReference,
  339. source.extrudedHeightReference
  340. );
  341. this.stRotation = defaultValue(this.stRotation, source.stRotation);
  342. this.granularity = defaultValue(this.granularity, source.granularity);
  343. this.fill = defaultValue(this.fill, source.fill);
  344. this.material = defaultValue(this.material, source.material);
  345. this.outline = defaultValue(this.outline, source.outline);
  346. this.outlineColor = defaultValue(this.outlineColor, source.outlineColor);
  347. this.outlineWidth = defaultValue(this.outlineWidth, source.outlineWidth);
  348. this.perPositionHeight = defaultValue(
  349. this.perPositionHeight,
  350. source.perPositionHeight
  351. );
  352. this.closeTop = defaultValue(this.closeTop, source.closeTop);
  353. this.closeBottom = defaultValue(this.closeBottom, source.closeBottom);
  354. this.arcType = defaultValue(this.arcType, source.arcType);
  355. this.shadows = defaultValue(this.shadows, source.shadows);
  356. this.distanceDisplayCondition = defaultValue(
  357. this.distanceDisplayCondition,
  358. source.distanceDisplayCondition
  359. );
  360. this.classificationType = defaultValue(
  361. this.classificationType,
  362. source.classificationType
  363. );
  364. this.zIndex = defaultValue(this.zIndex, source.zIndex);
  365. this.textureCoordinates = defaultValue(
  366. this.textureCoordinates,
  367. source.textureCoordinates
  368. );
  369. };
  370. export default PolygonGraphics;