智慧水务管理系统 - 精河县供水工程综合管理平台

ImageryLayer.js 62KB

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