ImageryLayer.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. import Cartesian2 from "../Core/Cartesian2.js";
  2. import Cartesian4 from "../Core/Cartesian4.js";
  3. import Check from "../Core/Check.js";
  4. import createWorldImageryAsync from "../Scene/createWorldImageryAsync.js";
  5. import defaultValue from "../Core/defaultValue.js";
  6. import defined from "../Core/defined.js";
  7. import destroyObject from "../Core/destroyObject.js";
  8. import deprecationWarning from "../Core/deprecationWarning.js";
  9. import DeveloperError from "../Core/DeveloperError.js";
  10. import Event from "../Core/Event.js";
  11. import FeatureDetection from "../Core/FeatureDetection.js";
  12. import GeographicProjection from "../Core/GeographicProjection.js";
  13. import IndexDatatype from "../Core/IndexDatatype.js";
  14. import CesiumMath from "../Core/Math.js";
  15. import PixelFormat from "../Core/PixelFormat.js";
  16. import Rectangle from "../Core/Rectangle.js";
  17. import Request from "../Core/Request.js";
  18. import RequestState from "../Core/RequestState.js";
  19. import RequestType from "../Core/RequestType.js";
  20. import TerrainProvider from "../Core/TerrainProvider.js";
  21. import TileProviderError from "../Core/TileProviderError.js";
  22. import WebMercatorProjection from "../Core/WebMercatorProjection.js";
  23. import Buffer from "../Renderer/Buffer.js";
  24. import BufferUsage from "../Renderer/BufferUsage.js";
  25. import ComputeCommand from "../Renderer/ComputeCommand.js";
  26. import ContextLimits from "../Renderer/ContextLimits.js";
  27. import MipmapHint from "../Renderer/MipmapHint.js";
  28. import Sampler from "../Renderer/Sampler.js";
  29. import ShaderProgram from "../Renderer/ShaderProgram.js";
  30. import ShaderSource from "../Renderer/ShaderSource.js";
  31. import Texture from "../Renderer/Texture.js";
  32. import TextureMagnificationFilter from "../Renderer/TextureMagnificationFilter.js";
  33. import TextureMinificationFilter from "../Renderer/TextureMinificationFilter.js";
  34. import TextureWrap from "../Renderer/TextureWrap.js";
  35. import VertexArray from "../Renderer/VertexArray.js";
  36. import ReprojectWebMercatorFS from "../Shaders/ReprojectWebMercatorFS.js";
  37. import ReprojectWebMercatorVS from "../Shaders/ReprojectWebMercatorVS.js";
  38. import Imagery from "./Imagery.js";
  39. import ImageryState from "./ImageryState.js";
  40. import SplitDirection from "./SplitDirection.js";
  41. import TileImagery from "./TileImagery.js";
  42. /**
  43. * @typedef {Object} ImageryLayer.ConstructorOptions
  44. *
  45. * Initialization options for the ImageryLayer constructor.
  46. *
  47. * @property {Rectangle} [rectangle=imageryProvider.rectangle] The rectangle of the layer. This rectangle
  48. * can limit the visible portion of the imagery provider.
  49. * @property {number|Function} [alpha=1.0] The alpha blending value of this layer, from 0.0 to 1.0.
  50. * This can either be a simple number or a function with the signature
  51. * <code>function(frameState, layer, x, y, level)</code>. The function is passed the
  52. * current frame state, this layer, and the x, y, and level coordinates of the
  53. * imagery tile for which the alpha is required, and it is expected to return
  54. * the alpha value to use for the tile.
  55. * @property {number|Function} [nightAlpha=1.0] The alpha blending value of this layer on the night side of the globe, from 0.0 to 1.0.
  56. * This can either be a simple number or a function with the signature
  57. * <code>function(frameState, layer, x, y, level)</code>. The function is passed the
  58. * current frame state, this layer, and the x, y, and level coordinates of the
  59. * imagery tile for which the alpha is required, and it is expected to return
  60. * the alpha value to use for the tile. This only takes effect when <code>enableLighting</code> is <code>true</code>.
  61. * @property {number|Function} [dayAlpha=1.0] The alpha blending value of this layer on the day side of the globe, from 0.0 to 1.0.
  62. * This can either be a simple number or a function with the signature
  63. * <code>function(frameState, layer, x, y, level)</code>. The function is passed the
  64. * current frame state, this layer, and the x, y, and level coordinates of the
  65. * imagery tile for which the alpha is required, and it is expected to return
  66. * the alpha value to use for the tile. This only takes effect when <code>enableLighting</code> is <code>true</code>.
  67. * @property {number|Function} [brightness=1.0] The brightness of this layer. 1.0 uses the unmodified imagery
  68. * color. Less than 1.0 makes the imagery darker while greater than 1.0 makes it brighter.
  69. * This can either be a simple number or a function with the signature
  70. * <code>function(frameState, layer, x, y, level)</code>. The function is passed the
  71. * current frame state, this layer, and the x, y, and level coordinates of the
  72. * imagery tile for which the brightness is required, and it is expected to return
  73. * the brightness value to use for the tile. The function is executed for every
  74. * frame and for every tile, so it must be fast.
  75. * @property {number|Function} [contrast=1.0] The contrast of this layer. 1.0 uses the unmodified imagery color.
  76. * Less than 1.0 reduces the contrast while greater than 1.0 increases it.
  77. * This can either be a simple number or a function with the signature
  78. * <code>function(frameState, layer, x, y, level)</code>. The function is passed the
  79. * current frame state, this layer, and the x, y, and level coordinates of the
  80. * imagery tile for which the contrast is required, and it is expected to return
  81. * the contrast value to use for the tile. The function is executed for every
  82. * frame and for every tile, so it must be fast.
  83. * @property {number|Function} [hue=0.0] The hue of this layer. 0.0 uses the unmodified imagery color.
  84. * This can either be a simple number or a function with the signature
  85. * <code>function(frameState, layer, x, y, level)</code>. The function is passed the
  86. * current frame state, this layer, and the x, y, and level coordinates
  87. * of the imagery tile for which the hue is required, and it is expected to return
  88. * the contrast value to use for the tile. The function is executed for every
  89. * frame and for every tile, so it must be fast.
  90. * @property {number|Function} [saturation=1.0] The saturation of this layer. 1.0 uses the unmodified imagery color.
  91. * Less than 1.0 reduces the saturation while greater than 1.0 increases it.
  92. * This can either be a simple number or a function with the signature
  93. * <code>function(frameState, layer, x, y, level)</code>. The function is passed the
  94. * current frame state, this layer, and the x, y, and level coordinates
  95. * of the imagery tile for which the saturation is required, and it is expected to return
  96. * the contrast value to use for the tile. The function is executed for every
  97. * frame and for every tile, so it must be fast.
  98. * @property {number|Function} [gamma=1.0] The gamma correction to apply to this layer. 1.0 uses the unmodified imagery color.
  99. * This can either be a simple number or a function with the signature
  100. * <code>function(frameState, layer, x, y, level)</code>. The function is passed the
  101. * current frame state, this layer, and the x, y, and level coordinates of the
  102. * imagery tile for which the gamma is required, and it is expected to return
  103. * the gamma value to use for the tile. The function is executed for every
  104. * frame and for every tile, so it must be fast.
  105. * @property {SplitDirection|Function} [splitDirection=SplitDirection.NONE] The {@link SplitDirection} split to apply to this layer.
  106. * @property {TextureMinificationFilter} [minificationFilter=TextureMinificationFilter.LINEAR] The
  107. * texture minification filter to apply to this layer. Possible values
  108. * are <code>TextureMinificationFilter.LINEAR</code> and
  109. * <code>TextureMinificationFilter.NEAREST</code>.
  110. * @property {TextureMagnificationFilter} [magnificationFilter=TextureMagnificationFilter.LINEAR] The
  111. * texture minification filter to apply to this layer. Possible values
  112. * are <code>TextureMagnificationFilter.LINEAR</code> and
  113. * <code>TextureMagnificationFilter.NEAREST</code>.
  114. * @property {boolean} [show=true] True if the layer is shown; otherwise, false.
  115. * @property {number} [maximumAnisotropy=maximum supported] The maximum anisotropy level to use
  116. * for texture filtering. If this parameter is not specified, the maximum anisotropy supported
  117. * by the WebGL stack will be used. Larger values make the imagery look better in horizon
  118. * views.
  119. * @property {number} [minimumTerrainLevel] The minimum terrain level-of-detail at which to show this imagery layer,
  120. * or undefined to show it at all levels. Level zero is the least-detailed level.
  121. * @property {number} [maximumTerrainLevel] The maximum terrain level-of-detail at which to show this imagery layer,
  122. * or undefined to show it at all levels. Level zero is the least-detailed level.
  123. * @property {Rectangle} [cutoutRectangle] Cartographic rectangle for cutting out a portion of this ImageryLayer.
  124. * @property {Color} [colorToAlpha] Color to be used as alpha.
  125. * @property {number} [colorToAlphaThreshold=0.004] Threshold for color-to-alpha.
  126. */
  127. /**
  128. * An imagery layer that displays tiled image data from a single imagery provider
  129. * on a {@link Globe}.
  130. *
  131. * @alias ImageryLayer
  132. * @constructor
  133. *
  134. * @param {ImageryProvider} imageryProvider The imagery provider to use.
  135. * @param {ImageryLayer.ConstructorOptions} options An object describing initialization options
  136. *
  137. * @see ImageryLayer.fromProviderAsync
  138. * @see ImageryLayer.fromWorldImagery
  139. *
  140. * @example
  141. * // Add an OpenStreetMaps layer
  142. * const imageryLayer = new Cesium.ImageryLayer(OpenStreetMapImageryProvider({
  143. * url: "https://a.tile.openstreetmap.org/"
  144. * })),
  145. * scene.imageryLayers.add(imageryLayer);
  146. *
  147. * @example
  148. * // Add Cesium ion's default world imagery layer
  149. * const imageryLayer = Cesium.ImageryLayer.fromWorldImagery();
  150. * scene.imageryLayers.add(imageryLayer);
  151. *
  152. * @example
  153. * // Add a new transparent layer from Cesium ion
  154. * const imageryLayer = Cesium.ImageryLayer.fromProviderAsync(Cesium.IonImageryProvider.fromAssetId(3812));
  155. * imageryLayer.alpha = 0.5;
  156. * scene.imageryLayers.add(imageryLayer);
  157. */
  158. function ImageryLayer(imageryProvider, options) {
  159. this._imageryProvider = imageryProvider;
  160. this._readyEvent = new Event();
  161. this._errorEvent = new Event();
  162. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  163. imageryProvider = defaultValue(imageryProvider, defaultValue.EMPTY_OBJECT);
  164. /**
  165. * The alpha blending value of this layer, with 0.0 representing fully transparent and
  166. * 1.0 representing fully opaque.
  167. *
  168. * @type {number}
  169. * @default 1.0
  170. */
  171. this.alpha = defaultValue(
  172. options.alpha,
  173. defaultValue(imageryProvider._defaultAlpha, 1.0)
  174. );
  175. /**
  176. * The alpha blending value of this layer on the night side of the globe, with 0.0 representing fully transparent and
  177. * 1.0 representing fully opaque. This only takes effect when {@link Globe#enableLighting} is <code>true</code>.
  178. *
  179. * @type {number}
  180. * @default 1.0
  181. */
  182. this.nightAlpha = defaultValue(
  183. options.nightAlpha,
  184. defaultValue(imageryProvider._defaultNightAlpha, 1.0)
  185. );
  186. /**
  187. * The alpha blending value of this layer on the day side of the globe, with 0.0 representing fully transparent and
  188. * 1.0 representing fully opaque. This only takes effect when {@link Globe#enableLighting} is <code>true</code>.
  189. *
  190. * @type {number}
  191. * @default 1.0
  192. */
  193. this.dayAlpha = defaultValue(
  194. options.dayAlpha,
  195. defaultValue(imageryProvider._defaultDayAlpha, 1.0)
  196. );
  197. /**
  198. * The brightness of this layer. 1.0 uses the unmodified imagery color. Less than 1.0
  199. * makes the imagery darker while greater than 1.0 makes it brighter.
  200. *
  201. * @type {number}
  202. * @default {@link ImageryLayer.DEFAULT_BRIGHTNESS}
  203. */
  204. this.brightness = defaultValue(
  205. options.brightness,
  206. defaultValue(
  207. imageryProvider._defaultBrightness,
  208. ImageryLayer.DEFAULT_BRIGHTNESS
  209. )
  210. );
  211. /**
  212. * The contrast of this layer. 1.0 uses the unmodified imagery color. Less than 1.0 reduces
  213. * the contrast while greater than 1.0 increases it.
  214. *
  215. * @type {number}
  216. * @default {@link ImageryLayer.DEFAULT_CONTRAST}
  217. */
  218. this.contrast = defaultValue(
  219. options.contrast,
  220. defaultValue(
  221. imageryProvider._defaultContrast,
  222. ImageryLayer.DEFAULT_CONTRAST
  223. )
  224. );
  225. /**
  226. * The hue of this layer in radians. 0.0 uses the unmodified imagery color.
  227. *
  228. * @type {number}
  229. * @default {@link ImageryLayer.DEFAULT_HUE}
  230. */
  231. this.hue = defaultValue(
  232. options.hue,
  233. defaultValue(imageryProvider._defaultHue, ImageryLayer.DEFAULT_HUE)
  234. );
  235. /**
  236. * The saturation of this layer. 1.0 uses the unmodified imagery color. Less than 1.0 reduces the
  237. * saturation while greater than 1.0 increases it.
  238. *
  239. * @type {number}
  240. * @default {@link ImageryLayer.DEFAULT_SATURATION}
  241. */
  242. this.saturation = defaultValue(
  243. options.saturation,
  244. defaultValue(
  245. imageryProvider._defaultSaturation,
  246. ImageryLayer.DEFAULT_SATURATION
  247. )
  248. );
  249. /**
  250. * The gamma correction to apply to this layer. 1.0 uses the unmodified imagery color.
  251. *
  252. * @type {number}
  253. * @default {@link ImageryLayer.DEFAULT_GAMMA}
  254. */
  255. this.gamma = defaultValue(
  256. options.gamma,
  257. defaultValue(imageryProvider._defaultGamma, ImageryLayer.DEFAULT_GAMMA)
  258. );
  259. /**
  260. * The {@link SplitDirection} to apply to this layer.
  261. *
  262. * @type {SplitDirection}
  263. * @default {@link ImageryLayer.DEFAULT_SPLIT}
  264. */
  265. this.splitDirection = defaultValue(
  266. options.splitDirection,
  267. ImageryLayer.DEFAULT_SPLIT
  268. );
  269. /**
  270. * The {@link TextureMinificationFilter} to apply to this layer.
  271. * Possible values are {@link TextureMinificationFilter.LINEAR} (the default)
  272. * and {@link TextureMinificationFilter.NEAREST}.
  273. *
  274. * To take effect, this property must be set immediately after adding the imagery layer.
  275. * Once a texture is loaded it won't be possible to change the texture filter used.
  276. *
  277. * @type {TextureMinificationFilter}
  278. * @default {@link ImageryLayer.DEFAULT_MINIFICATION_FILTER}
  279. */
  280. this.minificationFilter = defaultValue(
  281. options.minificationFilter,
  282. defaultValue(
  283. imageryProvider._defaultMinificationFilter,
  284. ImageryLayer.DEFAULT_MINIFICATION_FILTER
  285. )
  286. );
  287. /**
  288. * The {@link TextureMagnificationFilter} to apply to this layer.
  289. * Possible values are {@link TextureMagnificationFilter.LINEAR} (the default)
  290. * and {@link TextureMagnificationFilter.NEAREST}.
  291. *
  292. * To take effect, this property must be set immediately after adding the imagery layer.
  293. * Once a texture is loaded it won't be possible to change the texture filter used.
  294. *
  295. * @type {TextureMagnificationFilter}
  296. * @default {@link ImageryLayer.DEFAULT_MAGNIFICATION_FILTER}
  297. */
  298. this.magnificationFilter = defaultValue(
  299. options.magnificationFilter,
  300. defaultValue(
  301. imageryProvider._defaultMagnificationFilter,
  302. ImageryLayer.DEFAULT_MAGNIFICATION_FILTER
  303. )
  304. );
  305. /**
  306. * Determines if this layer is shown.
  307. *
  308. * @type {boolean}
  309. * @default true
  310. */
  311. this.show = defaultValue(options.show, true);
  312. this._minimumTerrainLevel = options.minimumTerrainLevel;
  313. this._maximumTerrainLevel = options.maximumTerrainLevel;
  314. this._rectangle = defaultValue(options.rectangle, Rectangle.MAX_VALUE);
  315. this._maximumAnisotropy = options.maximumAnisotropy;
  316. this._imageryCache = {};
  317. this._skeletonPlaceholder = new TileImagery(Imagery.createPlaceholder(this));
  318. // The value of the show property on the last update.
  319. this._show = true;
  320. // The index of this layer in the ImageryLayerCollection.
  321. this._layerIndex = -1;
  322. // true if this is the base (lowest shown) layer.
  323. this._isBaseLayer = false;
  324. this._requestImageError = undefined;
  325. this._reprojectComputeCommands = [];
  326. /**
  327. * Rectangle cutout in this layer of imagery.
  328. *
  329. * @type {Rectangle}
  330. */
  331. this.cutoutRectangle = options.cutoutRectangle;
  332. /**
  333. * Color value that should be set to transparent.
  334. *
  335. * @type {Color}
  336. */
  337. this.colorToAlpha = options.colorToAlpha;
  338. /**
  339. * Normalized (0-1) threshold for color-to-alpha.
  340. *
  341. * @type {number}
  342. */
  343. this.colorToAlphaThreshold = defaultValue(
  344. options.colorToAlphaThreshold,
  345. ImageryLayer.DEFAULT_APPLY_COLOR_TO_ALPHA_THRESHOLD
  346. );
  347. }
  348. Object.defineProperties(ImageryLayer.prototype, {
  349. /**
  350. * Gets the imagery provider for this layer. This should not be called before {@link ImageryLayer#ready} returns true.
  351. * @memberof ImageryLayer.prototype
  352. * @type {ImageryProvider}
  353. * @readonly
  354. */
  355. imageryProvider: {
  356. get: function () {
  357. return this._imageryProvider;
  358. },
  359. },
  360. /**
  361. * Returns true when the terrain provider has been successfully created. Otherwise, returns false.
  362. * @memberof ImageryLayer.prototype
  363. * @type {boolean}
  364. * @readonly
  365. */
  366. ready: {
  367. get: function () {
  368. return defined(this._imageryProvider);
  369. },
  370. },
  371. /**
  372. * Gets an event that is raised when the imagery provider encounters an asynchronous error. By subscribing
  373. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  374. * are passed an instance of the thrown error.
  375. * @memberof Imagery.prototype
  376. * @type {Event<Imagery.ErrorEventCallback>}
  377. * @readonly
  378. */
  379. errorEvent: {
  380. get: function () {
  381. return this._errorEvent;
  382. },
  383. },
  384. /**
  385. * Gets an event that is raised when the imagery provider has been successfully created. Event listeners
  386. * are passed the created instance of {@link ImageryProvider}.
  387. * @memberof ImageryLayer.prototype
  388. * @type {Event<ImageryLayer.ReadyEventCallback>}
  389. * @readonly
  390. */
  391. readyEvent: {
  392. get: function () {
  393. return this._readyEvent;
  394. },
  395. },
  396. /**
  397. * Gets the rectangle of this layer. If this rectangle is smaller than the rectangle of the
  398. * {@link ImageryProvider}, only a portion of the imagery provider is shown.
  399. * @memberof ImageryLayer.prototype
  400. * @type {Rectangle}
  401. * @readonly
  402. */
  403. rectangle: {
  404. get: function () {
  405. return this._rectangle;
  406. },
  407. },
  408. });
  409. /**
  410. * This value is used as the default brightness for the imagery layer if one is not provided during construction
  411. * or by the imagery provider. This value does not modify the brightness of the imagery.
  412. * @type {number}
  413. * @default 1.0
  414. */
  415. ImageryLayer.DEFAULT_BRIGHTNESS = 1.0;
  416. /**
  417. * This value is used as the default contrast for the imagery layer if one is not provided during construction
  418. * or by the imagery provider. This value does not modify the contrast of the imagery.
  419. * @type {number}
  420. * @default 1.0
  421. */
  422. ImageryLayer.DEFAULT_CONTRAST = 1.0;
  423. /**
  424. * This value is used as the default hue for the imagery layer if one is not provided during construction
  425. * or by the imagery provider. This value does not modify the hue of the imagery.
  426. * @type {number}
  427. * @default 0.0
  428. */
  429. ImageryLayer.DEFAULT_HUE = 0.0;
  430. /**
  431. * This value is used as the default saturation for the imagery layer if one is not provided during construction
  432. * or by the imagery provider. This value does not modify the saturation of the imagery.
  433. * @type {number}
  434. * @default 1.0
  435. */
  436. ImageryLayer.DEFAULT_SATURATION = 1.0;
  437. /**
  438. * This value is used as the default gamma for the imagery layer if one is not provided during construction
  439. * or by the imagery provider. This value does not modify the gamma of the imagery.
  440. * @type {number}
  441. * @default 1.0
  442. */
  443. ImageryLayer.DEFAULT_GAMMA = 1.0;
  444. /**
  445. * This value is used as the default split for the imagery layer if one is not provided during construction
  446. * or by the imagery provider.
  447. * @type {SplitDirection}
  448. * @default SplitDirection.NONE
  449. */
  450. ImageryLayer.DEFAULT_SPLIT = SplitDirection.NONE;
  451. /**
  452. * This value is used as the default texture minification filter for the imagery layer if one is not provided
  453. * during construction or by the imagery provider.
  454. * @type {TextureMinificationFilter}
  455. * @default TextureMinificationFilter.LINEAR
  456. */
  457. ImageryLayer.DEFAULT_MINIFICATION_FILTER = TextureMinificationFilter.LINEAR;
  458. /**
  459. * This value is used as the default texture magnification filter for the imagery layer if one is not provided
  460. * during construction or by the imagery provider.
  461. * @type {TextureMagnificationFilter}
  462. * @default TextureMagnificationFilter.LINEAR
  463. */
  464. ImageryLayer.DEFAULT_MAGNIFICATION_FILTER = TextureMagnificationFilter.LINEAR;
  465. /**
  466. * This value is used as the default threshold for color-to-alpha if one is not provided
  467. * during construction or by the imagery provider.
  468. * @type {number}
  469. * @default 0.004
  470. */
  471. ImageryLayer.DEFAULT_APPLY_COLOR_TO_ALPHA_THRESHOLD = 0.004;
  472. /**
  473. * Create a new imagery layer from an asynchronous imagery provider. The layer will handle any asynchronous loads or errors, and begin rendering the imagery layer once ready.
  474. *
  475. * @param {Promise<ImageryProvider>} imageryProviderPromise A promise which resolves to a imagery provider
  476. * @param {ImageryLayer.ConstructorOptions} options An object describing initialization options
  477. * @returns {ImageryLayer} The created imagery layer.
  478. *
  479. * @example
  480. * // Create a new base layer
  481. * const viewer = new Cesium.Viewer("cesiumContainer", {
  482. * baseLayer: Cesium.ImageryLayer.fromProviderAsync(Cesium.IonImageryProvider.fromAssetId(3812));
  483. * });
  484. *
  485. * @example
  486. * // Add a new transparent layer
  487. * const imageryLayer = Cesium.ImageryLayer.fromProviderAsync(Cesium.IonImageryProvider.fromAssetId(3812));
  488. * imageryLayer.alpha = 0.5;
  489. * viewer.imageryLayers.add(imageryLayer);
  490. *
  491. * @example
  492. * // Handle loading events
  493. * const imageryLayer = Cesium.ImageryLayer.fromProviderAsync(Cesium.IonImageryProvider.fromAssetId(3812));
  494. * viewer.imageryLayers.add(imageryLayer);
  495. *
  496. * imageryLayer.readyEvent.addEventListener(provider => {
  497. * imageryLayer.provider.errorEvent.addEventListener(error => {
  498. * alert(`Encountered an error while loading imagery tiles! ${error}`);
  499. * });
  500. * });
  501. *
  502. * imageryLayer.errorEvent.addEventListener(error => {
  503. * alert(`Encountered an error while creating an imagery layer! ${error}`);
  504. * });
  505. *
  506. * @see ImageryLayer.errorEvent
  507. * @see ImageryLayer.readyEvent
  508. * @see ImageryLayer.provider
  509. * @see ImageryLayer.fromWorldImagery
  510. */
  511. ImageryLayer.fromProviderAsync = function (imageryProviderPromise, options) {
  512. //>>includeStart('debug', pragmas.debug);
  513. Check.typeOf.object("imageryProviderPromise", imageryProviderPromise);
  514. //>>includeEnd('debug');
  515. const layer = new ImageryLayer(undefined, options);
  516. handlePromise(layer, Promise.resolve(imageryProviderPromise));
  517. return layer;
  518. };
  519. /**
  520. * @typedef {ImageryLayer.ConstructorOptions} ImageryLayer.WorldImageryConstructorOptions
  521. *
  522. * Initialization options for ImageryLayer.fromWorldImagery
  523. *
  524. * @property {IonWorldImageryStyle} [options.style=IonWorldImageryStyle] The style of base imagery, only AERIAL, AERIAL_WITH_LABELS, and ROAD are currently supported.
  525. */
  526. /**
  527. * Create a new imagery layer for ion's default global base imagery layer, currently Bing Maps. The layer will handle any asynchronous loads or errors, and begin rendering the imagery layer once ready.
  528. *
  529. * @param {ImageryLayer.WorldImageryConstructorOptions} options An object describing initialization options
  530. * @returns {ImageryLayer} The created imagery layer.
  531. *
  532. * * @example
  533. * // Create a new base layer
  534. * const viewer = new Cesium.Viewer("cesiumContainer", {
  535. * baseLayer: Cesium.ImageryLayer.fromWorldImagery();
  536. * });
  537. *
  538. * @example
  539. * // Add a new transparent layer
  540. * const imageryLayer = Cesium.ImageryLayer.fromWorldImagery();
  541. * imageryLayer.alpha = 0.5;
  542. * viewer.imageryLayers.add(imageryLayer);
  543. *
  544. * @example
  545. * // Handle loading events
  546. * const imageryLayer = Cesium.ImageryLayer.fromWorldImagery();
  547. * viewer.imageryLayers.add(imageryLayer);
  548. *
  549. * imageryLayer.readyEvent.addEventListener(provider => {
  550. * imageryLayer.provider.errorEvent.addEventListener(error => {
  551. * alert(`Encountered an error while loading imagery tiles! ${error}`);
  552. * });
  553. * });
  554. *
  555. * imageryLayer.errorEvent.addEventListener(error => {
  556. * alert(`Encountered an error while creating an imagery layer! ${error}`);
  557. * });
  558. *
  559. * @see ImageryLayer.errorEvent
  560. * @see ImageryLayer.readyEvent
  561. * @see ImageryLayer.provider
  562. */
  563. ImageryLayer.fromWorldImagery = function (options) {
  564. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  565. return ImageryLayer.fromProviderAsync(
  566. createWorldImageryAsync({
  567. style: options.style,
  568. }),
  569. options
  570. );
  571. };
  572. /**
  573. * Gets a value indicating whether this layer is the base layer in the
  574. * {@link ImageryLayerCollection}. The base layer is the one that underlies all
  575. * others. It is special in that it is treated as if it has global rectangle, even if
  576. * it actually does not, by stretching the texels at the edges over the entire
  577. * globe.
  578. *
  579. * @returns {boolean} true if this is the base layer; otherwise, false.
  580. */
  581. ImageryLayer.prototype.isBaseLayer = function () {
  582. return this._isBaseLayer;
  583. };
  584. /**
  585. * Returns true if this object was destroyed; otherwise, false.
  586. * <br /><br />
  587. * If this object was destroyed, it should not be used; calling any function other than
  588. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  589. *
  590. * @returns {boolean} True if this object was destroyed; otherwise, false.
  591. *
  592. * @see ImageryLayer#destroy
  593. */
  594. ImageryLayer.prototype.isDestroyed = function () {
  595. return false;
  596. };
  597. /**
  598. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  599. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  600. * <br /><br />
  601. * Once an object is destroyed, it should not be used; calling any function other than
  602. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  603. * assign the return value (<code>undefined</code>) to the object as done in the example.
  604. *
  605. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  606. *
  607. *
  608. * @example
  609. * imageryLayer = imageryLayer && imageryLayer.destroy();
  610. *
  611. * @see ImageryLayer#isDestroyed
  612. */
  613. ImageryLayer.prototype.destroy = function () {
  614. return destroyObject(this);
  615. };
  616. const imageryBoundsScratch = new Rectangle();
  617. const tileImageryBoundsScratch = new Rectangle();
  618. const clippedRectangleScratch = new Rectangle();
  619. const terrainRectangleScratch = new Rectangle();
  620. /**
  621. * Computes the intersection of this layer's rectangle with the imagery provider's availability rectangle,
  622. * producing the overall bounds of imagery that can be produced by this layer.
  623. *
  624. * @returns {Promise<Rectangle>} A promise to a rectangle which defines the overall bounds of imagery that can be produced by this layer.
  625. *
  626. * @example
  627. * // Zoom to an imagery layer.
  628. * imageryLayer.getViewableRectangle().then(function (rectangle) {
  629. * return camera.flyTo({
  630. * destination: rectangle
  631. * });
  632. * });
  633. */
  634. ImageryLayer.prototype.getViewableRectangle = async function () {
  635. deprecationWarning(
  636. "ImageryLayer.getViewableRectangle",
  637. "ImageryLayer.getViewableRectangle was deprecated in CesiumJS 1.104. It will be removed in CesiumJS 1.107. Use ImageryLayer.getImageryRectangle instead."
  638. );
  639. const imageryProvider = this._imageryProvider;
  640. const rectangle = this._rectangle;
  641. // readyPromise has been deprecated. This is here for backward compatibility and can be removed with readyPromise.
  642. await imageryProvider._readyPromise;
  643. return Rectangle.intersection(imageryProvider.rectangle, rectangle);
  644. };
  645. /**
  646. * Computes the intersection of this layer's rectangle with the imagery provider's availability rectangle,
  647. * producing the overall bounds of imagery that can be produced by this layer.
  648. *
  649. * @returns {Rectangle} A rectangle which defines the overall bounds of imagery that can be produced by this layer.
  650. *
  651. * @example
  652. * // Zoom to an imagery layer.
  653. * const imageryRectangle = imageryLayer.getImageryRectangle();
  654. * scene.camera.flyTo({
  655. * destination: rectangle
  656. * });
  657. *
  658. */
  659. ImageryLayer.prototype.getImageryRectangle = function () {
  660. const imageryProvider = this._imageryProvider;
  661. const rectangle = this._rectangle;
  662. return Rectangle.intersection(imageryProvider.rectangle, rectangle);
  663. };
  664. /**
  665. * Create skeletons for the imagery tiles that partially or completely overlap a given terrain
  666. * tile.
  667. *
  668. * @private
  669. *
  670. * @param {Tile} tile The terrain tile.
  671. * @param {TerrainProvider|undefined} terrainProvider The terrain provider associated with the terrain tile.
  672. * @param {number} insertionPoint The position to insert new skeletons before in the tile's imagery list.
  673. * @returns {boolean} true if this layer overlaps any portion of the terrain tile; otherwise, false.
  674. */
  675. ImageryLayer.prototype._createTileImagerySkeletons = function (
  676. tile,
  677. terrainProvider,
  678. insertionPoint
  679. ) {
  680. const surfaceTile = tile.data;
  681. if (
  682. !defined(terrainProvider) ||
  683. (defined(this._minimumTerrainLevel) &&
  684. tile.level < this._minimumTerrainLevel)
  685. ) {
  686. return false;
  687. }
  688. if (
  689. defined(this._maximumTerrainLevel) &&
  690. tile.level > this._maximumTerrainLevel
  691. ) {
  692. return false;
  693. }
  694. if (!defined(insertionPoint)) {
  695. insertionPoint = surfaceTile.imagery.length;
  696. }
  697. const imageryProvider = this._imageryProvider;
  698. // ready is deprecated. This is here for backwards compatibility
  699. if (!this.ready || !imageryProvider._ready) {
  700. // The imagery provider is not ready, so we can't create skeletons, yet.
  701. // Instead, add a placeholder so that we'll know to create
  702. // the skeletons once the provider is ready.
  703. this._skeletonPlaceholder.loadingImagery.addReference();
  704. surfaceTile.imagery.splice(insertionPoint, 0, this._skeletonPlaceholder);
  705. return true;
  706. }
  707. // Use Web Mercator for our texture coordinate computations if this imagery layer uses
  708. // that projection and the terrain tile falls entirely inside the valid bounds of the
  709. // projection.
  710. const useWebMercatorT =
  711. imageryProvider.tilingScheme.projection instanceof WebMercatorProjection &&
  712. tile.rectangle.north < WebMercatorProjection.MaximumLatitude &&
  713. tile.rectangle.south > -WebMercatorProjection.MaximumLatitude;
  714. // Compute the rectangle of the imagery from this imageryProvider that overlaps
  715. // the geometry tile. The ImageryProvider and ImageryLayer both have the
  716. // opportunity to constrain the rectangle. The imagery TilingScheme's rectangle
  717. // always fully contains the ImageryProvider's rectangle.
  718. const imageryBounds = Rectangle.intersection(
  719. imageryProvider.rectangle,
  720. this._rectangle,
  721. imageryBoundsScratch
  722. );
  723. let rectangle = Rectangle.intersection(
  724. tile.rectangle,
  725. imageryBounds,
  726. tileImageryBoundsScratch
  727. );
  728. if (!defined(rectangle)) {
  729. // There is no overlap between this terrain tile and this imagery
  730. // provider. Unless this is the base layer, no skeletons need to be created.
  731. // We stretch texels at the edge of the base layer over the entire globe.
  732. if (!this.isBaseLayer()) {
  733. return false;
  734. }
  735. const baseImageryRectangle = imageryBounds;
  736. const baseTerrainRectangle = tile.rectangle;
  737. rectangle = tileImageryBoundsScratch;
  738. if (baseTerrainRectangle.south >= baseImageryRectangle.north) {
  739. rectangle.north = rectangle.south = baseImageryRectangle.north;
  740. } else if (baseTerrainRectangle.north <= baseImageryRectangle.south) {
  741. rectangle.north = rectangle.south = baseImageryRectangle.south;
  742. } else {
  743. rectangle.south = Math.max(
  744. baseTerrainRectangle.south,
  745. baseImageryRectangle.south
  746. );
  747. rectangle.north = Math.min(
  748. baseTerrainRectangle.north,
  749. baseImageryRectangle.north
  750. );
  751. }
  752. if (baseTerrainRectangle.west >= baseImageryRectangle.east) {
  753. rectangle.west = rectangle.east = baseImageryRectangle.east;
  754. } else if (baseTerrainRectangle.east <= baseImageryRectangle.west) {
  755. rectangle.west = rectangle.east = baseImageryRectangle.west;
  756. } else {
  757. rectangle.west = Math.max(
  758. baseTerrainRectangle.west,
  759. baseImageryRectangle.west
  760. );
  761. rectangle.east = Math.min(
  762. baseTerrainRectangle.east,
  763. baseImageryRectangle.east
  764. );
  765. }
  766. }
  767. let latitudeClosestToEquator = 0.0;
  768. if (rectangle.south > 0.0) {
  769. latitudeClosestToEquator = rectangle.south;
  770. } else if (rectangle.north < 0.0) {
  771. latitudeClosestToEquator = rectangle.north;
  772. }
  773. // Compute the required level in the imagery tiling scheme.
  774. // The errorRatio should really be imagerySSE / terrainSSE rather than this hard-coded value.
  775. // But first we need configurable imagery SSE and we need the rendering to be able to handle more
  776. // images attached to a terrain tile than there are available texture units. So that's for the future.
  777. const errorRatio = 1.0;
  778. const targetGeometricError =
  779. errorRatio * terrainProvider.getLevelMaximumGeometricError(tile.level);
  780. let imageryLevel = getLevelWithMaximumTexelSpacing(
  781. this,
  782. targetGeometricError,
  783. latitudeClosestToEquator
  784. );
  785. imageryLevel = Math.max(0, imageryLevel);
  786. const maximumLevel = imageryProvider.maximumLevel;
  787. if (imageryLevel > maximumLevel) {
  788. imageryLevel = maximumLevel;
  789. }
  790. if (defined(imageryProvider.minimumLevel)) {
  791. const minimumLevel = imageryProvider.minimumLevel;
  792. if (imageryLevel < minimumLevel) {
  793. imageryLevel = minimumLevel;
  794. }
  795. }
  796. const imageryTilingScheme = imageryProvider.tilingScheme;
  797. const northwestTileCoordinates = imageryTilingScheme.positionToTileXY(
  798. Rectangle.northwest(rectangle),
  799. imageryLevel
  800. );
  801. const southeastTileCoordinates = imageryTilingScheme.positionToTileXY(
  802. Rectangle.southeast(rectangle),
  803. imageryLevel
  804. );
  805. // If the southeast corner of the rectangle lies very close to the north or west side
  806. // of the southeast tile, we don't actually need the southernmost or easternmost
  807. // tiles.
  808. // Similarly, if the northwest corner of the rectangle lies very close to the south or east side
  809. // of the northwest tile, we don't actually need the northernmost or westernmost tiles.
  810. // We define "very close" as being within 1/512 of the width of the tile.
  811. let veryCloseX = tile.rectangle.width / 512.0;
  812. let veryCloseY = tile.rectangle.height / 512.0;
  813. const northwestTileRectangle = imageryTilingScheme.tileXYToRectangle(
  814. northwestTileCoordinates.x,
  815. northwestTileCoordinates.y,
  816. imageryLevel
  817. );
  818. if (
  819. Math.abs(northwestTileRectangle.south - tile.rectangle.north) <
  820. veryCloseY &&
  821. northwestTileCoordinates.y < southeastTileCoordinates.y
  822. ) {
  823. ++northwestTileCoordinates.y;
  824. }
  825. if (
  826. Math.abs(northwestTileRectangle.east - tile.rectangle.west) < veryCloseX &&
  827. northwestTileCoordinates.x < southeastTileCoordinates.x
  828. ) {
  829. ++northwestTileCoordinates.x;
  830. }
  831. const southeastTileRectangle = imageryTilingScheme.tileXYToRectangle(
  832. southeastTileCoordinates.x,
  833. southeastTileCoordinates.y,
  834. imageryLevel
  835. );
  836. if (
  837. Math.abs(southeastTileRectangle.north - tile.rectangle.south) <
  838. veryCloseY &&
  839. southeastTileCoordinates.y > northwestTileCoordinates.y
  840. ) {
  841. --southeastTileCoordinates.y;
  842. }
  843. if (
  844. Math.abs(southeastTileRectangle.west - tile.rectangle.east) < veryCloseX &&
  845. southeastTileCoordinates.x > northwestTileCoordinates.x
  846. ) {
  847. --southeastTileCoordinates.x;
  848. }
  849. // Create TileImagery instances for each imagery tile overlapping this terrain tile.
  850. // We need to do all texture coordinate computations in the imagery tile's tiling scheme.
  851. const terrainRectangle = Rectangle.clone(
  852. tile.rectangle,
  853. terrainRectangleScratch
  854. );
  855. let imageryRectangle = imageryTilingScheme.tileXYToRectangle(
  856. northwestTileCoordinates.x,
  857. northwestTileCoordinates.y,
  858. imageryLevel
  859. );
  860. let clippedImageryRectangle = Rectangle.intersection(
  861. imageryRectangle,
  862. imageryBounds,
  863. clippedRectangleScratch
  864. );
  865. let imageryTileXYToRectangle;
  866. if (useWebMercatorT) {
  867. imageryTilingScheme.rectangleToNativeRectangle(
  868. terrainRectangle,
  869. terrainRectangle
  870. );
  871. imageryTilingScheme.rectangleToNativeRectangle(
  872. imageryRectangle,
  873. imageryRectangle
  874. );
  875. imageryTilingScheme.rectangleToNativeRectangle(
  876. clippedImageryRectangle,
  877. clippedImageryRectangle
  878. );
  879. imageryTilingScheme.rectangleToNativeRectangle(
  880. imageryBounds,
  881. imageryBounds
  882. );
  883. imageryTileXYToRectangle = imageryTilingScheme.tileXYToNativeRectangle.bind(
  884. imageryTilingScheme
  885. );
  886. veryCloseX = terrainRectangle.width / 512.0;
  887. veryCloseY = terrainRectangle.height / 512.0;
  888. } else {
  889. imageryTileXYToRectangle = imageryTilingScheme.tileXYToRectangle.bind(
  890. imageryTilingScheme
  891. );
  892. }
  893. let minU;
  894. let maxU = 0.0;
  895. let minV = 1.0;
  896. let maxV;
  897. // If this is the northern-most or western-most tile in the imagery tiling scheme,
  898. // it may not start at the northern or western edge of the terrain tile.
  899. // Calculate where it does start.
  900. if (
  901. !this.isBaseLayer() &&
  902. Math.abs(clippedImageryRectangle.west - terrainRectangle.west) >= veryCloseX
  903. ) {
  904. maxU = Math.min(
  905. 1.0,
  906. (clippedImageryRectangle.west - terrainRectangle.west) /
  907. terrainRectangle.width
  908. );
  909. }
  910. if (
  911. !this.isBaseLayer() &&
  912. Math.abs(clippedImageryRectangle.north - terrainRectangle.north) >=
  913. veryCloseY
  914. ) {
  915. minV = Math.max(
  916. 0.0,
  917. (clippedImageryRectangle.north - terrainRectangle.south) /
  918. terrainRectangle.height
  919. );
  920. }
  921. const initialMinV = minV;
  922. for (
  923. let i = northwestTileCoordinates.x;
  924. i <= southeastTileCoordinates.x;
  925. i++
  926. ) {
  927. minU = maxU;
  928. imageryRectangle = imageryTileXYToRectangle(
  929. i,
  930. northwestTileCoordinates.y,
  931. imageryLevel
  932. );
  933. clippedImageryRectangle = Rectangle.simpleIntersection(
  934. imageryRectangle,
  935. imageryBounds,
  936. clippedRectangleScratch
  937. );
  938. if (!defined(clippedImageryRectangle)) {
  939. continue;
  940. }
  941. maxU = Math.min(
  942. 1.0,
  943. (clippedImageryRectangle.east - terrainRectangle.west) /
  944. terrainRectangle.width
  945. );
  946. // If this is the eastern-most imagery tile mapped to this terrain tile,
  947. // and there are more imagery tiles to the east of this one, the maxU
  948. // should be 1.0 to make sure rounding errors don't make the last
  949. // image fall shy of the edge of the terrain tile.
  950. if (
  951. i === southeastTileCoordinates.x &&
  952. (this.isBaseLayer() ||
  953. Math.abs(clippedImageryRectangle.east - terrainRectangle.east) <
  954. veryCloseX)
  955. ) {
  956. maxU = 1.0;
  957. }
  958. minV = initialMinV;
  959. for (
  960. let j = northwestTileCoordinates.y;
  961. j <= southeastTileCoordinates.y;
  962. j++
  963. ) {
  964. maxV = minV;
  965. imageryRectangle = imageryTileXYToRectangle(i, j, imageryLevel);
  966. clippedImageryRectangle = Rectangle.simpleIntersection(
  967. imageryRectangle,
  968. imageryBounds,
  969. clippedRectangleScratch
  970. );
  971. if (!defined(clippedImageryRectangle)) {
  972. continue;
  973. }
  974. minV = Math.max(
  975. 0.0,
  976. (clippedImageryRectangle.south - terrainRectangle.south) /
  977. terrainRectangle.height
  978. );
  979. // If this is the southern-most imagery tile mapped to this terrain tile,
  980. // and there are more imagery tiles to the south of this one, the minV
  981. // should be 0.0 to make sure rounding errors don't make the last
  982. // image fall shy of the edge of the terrain tile.
  983. if (
  984. j === southeastTileCoordinates.y &&
  985. (this.isBaseLayer() ||
  986. Math.abs(clippedImageryRectangle.south - terrainRectangle.south) <
  987. veryCloseY)
  988. ) {
  989. minV = 0.0;
  990. }
  991. const texCoordsRectangle = new Cartesian4(minU, minV, maxU, maxV);
  992. const imagery = this.getImageryFromCache(i, j, imageryLevel);
  993. surfaceTile.imagery.splice(
  994. insertionPoint,
  995. 0,
  996. new TileImagery(imagery, texCoordsRectangle, useWebMercatorT)
  997. );
  998. ++insertionPoint;
  999. }
  1000. }
  1001. return true;
  1002. };
  1003. /**
  1004. * Calculate the translation and scale for a particular {@link TileImagery} attached to a
  1005. * particular terrain tile.
  1006. *
  1007. * @private
  1008. *
  1009. * @param {Tile} tile The terrain tile.
  1010. * @param {TileImagery} tileImagery The imagery tile mapping.
  1011. * @returns {Cartesian4} The translation and scale where X and Y are the translation and Z and W
  1012. * are the scale.
  1013. */
  1014. ImageryLayer.prototype._calculateTextureTranslationAndScale = function (
  1015. tile,
  1016. tileImagery
  1017. ) {
  1018. let imageryRectangle = tileImagery.readyImagery.rectangle;
  1019. let terrainRectangle = tile.rectangle;
  1020. if (tileImagery.useWebMercatorT) {
  1021. const tilingScheme =
  1022. tileImagery.readyImagery.imageryLayer.imageryProvider.tilingScheme;
  1023. imageryRectangle = tilingScheme.rectangleToNativeRectangle(
  1024. imageryRectangle,
  1025. imageryBoundsScratch
  1026. );
  1027. terrainRectangle = tilingScheme.rectangleToNativeRectangle(
  1028. terrainRectangle,
  1029. terrainRectangleScratch
  1030. );
  1031. }
  1032. const terrainWidth = terrainRectangle.width;
  1033. const terrainHeight = terrainRectangle.height;
  1034. const scaleX = terrainWidth / imageryRectangle.width;
  1035. const scaleY = terrainHeight / imageryRectangle.height;
  1036. return new Cartesian4(
  1037. (scaleX * (terrainRectangle.west - imageryRectangle.west)) / terrainWidth,
  1038. (scaleY * (terrainRectangle.south - imageryRectangle.south)) /
  1039. terrainHeight,
  1040. scaleX,
  1041. scaleY
  1042. );
  1043. };
  1044. /**
  1045. * Request a particular piece of imagery from the imagery provider. This method handles raising an
  1046. * error event if the request fails, and retrying the request if necessary.
  1047. *
  1048. * @private
  1049. *
  1050. * @param {Imagery} imagery The imagery to request.
  1051. */
  1052. ImageryLayer.prototype._requestImagery = function (imagery) {
  1053. const imageryProvider = this._imageryProvider;
  1054. const that = this;
  1055. function success(image) {
  1056. if (!defined(image)) {
  1057. return failure();
  1058. }
  1059. imagery.image = image;
  1060. imagery.state = ImageryState.RECEIVED;
  1061. imagery.request = undefined;
  1062. TileProviderError.reportSuccess(that._requestImageError);
  1063. }
  1064. function failure(e) {
  1065. if (imagery.request.state === RequestState.CANCELLED) {
  1066. // Cancelled due to low priority - try again later.
  1067. imagery.state = ImageryState.UNLOADED;
  1068. imagery.request = undefined;
  1069. return;
  1070. }
  1071. // Initially assume failure. An error handler may retry, in which case the state will
  1072. // change to TRANSITIONING.
  1073. imagery.state = ImageryState.FAILED;
  1074. imagery.request = undefined;
  1075. const message = `Failed to obtain image tile X: ${imagery.x} Y: ${imagery.y} Level: ${imagery.level}.`;
  1076. that._requestImageError = TileProviderError.reportError(
  1077. that._requestImageError,
  1078. imageryProvider,
  1079. imageryProvider.errorEvent,
  1080. message,
  1081. imagery.x,
  1082. imagery.y,
  1083. imagery.level,
  1084. e
  1085. );
  1086. if (that._requestImageError.retry) {
  1087. doRequest();
  1088. }
  1089. }
  1090. function doRequest() {
  1091. const request = new Request({
  1092. throttle: false,
  1093. throttleByServer: true,
  1094. type: RequestType.IMAGERY,
  1095. });
  1096. imagery.request = request;
  1097. imagery.state = ImageryState.TRANSITIONING;
  1098. const imagePromise = imageryProvider.requestImage(
  1099. imagery.x,
  1100. imagery.y,
  1101. imagery.level,
  1102. request
  1103. );
  1104. if (!defined(imagePromise)) {
  1105. // Too many parallel requests, so postpone loading tile.
  1106. imagery.state = ImageryState.UNLOADED;
  1107. imagery.request = undefined;
  1108. return;
  1109. }
  1110. if (defined(imageryProvider.getTileCredits)) {
  1111. imagery.credits = imageryProvider.getTileCredits(
  1112. imagery.x,
  1113. imagery.y,
  1114. imagery.level
  1115. );
  1116. }
  1117. imagePromise
  1118. .then(function (image) {
  1119. success(image);
  1120. })
  1121. .catch(function (e) {
  1122. failure(e);
  1123. });
  1124. }
  1125. doRequest();
  1126. };
  1127. ImageryLayer.prototype._createTextureWebGL = function (context, imagery) {
  1128. const sampler = new Sampler({
  1129. minificationFilter: this.minificationFilter,
  1130. magnificationFilter: this.magnificationFilter,
  1131. });
  1132. const image = imagery.image;
  1133. if (defined(image.internalFormat)) {
  1134. return new Texture({
  1135. context: context,
  1136. pixelFormat: image.internalFormat,
  1137. width: image.width,
  1138. height: image.height,
  1139. source: {
  1140. arrayBufferView: image.bufferView,
  1141. },
  1142. sampler: sampler,
  1143. });
  1144. }
  1145. return new Texture({
  1146. context: context,
  1147. source: image,
  1148. pixelFormat: this._imageryProvider.hasAlphaChannel
  1149. ? PixelFormat.RGBA
  1150. : PixelFormat.RGB,
  1151. sampler: sampler,
  1152. });
  1153. };
  1154. /**
  1155. * Create a WebGL texture for a given {@link Imagery} instance.
  1156. *
  1157. * @private
  1158. *
  1159. * @param {Context} context The rendered context to use to create textures.
  1160. * @param {Imagery} imagery The imagery for which to create a texture.
  1161. */
  1162. ImageryLayer.prototype._createTexture = function (context, imagery) {
  1163. const imageryProvider = this._imageryProvider;
  1164. const image = imagery.image;
  1165. // If this imagery provider has a discard policy, use it to check if this
  1166. // image should be discarded.
  1167. if (defined(imageryProvider.tileDiscardPolicy)) {
  1168. const discardPolicy = imageryProvider.tileDiscardPolicy;
  1169. if (defined(discardPolicy)) {
  1170. // If the discard policy is not ready yet, transition back to the
  1171. // RECEIVED state and we'll try again next time.
  1172. if (!discardPolicy.isReady()) {
  1173. imagery.state = ImageryState.RECEIVED;
  1174. return;
  1175. }
  1176. // Mark discarded imagery tiles invalid. Parent imagery will be used instead.
  1177. if (discardPolicy.shouldDiscardImage(image)) {
  1178. imagery.state = ImageryState.INVALID;
  1179. return;
  1180. }
  1181. }
  1182. }
  1183. //>>includeStart('debug', pragmas.debug);
  1184. if (
  1185. this.minificationFilter !== TextureMinificationFilter.NEAREST &&
  1186. this.minificationFilter !== TextureMinificationFilter.LINEAR
  1187. ) {
  1188. throw new DeveloperError(
  1189. "ImageryLayer minification filter must be NEAREST or LINEAR"
  1190. );
  1191. }
  1192. //>>includeEnd('debug');
  1193. // Imagery does not need to be discarded, so upload it to WebGL.
  1194. const texture = this._createTextureWebGL(context, imagery);
  1195. if (
  1196. imageryProvider.tilingScheme.projection instanceof WebMercatorProjection
  1197. ) {
  1198. imagery.textureWebMercator = texture;
  1199. } else {
  1200. imagery.texture = texture;
  1201. }
  1202. imagery.image = undefined;
  1203. imagery.state = ImageryState.TEXTURE_LOADED;
  1204. };
  1205. function getSamplerKey(
  1206. minificationFilter,
  1207. magnificationFilter,
  1208. maximumAnisotropy
  1209. ) {
  1210. return `${minificationFilter}:${magnificationFilter}:${maximumAnisotropy}`;
  1211. }
  1212. ImageryLayer.prototype._finalizeReprojectTexture = function (context, texture) {
  1213. let minificationFilter = this.minificationFilter;
  1214. const magnificationFilter = this.magnificationFilter;
  1215. const usesLinearTextureFilter =
  1216. minificationFilter === TextureMinificationFilter.LINEAR &&
  1217. magnificationFilter === TextureMagnificationFilter.LINEAR;
  1218. // Use mipmaps if this texture has power-of-two dimensions.
  1219. // In addition, mipmaps are only generated if the texture filters are both LINEAR.
  1220. if (
  1221. usesLinearTextureFilter &&
  1222. !PixelFormat.isCompressedFormat(texture.pixelFormat) &&
  1223. CesiumMath.isPowerOfTwo(texture.width) &&
  1224. CesiumMath.isPowerOfTwo(texture.height)
  1225. ) {
  1226. minificationFilter = TextureMinificationFilter.LINEAR_MIPMAP_LINEAR;
  1227. const maximumSupportedAnisotropy =
  1228. ContextLimits.maximumTextureFilterAnisotropy;
  1229. const maximumAnisotropy = Math.min(
  1230. maximumSupportedAnisotropy,
  1231. defaultValue(this._maximumAnisotropy, maximumSupportedAnisotropy)
  1232. );
  1233. const mipmapSamplerKey = getSamplerKey(
  1234. minificationFilter,
  1235. magnificationFilter,
  1236. maximumAnisotropy
  1237. );
  1238. let mipmapSamplers = context.cache.imageryLayerMipmapSamplers;
  1239. if (!defined(mipmapSamplers)) {
  1240. mipmapSamplers = {};
  1241. context.cache.imageryLayerMipmapSamplers = mipmapSamplers;
  1242. }
  1243. let mipmapSampler = mipmapSamplers[mipmapSamplerKey];
  1244. if (!defined(mipmapSampler)) {
  1245. mipmapSampler = mipmapSamplers[mipmapSamplerKey] = new Sampler({
  1246. wrapS: TextureWrap.CLAMP_TO_EDGE,
  1247. wrapT: TextureWrap.CLAMP_TO_EDGE,
  1248. minificationFilter: minificationFilter,
  1249. magnificationFilter: magnificationFilter,
  1250. maximumAnisotropy: maximumAnisotropy,
  1251. });
  1252. }
  1253. texture.generateMipmap(MipmapHint.NICEST);
  1254. texture.sampler = mipmapSampler;
  1255. } else {
  1256. const nonMipmapSamplerKey = getSamplerKey(
  1257. minificationFilter,
  1258. magnificationFilter,
  1259. 0
  1260. );
  1261. let nonMipmapSamplers = context.cache.imageryLayerNonMipmapSamplers;
  1262. if (!defined(nonMipmapSamplers)) {
  1263. nonMipmapSamplers = {};
  1264. context.cache.imageryLayerNonMipmapSamplers = nonMipmapSamplers;
  1265. }
  1266. let nonMipmapSampler = nonMipmapSamplers[nonMipmapSamplerKey];
  1267. if (!defined(nonMipmapSampler)) {
  1268. nonMipmapSampler = nonMipmapSamplers[nonMipmapSamplerKey] = new Sampler({
  1269. wrapS: TextureWrap.CLAMP_TO_EDGE,
  1270. wrapT: TextureWrap.CLAMP_TO_EDGE,
  1271. minificationFilter: minificationFilter,
  1272. magnificationFilter: magnificationFilter,
  1273. });
  1274. }
  1275. texture.sampler = nonMipmapSampler;
  1276. }
  1277. };
  1278. /**
  1279. * Enqueues a command re-projecting a texture to a {@link GeographicProjection} on the next update, if necessary, and generate
  1280. * mipmaps for the geographic texture.
  1281. *
  1282. * @private
  1283. *
  1284. * @param {FrameState} frameState The frameState.
  1285. * @param {Imagery} imagery The imagery instance to reproject.
  1286. * @param {boolean} [needGeographicProjection=true] True to reproject to geographic, or false if Web Mercator is fine.
  1287. */
  1288. ImageryLayer.prototype._reprojectTexture = function (
  1289. frameState,
  1290. imagery,
  1291. needGeographicProjection
  1292. ) {
  1293. const texture = imagery.textureWebMercator || imagery.texture;
  1294. const rectangle = imagery.rectangle;
  1295. const context = frameState.context;
  1296. needGeographicProjection = defaultValue(needGeographicProjection, true);
  1297. // Reproject this texture if it is not already in a geographic projection and
  1298. // the pixels are more than 1e-5 radians apart. The pixel spacing cutoff
  1299. // avoids precision problems in the reprojection transformation while making
  1300. // no noticeable difference in the georeferencing of the image.
  1301. if (
  1302. needGeographicProjection &&
  1303. !(
  1304. this._imageryProvider.tilingScheme.projection instanceof
  1305. GeographicProjection
  1306. ) &&
  1307. rectangle.width / texture.width > 1e-5
  1308. ) {
  1309. const that = this;
  1310. imagery.addReference();
  1311. const computeCommand = new ComputeCommand({
  1312. persists: true,
  1313. owner: this,
  1314. // Update render resources right before execution instead of now.
  1315. // This allows different ImageryLayers to share the same vao and buffers.
  1316. preExecute: function (command) {
  1317. reprojectToGeographic(command, context, texture, imagery.rectangle);
  1318. },
  1319. postExecute: function (outputTexture) {
  1320. imagery.texture = outputTexture;
  1321. that._finalizeReprojectTexture(context, outputTexture);
  1322. imagery.state = ImageryState.READY;
  1323. imagery.releaseReference();
  1324. },
  1325. canceled: function () {
  1326. imagery.state = ImageryState.TEXTURE_LOADED;
  1327. imagery.releaseReference();
  1328. },
  1329. });
  1330. this._reprojectComputeCommands.push(computeCommand);
  1331. } else {
  1332. if (needGeographicProjection) {
  1333. imagery.texture = texture;
  1334. }
  1335. this._finalizeReprojectTexture(context, texture);
  1336. imagery.state = ImageryState.READY;
  1337. }
  1338. };
  1339. /**
  1340. * Updates frame state to execute any queued texture re-projections.
  1341. *
  1342. * @private
  1343. *
  1344. * @param {FrameState} frameState The frameState.
  1345. */
  1346. ImageryLayer.prototype.queueReprojectionCommands = function (frameState) {
  1347. const computeCommands = this._reprojectComputeCommands;
  1348. const length = computeCommands.length;
  1349. for (let i = 0; i < length; ++i) {
  1350. frameState.commandList.push(computeCommands[i]);
  1351. }
  1352. computeCommands.length = 0;
  1353. };
  1354. /**
  1355. * Cancels re-projection commands queued for the next frame.
  1356. *
  1357. * @private
  1358. */
  1359. ImageryLayer.prototype.cancelReprojections = function () {
  1360. this._reprojectComputeCommands.forEach(function (command) {
  1361. if (defined(command.canceled)) {
  1362. command.canceled();
  1363. }
  1364. });
  1365. this._reprojectComputeCommands.length = 0;
  1366. };
  1367. ImageryLayer.prototype.getImageryFromCache = function (
  1368. x,
  1369. y,
  1370. level,
  1371. imageryRectangle
  1372. ) {
  1373. const cacheKey = getImageryCacheKey(x, y, level);
  1374. let imagery = this._imageryCache[cacheKey];
  1375. if (!defined(imagery)) {
  1376. imagery = new Imagery(this, x, y, level, imageryRectangle);
  1377. this._imageryCache[cacheKey] = imagery;
  1378. }
  1379. imagery.addReference();
  1380. return imagery;
  1381. };
  1382. ImageryLayer.prototype.removeImageryFromCache = function (imagery) {
  1383. const cacheKey = getImageryCacheKey(imagery.x, imagery.y, imagery.level);
  1384. delete this._imageryCache[cacheKey];
  1385. };
  1386. function getImageryCacheKey(x, y, level) {
  1387. return JSON.stringify([x, y, level]);
  1388. }
  1389. const uniformMap = {
  1390. u_textureDimensions: function () {
  1391. return this.textureDimensions;
  1392. },
  1393. u_texture: function () {
  1394. return this.texture;
  1395. },
  1396. textureDimensions: new Cartesian2(),
  1397. texture: undefined,
  1398. };
  1399. const float32ArrayScratch = FeatureDetection.supportsTypedArrays()
  1400. ? new Float32Array(2 * 64)
  1401. : undefined;
  1402. function reprojectToGeographic(command, context, texture, rectangle) {
  1403. // This function has gone through a number of iterations, because GPUs are awesome.
  1404. //
  1405. // Originally, we had a very simple vertex shader and computed the Web Mercator texture coordinates
  1406. // per-fragment in the fragment shader. That worked well, except on mobile devices, because
  1407. // fragment shaders have limited precision on many mobile devices. The result was smearing artifacts
  1408. // at medium zoom levels because different geographic texture coordinates would be reprojected to Web
  1409. // Mercator as the same value.
  1410. //
  1411. // Our solution was to reproject to Web Mercator in the vertex shader instead of the fragment shader.
  1412. // This required far more vertex data. With fragment shader reprojection, we only needed a single quad.
  1413. // But to achieve the same precision with vertex shader reprojection, we needed a vertex for each
  1414. // output pixel. So we used a grid of 256x256 vertices, because most of our imagery
  1415. // tiles are 256x256. Fortunately the grid could be created and uploaded to the GPU just once and
  1416. // re-used for all reprojections, so the performance was virtually unchanged from our original fragment
  1417. // shader approach. See https://github.com/CesiumGS/cesium/pull/714.
  1418. //
  1419. // Over a year later, we noticed (https://github.com/CesiumGS/cesium/issues/2110)
  1420. // that our reprojection code was creating a rare but severe artifact on some GPUs (Intel HD 4600
  1421. // for one). The problem was that the GLSL sin function on these GPUs had a discontinuity at fine scales in
  1422. // a few places.
  1423. //
  1424. // We solved this by implementing a more reliable sin function based on the CORDIC algorithm
  1425. // (https://github.com/CesiumGS/cesium/pull/2111). Even though this was a fair
  1426. // amount of code to be executing per vertex, the performance seemed to be pretty good on most GPUs.
  1427. // Unfortunately, on some GPUs, the performance was absolutely terrible
  1428. // (https://github.com/CesiumGS/cesium/issues/2258).
  1429. //
  1430. // So that brings us to our current solution, the one you see here. Effectively, we compute the Web
  1431. // Mercator texture coordinates on the CPU and store the T coordinate with each vertex (the S coordinate
  1432. // is the same in Geographic and Web Mercator). To make this faster, we reduced our reprojection mesh
  1433. // to be only 2 vertices wide and 64 vertices high. We should have reduced the width to 2 sooner,
  1434. // because the extra vertices weren't buying us anything. The height of 64 means we are technically
  1435. // doing a slightly less accurate reprojection than we were before, but we can't see the difference
  1436. // so it's worth the 4x speedup.
  1437. let reproject = context.cache.imageryLayer_reproject;
  1438. if (!defined(reproject)) {
  1439. reproject = context.cache.imageryLayer_reproject = {
  1440. vertexArray: undefined,
  1441. shaderProgram: undefined,
  1442. sampler: undefined,
  1443. destroy: function () {
  1444. if (defined(this.framebuffer)) {
  1445. this.framebuffer.destroy();
  1446. }
  1447. if (defined(this.vertexArray)) {
  1448. this.vertexArray.destroy();
  1449. }
  1450. if (defined(this.shaderProgram)) {
  1451. this.shaderProgram.destroy();
  1452. }
  1453. },
  1454. };
  1455. const positions = new Float32Array(2 * 64 * 2);
  1456. let index = 0;
  1457. for (let j = 0; j < 64; ++j) {
  1458. const y = j / 63.0;
  1459. positions[index++] = 0.0;
  1460. positions[index++] = y;
  1461. positions[index++] = 1.0;
  1462. positions[index++] = y;
  1463. }
  1464. const reprojectAttributeIndices = {
  1465. position: 0,
  1466. webMercatorT: 1,
  1467. };
  1468. const indices = TerrainProvider.getRegularGridIndices(2, 64);
  1469. const indexBuffer = Buffer.createIndexBuffer({
  1470. context: context,
  1471. typedArray: indices,
  1472. usage: BufferUsage.STATIC_DRAW,
  1473. indexDatatype: IndexDatatype.UNSIGNED_SHORT,
  1474. });
  1475. reproject.vertexArray = new VertexArray({
  1476. context: context,
  1477. attributes: [
  1478. {
  1479. index: reprojectAttributeIndices.position,
  1480. vertexBuffer: Buffer.createVertexBuffer({
  1481. context: context,
  1482. typedArray: positions,
  1483. usage: BufferUsage.STATIC_DRAW,
  1484. }),
  1485. componentsPerAttribute: 2,
  1486. },
  1487. {
  1488. index: reprojectAttributeIndices.webMercatorT,
  1489. vertexBuffer: Buffer.createVertexBuffer({
  1490. context: context,
  1491. sizeInBytes: 64 * 2 * 4,
  1492. usage: BufferUsage.STREAM_DRAW,
  1493. }),
  1494. componentsPerAttribute: 1,
  1495. },
  1496. ],
  1497. indexBuffer: indexBuffer,
  1498. });
  1499. const vs = new ShaderSource({
  1500. sources: [ReprojectWebMercatorVS],
  1501. });
  1502. reproject.shaderProgram = ShaderProgram.fromCache({
  1503. context: context,
  1504. vertexShaderSource: vs,
  1505. fragmentShaderSource: ReprojectWebMercatorFS,
  1506. attributeLocations: reprojectAttributeIndices,
  1507. });
  1508. reproject.sampler = new Sampler({
  1509. wrapS: TextureWrap.CLAMP_TO_EDGE,
  1510. wrapT: TextureWrap.CLAMP_TO_EDGE,
  1511. minificationFilter: TextureMinificationFilter.LINEAR,
  1512. magnificationFilter: TextureMagnificationFilter.LINEAR,
  1513. });
  1514. }
  1515. texture.sampler = reproject.sampler;
  1516. const width = texture.width;
  1517. const height = texture.height;
  1518. uniformMap.textureDimensions.x = width;
  1519. uniformMap.textureDimensions.y = height;
  1520. uniformMap.texture = texture;
  1521. let sinLatitude = Math.sin(rectangle.south);
  1522. const southMercatorY = 0.5 * Math.log((1 + sinLatitude) / (1 - sinLatitude));
  1523. sinLatitude = Math.sin(rectangle.north);
  1524. const northMercatorY = 0.5 * Math.log((1 + sinLatitude) / (1 - sinLatitude));
  1525. const oneOverMercatorHeight = 1.0 / (northMercatorY - southMercatorY);
  1526. const outputTexture = new Texture({
  1527. context: context,
  1528. width: width,
  1529. height: height,
  1530. pixelFormat: texture.pixelFormat,
  1531. pixelDatatype: texture.pixelDatatype,
  1532. preMultiplyAlpha: texture.preMultiplyAlpha,
  1533. });
  1534. // Allocate memory for the mipmaps. Failure to do this before rendering
  1535. // to the texture via the FBO, and calling generateMipmap later,
  1536. // will result in the texture appearing blank. I can't pretend to
  1537. // understand exactly why this is.
  1538. if (CesiumMath.isPowerOfTwo(width) && CesiumMath.isPowerOfTwo(height)) {
  1539. outputTexture.generateMipmap(MipmapHint.NICEST);
  1540. }
  1541. const south = rectangle.south;
  1542. const north = rectangle.north;
  1543. const webMercatorT = float32ArrayScratch;
  1544. let outputIndex = 0;
  1545. for (let webMercatorTIndex = 0; webMercatorTIndex < 64; ++webMercatorTIndex) {
  1546. const fraction = webMercatorTIndex / 63.0;
  1547. const latitude = CesiumMath.lerp(south, north, fraction);
  1548. sinLatitude = Math.sin(latitude);
  1549. const mercatorY = 0.5 * Math.log((1.0 + sinLatitude) / (1.0 - sinLatitude));
  1550. const mercatorFraction =
  1551. (mercatorY - southMercatorY) * oneOverMercatorHeight;
  1552. webMercatorT[outputIndex++] = mercatorFraction;
  1553. webMercatorT[outputIndex++] = mercatorFraction;
  1554. }
  1555. reproject.vertexArray
  1556. .getAttribute(1)
  1557. .vertexBuffer.copyFromArrayView(webMercatorT);
  1558. command.shaderProgram = reproject.shaderProgram;
  1559. command.outputTexture = outputTexture;
  1560. command.uniformMap = uniformMap;
  1561. command.vertexArray = reproject.vertexArray;
  1562. }
  1563. /**
  1564. * Gets the level with the specified world coordinate spacing between texels, or less.
  1565. *
  1566. * @param {ImageryLayer} layer The imagery layer to use.
  1567. * @param {number} texelSpacing The texel spacing for which to find a corresponding level.
  1568. * @param {number} latitudeClosestToEquator The latitude closest to the equator that we're concerned with.
  1569. * @returns {number} The level with the specified texel spacing or less.
  1570. * @private
  1571. */
  1572. function getLevelWithMaximumTexelSpacing(
  1573. layer,
  1574. texelSpacing,
  1575. latitudeClosestToEquator
  1576. ) {
  1577. // PERFORMANCE_IDEA: factor out the stuff that doesn't change.
  1578. const imageryProvider = layer._imageryProvider;
  1579. const tilingScheme = imageryProvider.tilingScheme;
  1580. const ellipsoid = tilingScheme.ellipsoid;
  1581. const latitudeFactor = !(
  1582. layer._imageryProvider.tilingScheme.projection instanceof
  1583. GeographicProjection
  1584. )
  1585. ? Math.cos(latitudeClosestToEquator)
  1586. : 1.0;
  1587. const tilingSchemeRectangle = tilingScheme.rectangle;
  1588. const levelZeroMaximumTexelSpacing =
  1589. (ellipsoid.maximumRadius * tilingSchemeRectangle.width * latitudeFactor) /
  1590. (imageryProvider.tileWidth * tilingScheme.getNumberOfXTilesAtLevel(0));
  1591. const twoToTheLevelPower = levelZeroMaximumTexelSpacing / texelSpacing;
  1592. const level = Math.log(twoToTheLevelPower) / Math.log(2);
  1593. const rounded = Math.round(level);
  1594. return rounded | 0;
  1595. }
  1596. function handleError(errorEvent, error) {
  1597. if (errorEvent.numberOfListeners > 0) {
  1598. errorEvent.raiseEvent(error);
  1599. } else {
  1600. // Default handler is to log to the console
  1601. console.error(error);
  1602. }
  1603. }
  1604. async function handlePromise(instance, promise) {
  1605. let provider;
  1606. try {
  1607. provider = await Promise.resolve(promise);
  1608. if (instance.isDestroyed()) {
  1609. return;
  1610. }
  1611. instance._imageryProvider = provider;
  1612. instance._readyEvent.raiseEvent(provider);
  1613. } catch (error) {
  1614. handleError(instance._errorEvent, error);
  1615. }
  1616. }
  1617. export default ImageryLayer;
  1618. /**
  1619. * A function that is called when an error occurs.
  1620. * @callback ImageryLayer.ErrorEventCallback
  1621. *
  1622. * @this ImageryLayer
  1623. * @param {Error} err An object holding details about the error that occurred.
  1624. */
  1625. /**
  1626. * A function that is called when the provider has been created
  1627. * @callback ImageryLayer.ReadyEventCallback
  1628. *
  1629. * @this ImageryLayer
  1630. * @param {ImageryProvider} provider The created imagery provider.
  1631. */