Billboard.js 49 KB

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