BillboardGraphics.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import defaultValue from '../Core/defaultValue.js';
  2. import defined from '../Core/defined.js';
  3. import defineProperties from '../Core/defineProperties.js';
  4. import DeveloperError from '../Core/DeveloperError.js';
  5. import Event from '../Core/Event.js';
  6. import createPropertyDescriptor from './createPropertyDescriptor.js';
  7. /**
  8. * Describes a two dimensional icon located at the position of the containing {@link Entity}.
  9. * <p>
  10. * <div align='center'>
  11. * <img src='Images/Billboard.png' width='400' height='300' /><br />
  12. * Example billboards
  13. * </div>
  14. * </p>
  15. *
  16. * @alias BillboardGraphics
  17. * @constructor
  18. *
  19. * @param {Object} [options] Object with the following properties:
  20. * @param {Property} [options.show=true] A boolean Property specifying the visibility of the billboard.
  21. * @param {Property} [options.image] A Property specifying the Image, URI, or Canvas to use for the billboard.
  22. * @param {Property} [options.scale=1.0] A numeric Property specifying the scale to apply to the image size.
  23. * @param {Property} [options.pixelOffset=Cartesian2.ZERO] A {@link Cartesian2} Property specifying the pixel offset.
  24. * @param {Property} [options.eyeOffset=Cartesian3.ZERO] A {@link Cartesian3} Property specifying the eye offset.
  25. * @param {Property} [options.horizontalOrigin=HorizontalOrigin.CENTER] A Property specifying the {@link HorizontalOrigin}.
  26. * @param {Property} [options.verticalOrigin=VerticalOrigin.CENTER] A Property specifying the {@link VerticalOrigin}.
  27. * @param {Property} [options.heightReference=HeightReference.NONE] A Property specifying what the height is relative to.
  28. * @param {Property} [options.color=Color.WHITE] A Property specifying the tint {@link Color} of the image.
  29. * @param {Property} [options.rotation=0] A numeric Property specifying the rotation about the alignedAxis.
  30. * @param {Property} [options.alignedAxis=Cartesian3.ZERO] A {@link Cartesian3} Property specifying the unit vector axis of rotation.
  31. * @param {Property} [options.sizeInMeters] A boolean Property specifying whether this billboard's size should be measured in meters.
  32. * @param {Property} [options.width] A numeric Property specifying the width of the billboard in pixels, overriding the native size.
  33. * @param {Property} [options.height] A numeric Property specifying the height of the billboard in pixels, overriding the native size.
  34. * @param {Property} [options.scaleByDistance] A {@link NearFarScalar} Property used to scale the point based on distance from the camera.
  35. * @param {Property} [options.translucencyByDistance] A {@link NearFarScalar} Property used to set translucency based on distance from the camera.
  36. * @param {Property} [options.pixelOffsetScaleByDistance] A {@link NearFarScalar} Property used to set pixelOffset based on distance from the camera.
  37. * @param {Property} [options.imageSubRegion] A Property specifying a {@link BoundingRectangle} that defines a sub-region of the image to use for the billboard, rather than the entire image, measured in pixels from the bottom-left.
  38. * @param {Property} [options.distanceDisplayCondition] A Property specifying at what distance from the camera that this billboard will be displayed.
  39. * @param {Property} [options.disableDepthTestDistance] A Property specifying the distance from the camera at which to disable the depth test to.
  40. *
  41. * @demo {@link https://sandcastle.cesium.com/index.html?src=Billboards.html|Cesium Sandcastle Billboard Demo}
  42. */
  43. function BillboardGraphics(options) {
  44. this._definitionChanged = new Event();
  45. this._show = undefined;
  46. this._showSubscription = undefined;
  47. this._image = undefined;
  48. this._imageSubscription = undefined;
  49. this._scale = undefined;
  50. this._scaleSubscription = undefined;
  51. this._pixelOffset = undefined;
  52. this._pixelOffsetSubscription = undefined;
  53. this._eyeOffset = undefined;
  54. this._eyeOffsetSubscription = undefined;
  55. this._horizontalOrigin = undefined;
  56. this._horizontalOriginSubscription = undefined;
  57. this._verticalOrigin = undefined;
  58. this._verticalOriginSubscription = undefined;
  59. this._heightReference = undefined;
  60. this._heightReferenceSubscription = undefined;
  61. this._color = undefined;
  62. this._colorSubscription = undefined;
  63. this._rotation = undefined;
  64. this._rotationSubscription = undefined;
  65. this._alignedAxis = undefined;
  66. this._alignedAxisSubscription = undefined;
  67. this._sizeInMeters = undefined;
  68. this._sizeInMetersSubscription = undefined;
  69. this._width = undefined;
  70. this._widthSubscription = undefined;
  71. this._height = undefined;
  72. this._heightSubscription = undefined;
  73. this._scaleByDistance = undefined;
  74. this._scaleByDistanceSubscription = undefined;
  75. this._translucencyByDistance = undefined;
  76. this._translucencyByDistanceSubscription = undefined;
  77. this._pixelOffsetScaleByDistance = undefined;
  78. this._pixelOffsetScaleByDistanceSubscription = undefined;
  79. this._imageSubRegion = undefined;
  80. this._imageSubRegionSubscription = undefined;
  81. this._distanceDisplayCondition = undefined;
  82. this._distanceDisplayConditionSubscription = undefined;
  83. this._disableDepthTestDistance = undefined;
  84. this._disableDepthTestDistanceSubscription = undefined;
  85. this.merge(defaultValue(options, defaultValue.EMPTY_OBJECT));
  86. }
  87. defineProperties(BillboardGraphics.prototype, {
  88. /**
  89. * Gets the event that is raised whenever a property or sub-property is changed or modified.
  90. * @memberof BillboardGraphics.prototype
  91. *
  92. * @type {Event}
  93. * @readonly
  94. */
  95. definitionChanged : {
  96. get : function() {
  97. return this._definitionChanged;
  98. }
  99. },
  100. /**
  101. * Gets or sets the boolean Property specifying the visibility of the billboard.
  102. * @memberof BillboardGraphics.prototype
  103. * @type {Property}
  104. * @default true
  105. */
  106. show : createPropertyDescriptor('show'),
  107. /**
  108. * Gets or sets the Property specifying the Image, URI, or Canvas to use for the billboard.
  109. * @memberof BillboardGraphics.prototype
  110. * @type {Property}
  111. */
  112. image : createPropertyDescriptor('image'),
  113. /**
  114. * Gets or sets the numeric Property specifying the uniform scale to apply to the image.
  115. * A scale greater than <code>1.0</code> enlarges the billboard while a scale less than <code>1.0</code> shrinks it.
  116. * <p>
  117. * <div align='center'>
  118. * <img src='Images/Billboard.setScale.png' width='400' height='300' /><br/>
  119. * From left to right in the above image, the scales are <code>0.5</code>, <code>1.0</code>, and <code>2.0</code>.
  120. * </div>
  121. * </p>
  122. * @memberof BillboardGraphics.prototype
  123. * @type {Property}
  124. * @default 1.0
  125. */
  126. scale : createPropertyDescriptor('scale'),
  127. /**
  128. * Gets or sets the {@link Cartesian2} Property specifying the billboard's pixel offset in screen space
  129. * from the origin of this billboard. This is commonly used to align multiple billboards and labels at
  130. * the same position, e.g., an image and text. The screen space origin is the top, left corner of the
  131. * canvas; <code>x</code> increases from left to right, and <code>y</code> increases from top to bottom.
  132. * <p>
  133. * <div align='center'>
  134. * <table border='0' cellpadding='5'><tr>
  135. * <td align='center'><code>default</code><br/><img src='Images/Billboard.setPixelOffset.default.png' width='250' height='188' /></td>
  136. * <td align='center'><code>b.pixeloffset = new Cartesian2(50, 25);</code><br/><img src='Images/Billboard.setPixelOffset.x50y-25.png' width='250' height='188' /></td>
  137. * </tr></table>
  138. * The billboard's origin is indicated by the yellow point.
  139. * </div>
  140. * </p>
  141. * @memberof BillboardGraphics.prototype
  142. * @type {Property}
  143. * @default Cartesian2.ZERO
  144. */
  145. pixelOffset : createPropertyDescriptor('pixelOffset'),
  146. /**
  147. * Gets or sets the {@link Cartesian3} Property specifying the billboard's offset in eye coordinates.
  148. * Eye coordinates is a left-handed coordinate system, where <code>x</code> points towards the viewer's
  149. * right, <code>y</code> points up, and <code>z</code> points into the screen.
  150. * <p>
  151. * An eye offset is commonly used to arrange multiple billboards or objects at the same position, e.g., to
  152. * arrange a billboard above its corresponding 3D model.
  153. * </p>
  154. * Below, the billboard is positioned at the center of the Earth but an eye offset makes it always
  155. * appear on top of the Earth regardless of the viewer's or Earth's orientation.
  156. * <p>
  157. * <div align='center'>
  158. * <table border='0' cellpadding='5'><tr>
  159. * <td align='center'><img src='Images/Billboard.setEyeOffset.one.png' width='250' height='188' /></td>
  160. * <td align='center'><img src='Images/Billboard.setEyeOffset.two.png' width='250' height='188' /></td>
  161. * </tr></table>
  162. * <code>b.eyeOffset = new Cartesian3(0.0, 8000000.0, 0.0);</code>
  163. * </div>
  164. * </p>
  165. * @memberof BillboardGraphics.prototype
  166. * @type {Property}
  167. * @default Cartesian3.ZERO
  168. */
  169. eyeOffset : createPropertyDescriptor('eyeOffset'),
  170. /**
  171. * Gets or sets the Property specifying the {@link HorizontalOrigin}.
  172. * @memberof BillboardGraphics.prototype
  173. * @type {Property}
  174. * @default HorizontalOrigin.CENTER
  175. */
  176. horizontalOrigin : createPropertyDescriptor('horizontalOrigin'),
  177. /**
  178. * Gets or sets the Property specifying the {@link VerticalOrigin}.
  179. * @memberof BillboardGraphics.prototype
  180. * @type {Property}
  181. * @default VerticalOrigin.CENTER
  182. */
  183. verticalOrigin : createPropertyDescriptor('verticalOrigin'),
  184. /**
  185. * Gets or sets the Property specifying the {@link HeightReference}.
  186. * @memberof BillboardGraphics.prototype
  187. * @type {Property}
  188. * @default HeightReference.NONE
  189. */
  190. heightReference : createPropertyDescriptor('heightReference'),
  191. /**
  192. * Gets or sets the Property specifying the {@link Color} that is multiplied with the <code>image</code>.
  193. * This has two common use cases. First, the same white texture may be used by many different billboards,
  194. * each with a different color, to create colored billboards. Second, the color's alpha component can be
  195. * used to make the billboard translucent as shown below. An alpha of <code>0.0</code> makes the billboard
  196. * transparent, and <code>1.0</code> makes the billboard opaque.
  197. * <p>
  198. * <div align='center'>
  199. * <table border='0' cellpadding='5'><tr>
  200. * <td align='center'><code>default</code><br/><img src='Images/Billboard.setColor.Alpha255.png' width='250' height='188' /></td>
  201. * <td align='center'><code>alpha : 0.5</code><br/><img src='Images/Billboard.setColor.Alpha127.png' width='250' height='188' /></td>
  202. * </tr></table>
  203. * </div>
  204. * </p>
  205. * @memberof BillboardGraphics.prototype
  206. * @type {Property}
  207. * @default Color.WHITE
  208. */
  209. color : createPropertyDescriptor('color'),
  210. /**
  211. * Gets or sets the numeric Property specifying the rotation of the image
  212. * counter clockwise from the <code>alignedAxis</code>.
  213. * @memberof BillboardGraphics.prototype
  214. * @type {Property}
  215. * @default 0
  216. */
  217. rotation : createPropertyDescriptor('rotation'),
  218. /**
  219. * Gets or sets the {@link Cartesian3} Property specifying the unit vector axis of rotation
  220. * in the fixed frame. When set to Cartesian3.ZERO the rotation is from the top of the screen.
  221. * @memberof BillboardGraphics.prototype
  222. * @type {Property}
  223. * @default Cartesian3.ZERO
  224. */
  225. alignedAxis : createPropertyDescriptor('alignedAxis'),
  226. /**
  227. * Gets or sets the boolean Property specifying if this billboard's size will be measured in meters.
  228. * @memberof BillboardGraphics.prototype
  229. * @type {Property}
  230. * @default false
  231. */
  232. sizeInMeters : createPropertyDescriptor('sizeInMeters'),
  233. /**
  234. * Gets or sets the numeric Property specifying the width of the billboard in pixels.
  235. * When undefined, the native width is used.
  236. * @memberof BillboardGraphics.prototype
  237. * @type {Property}
  238. */
  239. width : createPropertyDescriptor('width'),
  240. /**
  241. * Gets or sets the numeric Property specifying the height of the billboard in pixels.
  242. * When undefined, the native height is used.
  243. * @memberof BillboardGraphics.prototype
  244. * @type {Property}
  245. */
  246. height : createPropertyDescriptor('height'),
  247. /**
  248. * Gets or sets {@link NearFarScalar} Property specifying the scale of the billboard based on the distance from the camera.
  249. * A billboard's scale will interpolate between the {@link NearFarScalar#nearValue} and
  250. * {@link NearFarScalar#farValue} while the camera distance falls within the upper and lower bounds
  251. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  252. * Outside of these ranges the billboard's scale remains clamped to the nearest bound.
  253. * @memberof BillboardGraphics.prototype
  254. * @type {Property}
  255. */
  256. scaleByDistance : createPropertyDescriptor('scaleByDistance'),
  257. /**
  258. * Gets or sets {@link NearFarScalar} Property specifying the translucency of the billboard based on the distance from the camera.
  259. * A billboard's translucency will interpolate between the {@link NearFarScalar#nearValue} and
  260. * {@link NearFarScalar#farValue} while the camera distance falls within the upper and lower bounds
  261. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  262. * Outside of these ranges the billboard's translucency remains clamped to the nearest bound.
  263. * @memberof BillboardGraphics.prototype
  264. * @type {Property}
  265. */
  266. translucencyByDistance : createPropertyDescriptor('translucencyByDistance'),
  267. /**
  268. * Gets or sets {@link NearFarScalar} Property specifying the pixel offset of the billboard based on the distance from the camera.
  269. * A billboard's pixel offset will interpolate between the {@link NearFarScalar#nearValue} and
  270. * {@link NearFarScalar#farValue} while the camera distance falls within the upper and lower bounds
  271. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  272. * Outside of these ranges the billboard's pixel offset remains clamped to the nearest bound.
  273. * @memberof BillboardGraphics.prototype
  274. * @type {Property}
  275. */
  276. pixelOffsetScaleByDistance : createPropertyDescriptor('pixelOffsetScaleByDistance'),
  277. /**
  278. * Gets or sets the Property specifying a {@link BoundingRectangle} that defines a
  279. * sub-region of the <code>image</code> to use for the billboard, rather than the entire image,
  280. * measured in pixels from the bottom-left.
  281. * @memberof BillboardGraphics.prototype
  282. * @type {Property}
  283. */
  284. imageSubRegion : createPropertyDescriptor('imageSubRegion'),
  285. /**
  286. * Gets or sets the {@link DistanceDisplayCondition} Property specifying at what distance from the camera that this billboard will be displayed.
  287. * @memberof BillboardGraphics.prototype
  288. * @type {Property}
  289. */
  290. distanceDisplayCondition : createPropertyDescriptor('distanceDisplayCondition'),
  291. /**
  292. * Gets or sets the distance from the camera at which to disable the depth test to, for example, prevent clipping against terrain.
  293. * When set to zero, the depth test is always applied. When set to Number.POSITIVE_INFINITY, the depth test is never applied.
  294. * @memberof BillboardGraphics.prototype
  295. * @type {Property}
  296. */
  297. disableDepthTestDistance : createPropertyDescriptor('disableDepthTestDistance')
  298. });
  299. /**
  300. * Duplicates this instance.
  301. *
  302. * @param {BillboardGraphics} [result] The object onto which to store the result.
  303. * @returns {BillboardGraphics} The modified result parameter or a new instance if one was not provided.
  304. */
  305. BillboardGraphics.prototype.clone = function(result) {
  306. if (!defined(result)) {
  307. return new BillboardGraphics(this);
  308. }
  309. result.show = this._show;
  310. result.image = this._image;
  311. result.scale = this._scale;
  312. result.pixelOffset = this._pixelOffset;
  313. result.eyeOffset = this._eyeOffset;
  314. result.horizontalOrigin = this._horizontalOrigin;
  315. result.verticalOrigin = this._verticalOrigin;
  316. result.heightReference = this._heightReference;
  317. result.color = this._color;
  318. result.rotation = this._rotation;
  319. result.alignedAxis = this._alignedAxis;
  320. result.sizeInMeters = this._sizeInMeters;
  321. result.width = this._width;
  322. result.height = this._height;
  323. result.scaleByDistance = this._scaleByDistance;
  324. result.translucencyByDistance = this._translucencyByDistance;
  325. result.pixelOffsetScaleByDistance = this._pixelOffsetScaleByDistance;
  326. result.imageSubRegion = this._imageSubRegion;
  327. result.distanceDisplayCondition = this._distanceDisplayCondition;
  328. result.disableDepthTestDistance = this._disableDepthTestDistance;
  329. return result;
  330. };
  331. /**
  332. * Assigns each unassigned property on this object to the value
  333. * of the same property on the provided source object.
  334. *
  335. * @param {BillboardGraphics} source The object to be merged into this object.
  336. */
  337. BillboardGraphics.prototype.merge = function(source) {
  338. //>>includeStart('debug', pragmas.debug);
  339. if (!defined(source)) {
  340. throw new DeveloperError('source is required.');
  341. }
  342. //>>includeEnd('debug');
  343. this.show = defaultValue(this._show, source.show);
  344. this.image = defaultValue(this._image, source.image);
  345. this.scale = defaultValue(this._scale, source.scale);
  346. this.pixelOffset = defaultValue(this._pixelOffset, source.pixelOffset);
  347. this.eyeOffset = defaultValue(this._eyeOffset, source.eyeOffset);
  348. this.horizontalOrigin = defaultValue(this._horizontalOrigin, source.horizontalOrigin);
  349. this.verticalOrigin = defaultValue(this._verticalOrigin, source.verticalOrigin);
  350. this.heightReference = defaultValue(this._heightReference, source.heightReference);
  351. this.color = defaultValue(this._color, source.color);
  352. this.rotation = defaultValue(this._rotation, source.rotation);
  353. this.alignedAxis = defaultValue(this._alignedAxis, source.alignedAxis);
  354. this.sizeInMeters = defaultValue(this._sizeInMeters, source.sizeInMeters);
  355. this.width = defaultValue(this._width, source.width);
  356. this.height = defaultValue(this._height, source.height);
  357. this.scaleByDistance = defaultValue(this._scaleByDistance, source.scaleByDistance);
  358. this.translucencyByDistance = defaultValue(this._translucencyByDistance, source.translucencyByDistance);
  359. this.pixelOffsetScaleByDistance = defaultValue(this._pixelOffsetScaleByDistance, source.pixelOffsetScaleByDistance);
  360. this.imageSubRegion = defaultValue(this._imageSubRegion, source.imageSubRegion);
  361. this.distanceDisplayCondition = defaultValue(this._distanceDisplayCondition, source.distanceDisplayCondition);
  362. this.disableDepthTestDistance = defaultValue(this._disableDepthTestDistance, source.disableDepthTestDistance);
  363. };
  364. export default BillboardGraphics;