Billboard.js 49 KB

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