Billboard.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*global define*/
  2. define([
  3. '../Core/BoundingRectangle',
  4. '../Core/Cartesian2',
  5. '../Core/Cartesian3',
  6. '../Core/Cartesian4',
  7. '../Core/Color',
  8. '../Core/createGuid',
  9. '../Core/defaultValue',
  10. '../Core/defined',
  11. '../Core/defineProperties',
  12. '../Core/DeveloperError',
  13. '../Core/Matrix4',
  14. '../Core/NearFarScalar',
  15. './HorizontalOrigin',
  16. './SceneMode',
  17. './SceneTransforms',
  18. './VerticalOrigin'
  19. ], function(
  20. BoundingRectangle,
  21. Cartesian2,
  22. Cartesian3,
  23. Cartesian4,
  24. Color,
  25. createGuid,
  26. defaultValue,
  27. defined,
  28. defineProperties,
  29. DeveloperError,
  30. Matrix4,
  31. NearFarScalar,
  32. HorizontalOrigin,
  33. SceneMode,
  34. SceneTransforms,
  35. VerticalOrigin) {
  36. "use strict";
  37. /**
  38. * A viewport-aligned image positioned in the 3D scene, that is created
  39. * and rendered using a {@link BillboardCollection}. A billboard is created and its initial
  40. * properties are set by calling {@link BillboardCollection#add}.
  41. * <br /><br />
  42. * <div align='center'>
  43. * <img src='images/Billboard.png' width='400' height='300' /><br />
  44. * Example billboards
  45. * </div>
  46. *
  47. * @alias Billboard
  48. *
  49. * @performance Reading a property, e.g., {@link Billboard#show}, is constant time.
  50. * Assigning to a property is constant time but results in
  51. * CPU to GPU traffic when {@link BillboardCollection#update} is called. The per-billboard traffic is
  52. * the same regardless of how many properties were updated. If most billboards in a collection need to be
  53. * updated, it may be more efficient to clear the collection with {@link BillboardCollection#removeAll}
  54. * and add new billboards instead of modifying each one.
  55. *
  56. * @exception {DeveloperError} scaleByDistance.far must be greater than scaleByDistance.near
  57. * @exception {DeveloperError} translucencyByDistance.far must be greater than translucencyByDistance.near
  58. * @exception {DeveloperError} pixelOffsetScaleByDistance.far must be greater than pixelOffsetScaleByDistance.near
  59. *
  60. * @see BillboardCollection
  61. * @see BillboardCollection#add
  62. * @see Label
  63. *
  64. * @internalConstructor
  65. *
  66. * @demo {@link http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Billboards.html|Cesium Sandcastle Billboard Demo}
  67. */
  68. var Billboard = function(options, billboardCollection) {
  69. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  70. //>>includeStart('debug', pragmas.debug);
  71. if (defined(options.scaleByDistance) && options.scaleByDistance.far <= options.scaleByDistance.near) {
  72. throw new DeveloperError('scaleByDistance.far must be greater than scaleByDistance.near.');
  73. }
  74. if (defined(options.translucencyByDistance) && options.translucencyByDistance.far <= options.translucencyByDistance.near) {
  75. throw new DeveloperError('translucencyByDistance.far must be greater than translucencyByDistance.near.');
  76. }
  77. if (defined(options.pixelOffsetScaleByDistance) && options.pixelOffsetScaleByDistance.far <= options.pixelOffsetScaleByDistance.near) {
  78. throw new DeveloperError('pixelOffsetScaleByDistance.far must be greater than pixelOffsetScaleByDistance.near.');
  79. }
  80. //>>includeEnd('debug');
  81. this._show = defaultValue(options.show, true);
  82. this._position = Cartesian3.clone(defaultValue(options.position, Cartesian3.ZERO));
  83. this._actualPosition = Cartesian3.clone(this._position); // For columbus view and 2D
  84. this._pixelOffset = Cartesian2.clone(defaultValue(options.pixelOffset, Cartesian2.ZERO));
  85. this._translate = new Cartesian2(0.0, 0.0); // used by labels for glyph vertex translation
  86. this._eyeOffset = Cartesian3.clone(defaultValue(options.eyeOffset, Cartesian3.ZERO));
  87. this._verticalOrigin = defaultValue(options.verticalOrigin, VerticalOrigin.CENTER);
  88. this._horizontalOrigin = defaultValue(options.horizontalOrigin, HorizontalOrigin.CENTER);
  89. this._scale = defaultValue(options.scale, 1.0);
  90. this._color = Color.clone(defaultValue(options.color, Color.WHITE));
  91. this._rotation = defaultValue(options.rotation, 0.0);
  92. this._alignedAxis = Cartesian3.clone(defaultValue(options.alignedAxis, Cartesian3.ZERO));
  93. this._width = options.width;
  94. this._height = options.height;
  95. this._scaleByDistance = options.scaleByDistance;
  96. this._translucencyByDistance = options.translucencyByDistance;
  97. this._pixelOffsetScaleByDistance = options.pixelOffsetScaleByDistance;
  98. this._id = options.id;
  99. this._collection = defaultValue(options.collection, billboardCollection);
  100. this._pickId = undefined;
  101. this._pickPrimitive = defaultValue(options._pickPrimitive, this);
  102. this._billboardCollection = billboardCollection;
  103. this._dirty = false;
  104. this._index = -1; //Used only by BillboardCollection
  105. this._imageIndex = -1;
  106. this._imageIndexPromise = undefined;
  107. this._imageId = undefined;
  108. this._image = undefined;
  109. this._imageSubRegion = undefined;
  110. this._imageWidth = undefined;
  111. this._imageHeight = undefined;
  112. var image = options.image;
  113. var imageId = options.imageId;
  114. if (defined(image)) {
  115. if (!defined(imageId)) {
  116. if (typeof image === 'string') {
  117. imageId = image;
  118. } else if (defined(image.src)) {
  119. imageId = image.src;
  120. } else {
  121. imageId = createGuid();
  122. }
  123. }
  124. this._imageId = imageId;
  125. this._image = image;
  126. }
  127. if (defined(options.imageSubRegion)) {
  128. this._imageId = imageId;
  129. this._imageSubRegion = options.imageSubRegion;
  130. }
  131. if (defined(this._billboardCollection._textureAtlas)) {
  132. this._loadImage();
  133. }
  134. };
  135. var SHOW_INDEX = Billboard.SHOW_INDEX = 0;
  136. var POSITION_INDEX = Billboard.POSITION_INDEX = 1;
  137. var PIXEL_OFFSET_INDEX = Billboard.PIXEL_OFFSET_INDEX = 2;
  138. var EYE_OFFSET_INDEX = Billboard.EYE_OFFSET_INDEX = 3;
  139. var HORIZONTAL_ORIGIN_INDEX = Billboard.HORIZONTAL_ORIGIN_INDEX = 4;
  140. var VERTICAL_ORIGIN_INDEX = Billboard.VERTICAL_ORIGIN_INDEX = 5;
  141. var SCALE_INDEX = Billboard.SCALE_INDEX = 6;
  142. var IMAGE_INDEX_INDEX = Billboard.IMAGE_INDEX_INDEX = 7;
  143. var COLOR_INDEX = Billboard.COLOR_INDEX = 8;
  144. var ROTATION_INDEX = Billboard.ROTATION_INDEX = 9;
  145. var ALIGNED_AXIS_INDEX = Billboard.ALIGNED_AXIS_INDEX = 10;
  146. var SCALE_BY_DISTANCE_INDEX = Billboard.SCALE_BY_DISTANCE_INDEX = 11;
  147. var TRANSLUCENCY_BY_DISTANCE_INDEX = Billboard.TRANSLUCENCY_BY_DISTANCE_INDEX = 12;
  148. var PIXEL_OFFSET_SCALE_BY_DISTANCE_INDEX = Billboard.PIXEL_OFFSET_SCALE_BY_DISTANCE_INDEX = 13;
  149. Billboard.NUMBER_OF_PROPERTIES = 14;
  150. function makeDirty(billboard, propertyChanged) {
  151. var billboardCollection = billboard._billboardCollection;
  152. if (defined(billboardCollection)) {
  153. billboardCollection._updateBillboard(billboard, propertyChanged);
  154. billboard._dirty = true;
  155. }
  156. }
  157. defineProperties(Billboard.prototype, {
  158. /**
  159. * Determines if this billboard will be shown. Use this to hide or show a billboard, instead
  160. * of removing it and re-adding it to the collection.
  161. * @memberof Billboard.prototype
  162. * @type {Boolean}
  163. */
  164. show : {
  165. get : function() {
  166. return this._show;
  167. },
  168. set : function(value) {
  169. //>>includeStart('debug', pragmas.debug);
  170. if (!defined(value)) {
  171. throw new DeveloperError('value is required.');
  172. }
  173. //>>includeEnd('debug');
  174. if (this._show !== value) {
  175. this._show = value;
  176. makeDirty(this, SHOW_INDEX);
  177. }
  178. }
  179. },
  180. /**
  181. * Gets or sets the Cartesian position of this billboard.
  182. * @memberof Billboard.prototype
  183. * @type {Cartesian3}
  184. */
  185. position : {
  186. get : function() {
  187. return this._position;
  188. },
  189. set : function(value) {
  190. //>>includeStart('debug', pragmas.debug)
  191. if (!defined(value)) {
  192. throw new DeveloperError('value is required.');
  193. }
  194. //>>includeEnd('debug');
  195. var position = this._position;
  196. if (!Cartesian3.equals(position, value)) {
  197. Cartesian3.clone(value, position);
  198. Cartesian3.clone(value, this._actualPosition);
  199. makeDirty(this, POSITION_INDEX);
  200. }
  201. }
  202. },
  203. /**
  204. * Gets or sets the pixel offset in screen space from the origin of this billboard. This is commonly used
  205. * to align multiple billboards and labels at the same position, e.g., an image and text. The
  206. * screen space origin is the top, left corner of the canvas; <code>x</code> increases from
  207. * left to right, and <code>y</code> increases from top to bottom.
  208. * <br /><br />
  209. * <div align='center'>
  210. * <table border='0' cellpadding='5'><tr>
  211. * <td align='center'><code>default</code><br/><img src='images/Billboard.setPixelOffset.default.png' width='250' height='188' /></td>
  212. * <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>
  213. * </tr></table>
  214. * The billboard's origin is indicated by the yellow point.
  215. * </div>
  216. * @memberof Billboard.prototype
  217. * @type {Cartesian2}
  218. */
  219. pixelOffset : {
  220. get : function() {
  221. return this._pixelOffset;
  222. },
  223. set : function(value) {
  224. //>>includeStart('debug', pragmas.debug);
  225. if (!defined(value)) {
  226. throw new DeveloperError('value is required.');
  227. }
  228. //>>includeEnd('debug');
  229. var pixelOffset = this._pixelOffset;
  230. if (!Cartesian2.equals(pixelOffset, value)) {
  231. Cartesian2.clone(value, pixelOffset);
  232. makeDirty(this, PIXEL_OFFSET_INDEX);
  233. }
  234. }
  235. },
  236. /**
  237. * Gets or sets near and far scaling properties of a Billboard based on the billboard's distance from the camera.
  238. * A billboard's scale will interpolate between the {@link NearFarScalar#nearValue} and
  239. * {@link NearFarScalar#farValue} while the camera distance falls within the upper and lower bounds
  240. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  241. * Outside of these ranges the billboard's scale remains clamped to the nearest bound. If undefined,
  242. * scaleByDistance will be disabled.
  243. * @memberof Billboard.prototype
  244. * @type {NearFarScalar}
  245. *
  246. * @example
  247. * // Example 1.
  248. * // Set a billboard's scaleByDistance to scale by 1.5 when the
  249. * // camera is 1500 meters from the billboard and disappear as
  250. * // the camera distance approaches 8.0e6 meters.
  251. * b.scaleByDistance = new Cesium.NearFarScalar(1.5e2, 1.5, 8.0e6, 0.0);
  252. *
  253. * @example
  254. * // Example 2.
  255. * // disable scaling by distance
  256. * b.scaleByDistance = undefined;
  257. */
  258. scaleByDistance : {
  259. get : function() {
  260. return this._scaleByDistance;
  261. },
  262. set : function(value) {
  263. //>>includeStart('debug', pragmas.debug);
  264. if (defined(value) && value.far <= value.near) {
  265. throw new DeveloperError('far distance must be greater than near distance.');
  266. }
  267. //>>includeEnd('debug');
  268. var scaleByDistance = this._scaleByDistance;
  269. if (!NearFarScalar.equals(scaleByDistance, value)) {
  270. this._scaleByDistance = NearFarScalar.clone(value, scaleByDistance);
  271. makeDirty(this, SCALE_BY_DISTANCE_INDEX);
  272. }
  273. }
  274. },
  275. /**
  276. * Gets or sets near and far translucency properties of a Billboard based on the billboard's distance from the camera.
  277. * A billboard's translucency will interpolate between the {@link NearFarScalar#nearValue} and
  278. * {@link NearFarScalar#farValue} while the camera distance falls within the upper and lower bounds
  279. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  280. * Outside of these ranges the billboard's translucency remains clamped to the nearest bound. If undefined,
  281. * translucencyByDistance will be disabled.
  282. * @memberof Billboard.prototype
  283. * @type {NearFarScalar}
  284. *
  285. * @example
  286. * // Example 1.
  287. * // Set a billboard's translucency to 1.0 when the
  288. * // camera is 1500 meters from the billboard and disappear as
  289. * // the camera distance approaches 8.0e6 meters.
  290. * b.translucencyByDistance = new Cesium.NearFarScalar(1.5e2, 1.0, 8.0e6, 0.0);
  291. *
  292. * @example
  293. * // Example 2.
  294. * // disable translucency by distance
  295. * b.translucencyByDistance = undefined;
  296. */
  297. translucencyByDistance : {
  298. get : function() {
  299. return this._translucencyByDistance;
  300. },
  301. set : function(value) {
  302. //>>includeStart('debug', pragmas.debug);
  303. if (defined(value) && value.far <= value.near) {
  304. throw new DeveloperError('far distance must be greater than near distance.');
  305. }
  306. //>>includeEnd('debug');
  307. var translucencyByDistance = this._translucencyByDistance;
  308. if (!NearFarScalar.equals(translucencyByDistance, value)) {
  309. this._translucencyByDistance = NearFarScalar.clone(value, translucencyByDistance);
  310. makeDirty(this, TRANSLUCENCY_BY_DISTANCE_INDEX);
  311. }
  312. }
  313. },
  314. /**
  315. * Gets or sets near and far pixel offset scaling properties of a Billboard based on the billboard's distance from the camera.
  316. * A billboard's pixel offset will be scaled between the {@link NearFarScalar#nearValue} and
  317. * {@link NearFarScalar#farValue} while the camera distance falls within the upper and lower bounds
  318. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  319. * Outside of these ranges the billboard's pixel offset scale remains clamped to the nearest bound. If undefined,
  320. * pixelOffsetScaleByDistance will be disabled.
  321. * @memberof Billboard.prototype
  322. * @type {NearFarScalar}
  323. *
  324. * @example
  325. * // Example 1.
  326. * // Set a billboard's pixel offset scale to 0.0 when the
  327. * // camera is 1500 meters from the billboard and scale pixel offset to 10.0 pixels
  328. * // in the y direction the camera distance approaches 8.0e6 meters.
  329. * b.pixelOffset = new Cesium.Cartesian2(0.0, 1.0);
  330. * b.pixelOffsetScaleByDistance = new Cesium.NearFarScalar(1.5e2, 0.0, 8.0e6, 10.0);
  331. *
  332. * @example
  333. * // Example 2.
  334. * // disable pixel offset by distance
  335. * b.pixelOffsetScaleByDistance = undefined;
  336. */
  337. pixelOffsetScaleByDistance : {
  338. get : function() {
  339. return this._pixelOffsetScaleByDistance;
  340. },
  341. set : function(value) {
  342. //>>includeStart('debug', pragmas.debug);
  343. if (defined(value) && value.far <= value.near) {
  344. throw new DeveloperError('far distance must be greater than near distance.');
  345. }
  346. //>>includeEnd('debug');
  347. var pixelOffsetScaleByDistance = this._pixelOffsetScaleByDistance;
  348. if (!NearFarScalar.equals(pixelOffsetScaleByDistance, value)) {
  349. this._pixelOffsetScaleByDistance = NearFarScalar.clone(value, pixelOffsetScaleByDistance);
  350. makeDirty(this, PIXEL_OFFSET_SCALE_BY_DISTANCE_INDEX);
  351. }
  352. }
  353. },
  354. /**
  355. * Gets or sets the 3D Cartesian offset applied to this billboard in eye coordinates. Eye coordinates is a left-handed
  356. * coordinate system, where <code>x</code> points towards the viewer's right, <code>y</code> points up, and
  357. * <code>z</code> points into the screen. Eye coordinates use the same scale as world and model coordinates,
  358. * which is typically meters.
  359. * <br /><br />
  360. * An eye offset is commonly used to arrange multiple billboards or objects at the same position, e.g., to
  361. * arrange a billboard above its corresponding 3D model.
  362. * <br /><br />
  363. * Below, the billboard is positioned at the center of the Earth but an eye offset makes it always
  364. * appear on top of the Earth regardless of the viewer's or Earth's orientation.
  365. * <br /><br />
  366. * <div align='center'>
  367. * <table border='0' cellpadding='5'><tr>
  368. * <td align='center'><img src='images/Billboard.setEyeOffset.one.png' width='250' height='188' /></td>
  369. * <td align='center'><img src='images/Billboard.setEyeOffset.two.png' width='250' height='188' /></td>
  370. * </tr></table>
  371. * <code>b.eyeOffset = new Cartesian3(0.0, 8000000.0, 0.0);</code><br /><br />
  372. * </div>
  373. * @memberof Billboard.prototype
  374. * @type {Cartesian3}
  375. */
  376. eyeOffset : {
  377. get : function() {
  378. return this._eyeOffset;
  379. },
  380. set : function(value) {
  381. //>>includeStart('debug', pragmas.debug);
  382. if (!defined(value)) {
  383. throw new DeveloperError('value is required.');
  384. }
  385. //>>includeEnd('debug');
  386. var eyeOffset = this._eyeOffset;
  387. if (!Cartesian3.equals(eyeOffset, value)) {
  388. Cartesian3.clone(value, eyeOffset);
  389. makeDirty(this, EYE_OFFSET_INDEX);
  390. }
  391. }
  392. },
  393. /**
  394. * Gets or sets the horizontal origin of this billboard, which determines if the billboard is
  395. * to the left, center, or right of its position.
  396. * <br /><br />
  397. * <div align='center'>
  398. * <img src='images/Billboard.setHorizontalOrigin.png' width='400' height='300' /><br />
  399. * </div>
  400. * @memberof Billboard.prototype
  401. * @type {HorizontalOrigin}
  402. * @example
  403. * // Use a bottom, left origin
  404. * b.horizontalOrigin = Cesium.HorizontalOrigin.LEFT;
  405. * b.verticalOrigin = Cesium.VerticalOrigin.BOTTOM;
  406. */
  407. horizontalOrigin : {
  408. get : function() {
  409. return this._horizontalOrigin;
  410. },
  411. set : function(value) {
  412. //>>includeStart('debug', pragmas.debug);
  413. if (!defined(value)) {
  414. throw new DeveloperError('value is required.');
  415. }
  416. //>>includeEnd('debug');
  417. if (this._horizontalOrigin !== value) {
  418. this._horizontalOrigin = value;
  419. makeDirty(this, HORIZONTAL_ORIGIN_INDEX);
  420. }
  421. }
  422. },
  423. /**
  424. * Gets or sets the vertical origin of this billboard, which determines if the billboard is
  425. * to the above, below, or at the center of its position.
  426. * <br /><br />
  427. * <div align='center'>
  428. * <img src='images/Billboard.setVerticalOrigin.png' width='400' height='300' /><br />
  429. * </div>
  430. * @memberof Billboard.prototype
  431. * @type {VerticalOrigin}
  432. * @example
  433. * // Use a bottom, left origin
  434. * b.horizontalOrigin = Cesium.HorizontalOrigin.LEFT;
  435. * b.verticalOrigin = Cesium.VerticalOrigin.BOTTOM;
  436. */
  437. verticalOrigin : {
  438. get : function() {
  439. return this._verticalOrigin;
  440. },
  441. set : function(value) {
  442. //>>includeStart('debug', pragmas.debug);
  443. if (!defined(value)) {
  444. throw new DeveloperError('value is required.');
  445. }
  446. //>>includeEnd('debug');
  447. if (this._verticalOrigin !== value) {
  448. this._verticalOrigin = value;
  449. makeDirty(this, VERTICAL_ORIGIN_INDEX);
  450. }
  451. }
  452. },
  453. /**
  454. * Gets or sets the uniform scale that is multiplied with the billboard's image size in pixels.
  455. * A scale of <code>1.0</code> does not change the size of the billboard; a scale greater than
  456. * <code>1.0</code> enlarges the billboard; a positive scale less than <code>1.0</code> shrinks
  457. * the billboard.
  458. * <br /><br />
  459. * <div align='center'>
  460. * <img src='images/Billboard.setScale.png' width='400' height='300' /><br/>
  461. * From left to right in the above image, the scales are <code>0.5</code>, <code>1.0</code>,
  462. * and <code>2.0</code>.
  463. * </div>
  464. * @memberof Billboard.prototype
  465. * @type {Number}
  466. */
  467. scale : {
  468. get : function() {
  469. return this._scale;
  470. },
  471. set : function(value) {
  472. //>>includeStart('debug', pragmas.debug);
  473. if (!defined(value)) {
  474. throw new DeveloperError('value is required.');
  475. }
  476. //>>includeEnd('debug');
  477. if (this._scale !== value) {
  478. this._scale = value;
  479. makeDirty(this, SCALE_INDEX);
  480. }
  481. }
  482. },
  483. /**
  484. * Gets or sets the color that is multiplied with the billboard's texture. This has two common use cases. First,
  485. * the same white texture may be used by many different billboards, each with a different color, to create
  486. * colored billboards. Second, the color's alpha component can be used to make the billboard translucent as shown below.
  487. * An alpha of <code>0.0</code> makes the billboard transparent, and <code>1.0</code> makes the billboard opaque.
  488. * <br /><br />
  489. * <div align='center'>
  490. * <table border='0' cellpadding='5'><tr>
  491. * <td align='center'><code>default</code><br/><img src='images/Billboard.setColor.Alpha255.png' width='250' height='188' /></td>
  492. * <td align='center'><code>alpha : 0.5</code><br/><img src='images/Billboard.setColor.Alpha127.png' width='250' height='188' /></td>
  493. * </tr></table>
  494. * </div>
  495. * <br />
  496. * The red, green, blue, and alpha values are indicated by <code>value</code>'s <code>red</code>, <code>green</code>,
  497. * <code>blue</code>, and <code>alpha</code> properties as shown in Example 1. These components range from <code>0.0</code>
  498. * (no intensity) to <code>1.0</code> (full intensity).
  499. * @memberof Billboard.prototype
  500. * @param {Color}
  501. *
  502. * @example
  503. * // Example 1. Assign yellow.
  504. * b.color = Cesium.Color.YELLOW;
  505. *
  506. * @example
  507. * // Example 2. Make a billboard 50% translucent.
  508. * b.color = new Cesium.Color(1.0, 1.0, 1.0, 0.5);
  509. */
  510. color : {
  511. get : function() {
  512. return this._color;
  513. },
  514. set : function(value) {
  515. //>>includeStart('debug', pragmas.debug);
  516. if (!defined(value)) {
  517. throw new DeveloperError('value is required.');
  518. }
  519. //>>includeEnd('debug');
  520. var color = this._color;
  521. if (!Color.equals(color, value)) {
  522. Color.clone(value, color);
  523. makeDirty(this, COLOR_INDEX);
  524. }
  525. }
  526. },
  527. /**
  528. * Gets or sets the rotation angle in radians.
  529. * @memberof Billboard.prototype
  530. * @type {Number}
  531. */
  532. rotation : {
  533. get : function() {
  534. return this._rotation;
  535. },
  536. set : function(value) {
  537. //>>includeStart('debug', pragmas.debug);
  538. if (!defined(value)) {
  539. throw new DeveloperError('value is required.');
  540. }
  541. //>>includeEnd('debug');
  542. if (this._rotation !== value) {
  543. this._rotation = value;
  544. makeDirty(this, ROTATION_INDEX);
  545. }
  546. }
  547. },
  548. /**
  549. * Gets or sets the aligned axis in world space. The aligned axis is the unit vector that the billboard up vector points towards.
  550. * The default is the zero vector, which means the billboard is aligned to the screen up vector.
  551. * @memberof Billboard.prototype
  552. * @type {Cartesian3}
  553. * @example
  554. * // Example 1.
  555. * // Have the billboard up vector point north
  556. * billboard.alignedAxis = Cesium.Cartesian3.UNIT_Z;
  557. *
  558. * @example
  559. * // Example 2.
  560. * // Have the billboard point east.
  561. * billboard.alignedAxis = Cartesian3.UNIT_Z;
  562. * billboard.rotation = -Cesium.Math.PI_OVER_TWO;
  563. *
  564. * @example
  565. * // Example 3.
  566. * // Reset the aligned axis
  567. * billboard.alignedAxis = Cesium.Cartesian3.ZERO;
  568. */
  569. alignedAxis : {
  570. get : function() {
  571. return this._alignedAxis;
  572. },
  573. set : function(value) {
  574. //>>includeStart('debug', pragmas.debug);
  575. if (!defined(value)) {
  576. throw new DeveloperError('value is required.');
  577. }
  578. //>>includeEnd('debug');
  579. var alignedAxis = this._alignedAxis;
  580. if (!Cartesian3.equals(alignedAxis, value)) {
  581. Cartesian3.clone(value, alignedAxis);
  582. makeDirty(this, ALIGNED_AXIS_INDEX);
  583. }
  584. }
  585. },
  586. /**
  587. * Gets or sets a width for the billboard. If undefined, the image width will be used.
  588. * @memberof Billboard.prototype
  589. * @type {Number}
  590. */
  591. width : {
  592. get : function() {
  593. return defaultValue(this._width, this._imageWidth);
  594. },
  595. set : function(value) {
  596. if (this._width !== value) {
  597. this._width = value;
  598. makeDirty(this, IMAGE_INDEX_INDEX);
  599. }
  600. }
  601. },
  602. /**
  603. * Gets or sets a height for the billboard. If undefined, the image height will be used.
  604. * @memberof Billboard.prototype
  605. * @type {Number}
  606. */
  607. height : {
  608. get : function() {
  609. return defaultValue(this._height, this._imageHeight);
  610. },
  611. set : function(value) {
  612. if (this._height !== value) {
  613. this._height = value;
  614. makeDirty(this, IMAGE_INDEX_INDEX);
  615. }
  616. }
  617. },
  618. /**
  619. * Gets or sets the user-defined object returned when the billboard is picked.
  620. * @memberof Billboard.prototype
  621. * @type {Object}
  622. */
  623. id : {
  624. get : function() {
  625. return this._id;
  626. },
  627. set : function(value) {
  628. this._id = value;
  629. if (defined(this._pickId)) {
  630. this._pickId.object.id = value;
  631. }
  632. }
  633. },
  634. /**
  635. * The primitive to return when picking this billboard.
  636. * @memberof Billboard.prototype
  637. * @private
  638. */
  639. pickPrimitive : {
  640. get : function() {
  641. return this._pickPrimitive;
  642. },
  643. set : function(value) {
  644. this._pickPrimitive = value;
  645. if (defined(this._pickId)) {
  646. this._pickId.object.primitive = value;
  647. }
  648. }
  649. },
  650. /**
  651. * <p>
  652. * Gets or sets the image to be used for this billboard. If a texture has already been created for the
  653. * given image, the existing texture is used.
  654. * </p>
  655. * <p>
  656. * This property can be set to a loaded Image, a URL which will be loaded as an Image automatically,
  657. * a canvas, or another billboard's image property (from the same billboard collection).
  658. * </p>
  659. *
  660. * @memberof Billboard.prototype
  661. * @type {String}
  662. * @example
  663. * // load an image from a URL
  664. * b.image = 'some/image/url.png';
  665. *
  666. * // assuming b1 and b2 are billboards in the same billboard collection,
  667. * // use the same image for both billboards.
  668. * b2.image = b1.image;
  669. */
  670. image : {
  671. get : function() {
  672. return this._imageId;
  673. },
  674. set : function(value) {
  675. if (!defined(value)) {
  676. this._imageIndex = -1;
  677. this._imageSubRegion = undefined;
  678. this._imageId = undefined;
  679. this._image = undefined;
  680. this._imageIndexPromise = undefined;
  681. makeDirty(this, IMAGE_INDEX_INDEX);
  682. } else if (typeof value === 'string') {
  683. this.setImage(value, value);
  684. } else if (defined(value.src)) {
  685. this.setImage(value.src, value);
  686. } else {
  687. this.setImage(createGuid(), value);
  688. }
  689. }
  690. },
  691. /**
  692. * When <code>true</code>, this billboard is ready to render, i.e., the image
  693. * has been downloaded and the WebGL resources are created.
  694. *
  695. * @memberof Billboard.prototype
  696. *
  697. * @type {Boolean}
  698. * @readonly
  699. *
  700. * @default false
  701. */
  702. ready : {
  703. get : function() {
  704. return this._imageIndex !== -1;
  705. }
  706. }
  707. });
  708. Billboard.prototype.getPickId = function(context) {
  709. if (!defined(this._pickId)) {
  710. this._pickId = context.createPickId({
  711. primitive : this._pickPrimitive,
  712. collection : this._collection,
  713. id : this._id
  714. });
  715. }
  716. return this._pickId;
  717. };
  718. Billboard.prototype._loadImage = function() {
  719. var atlas = this._billboardCollection._textureAtlas;
  720. var imageId = this._imageId;
  721. var image = this._image;
  722. var imageSubRegion = this._imageSubRegion;
  723. var imageIndexPromise;
  724. if (defined(image)) {
  725. imageIndexPromise = atlas.addImage(imageId, image);
  726. }
  727. if (defined(imageSubRegion)) {
  728. imageIndexPromise = atlas.addSubRegion(imageId, imageSubRegion);
  729. }
  730. this._imageIndexPromise = imageIndexPromise;
  731. if (!defined(imageIndexPromise)) {
  732. return;
  733. }
  734. var that = this;
  735. imageIndexPromise.then(function(index) {
  736. if (that._imageId !== imageId || that._image !== image || !BoundingRectangle.equals(that._imageSubRegion, imageSubRegion)) {
  737. // another load occurred before this one finished, ignore the index
  738. return;
  739. }
  740. // fill in imageWidth and imageHeight
  741. var textureCoordinates = atlas.textureCoordinates[index];
  742. that._imageWidth = atlas.texture.width * textureCoordinates.width;
  743. that._imageHeight = atlas.texture.height * textureCoordinates.height;
  744. that._imageIndex = index;
  745. that._ready = true;
  746. that._image = undefined;
  747. that._imageIndexPromise = undefined;
  748. makeDirty(that, IMAGE_INDEX_INDEX);
  749. }).otherwise(function(error) {
  750. /*global console*/
  751. console.error('Error loading image for billboard: ' + error);
  752. that._imageIndexPromise = undefined;
  753. });
  754. };
  755. /**
  756. * <p>
  757. * Sets the image to be used for this billboard. If a texture has already been created for the
  758. * given id, the existing texture is used.
  759. * </p>
  760. * <p>
  761. * This function is useful for dynamically creating textures that are shared across many billboards.
  762. * Only the first billboard will actually call the function and create the texture, while subsequent
  763. * billboards created with the same id will simply re-use the existing texture.
  764. * </p>
  765. * <p>
  766. * To load an image from a URL, setting the {@link Billboard#image} property is more convenient.
  767. * </p>
  768. *
  769. * @param {String} id The id of the image. This can be any string that uniquely identifies the image.
  770. * @param {Image|Canvas|String|Billboard~CreateImageCallback} image The image to load. This parameter
  771. * can either be a loaded Image or Canvas, a URL which will be loaded as an Image automatically,
  772. * or a function which will be called to create the image if it hasn't been loaded already.
  773. * @example
  774. * // create a billboard image dynamically
  775. * function drawImage(id) {
  776. * // create and draw an image using a canvas
  777. * var canvas = document.createElement('canvas');
  778. * var context2D = canvas.getContext('2d');
  779. * // ... draw image
  780. * return canvas;
  781. * }
  782. * // drawImage will be called to create the texture
  783. * b.setImage('myImage', drawImage);
  784. *
  785. * // subsequent billboards created in the same collection using the same id will use the existing
  786. * // texture, without the need to create the canvas or draw the image
  787. * b2.setImage('myImage', drawImage);
  788. */
  789. Billboard.prototype.setImage = function(id, image) {
  790. //>>includeStart('debug', pragmas.debug);
  791. if (!defined(id)) {
  792. throw new DeveloperError('id is required.');
  793. }
  794. if (!defined(image)) {
  795. throw new DeveloperError('image is required.');
  796. }
  797. //>>includeEnd('debug');
  798. if (this._imageId === id) {
  799. return;
  800. }
  801. this._imageIndex = -1;
  802. this._imageSubRegion = undefined;
  803. this._imageId = id;
  804. this._image = image;
  805. if (defined(this._billboardCollection._textureAtlas)) {
  806. this._loadImage();
  807. }
  808. };
  809. /**
  810. * Uses a sub-region of the image with the given id as the image for this billboard.
  811. *
  812. * @param {String} id The id of the image to use.
  813. * @param {BoundingRectangle} subRegion The sub-region of the image.
  814. *
  815. * @exception {RuntimeError} image with id must be in the atlas
  816. */
  817. Billboard.prototype.setImageSubRegion = function(id, subRegion) {
  818. //>>includeStart('debug', pragmas.debug);
  819. if (!defined(id)) {
  820. throw new DeveloperError('id is required.');
  821. }
  822. if (!defined(subRegion)) {
  823. throw new DeveloperError('subRegion is required.');
  824. }
  825. //>>includeEnd('debug');
  826. if (this._imageId === id && BoundingRectangle.equals(this._imageSubRegion, subRegion)) {
  827. return;
  828. }
  829. this._imageIndex = -1;
  830. this._imageId = id;
  831. this._imageSubRegion = subRegion;
  832. if (defined(this._billboardCollection._textureAtlas)) {
  833. this._loadImage();
  834. }
  835. };
  836. Billboard.prototype._setTranslate = function(value) {
  837. //>>includeStart('debug', pragmas.debug);
  838. if (!defined(value)) {
  839. throw new DeveloperError('value is required.');
  840. }
  841. //>>includeEnd('debug');
  842. var translate = this._translate;
  843. if (!Cartesian2.equals(translate, value)) {
  844. Cartesian2.clone(value, translate);
  845. makeDirty(this, PIXEL_OFFSET_INDEX);
  846. }
  847. };
  848. Billboard.prototype._getActualPosition = function() {
  849. return this._actualPosition;
  850. };
  851. Billboard.prototype._setActualPosition = function(value) {
  852. Cartesian3.clone(value, this._actualPosition);
  853. makeDirty(this, POSITION_INDEX);
  854. };
  855. var tempCartesian3 = new Cartesian4();
  856. Billboard._computeActualPosition = function(position, frameState, modelMatrix) {
  857. if (frameState.mode === SceneMode.SCENE3D) {
  858. return position;
  859. }
  860. Matrix4.multiplyByPoint(modelMatrix, position, tempCartesian3);
  861. return SceneTransforms.computeActualWgs84Position(frameState, tempCartesian3);
  862. };
  863. var scratchMatrix4 = new Matrix4();
  864. var scratchCartesian4 = new Cartesian4();
  865. var scrachEyeOffset = new Cartesian3();
  866. var scratchCartesian2 = new Cartesian2();
  867. var scratchComputePixelOffset = new Cartesian2();
  868. Billboard._computeScreenSpacePosition = function(modelMatrix, position, eyeOffset, pixelOffset, scene) {
  869. // This function is basically a stripped-down JavaScript version of BillboardCollectionVS.glsl
  870. var camera = scene.camera;
  871. var view = camera.viewMatrix;
  872. var projection = camera.frustum.projectionMatrix;
  873. // Model to eye coordinates
  874. var mv = Matrix4.multiplyTransformation(view, modelMatrix, scratchMatrix4);
  875. var positionEC = Matrix4.multiplyByVector(mv, Cartesian4.fromElements(position.x, position.y, position.z, 1, scratchCartesian4), scratchCartesian4);
  876. // Apply eye offset, e.g., czm_eyeOffset
  877. var zEyeOffset = Cartesian3.multiplyComponents(eyeOffset, Cartesian3.normalize(positionEC, scrachEyeOffset), scrachEyeOffset);
  878. positionEC.x += eyeOffset.x + zEyeOffset.x;
  879. positionEC.y += eyeOffset.y + zEyeOffset.y;
  880. positionEC.z += zEyeOffset.z;
  881. var positionCC = Matrix4.multiplyByVector(projection, positionEC, scratchCartesian4); // clip coordinates
  882. var positionWC = SceneTransforms.clipToGLWindowCoordinates(scene, positionCC, new Cartesian2());
  883. // Apply pixel offset
  884. pixelOffset = Cartesian2.clone(pixelOffset, scratchComputePixelOffset);
  885. pixelOffset.y = -pixelOffset.y;
  886. var po = Cartesian2.multiplyByScalar(pixelOffset, scene.context.uniformState.resolutionScale, scratchCartesian2);
  887. positionWC.x += po.x;
  888. positionWC.y += po.y;
  889. return positionWC;
  890. };
  891. var scratchPixelOffset = new Cartesian2(0.0, 0.0);
  892. /**
  893. * Computes the screen-space position of the billboard's origin, taking into account eye and pixel offsets.
  894. * The screen space origin is the top, left corner of the canvas; <code>x</code> increases from
  895. * left to right, and <code>y</code> increases from top to bottom.
  896. *
  897. * @param {Scene} scene The scene.
  898. * @returns {Cartesian2} The screen-space position of the billboard.
  899. *
  900. * @exception {DeveloperError} Billboard must be in a collection.
  901. *
  902. * @see Billboard#eyeOffset
  903. * @see Billboard#pixelOffset
  904. *
  905. * @example
  906. * console.log(b.computeScreenSpacePosition(scene).toString());
  907. */
  908. Billboard.prototype.computeScreenSpacePosition = function(scene) {
  909. var billboardCollection = this._billboardCollection;
  910. //>>includeStart('debug', pragmas.debug);
  911. if (!defined(billboardCollection)) {
  912. throw new DeveloperError('Billboard must be in a collection. Was it removed?');
  913. }
  914. if (!defined(scene)) {
  915. throw new DeveloperError('scene is required.');
  916. }
  917. //>>includeEnd('debug');
  918. // pixel offset for screenspace computation is the pixelOffset + screenspace translate
  919. Cartesian2.clone(this._pixelOffset, scratchPixelOffset);
  920. Cartesian2.add(scratchPixelOffset, this._translate, scratchPixelOffset);
  921. var modelMatrix = billboardCollection.modelMatrix;
  922. var windowCoordinates = Billboard._computeScreenSpacePosition(modelMatrix, this._actualPosition, this._eyeOffset, scratchPixelOffset, scene);
  923. windowCoordinates.y = scene.canvas.clientHeight - windowCoordinates.y;
  924. return windowCoordinates;
  925. };
  926. /**
  927. * Determines if this billboard equals another billboard. Billboards are equal if all their properties
  928. * are equal. Billboards in different collections can be equal.
  929. *
  930. * @param {Billboard} other The billboard to compare for equality.
  931. * @returns {Boolean} <code>true</code> if the billboards are equal; otherwise, <code>false</code>.
  932. */
  933. Billboard.prototype.equals = function(other) {
  934. return this === other ||
  935. defined(other) &&
  936. this._show === other._show &&
  937. this._scale === other._scale &&
  938. this._verticalOrigin === other._verticalOrigin &&
  939. this._horizontalOrigin === other._horizontalOrigin &&
  940. this._id === other._id &&
  941. this._imageId === other._imageId &&
  942. BoundingRectangle.equals(this._imageSubRegion, other._imageSubRegion) &&
  943. Cartesian3.equals(this._position, other._position) &&
  944. Color.equals(this._color, other._color) &&
  945. Cartesian2.equals(this._pixelOffset, other._pixelOffset) &&
  946. Cartesian2.equals(this._translate, other._translate) &&
  947. Cartesian3.equals(this._eyeOffset, other._eyeOffset) &&
  948. NearFarScalar.equals(this._scaleByDistance, other._scaleByDistance) &&
  949. NearFarScalar.equals(this._translucencyByDistance, other._translucencyByDistance) &&
  950. NearFarScalar.equals(this._pixelOffsetScaleByDistance, other._pixelOffsetScaleByDistance);
  951. };
  952. Billboard.prototype._destroy = function() {
  953. this.image = undefined;
  954. this._pickId = this._pickId && this._pickId.destroy();
  955. this._billboardCollection = undefined;
  956. };
  957. /**
  958. * A function that creates an image.
  959. * @callback Billboard~CreateImageCallback
  960. * @param {String} id The identifier of the image to load.
  961. * @returns {Image|Canvas|Promise} The image, or a promise that will resolve to an image.
  962. */
  963. return Billboard;
  964. });