Billboard.js 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. import BoundingRectangle from '../Core/BoundingRectangle.js';
  2. import Cartesian2 from '../Core/Cartesian2.js';
  3. import Cartesian3 from '../Core/Cartesian3.js';
  4. import Cartesian4 from '../Core/Cartesian4.js';
  5. import Cartographic from '../Core/Cartographic.js';
  6. import Color from '../Core/Color.js';
  7. import createGuid from '../Core/createGuid.js';
  8. import defaultValue from '../Core/defaultValue.js';
  9. import defined from '../Core/defined.js';
  10. import defineProperties from '../Core/defineProperties.js';
  11. import DeveloperError from '../Core/DeveloperError.js';
  12. import DistanceDisplayCondition from '../Core/DistanceDisplayCondition.js';
  13. import Matrix4 from '../Core/Matrix4.js';
  14. import NearFarScalar from '../Core/NearFarScalar.js';
  15. import Resource from '../Core/Resource.js';
  16. import HeightReference from './HeightReference.js';
  17. import HorizontalOrigin from './HorizontalOrigin.js';
  18. import SceneMode from './SceneMode.js';
  19. import SceneTransforms from './SceneTransforms.js';
  20. import VerticalOrigin from './VerticalOrigin.js';
  21. /**
  22. * A viewport-aligned image positioned in the 3D scene, that is created
  23. * and rendered using a {@link BillboardCollection}. A billboard is created and its initial
  24. * properties are set by calling {@link BillboardCollection#add}.
  25. * <br /><br />
  26. * <div align='center'>
  27. * <img src='Images/Billboard.png' width='400' height='300' /><br />
  28. * Example billboards
  29. * </div>
  30. *
  31. * @alias Billboard
  32. *
  33. * @performance Reading a property, e.g., {@link Billboard#show}, is constant time.
  34. * Assigning to a property is constant time but results in
  35. * CPU to GPU traffic when {@link BillboardCollection#update} is called. The per-billboard traffic is
  36. * the same regardless of how many properties were updated. If most billboards in a collection need to be
  37. * updated, it may be more efficient to clear the collection with {@link BillboardCollection#removeAll}
  38. * and add new billboards instead of modifying each one.
  39. *
  40. * @exception {DeveloperError} scaleByDistance.far must be greater than scaleByDistance.near
  41. * @exception {DeveloperError} translucencyByDistance.far must be greater than translucencyByDistance.near
  42. * @exception {DeveloperError} pixelOffsetScaleByDistance.far must be greater than pixelOffsetScaleByDistance.near
  43. * @exception {DeveloperError} distanceDisplayCondition.far must be greater than distanceDisplayCondition.near
  44. *
  45. * @see BillboardCollection
  46. * @see BillboardCollection#add
  47. * @see Label
  48. *
  49. * @internalConstructor
  50. * @class
  51. *
  52. * @demo {@link https://sandcastle.cesium.com/index.html?src=Billboards.html|Cesium Sandcastle Billboard Demo}
  53. */
  54. function Billboard(options, billboardCollection) {
  55. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  56. //>>includeStart('debug', pragmas.debug);
  57. if (defined(options.disableDepthTestDistance) && options.disableDepthTestDistance < 0.0) {
  58. throw new DeveloperError('disableDepthTestDistance must be greater than or equal to 0.0.');
  59. }
  60. //>>includeEnd('debug');
  61. var translucencyByDistance = options.translucencyByDistance;
  62. var pixelOffsetScaleByDistance = options.pixelOffsetScaleByDistance;
  63. var scaleByDistance = options.scaleByDistance;
  64. var distanceDisplayCondition = options.distanceDisplayCondition;
  65. if (defined(translucencyByDistance)) {
  66. //>>includeStart('debug', pragmas.debug);
  67. if (translucencyByDistance.far <= translucencyByDistance.near) {
  68. throw new DeveloperError('translucencyByDistance.far must be greater than translucencyByDistance.near.');
  69. }
  70. //>>includeEnd('debug');
  71. translucencyByDistance = NearFarScalar.clone(translucencyByDistance);
  72. }
  73. if (defined(pixelOffsetScaleByDistance)) {
  74. //>>includeStart('debug', pragmas.debug);
  75. if (pixelOffsetScaleByDistance.far <= pixelOffsetScaleByDistance.near) {
  76. throw new DeveloperError('pixelOffsetScaleByDistance.far must be greater than pixelOffsetScaleByDistance.near.');
  77. }
  78. //>>includeEnd('debug');
  79. pixelOffsetScaleByDistance = NearFarScalar.clone(pixelOffsetScaleByDistance);
  80. }
  81. if (defined(scaleByDistance)) {
  82. //>>includeStart('debug', pragmas.debug);
  83. if (scaleByDistance.far <= scaleByDistance.near) {
  84. throw new DeveloperError('scaleByDistance.far must be greater than scaleByDistance.near.');
  85. }
  86. //>>includeEnd('debug');
  87. scaleByDistance = NearFarScalar.clone(scaleByDistance);
  88. }
  89. if (defined(distanceDisplayCondition)) {
  90. //>>includeStart('debug', pragmas.debug);
  91. if (distanceDisplayCondition.far <= distanceDisplayCondition.near) {
  92. throw new DeveloperError('distanceDisplayCondition.far must be greater than distanceDisplayCondition.near.');
  93. }
  94. //>>includeEnd('debug');
  95. distanceDisplayCondition = DistanceDisplayCondition.clone(distanceDisplayCondition);
  96. }
  97. this._show = defaultValue(options.show, true);
  98. this._position = Cartesian3.clone(defaultValue(options.position, Cartesian3.ZERO));
  99. this._actualPosition = Cartesian3.clone(this._position); // For columbus view and 2D
  100. this._pixelOffset = Cartesian2.clone(defaultValue(options.pixelOffset, Cartesian2.ZERO));
  101. this._translate = new Cartesian2(0.0, 0.0); // used by labels for glyph vertex translation
  102. this._eyeOffset = Cartesian3.clone(defaultValue(options.eyeOffset, Cartesian3.ZERO));
  103. this._heightReference = defaultValue(options.heightReference, HeightReference.NONE);
  104. this._verticalOrigin = defaultValue(options.verticalOrigin, VerticalOrigin.CENTER);
  105. this._horizontalOrigin = defaultValue(options.horizontalOrigin, HorizontalOrigin.CENTER);
  106. this._scale = defaultValue(options.scale, 1.0);
  107. this._color = Color.clone(defaultValue(options.color, Color.WHITE));
  108. this._rotation = defaultValue(options.rotation, 0.0);
  109. this._alignedAxis = Cartesian3.clone(defaultValue(options.alignedAxis, Cartesian3.ZERO));
  110. this._width = options.width;
  111. this._height = options.height;
  112. this._scaleByDistance = scaleByDistance;
  113. this._translucencyByDistance = translucencyByDistance;
  114. this._pixelOffsetScaleByDistance = pixelOffsetScaleByDistance;
  115. this._sizeInMeters = defaultValue(options.sizeInMeters, false);
  116. this._distanceDisplayCondition = distanceDisplayCondition;
  117. this._disableDepthTestDistance = options.disableDepthTestDistance;
  118. this._id = options.id;
  119. this._collection = defaultValue(options.collection, billboardCollection);
  120. this._pickId = undefined;
  121. this._pickPrimitive = defaultValue(options._pickPrimitive, this);
  122. this._billboardCollection = billboardCollection;
  123. this._dirty = false;
  124. this._index = -1; //Used only by BillboardCollection
  125. this._batchIndex = undefined; // Used only by Vector3DTilePoints and BillboardCollection
  126. this._imageIndex = -1;
  127. this._imageIndexPromise = undefined;
  128. this._imageId = undefined;
  129. this._image = undefined;
  130. this._imageSubRegion = undefined;
  131. this._imageWidth = undefined;
  132. this._imageHeight = undefined;
  133. this._labelDimensions = undefined;
  134. this._labelHorizontalOrigin = undefined;
  135. this._labelTranslate = undefined;
  136. var image = options.image;
  137. var imageId = options.imageId;
  138. if (defined(image)) {
  139. if (!defined(imageId)) {
  140. if (typeof image === 'string') {
  141. imageId = image;
  142. } else if (defined(image.src)) {
  143. imageId = image.src;
  144. } else {
  145. imageId = createGuid();
  146. }
  147. }
  148. this._imageId = imageId;
  149. this._image = image;
  150. }
  151. if (defined(options.imageSubRegion)) {
  152. this._imageId = imageId;
  153. this._imageSubRegion = options.imageSubRegion;
  154. }
  155. if (defined(this._billboardCollection._textureAtlas)) {
  156. this._loadImage();
  157. }
  158. this._actualClampedPosition = undefined;
  159. this._removeCallbackFunc = undefined;
  160. this._mode = SceneMode.SCENE3D;
  161. this._clusterShow = true;
  162. this._outlineColor = Color.clone(defaultValue(options.outlineColor, Color.BLACK));
  163. this._outlineWidth = defaultValue(options.outlineWidth, 0.0);
  164. this._updateClamping();
  165. }
  166. var SHOW_INDEX = Billboard.SHOW_INDEX = 0;
  167. var POSITION_INDEX = Billboard.POSITION_INDEX = 1;
  168. var PIXEL_OFFSET_INDEX = Billboard.PIXEL_OFFSET_INDEX = 2;
  169. var EYE_OFFSET_INDEX = Billboard.EYE_OFFSET_INDEX = 3;
  170. var HORIZONTAL_ORIGIN_INDEX = Billboard.HORIZONTAL_ORIGIN_INDEX = 4;
  171. var VERTICAL_ORIGIN_INDEX = Billboard.VERTICAL_ORIGIN_INDEX = 5;
  172. var SCALE_INDEX = Billboard.SCALE_INDEX = 6;
  173. var IMAGE_INDEX_INDEX = Billboard.IMAGE_INDEX_INDEX = 7;
  174. var COLOR_INDEX = Billboard.COLOR_INDEX = 8;
  175. var ROTATION_INDEX = Billboard.ROTATION_INDEX = 9;
  176. var ALIGNED_AXIS_INDEX = Billboard.ALIGNED_AXIS_INDEX = 10;
  177. var SCALE_BY_DISTANCE_INDEX = Billboard.SCALE_BY_DISTANCE_INDEX = 11;
  178. var TRANSLUCENCY_BY_DISTANCE_INDEX = Billboard.TRANSLUCENCY_BY_DISTANCE_INDEX = 12;
  179. var PIXEL_OFFSET_SCALE_BY_DISTANCE_INDEX = Billboard.PIXEL_OFFSET_SCALE_BY_DISTANCE_INDEX = 13;
  180. var DISTANCE_DISPLAY_CONDITION = Billboard.DISTANCE_DISPLAY_CONDITION = 14;
  181. var DISABLE_DEPTH_DISTANCE = Billboard.DISABLE_DEPTH_DISTANCE = 15;
  182. Billboard.TEXTURE_COORDINATE_BOUNDS = 16;
  183. var SDF_INDEX = Billboard.SDF_INDEX = 17;
  184. Billboard.NUMBER_OF_PROPERTIES = 18;
  185. function makeDirty(billboard, propertyChanged) {
  186. var billboardCollection = billboard._billboardCollection;
  187. if (defined(billboardCollection)) {
  188. billboardCollection._updateBillboard(billboard, propertyChanged);
  189. billboard._dirty = true;
  190. }
  191. }
  192. defineProperties(Billboard.prototype, {
  193. /**
  194. * Determines if this billboard will be shown. Use this to hide or show a billboard, instead
  195. * of removing it and re-adding it to the collection.
  196. * @memberof Billboard.prototype
  197. * @type {Boolean}
  198. * @default true
  199. */
  200. show : {
  201. get : function() {
  202. return this._show;
  203. },
  204. set : function(value) {
  205. //>>includeStart('debug', pragmas.debug);
  206. if (!defined(value)) {
  207. throw new DeveloperError('value is required.');
  208. }
  209. //>>includeEnd('debug');
  210. if (this._show !== value) {
  211. this._show = value;
  212. makeDirty(this, SHOW_INDEX);
  213. }
  214. }
  215. },
  216. /**
  217. * Gets or sets the Cartesian position of this billboard.
  218. * @memberof Billboard.prototype
  219. * @type {Cartesian3}
  220. */
  221. position : {
  222. get : function() {
  223. return this._position;
  224. },
  225. set : function(value) {
  226. //>>includeStart('debug', pragmas.debug)
  227. if (!defined(value)) {
  228. throw new DeveloperError('value is required.');
  229. }
  230. //>>includeEnd('debug');
  231. var position = this._position;
  232. if (!Cartesian3.equals(position, value)) {
  233. Cartesian3.clone(value, position);
  234. Cartesian3.clone(value, this._actualPosition);
  235. this._updateClamping();
  236. makeDirty(this, POSITION_INDEX);
  237. }
  238. }
  239. },
  240. /**
  241. * Gets or sets the height reference of this billboard.
  242. * @memberof Billboard.prototype
  243. * @type {HeightReference}
  244. * @default HeightReference.NONE
  245. */
  246. heightReference : {
  247. get : function() {
  248. return this._heightReference;
  249. },
  250. set : function(value) {
  251. //>>includeStart('debug', pragmas.debug)
  252. if (!defined(value)) {
  253. throw new DeveloperError('value is required.');
  254. }
  255. //>>includeEnd('debug');
  256. var heightReference = this._heightReference;
  257. if (value !== heightReference) {
  258. this._heightReference = value;
  259. this._updateClamping();
  260. makeDirty(this, POSITION_INDEX);
  261. }
  262. }
  263. },
  264. /**
  265. * Gets or sets the pixel offset in screen space from the origin of this billboard. This is commonly used
  266. * to align multiple billboards and labels at the same position, e.g., an image and text. The
  267. * screen space origin is the top, left corner of the canvas; <code>x</code> increases from
  268. * left to right, and <code>y</code> increases from top to bottom.
  269. * <br /><br />
  270. * <div align='center'>
  271. * <table border='0' cellpadding='5'><tr>
  272. * <td align='center'><code>default</code><br/><img src='Images/Billboard.setPixelOffset.default.png' width='250' height='188' /></td>
  273. * <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>
  274. * </tr></table>
  275. * The billboard's origin is indicated by the yellow point.
  276. * </div>
  277. * @memberof Billboard.prototype
  278. * @type {Cartesian2}
  279. */
  280. pixelOffset : {
  281. get : function() {
  282. return this._pixelOffset;
  283. },
  284. set : function(value) {
  285. //>>includeStart('debug', pragmas.debug);
  286. if (!defined(value)) {
  287. throw new DeveloperError('value is required.');
  288. }
  289. //>>includeEnd('debug');
  290. var pixelOffset = this._pixelOffset;
  291. if (!Cartesian2.equals(pixelOffset, value)) {
  292. Cartesian2.clone(value, pixelOffset);
  293. makeDirty(this, PIXEL_OFFSET_INDEX);
  294. }
  295. }
  296. },
  297. /**
  298. * Gets or sets near and far scaling properties of a Billboard based on the billboard's distance from the camera.
  299. * A billboard's scale will interpolate between the {@link NearFarScalar#nearValue} and
  300. * {@link NearFarScalar#farValue} while the camera distance falls within the upper and lower bounds
  301. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  302. * Outside of these ranges the billboard's scale remains clamped to the nearest bound. If undefined,
  303. * scaleByDistance will be disabled.
  304. * @memberof Billboard.prototype
  305. * @type {NearFarScalar}
  306. *
  307. * @example
  308. * // Example 1.
  309. * // Set a billboard's scaleByDistance to scale by 1.5 when the
  310. * // camera is 1500 meters from the billboard and disappear as
  311. * // the camera distance approaches 8.0e6 meters.
  312. * b.scaleByDistance = new Cesium.NearFarScalar(1.5e2, 1.5, 8.0e6, 0.0);
  313. *
  314. * @example
  315. * // Example 2.
  316. * // disable scaling by distance
  317. * b.scaleByDistance = undefined;
  318. */
  319. scaleByDistance : {
  320. get : function() {
  321. return this._scaleByDistance;
  322. },
  323. set : function(value) {
  324. //>>includeStart('debug', pragmas.debug);
  325. if (defined(value) && value.far <= value.near) {
  326. throw new DeveloperError('far distance must be greater than near distance.');
  327. }
  328. //>>includeEnd('debug');
  329. var scaleByDistance = this._scaleByDistance;
  330. if (!NearFarScalar.equals(scaleByDistance, value)) {
  331. this._scaleByDistance = NearFarScalar.clone(value, scaleByDistance);
  332. makeDirty(this, SCALE_BY_DISTANCE_INDEX);
  333. }
  334. }
  335. },
  336. /**
  337. * Gets or sets near and far translucency properties of a Billboard based on the billboard's distance from the camera.
  338. * A billboard's translucency will interpolate between the {@link NearFarScalar#nearValue} and
  339. * {@link NearFarScalar#farValue} while the camera distance falls within the upper and lower bounds
  340. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  341. * Outside of these ranges the billboard's translucency remains clamped to the nearest bound. If undefined,
  342. * translucencyByDistance will be disabled.
  343. * @memberof Billboard.prototype
  344. * @type {NearFarScalar}
  345. *
  346. * @example
  347. * // Example 1.
  348. * // Set a billboard's translucency to 1.0 when the
  349. * // camera is 1500 meters from the billboard and disappear as
  350. * // the camera distance approaches 8.0e6 meters.
  351. * b.translucencyByDistance = new Cesium.NearFarScalar(1.5e2, 1.0, 8.0e6, 0.0);
  352. *
  353. * @example
  354. * // Example 2.
  355. * // disable translucency by distance
  356. * b.translucencyByDistance = undefined;
  357. */
  358. translucencyByDistance : {
  359. get : function() {
  360. return this._translucencyByDistance;
  361. },
  362. set : function(value) {
  363. //>>includeStart('debug', pragmas.debug);
  364. if (defined(value) && value.far <= value.near) {
  365. throw new DeveloperError('far distance must be greater than near distance.');
  366. }
  367. //>>includeEnd('debug');
  368. var translucencyByDistance = this._translucencyByDistance;
  369. if (!NearFarScalar.equals(translucencyByDistance, value)) {
  370. this._translucencyByDistance = NearFarScalar.clone(value, translucencyByDistance);
  371. makeDirty(this, TRANSLUCENCY_BY_DISTANCE_INDEX);
  372. }
  373. }
  374. },
  375. /**
  376. * Gets or sets near and far pixel offset scaling properties of a Billboard based on the billboard's distance from the camera.
  377. * A billboard's pixel offset will be scaled between the {@link NearFarScalar#nearValue} and
  378. * {@link NearFarScalar#farValue} while the camera distance falls within the upper and lower bounds
  379. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  380. * Outside of these ranges the billboard's pixel offset scale remains clamped to the nearest bound. If undefined,
  381. * pixelOffsetScaleByDistance will be disabled.
  382. * @memberof Billboard.prototype
  383. * @type {NearFarScalar}
  384. *
  385. * @example
  386. * // Example 1.
  387. * // Set a billboard's pixel offset scale to 0.0 when the
  388. * // camera is 1500 meters from the billboard and scale pixel offset to 10.0 pixels
  389. * // in the y direction the camera distance approaches 8.0e6 meters.
  390. * b.pixelOffset = new Cesium.Cartesian2(0.0, 1.0);
  391. * b.pixelOffsetScaleByDistance = new Cesium.NearFarScalar(1.5e2, 0.0, 8.0e6, 10.0);
  392. *
  393. * @example
  394. * // Example 2.
  395. * // disable pixel offset by distance
  396. * b.pixelOffsetScaleByDistance = undefined;
  397. */
  398. pixelOffsetScaleByDistance : {
  399. get : function() {
  400. return this._pixelOffsetScaleByDistance;
  401. },
  402. set : function(value) {
  403. //>>includeStart('debug', pragmas.debug);
  404. if (defined(value) && value.far <= value.near) {
  405. throw new DeveloperError('far distance must be greater than near distance.');
  406. }
  407. //>>includeEnd('debug');
  408. var pixelOffsetScaleByDistance = this._pixelOffsetScaleByDistance;
  409. if (!NearFarScalar.equals(pixelOffsetScaleByDistance, value)) {
  410. this._pixelOffsetScaleByDistance = NearFarScalar.clone(value, pixelOffsetScaleByDistance);
  411. makeDirty(this, PIXEL_OFFSET_SCALE_BY_DISTANCE_INDEX);
  412. }
  413. }
  414. },
  415. /**
  416. * Gets or sets the 3D Cartesian offset applied to this billboard in eye coordinates. Eye coordinates is a left-handed
  417. * coordinate system, where <code>x</code> points towards the viewer's right, <code>y</code> points up, and
  418. * <code>z</code> points into the screen. Eye coordinates use the same scale as world and model coordinates,
  419. * which is typically meters.
  420. * <br /><br />
  421. * An eye offset is commonly used to arrange multiple billboards or objects at the same position, e.g., to
  422. * arrange a billboard above its corresponding 3D model.
  423. * <br /><br />
  424. * Below, the billboard is positioned at the center of the Earth but an eye offset makes it always
  425. * appear on top of the Earth regardless of the viewer's or Earth's orientation.
  426. * <br /><br />
  427. * <div align='center'>
  428. * <table border='0' cellpadding='5'><tr>
  429. * <td align='center'><img src='Images/Billboard.setEyeOffset.one.png' width='250' height='188' /></td>
  430. * <td align='center'><img src='Images/Billboard.setEyeOffset.two.png' width='250' height='188' /></td>
  431. * </tr></table>
  432. * <code>b.eyeOffset = new Cartesian3(0.0, 8000000.0, 0.0);</code><br /><br />
  433. * </div>
  434. * @memberof Billboard.prototype
  435. * @type {Cartesian3}
  436. */
  437. eyeOffset : {
  438. get : function() {
  439. return this._eyeOffset;
  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. var eyeOffset = this._eyeOffset;
  448. if (!Cartesian3.equals(eyeOffset, value)) {
  449. Cartesian3.clone(value, eyeOffset);
  450. makeDirty(this, EYE_OFFSET_INDEX);
  451. }
  452. }
  453. },
  454. /**
  455. * Gets or sets the horizontal origin of this billboard, which determines if the billboard is
  456. * to the left, center, or right of its anchor position.
  457. * <br /><br />
  458. * <div align='center'>
  459. * <img src='Images/Billboard.setHorizontalOrigin.png' width='648' height='196' /><br />
  460. * </div>
  461. * @memberof Billboard.prototype
  462. * @type {HorizontalOrigin}
  463. * @example
  464. * // Use a bottom, left origin
  465. * b.horizontalOrigin = Cesium.HorizontalOrigin.LEFT;
  466. * b.verticalOrigin = Cesium.VerticalOrigin.BOTTOM;
  467. */
  468. horizontalOrigin : {
  469. get : function() {
  470. return this._horizontalOrigin;
  471. },
  472. set : function(value) {
  473. //>>includeStart('debug', pragmas.debug);
  474. if (!defined(value)) {
  475. throw new DeveloperError('value is required.');
  476. }
  477. //>>includeEnd('debug');
  478. if (this._horizontalOrigin !== value) {
  479. this._horizontalOrigin = value;
  480. makeDirty(this, HORIZONTAL_ORIGIN_INDEX);
  481. }
  482. }
  483. },
  484. /**
  485. * Gets or sets the vertical origin of this billboard, which determines if the billboard is
  486. * to the above, below, or at the center of its anchor position.
  487. * <br /><br />
  488. * <div align='center'>
  489. * <img src='Images/Billboard.setVerticalOrigin.png' width='695' height='175' /><br />
  490. * </div>
  491. * @memberof Billboard.prototype
  492. * @type {VerticalOrigin}
  493. * @example
  494. * // Use a bottom, left origin
  495. * b.horizontalOrigin = Cesium.HorizontalOrigin.LEFT;
  496. * b.verticalOrigin = Cesium.VerticalOrigin.BOTTOM;
  497. */
  498. verticalOrigin : {
  499. get : function() {
  500. return this._verticalOrigin;
  501. },
  502. set : function(value) {
  503. //>>includeStart('debug', pragmas.debug);
  504. if (!defined(value)) {
  505. throw new DeveloperError('value is required.');
  506. }
  507. //>>includeEnd('debug');
  508. if (this._verticalOrigin !== value) {
  509. this._verticalOrigin = value;
  510. makeDirty(this, VERTICAL_ORIGIN_INDEX);
  511. }
  512. }
  513. },
  514. /**
  515. * Gets or sets the uniform scale that is multiplied with the billboard's image size in pixels.
  516. * A scale of <code>1.0</code> does not change the size of the billboard; a scale greater than
  517. * <code>1.0</code> enlarges the billboard; a positive scale less than <code>1.0</code> shrinks
  518. * the billboard.
  519. * <br /><br />
  520. * <div align='center'>
  521. * <img src='Images/Billboard.setScale.png' width='400' height='300' /><br/>
  522. * From left to right in the above image, the scales are <code>0.5</code>, <code>1.0</code>,
  523. * and <code>2.0</code>.
  524. * </div>
  525. * @memberof Billboard.prototype
  526. * @type {Number}
  527. */
  528. scale : {
  529. get : function() {
  530. return this._scale;
  531. },
  532. set : function(value) {
  533. //>>includeStart('debug', pragmas.debug);
  534. if (!defined(value)) {
  535. throw new DeveloperError('value is required.');
  536. }
  537. //>>includeEnd('debug');
  538. if (this._scale !== value) {
  539. this._scale = value;
  540. makeDirty(this, SCALE_INDEX);
  541. }
  542. }
  543. },
  544. /**
  545. * Gets or sets the color that is multiplied with the billboard's texture. This has two common use cases. First,
  546. * the same white texture may be used by many different billboards, each with a different color, to create
  547. * colored billboards. Second, the color's alpha component can be used to make the billboard translucent as shown below.
  548. * An alpha of <code>0.0</code> makes the billboard transparent, and <code>1.0</code> makes the billboard opaque.
  549. * <br /><br />
  550. * <div align='center'>
  551. * <table border='0' cellpadding='5'><tr>
  552. * <td align='center'><code>default</code><br/><img src='Images/Billboard.setColor.Alpha255.png' width='250' height='188' /></td>
  553. * <td align='center'><code>alpha : 0.5</code><br/><img src='Images/Billboard.setColor.Alpha127.png' width='250' height='188' /></td>
  554. * </tr></table>
  555. * </div>
  556. * <br />
  557. * The red, green, blue, and alpha values are indicated by <code>value</code>'s <code>red</code>, <code>green</code>,
  558. * <code>blue</code>, and <code>alpha</code> properties as shown in Example 1. These components range from <code>0.0</code>
  559. * (no intensity) to <code>1.0</code> (full intensity).
  560. * @memberof Billboard.prototype
  561. * @type {Color}
  562. *
  563. * @example
  564. * // Example 1. Assign yellow.
  565. * b.color = Cesium.Color.YELLOW;
  566. *
  567. * @example
  568. * // Example 2. Make a billboard 50% translucent.
  569. * b.color = new Cesium.Color(1.0, 1.0, 1.0, 0.5);
  570. */
  571. color : {
  572. get : function() {
  573. return this._color;
  574. },
  575. set : function(value) {
  576. //>>includeStart('debug', pragmas.debug);
  577. if (!defined(value)) {
  578. throw new DeveloperError('value is required.');
  579. }
  580. //>>includeEnd('debug');
  581. var color = this._color;
  582. if (!Color.equals(color, value)) {
  583. Color.clone(value, color);
  584. makeDirty(this, COLOR_INDEX);
  585. }
  586. }
  587. },
  588. /**
  589. * Gets or sets the rotation angle in radians.
  590. * @memberof Billboard.prototype
  591. * @type {Number}
  592. */
  593. rotation : {
  594. get : function() {
  595. return this._rotation;
  596. },
  597. set : function(value) {
  598. //>>includeStart('debug', pragmas.debug);
  599. if (!defined(value)) {
  600. throw new DeveloperError('value is required.');
  601. }
  602. //>>includeEnd('debug');
  603. if (this._rotation !== value) {
  604. this._rotation = value;
  605. makeDirty(this, ROTATION_INDEX);
  606. }
  607. }
  608. },
  609. /**
  610. * Gets or sets the aligned axis in world space. The aligned axis is the unit vector that the billboard up vector points towards.
  611. * The default is the zero vector, which means the billboard is aligned to the screen up vector.
  612. * @memberof Billboard.prototype
  613. * @type {Cartesian3}
  614. * @example
  615. * // Example 1.
  616. * // Have the billboard up vector point north
  617. * billboard.alignedAxis = Cesium.Cartesian3.UNIT_Z;
  618. *
  619. * @example
  620. * // Example 2.
  621. * // Have the billboard point east.
  622. * billboard.alignedAxis = Cesium.Cartesian3.UNIT_Z;
  623. * billboard.rotation = -Cesium.Math.PI_OVER_TWO;
  624. *
  625. * @example
  626. * // Example 3.
  627. * // Reset the aligned axis
  628. * billboard.alignedAxis = Cesium.Cartesian3.ZERO;
  629. */
  630. alignedAxis : {
  631. get : function() {
  632. return this._alignedAxis;
  633. },
  634. set : function(value) {
  635. //>>includeStart('debug', pragmas.debug);
  636. if (!defined(value)) {
  637. throw new DeveloperError('value is required.');
  638. }
  639. //>>includeEnd('debug');
  640. var alignedAxis = this._alignedAxis;
  641. if (!Cartesian3.equals(alignedAxis, value)) {
  642. Cartesian3.clone(value, alignedAxis);
  643. makeDirty(this, ALIGNED_AXIS_INDEX);
  644. }
  645. }
  646. },
  647. /**
  648. * Gets or sets a width for the billboard. If undefined, the image width will be used.
  649. * @memberof Billboard.prototype
  650. * @type {Number}
  651. */
  652. width : {
  653. get : function() {
  654. return defaultValue(this._width, this._imageWidth);
  655. },
  656. set : function(value) {
  657. if (this._width !== value) {
  658. this._width = value;
  659. makeDirty(this, IMAGE_INDEX_INDEX);
  660. }
  661. }
  662. },
  663. /**
  664. * Gets or sets a height for the billboard. If undefined, the image height will be used.
  665. * @memberof Billboard.prototype
  666. * @type {Number}
  667. */
  668. height : {
  669. get : function() {
  670. return defaultValue(this._height, this._imageHeight);
  671. },
  672. set : function(value) {
  673. if (this._height !== value) {
  674. this._height = value;
  675. makeDirty(this, IMAGE_INDEX_INDEX);
  676. }
  677. }
  678. },
  679. /**
  680. * Gets or sets if the billboard size is in meters or pixels. <code>true</code> to size the billboard in meters;
  681. * otherwise, the size is in pixels.
  682. * @memberof Billboard.prototype
  683. * @type {Boolean}
  684. * @default false
  685. */
  686. sizeInMeters : {
  687. get : function() {
  688. return this._sizeInMeters;
  689. },
  690. set : function(value) {
  691. if (this._sizeInMeters !== value) {
  692. this._sizeInMeters = value;
  693. makeDirty(this, COLOR_INDEX);
  694. }
  695. }
  696. },
  697. /**
  698. * Gets or sets the condition specifying at what distance from the camera that this billboard will be displayed.
  699. * @memberof Billboard.prototype
  700. * @type {DistanceDisplayCondition}
  701. * @default undefined
  702. */
  703. distanceDisplayCondition : {
  704. get : function() {
  705. return this._distanceDisplayCondition;
  706. },
  707. set : function(value) {
  708. if (!DistanceDisplayCondition.equals(value, this._distanceDisplayCondition)) {
  709. //>>includeStart('debug', pragmas.debug);
  710. if (defined(value) && value.far <= value.near) {
  711. throw new DeveloperError('far distance must be greater than near distance.');
  712. }
  713. //>>includeEnd('debug');
  714. this._distanceDisplayCondition = DistanceDisplayCondition.clone(value, this._distanceDisplayCondition);
  715. makeDirty(this, DISTANCE_DISPLAY_CONDITION);
  716. }
  717. }
  718. },
  719. /**
  720. * Gets or sets the distance from the camera at which to disable the depth test to, for example, prevent clipping against terrain.
  721. * When set to zero, the depth test is always applied. When set to Number.POSITIVE_INFINITY, the depth test is never applied.
  722. * @memberof Billboard.prototype
  723. * @type {Number}
  724. */
  725. disableDepthTestDistance : {
  726. get : function() {
  727. return this._disableDepthTestDistance;
  728. },
  729. set : function(value) {
  730. if (this._disableDepthTestDistance !== value) {
  731. //>>includeStart('debug', pragmas.debug);
  732. if (defined(value) && value < 0.0) {
  733. throw new DeveloperError('disableDepthTestDistance must be greater than or equal to 0.0.');
  734. }
  735. //>>includeEnd('debug');
  736. this._disableDepthTestDistance = value;
  737. makeDirty(this, DISABLE_DEPTH_DISTANCE);
  738. }
  739. }
  740. },
  741. /**
  742. * Gets or sets the user-defined object returned when the billboard is picked.
  743. * @memberof Billboard.prototype
  744. * @type {Object}
  745. */
  746. id : {
  747. get : function() {
  748. return this._id;
  749. },
  750. set : function(value) {
  751. this._id = value;
  752. if (defined(this._pickId)) {
  753. this._pickId.object.id = value;
  754. }
  755. }
  756. },
  757. /**
  758. * The primitive to return when picking this billboard.
  759. * @memberof Billboard.prototype
  760. * @private
  761. */
  762. pickPrimitive : {
  763. get : function() {
  764. return this._pickPrimitive;
  765. },
  766. set : function(value) {
  767. this._pickPrimitive = value;
  768. if (defined(this._pickId)) {
  769. this._pickId.object.primitive = value;
  770. }
  771. }
  772. },
  773. /**
  774. * @private
  775. */
  776. pickId : {
  777. get : function() {
  778. return this._pickId;
  779. }
  780. },
  781. /**
  782. * <p>
  783. * Gets or sets the image to be used for this billboard. If a texture has already been created for the
  784. * given image, the existing texture is used.
  785. * </p>
  786. * <p>
  787. * This property can be set to a loaded Image, a URL which will be loaded as an Image automatically,
  788. * a canvas, or another billboard's image property (from the same billboard collection).
  789. * </p>
  790. *
  791. * @memberof Billboard.prototype
  792. * @type {String}
  793. * @example
  794. * // load an image from a URL
  795. * b.image = 'some/image/url.png';
  796. *
  797. * // assuming b1 and b2 are billboards in the same billboard collection,
  798. * // use the same image for both billboards.
  799. * b2.image = b1.image;
  800. */
  801. image : {
  802. get : function() {
  803. return this._imageId;
  804. },
  805. set : function(value) {
  806. if (!defined(value)) {
  807. this._imageIndex = -1;
  808. this._imageSubRegion = undefined;
  809. this._imageId = undefined;
  810. this._image = undefined;
  811. this._imageIndexPromise = undefined;
  812. makeDirty(this, IMAGE_INDEX_INDEX);
  813. } else if (typeof value === 'string') {
  814. this.setImage(value, value);
  815. } else if (value instanceof Resource) {
  816. this.setImage(value.url, value);
  817. } else if (defined(value.src)) {
  818. this.setImage(value.src, value);
  819. } else {
  820. this.setImage(createGuid(), value);
  821. }
  822. }
  823. },
  824. /**
  825. * When <code>true</code>, this billboard is ready to render, i.e., the image
  826. * has been downloaded and the WebGL resources are created.
  827. *
  828. * @memberof Billboard.prototype
  829. *
  830. * @type {Boolean}
  831. * @readonly
  832. *
  833. * @default false
  834. */
  835. ready : {
  836. get : function() {
  837. return this._imageIndex !== -1;
  838. }
  839. },
  840. /**
  841. * Keeps track of the position of the billboard based on the height reference.
  842. * @memberof Billboard.prototype
  843. * @type {Cartesian3}
  844. * @private
  845. */
  846. _clampedPosition : {
  847. get : function() {
  848. return this._actualClampedPosition;
  849. },
  850. set : function(value) {
  851. this._actualClampedPosition = Cartesian3.clone(value, this._actualClampedPosition);
  852. makeDirty(this, POSITION_INDEX);
  853. }
  854. },
  855. /**
  856. * Determines whether or not this billboard will be shown or hidden because it was clustered.
  857. * @memberof Billboard.prototype
  858. * @type {Boolean}
  859. * @private
  860. */
  861. clusterShow : {
  862. get : function() {
  863. return this._clusterShow;
  864. },
  865. set : function(value) {
  866. if (this._clusterShow !== value) {
  867. this._clusterShow = value;
  868. makeDirty(this, SHOW_INDEX);
  869. }
  870. }
  871. },
  872. /**
  873. * The outline color of this Billboard. Effective only for SDF billboards like Label glyphs.
  874. * @memberof Billboard.prototype
  875. * @type {Color}
  876. * @private
  877. */
  878. outlineColor : {
  879. get : function() {
  880. return this._outlineColor;
  881. },
  882. set : function(value) {
  883. //>>includeStart('debug', pragmas.debug);
  884. if (!defined(value)) {
  885. throw new DeveloperError('value is required.');
  886. }
  887. //>>includeEnd('debug');
  888. var outlineColor = this._outlineColor;
  889. if (!Color.equals(outlineColor, value)) {
  890. Color.clone(value, outlineColor);
  891. makeDirty(this, SDF_INDEX);
  892. }
  893. }
  894. },
  895. /**
  896. * The outline width of this Billboard in pixels. Effective only for SDF billboards like Label glyphs.
  897. * @memberof Billboard.prototype
  898. * @type {Number}
  899. * @private
  900. */
  901. outlineWidth : {
  902. get : function() {
  903. return this._outlineWidth;
  904. },
  905. set : function(value) {
  906. if (this._outlineWidth !== value) {
  907. this._outlineWidth = value;
  908. makeDirty(this, SDF_INDEX);
  909. }
  910. }
  911. }
  912. });
  913. Billboard.prototype.getPickId = function(context) {
  914. if (!defined(this._pickId)) {
  915. this._pickId = context.createPickId({
  916. primitive : this._pickPrimitive,
  917. collection : this._collection,
  918. id : this._id
  919. });
  920. }
  921. return this._pickId;
  922. };
  923. Billboard.prototype._updateClamping = function() {
  924. Billboard._updateClamping(this._billboardCollection, this);
  925. };
  926. var scratchCartographic = new Cartographic();
  927. var scratchPosition = new Cartesian3();
  928. Billboard._updateClamping = function(collection, owner) {
  929. var scene = collection._scene;
  930. if (!defined(scene) || !defined(scene.globe)) {
  931. //>>includeStart('debug', pragmas.debug);
  932. if (owner._heightReference !== HeightReference.NONE) {
  933. throw new DeveloperError('Height reference is not supported without a scene and globe.');
  934. }
  935. //>>includeEnd('debug');
  936. return;
  937. }
  938. var globe = scene.globe;
  939. var ellipsoid = globe.ellipsoid;
  940. var surface = globe._surface;
  941. var mode = scene.frameState.mode;
  942. var modeChanged = mode !== owner._mode;
  943. owner._mode = mode;
  944. if ((owner._heightReference === HeightReference.NONE || modeChanged) && defined(owner._removeCallbackFunc)) {
  945. owner._removeCallbackFunc();
  946. owner._removeCallbackFunc = undefined;
  947. owner._clampedPosition = undefined;
  948. }
  949. if (owner._heightReference === HeightReference.NONE || !defined(owner._position)) {
  950. return;
  951. }
  952. var position = ellipsoid.cartesianToCartographic(owner._position);
  953. if (!defined(position)) {
  954. owner._actualClampedPosition = undefined;
  955. return;
  956. }
  957. if (defined(owner._removeCallbackFunc)) {
  958. owner._removeCallbackFunc();
  959. }
  960. function updateFunction(clampedPosition) {
  961. if (owner._heightReference === HeightReference.RELATIVE_TO_GROUND) {
  962. if (owner._mode === SceneMode.SCENE3D) {
  963. var clampedCart = ellipsoid.cartesianToCartographic(clampedPosition, scratchCartographic);
  964. clampedCart.height += position.height;
  965. ellipsoid.cartographicToCartesian(clampedCart, clampedPosition);
  966. } else {
  967. clampedPosition.x += position.height;
  968. }
  969. }
  970. owner._clampedPosition = Cartesian3.clone(clampedPosition, owner._clampedPosition);
  971. }
  972. owner._removeCallbackFunc = surface.updateHeight(position, updateFunction);
  973. Cartographic.clone(position, scratchCartographic);
  974. var height = globe.getHeight(position);
  975. if (defined(height)) {
  976. scratchCartographic.height = height;
  977. }
  978. ellipsoid.cartographicToCartesian(scratchCartographic, scratchPosition);
  979. updateFunction(scratchPosition);
  980. };
  981. Billboard.prototype._loadImage = function() {
  982. var atlas = this._billboardCollection._textureAtlas;
  983. var imageId = this._imageId;
  984. var image = this._image;
  985. var imageSubRegion = this._imageSubRegion;
  986. var imageIndexPromise;
  987. if (defined(image)) {
  988. imageIndexPromise = atlas.addImage(imageId, image);
  989. }
  990. if (defined(imageSubRegion)) {
  991. imageIndexPromise = atlas.addSubRegion(imageId, imageSubRegion);
  992. }
  993. this._imageIndexPromise = imageIndexPromise;
  994. if (!defined(imageIndexPromise)) {
  995. return;
  996. }
  997. var that = this;
  998. imageIndexPromise.then(function(index) {
  999. if (that._imageId !== imageId || that._image !== image || !BoundingRectangle.equals(that._imageSubRegion, imageSubRegion)) {
  1000. // another load occurred before this one finished, ignore the index
  1001. return;
  1002. }
  1003. // fill in imageWidth and imageHeight
  1004. var textureCoordinates = atlas.textureCoordinates[index];
  1005. that._imageWidth = atlas.texture.width * textureCoordinates.width;
  1006. that._imageHeight = atlas.texture.height * textureCoordinates.height;
  1007. that._imageIndex = index;
  1008. that._ready = true;
  1009. that._image = undefined;
  1010. that._imageIndexPromise = undefined;
  1011. makeDirty(that, IMAGE_INDEX_INDEX);
  1012. }).otherwise(function(error) {
  1013. console.error('Error loading image for billboard: ' + error);
  1014. that._imageIndexPromise = undefined;
  1015. });
  1016. };
  1017. /**
  1018. * <p>
  1019. * Sets the image to be used for this billboard. If a texture has already been created for the
  1020. * given id, the existing texture is used.
  1021. * </p>
  1022. * <p>
  1023. * This function is useful for dynamically creating textures that are shared across many billboards.
  1024. * Only the first billboard will actually call the function and create the texture, while subsequent
  1025. * billboards created with the same id will simply re-use the existing texture.
  1026. * </p>
  1027. * <p>
  1028. * To load an image from a URL, setting the {@link Billboard#image} property is more convenient.
  1029. * </p>
  1030. *
  1031. * @param {String} id The id of the image. This can be any string that uniquely identifies the image.
  1032. * @param {Image|Canvas|String|Resource|Billboard~CreateImageCallback} image The image to load. This parameter
  1033. * can either be a loaded Image or Canvas, a URL which will be loaded as an Image automatically,
  1034. * or a function which will be called to create the image if it hasn't been loaded already.
  1035. * @example
  1036. * // create a billboard image dynamically
  1037. * function drawImage(id) {
  1038. * // create and draw an image using a canvas
  1039. * var canvas = document.createElement('canvas');
  1040. * var context2D = canvas.getContext('2d');
  1041. * // ... draw image
  1042. * return canvas;
  1043. * }
  1044. * // drawImage will be called to create the texture
  1045. * b.setImage('myImage', drawImage);
  1046. *
  1047. * // subsequent billboards created in the same collection using the same id will use the existing
  1048. * // texture, without the need to create the canvas or draw the image
  1049. * b2.setImage('myImage', drawImage);
  1050. */
  1051. Billboard.prototype.setImage = function(id, image) {
  1052. //>>includeStart('debug', pragmas.debug);
  1053. if (!defined(id)) {
  1054. throw new DeveloperError('id is required.');
  1055. }
  1056. if (!defined(image)) {
  1057. throw new DeveloperError('image is required.');
  1058. }
  1059. //>>includeEnd('debug');
  1060. if (this._imageId === id) {
  1061. return;
  1062. }
  1063. this._imageIndex = -1;
  1064. this._imageSubRegion = undefined;
  1065. this._imageId = id;
  1066. this._image = image;
  1067. if (defined(this._billboardCollection._textureAtlas)) {
  1068. this._loadImage();
  1069. }
  1070. };
  1071. /**
  1072. * Uses a sub-region of the image with the given id as the image for this billboard,
  1073. * measured in pixels from the bottom-left.
  1074. *
  1075. * @param {String} id The id of the image to use.
  1076. * @param {BoundingRectangle} subRegion The sub-region of the image.
  1077. *
  1078. * @exception {RuntimeError} image with id must be in the atlas
  1079. */
  1080. Billboard.prototype.setImageSubRegion = function(id, subRegion) {
  1081. //>>includeStart('debug', pragmas.debug);
  1082. if (!defined(id)) {
  1083. throw new DeveloperError('id is required.');
  1084. }
  1085. if (!defined(subRegion)) {
  1086. throw new DeveloperError('subRegion is required.');
  1087. }
  1088. //>>includeEnd('debug');
  1089. if (this._imageId === id && BoundingRectangle.equals(this._imageSubRegion, subRegion)) {
  1090. return;
  1091. }
  1092. this._imageIndex = -1;
  1093. this._imageId = id;
  1094. this._imageSubRegion = BoundingRectangle.clone(subRegion);
  1095. if (defined(this._billboardCollection._textureAtlas)) {
  1096. this._loadImage();
  1097. }
  1098. };
  1099. Billboard.prototype._setTranslate = function(value) {
  1100. //>>includeStart('debug', pragmas.debug);
  1101. if (!defined(value)) {
  1102. throw new DeveloperError('value is required.');
  1103. }
  1104. //>>includeEnd('debug');
  1105. var translate = this._translate;
  1106. if (!Cartesian2.equals(translate, value)) {
  1107. Cartesian2.clone(value, translate);
  1108. makeDirty(this, PIXEL_OFFSET_INDEX);
  1109. }
  1110. };
  1111. Billboard.prototype._getActualPosition = function() {
  1112. return defined(this._clampedPosition) ? this._clampedPosition : this._actualPosition;
  1113. };
  1114. Billboard.prototype._setActualPosition = function(value) {
  1115. if (!(defined(this._clampedPosition))) {
  1116. Cartesian3.clone(value, this._actualPosition);
  1117. }
  1118. makeDirty(this, POSITION_INDEX);
  1119. };
  1120. var tempCartesian3 = new Cartesian4();
  1121. Billboard._computeActualPosition = function(billboard, position, frameState, modelMatrix) {
  1122. if (defined(billboard._clampedPosition)) {
  1123. if (frameState.mode !== billboard._mode) {
  1124. billboard._updateClamping();
  1125. }
  1126. return billboard._clampedPosition;
  1127. } else if (frameState.mode === SceneMode.SCENE3D) {
  1128. return position;
  1129. }
  1130. Matrix4.multiplyByPoint(modelMatrix, position, tempCartesian3);
  1131. return SceneTransforms.computeActualWgs84Position(frameState, tempCartesian3);
  1132. };
  1133. var scratchCartesian3 = new Cartesian3();
  1134. // This function is basically a stripped-down JavaScript version of BillboardCollectionVS.glsl
  1135. Billboard._computeScreenSpacePosition = function(modelMatrix, position, eyeOffset, pixelOffset, scene, result) {
  1136. // Model to world coordinates
  1137. var positionWorld = Matrix4.multiplyByPoint(modelMatrix, position, scratchCartesian3);
  1138. // World to window coordinates
  1139. var positionWC = SceneTransforms.wgs84WithEyeOffsetToWindowCoordinates(scene, positionWorld, eyeOffset, result);
  1140. if (!defined(positionWC)) {
  1141. return undefined;
  1142. }
  1143. // Apply pixel offset
  1144. Cartesian2.add(positionWC, pixelOffset, positionWC);
  1145. return positionWC;
  1146. };
  1147. var scratchPixelOffset = new Cartesian2(0.0, 0.0);
  1148. /**
  1149. * Computes the screen-space position of the billboard's origin, taking into account eye and pixel offsets.
  1150. * The screen space origin is the top, left corner of the canvas; <code>x</code> increases from
  1151. * left to right, and <code>y</code> increases from top to bottom.
  1152. *
  1153. * @param {Scene} scene The scene.
  1154. * @param {Cartesian2} [result] The object onto which to store the result.
  1155. * @returns {Cartesian2} The screen-space position of the billboard.
  1156. *
  1157. * @exception {DeveloperError} Billboard must be in a collection.
  1158. *
  1159. * @example
  1160. * console.log(b.computeScreenSpacePosition(scene).toString());
  1161. *
  1162. * @see Billboard#eyeOffset
  1163. * @see Billboard#pixelOffset
  1164. */
  1165. Billboard.prototype.computeScreenSpacePosition = function(scene, result) {
  1166. var billboardCollection = this._billboardCollection;
  1167. if (!defined(result)) {
  1168. result = new Cartesian2();
  1169. }
  1170. //>>includeStart('debug', pragmas.debug);
  1171. if (!defined(billboardCollection)) {
  1172. throw new DeveloperError('Billboard must be in a collection. Was it removed?');
  1173. }
  1174. if (!defined(scene)) {
  1175. throw new DeveloperError('scene is required.');
  1176. }
  1177. //>>includeEnd('debug');
  1178. // pixel offset for screen space computation is the pixelOffset + screen space translate
  1179. Cartesian2.clone(this._pixelOffset, scratchPixelOffset);
  1180. Cartesian2.add(scratchPixelOffset, this._translate, scratchPixelOffset);
  1181. var modelMatrix = billboardCollection.modelMatrix;
  1182. var position = this._position;
  1183. if (defined(this._clampedPosition)) {
  1184. position = this._clampedPosition;
  1185. if (scene.mode !== SceneMode.SCENE3D) {
  1186. // position needs to be in world coordinates
  1187. var projection = scene.mapProjection;
  1188. var ellipsoid = projection.ellipsoid;
  1189. var cart = projection.unproject(position, scratchCartographic);
  1190. position = ellipsoid.cartographicToCartesian(cart, scratchCartesian3);
  1191. modelMatrix = Matrix4.IDENTITY;
  1192. }
  1193. }
  1194. var windowCoordinates = Billboard._computeScreenSpacePosition(modelMatrix, position,
  1195. this._eyeOffset, scratchPixelOffset, scene, result);
  1196. return windowCoordinates;
  1197. };
  1198. /**
  1199. * Gets a billboard's screen space bounding box centered around screenSpacePosition.
  1200. * @param {Billboard} billboard The billboard to get the screen space bounding box for.
  1201. * @param {Cartesian2} screenSpacePosition The screen space center of the label.
  1202. * @param {BoundingRectangle} [result] The object onto which to store the result.
  1203. * @returns {BoundingRectangle} The screen space bounding box.
  1204. *
  1205. * @private
  1206. */
  1207. Billboard.getScreenSpaceBoundingBox = function(billboard, screenSpacePosition, result) {
  1208. var width = billboard.width;
  1209. var height = billboard.height;
  1210. var scale = billboard.scale;
  1211. width *= scale;
  1212. height *= scale;
  1213. var x = screenSpacePosition.x;
  1214. if (billboard.horizontalOrigin === HorizontalOrigin.RIGHT) {
  1215. x -= width;
  1216. } else if (billboard.horizontalOrigin === HorizontalOrigin.CENTER) {
  1217. x -= width * 0.5;
  1218. }
  1219. var y = screenSpacePosition.y;
  1220. if (billboard.verticalOrigin === VerticalOrigin.BOTTOM || billboard.verticalOrigin === VerticalOrigin.BASELINE) {
  1221. y -= height;
  1222. } else if (billboard.verticalOrigin === VerticalOrigin.CENTER) {
  1223. y -= height * 0.5;
  1224. }
  1225. if (!defined(result)) {
  1226. result = new BoundingRectangle();
  1227. }
  1228. result.x = x;
  1229. result.y = y;
  1230. result.width = width;
  1231. result.height = height;
  1232. return result;
  1233. };
  1234. /**
  1235. * Determines if this billboard equals another billboard. Billboards are equal if all their properties
  1236. * are equal. Billboards in different collections can be equal.
  1237. *
  1238. * @param {Billboard} other The billboard to compare for equality.
  1239. * @returns {Boolean} <code>true</code> if the billboards are equal; otherwise, <code>false</code>.
  1240. */
  1241. Billboard.prototype.equals = function(other) {
  1242. return this === other ||
  1243. defined(other) &&
  1244. this._id === other._id &&
  1245. Cartesian3.equals(this._position, other._position) &&
  1246. this._imageId === other._imageId &&
  1247. this._show === other._show &&
  1248. this._scale === other._scale &&
  1249. this._verticalOrigin === other._verticalOrigin &&
  1250. this._horizontalOrigin === other._horizontalOrigin &&
  1251. this._heightReference === other._heightReference &&
  1252. BoundingRectangle.equals(this._imageSubRegion, other._imageSubRegion) &&
  1253. Color.equals(this._color, other._color) &&
  1254. Cartesian2.equals(this._pixelOffset, other._pixelOffset) &&
  1255. Cartesian2.equals(this._translate, other._translate) &&
  1256. Cartesian3.equals(this._eyeOffset, other._eyeOffset) &&
  1257. NearFarScalar.equals(this._scaleByDistance, other._scaleByDistance) &&
  1258. NearFarScalar.equals(this._translucencyByDistance, other._translucencyByDistance) &&
  1259. NearFarScalar.equals(this._pixelOffsetScaleByDistance, other._pixelOffsetScaleByDistance) &&
  1260. DistanceDisplayCondition.equals(this._distanceDisplayCondition, other._distanceDisplayCondition) &&
  1261. this._disableDepthTestDistance === other._disableDepthTestDistance;
  1262. };
  1263. Billboard.prototype._destroy = function() {
  1264. if (defined(this._customData)) {
  1265. this._billboardCollection._scene.globe._surface.removeTileCustomData(this._customData);
  1266. this._customData = undefined;
  1267. }
  1268. if (defined(this._removeCallbackFunc)) {
  1269. this._removeCallbackFunc();
  1270. this._removeCallbackFunc = undefined;
  1271. }
  1272. this.image = undefined;
  1273. this._pickId = this._pickId && this._pickId.destroy();
  1274. this._billboardCollection = undefined;
  1275. };
  1276. /**
  1277. * A function that creates an image.
  1278. * @callback Billboard~CreateImageCallback
  1279. * @param {String} id The identifier of the image to load.
  1280. * @returns {Image|Canvas|Promise<Image|Canvas>} The image, or a promise that will resolve to an image.
  1281. */
  1282. export default Billboard;