@tangram.gl/basemap-layers 0.0.0 → 10.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -7
- package/THIRD-PARTY-NOTICES.md +7 -5
- package/dist/geojson-task-worker.js +3166 -0
- package/dist/geojson-task-worker.js.map +6 -0
- package/dist/geojson-worker.js +1089 -7
- package/dist/geojson-worker.js.map +3 -3
- package/dist/index.cjs +9588 -2093
- package/dist/index.cjs.map +3 -3
- package/dist/index.js +9599 -2090
- package/dist/index.js.map +3 -3
- package/dist/map-style.cjs +456 -20
- package/dist/map-style.cjs.map +3 -3
- package/dist/map-style.js +456 -19
- package/dist/map-style.js.map +2 -2
- package/dist/types/basemap-error.d.ts +52 -0
- package/dist/types/basemap-layer.d.ts +378 -25
- package/dist/types/basemap-protocol.d.ts +91 -0
- package/dist/types/basemap-request.d.ts +73 -0
- package/dist/types/circle-layer.d.ts +65 -8
- package/dist/types/color-relief-layer.d.ts +184 -0
- package/dist/types/dem-pixels.d.ts +40 -0
- package/dist/types/feature-state.d.ts +55 -0
- package/dist/types/fill-extrusion-lighting-extension.d.ts +1 -2
- package/dist/types/fill-outline-extension.d.ts +2 -3
- package/dist/types/fill-pattern.d.ts +35 -3
- package/dist/types/fill-style-extensions.d.ts +11 -59
- package/dist/types/geojson-resource.d.ts +18 -0
- package/dist/types/geojson-source.d.ts +13 -2
- package/dist/types/geojson-task-handler.d.ts +2 -0
- package/dist/types/geojson-task.d.ts +26 -0
- package/dist/types/geojson-tiler.d.ts +29 -2
- package/dist/types/geojson-worker.d.ts +5 -1
- package/dist/types/globe-horizon-extension.d.ts +2 -45
- package/dist/types/globe-layers.d.ts +141 -45
- package/dist/types/globe-line-angle-extension.d.ts +20 -17
- package/dist/types/glyph-store.d.ts +10 -1
- package/dist/types/heatmap-layer.d.ts +99 -0
- package/dist/types/hillshade-layer.d.ts +159 -27
- package/dist/types/icon-text-fit.d.ts +92 -0
- package/dist/types/image-source-layer.d.ts +142 -0
- package/dist/types/index.d.ts +25 -1
- package/dist/types/line-blur-extension.d.ts +3 -11
- package/dist/types/line-dash-extension.d.ts +53 -0
- package/dist/types/line-gradient-extension.d.ts +69 -0
- package/dist/types/line-metrics.d.ts +24 -0
- package/dist/types/line-pattern-extension.d.ts +50 -0
- package/dist/types/line-shape-extension.d.ts +76 -0
- package/dist/types/map-style-loader.d.ts +1 -1
- package/dist/types/map-style.d.ts +2 -2
- package/dist/types/mvt-label-layer.d.ts +111 -20
- package/dist/types/pmtiles-source.d.ts +38 -0
- package/dist/types/projected-background-layer.d.ts +63 -0
- package/dist/types/projected-bitmap-layer.d.ts +58 -0
- package/dist/types/projected-tile-data.d.ts +16 -0
- package/dist/types/projected-tile-selection.d.ts +72 -0
- package/dist/types/projected-tileset.d.ts +92 -0
- package/dist/types/projected-viewport.d.ts +76 -0
- package/dist/types/raster-color-extension.d.ts +4 -7
- package/dist/types/raster-fade.d.ts +155 -0
- package/dist/types/raster-tile-fingerprint.d.ts +11 -0
- package/dist/types/raster-tile-loader.d.ts +80 -0
- package/dist/types/shader-injection-plugin.d.ts +8 -0
- package/dist/types/source-features.d.ts +58 -0
- package/dist/types/source-tiles.d.ts +32 -0
- package/dist/types/sprite.d.ts +37 -3
- package/dist/types/stretch-icon-layer.d.ts +28 -0
- package/dist/types/style-accessor.d.ts +50 -12
- package/dist/types/style-expression.d.ts +22 -2
- package/dist/types/style-resolver.d.ts +47 -2
- package/dist/types/terrain-query.d.ts +39 -0
- package/dist/types/zoom-load-gate.d.ts +22 -0
- package/dist/types/zoom-paint-extension.d.ts +80 -11
- package/package.json +24 -21
- package/src/basemap-error.ts +82 -0
- package/src/basemap-layer.ts +1289 -101
- package/src/basemap-protocol.ts +353 -0
- package/src/basemap-request.ts +212 -0
- package/src/circle-layer.ts +430 -54
- package/src/color-relief-layer.ts +572 -0
- package/src/dem-pixels.ts +174 -0
- package/src/feature-state.ts +164 -0
- package/src/fill-extrusion-lighting-extension.ts +126 -9
- package/src/fill-outline-extension.ts +4 -4
- package/src/fill-pattern.ts +87 -9
- package/src/fill-style-extensions.ts +114 -44
- package/src/geojson-resource.ts +58 -0
- package/src/geojson-source.ts +106 -85
- package/src/geojson-task-handler.ts +11 -0
- package/src/geojson-task-worker.ts +8 -0
- package/src/geojson-task.ts +34 -0
- package/src/geojson-tiler.ts +270 -4
- package/src/geojson-worker.ts +36 -6
- package/src/globe-horizon-extension.ts +14 -4
- package/src/globe-layers.ts +1905 -406
- package/src/globe-line-angle-extension.ts +183 -23
- package/src/glyph-store.ts +28 -6
- package/src/heatmap-layer.ts +591 -0
- package/src/hillshade-layer.ts +309 -88
- package/src/icon-text-fit.ts +358 -0
- package/src/image-source-layer.ts +380 -0
- package/src/index.ts +58 -1
- package/src/line-blur-extension.ts +14 -12
- package/src/line-dash-extension.ts +303 -0
- package/src/line-gradient-extension.ts +191 -0
- package/src/line-metrics.ts +90 -0
- package/src/line-pattern-extension.ts +173 -0
- package/src/line-shape-extension.ts +272 -0
- package/src/map-style-loader.ts +5 -4
- package/src/map-style.ts +2 -1
- package/src/mvt-label-layer.ts +568 -144
- package/src/pmtiles-source.ts +276 -0
- package/src/projected-background-layer.ts +202 -0
- package/src/projected-bitmap-layer.ts +262 -0
- package/src/projected-tile-data.ts +132 -0
- package/src/projected-tile-selection.ts +605 -0
- package/src/projected-tileset.ts +183 -0
- package/src/projected-viewport.ts +254 -0
- package/src/raster-color-extension.ts +43 -18
- package/src/raster-fade.ts +319 -0
- package/src/raster-tile-fingerprint.ts +31 -0
- package/src/raster-tile-loader.ts +211 -0
- package/src/shader-injection-plugin.ts +25 -0
- package/src/source-features.ts +170 -0
- package/src/source-tiles.ts +102 -0
- package/src/sprite.ts +122 -15
- package/src/stretch-icon-layer.ts +87 -0
- package/src/style-accessor.ts +105 -17
- package/src/style-expression.ts +80 -3
- package/src/style-resolver.ts +200 -22
- package/src/terrain-query.ts +86 -0
- package/src/text-layout.ts +2 -3
- package/src/zoom-load-gate.ts +57 -0
- package/src/zoom-paint-extension.ts +245 -47
- package/dist/types/arabic-shaping.d.ts +0 -7
- package/dist/types/bidi.d.ts +0 -8
- package/dist/types/polylabel.d.ts +0 -17
- package/src/arabic-shaping.ts +0 -366
- package/src/bidi.ts +0 -199
- package/src/polylabel.ts +0 -312
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
// tangram-layers
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {LayerExtension} from '@deck.gl/core';
|
|
6
|
+
import type {Layer, LayerProps} from '@deck.gl/core';
|
|
7
|
+
import type {_Tile2DHeader as Tile2DHeader} from '@deck.gl/geo-layers';
|
|
8
|
+
import type {Texture} from '@luma.gl/core';
|
|
9
|
+
import {createShaderInjectionPlugin} from './shader-injection-plugin';
|
|
10
|
+
import {ProjectedTileLayer} from './projected-tileset';
|
|
11
|
+
|
|
12
|
+
/** Returns the current time in milliseconds. `raster-fade-duration` is measured on this clock. */
|
|
13
|
+
export type RasterFadeClock = () => number;
|
|
14
|
+
|
|
15
|
+
/** The default clock: `performance.now`. */
|
|
16
|
+
export const defaultRasterFadeClock: RasterFadeClock = () => performance.now();
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The most zoom levels above a tile that its fading ancestor can be, as in MapLibre GL JS
|
|
20
|
+
* (`TileManager._maxFadingAncestorLevels`).
|
|
21
|
+
*/
|
|
22
|
+
export const MAX_FADING_ANCESTOR_LEVELS = 5;
|
|
23
|
+
|
|
24
|
+
/** How a tile is drawn at one moment of its fade. */
|
|
25
|
+
export type RasterFadeValues = {
|
|
26
|
+
/** The factor of the tile's alpha. */
|
|
27
|
+
opacity: number;
|
|
28
|
+
/** The share of the ancestor's color in the tile's color, 0 to 1. */
|
|
29
|
+
mix: number;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** A tile that has finished its fade. */
|
|
33
|
+
const FADED_IN: RasterFadeValues = {opacity: 1, mix: 0};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The fade of a tile `elapsed` milliseconds after it loaded, as MapLibre GL JS computes the fade
|
|
37
|
+
* of an incoming tile in `draw_raster`. A tile with a loaded ancestor cross-fades from the
|
|
38
|
+
* ancestor's image (`getCrossFadeValues`): the share of the ancestor goes from 1 to 0, and the
|
|
39
|
+
* tile stays opaque. A tile without one fades in from transparent (`getSelfFadeValues`). With a
|
|
40
|
+
* `duration` of 0, the tile is drawn at once.
|
|
41
|
+
*/
|
|
42
|
+
export function getRasterFadeValues(
|
|
43
|
+
elapsed: number,
|
|
44
|
+
duration: number,
|
|
45
|
+
crossFade: boolean
|
|
46
|
+
): RasterFadeValues {
|
|
47
|
+
if (!(duration > 0)) {
|
|
48
|
+
return FADED_IN;
|
|
49
|
+
}
|
|
50
|
+
const progress = Math.min(1, Math.max(0, elapsed / duration));
|
|
51
|
+
return crossFade ? {opacity: 1, mix: 1 - progress} : {opacity: progress, mix: 0};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The part of an ancestor tile's image that covers a tile. */
|
|
55
|
+
export type RasterFadeParent = {
|
|
56
|
+
/** The texture coordinates of the tile's top-left corner in the ancestor's image. */
|
|
57
|
+
topLeft: [number, number];
|
|
58
|
+
/** The size of the tile in the ancestor's texture coordinates: `2 ** -levels`. */
|
|
59
|
+
scale: number;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** The fade of one tile, fixed when the tile is first drawn. */
|
|
63
|
+
export type RasterFade = {
|
|
64
|
+
/** The time the tile was first drawn, on the layer's clock. */
|
|
65
|
+
start: number;
|
|
66
|
+
/** The ancestor that the tile cross-fades from, or `null` when it fades in alone. */
|
|
67
|
+
parent: RasterFadeParent | null;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** A tile's position in its pyramid. */
|
|
71
|
+
type TileIndex = {x: number; y: number; z: number};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The part of the image of `ancestor` that covers `index`, as MapLibre GL JS's `parentTopLeft` and
|
|
75
|
+
* `parentScaleBy`.
|
|
76
|
+
*/
|
|
77
|
+
export function getRasterFadeParent(index: TileIndex, ancestor: TileIndex): RasterFadeParent {
|
|
78
|
+
const scale = 2 ** (ancestor.z - index.z);
|
|
79
|
+
return {topLeft: [(index.x * scale) % 1, (index.y * scale) % 1], scale};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The nearest ancestor of a tile that has an image to fade from, at most
|
|
84
|
+
* `MAX_FADING_ANCESTOR_LEVELS` zoom levels above it.
|
|
85
|
+
*/
|
|
86
|
+
export function getFadingAncestor(tile: Tile2DHeader): Tile2DHeader | null {
|
|
87
|
+
const {z} = tile.index as TileIndex;
|
|
88
|
+
for (let ancestor = tile.parent; ancestor; ancestor = ancestor.parent) {
|
|
89
|
+
if (z - (ancestor.index as TileIndex).z > MAX_FADING_ANCESTOR_LEVELS) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
if (ancestor.isLoaded && ancestor.content) {
|
|
93
|
+
return ancestor;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const rasterFadePlugin = createShaderInjectionPlugin({
|
|
100
|
+
name: 'rasterFade',
|
|
101
|
+
fs: /* glsl */ `\
|
|
102
|
+
uniform sampler2D rasterFadeParentTexture;
|
|
103
|
+
|
|
104
|
+
layout(std140) uniform rasterFadeUniforms {
|
|
105
|
+
vec2 parentTopLeft;
|
|
106
|
+
float parentScale;
|
|
107
|
+
float parentMix;
|
|
108
|
+
float opacity;
|
|
109
|
+
} rasterFade;
|
|
110
|
+
`,
|
|
111
|
+
uniformTypes: {
|
|
112
|
+
parentTopLeft: 'vec2<f32>',
|
|
113
|
+
parentScale: 'f32',
|
|
114
|
+
parentMix: 'f32',
|
|
115
|
+
opacity: 'f32'
|
|
116
|
+
},
|
|
117
|
+
inject: {
|
|
118
|
+
// Before other color filters, such as `RasterColorExtension`'s adjustments: MapLibre mixes
|
|
119
|
+
// the two images, then adjusts the mixed color. `BitmapLayer` has drawn the tile's color with
|
|
120
|
+
// the layer's opacity in its alpha.
|
|
121
|
+
'fs:DECKGL_FILTER_COLOR': {
|
|
122
|
+
order: -1,
|
|
123
|
+
injection: /* glsl */ `
|
|
124
|
+
if (rasterFade.parentMix > 0.0) {
|
|
125
|
+
vec4 rasterFade_parent = texture(
|
|
126
|
+
rasterFadeParentTexture,
|
|
127
|
+
rasterFade.parentTopLeft + geometry.uv * rasterFade.parentScale
|
|
128
|
+
);
|
|
129
|
+
color = mix(color, vec4(rasterFade_parent.rgb, rasterFade_parent.a * layer.opacity), rasterFade.parentMix);
|
|
130
|
+
}
|
|
131
|
+
color.a *= rasterFade.opacity;
|
|
132
|
+
`
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
/** The props that `RasterFadeExtension` adds to a `BitmapLayer`. */
|
|
138
|
+
export type RasterFadeExtensionProps = {
|
|
139
|
+
/** `raster-fade-duration`, in milliseconds. */
|
|
140
|
+
rasterFadeDuration?: number;
|
|
141
|
+
/** The clock the fade is measured on. */
|
|
142
|
+
rasterFadeClock?: RasterFadeClock;
|
|
143
|
+
/** The fade of the tile; `null` draws the tile faded in. */
|
|
144
|
+
rasterFade?: RasterFade | null;
|
|
145
|
+
/** The image of the ancestor the tile cross-fades from. */
|
|
146
|
+
rasterFadeParentImage?: unknown;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/** Whether the extension's shader runs on a layer's device. It is written in GLSL only. */
|
|
150
|
+
export function isRasterFadeSupported(layer: Layer): boolean {
|
|
151
|
+
return layer.context.device.type !== 'webgpu';
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Fades a raster tile's `BitmapLayer` in over `raster-fade-duration`, as MapLibre GL JS does:
|
|
156
|
+
* from its ancestor's image (`rasterFadeParentImage`, enlarged) where one was loaded, else from
|
|
157
|
+
* transparent. The fade is read from the clock at each draw, and the layer redraws until it
|
|
158
|
+
* ends. WebGL only; on WebGPU tiles are drawn at once.
|
|
159
|
+
*/
|
|
160
|
+
export class RasterFadeExtension extends LayerExtension {
|
|
161
|
+
static extensionName = 'RasterFadeExtension';
|
|
162
|
+
static defaultProps = {
|
|
163
|
+
rasterFadeDuration: 0,
|
|
164
|
+
rasterFadeClock: {type: 'function', value: defaultRasterFadeClock},
|
|
165
|
+
rasterFade: {type: 'object', value: null},
|
|
166
|
+
// Made a texture with the layer's `textureParameters`, as `BitmapLayer.image` is.
|
|
167
|
+
rasterFadeParentImage: {type: 'image', value: null, async: true}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
getShaders(this: Layer<RasterFadeExtensionProps>) {
|
|
171
|
+
return isRasterFadeSupported(this) ? {plugins: [rasterFadePlugin]} : {};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
draw(this: Layer<RasterFadeExtensionProps & {image?: unknown}>) {
|
|
175
|
+
if (!isRasterFadeSupported(this)) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const {rasterFade, rasterFadeDuration = 0, rasterFadeParentImage, image} = this.props;
|
|
179
|
+
const clock = this.props.rasterFadeClock || defaultRasterFadeClock;
|
|
180
|
+
const parent = rasterFade?.parent && rasterFadeParentImage ? rasterFade.parent : null;
|
|
181
|
+
const values = rasterFade
|
|
182
|
+
? getRasterFadeValues(clock() - rasterFade.start, rasterFadeDuration, Boolean(parent))
|
|
183
|
+
: FADED_IN;
|
|
184
|
+
this.setShaderModuleProps({
|
|
185
|
+
rasterFade: {
|
|
186
|
+
parentTopLeft: parent?.topLeft ?? [0, 0],
|
|
187
|
+
parentScale: parent?.scale ?? 1,
|
|
188
|
+
parentMix: values.mix,
|
|
189
|
+
opacity: values.opacity,
|
|
190
|
+
// Without an ancestor, the tile's own texture is bound, and not sampled.
|
|
191
|
+
rasterFadeParentTexture: (parent ? rasterFadeParentImage : image) as Texture
|
|
192
|
+
}
|
|
193
|
+
} as any);
|
|
194
|
+
const drawnFadedIn = values.opacity === FADED_IN.opacity && values.mix === FADED_IN.mix;
|
|
195
|
+
this.state.rasterFadeDrawnFadedIn = drawnFadedIn;
|
|
196
|
+
if (!drawnFadedIn) {
|
|
197
|
+
this.setNeedsRedraw();
|
|
198
|
+
} else if (parent && rasterFadeDuration > 0) {
|
|
199
|
+
// The cross-fade has ended: the tile layer renders the tile again without the ancestor's
|
|
200
|
+
// image (see `RasterFadeTileLayer.getSubLayerPropsByTile`), which releases its texture.
|
|
201
|
+
this.parent?.setNeedsUpdate();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** The props of a `RasterFadeTileLayer`. */
|
|
207
|
+
export type RasterFadeTileLayerProps = {
|
|
208
|
+
/** `raster-fade-duration`, in milliseconds. 0 draws tiles at once. */
|
|
209
|
+
rasterFadeDuration?: number;
|
|
210
|
+
/** The clock the fade is measured on. Default: `performance.now`. */
|
|
211
|
+
rasterFadeClock?: RasterFadeClock;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/** The sublayer props of one tile's fade. */
|
|
215
|
+
type TileFadeProps = {rasterFade: RasterFade; rasterFadeParentImage: unknown};
|
|
216
|
+
|
|
217
|
+
/** The size of the texture that replaces an ancestor's image when a fade ends. */
|
|
218
|
+
const RELEASED_PARENT_IMAGE_SIZE = 1;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* A `ProjectedTileLayer` of raster tiles that fade in over `rasterFadeDuration` (see
|
|
222
|
+
* `RasterFadeExtension`, which `renderSubLayers` adds to the tiles' layers). A tile's fade starts
|
|
223
|
+
* when it is first drawn, from the nearest ancestor that was loaded then. When the fade ends, the
|
|
224
|
+
* tile lets go of the ancestor's image. The layer is loaded once its selected tiles have been
|
|
225
|
+
* drawn faded in, as MapLibre GL JS is idle only once its raster tiles have.
|
|
226
|
+
*/
|
|
227
|
+
export class RasterFadeTileLayer<DataT = any, ExtraPropsT extends {} = {}> extends ProjectedTileLayer<
|
|
228
|
+
DataT,
|
|
229
|
+
ExtraPropsT & RasterFadeTileLayerProps
|
|
230
|
+
> {
|
|
231
|
+
static layerName = 'RasterFadeTileLayer';
|
|
232
|
+
static defaultProps = {
|
|
233
|
+
...ProjectedTileLayer.defaultProps,
|
|
234
|
+
rasterFadeDuration: 0,
|
|
235
|
+
rasterFadeClock: {type: 'function', value: defaultRasterFadeClock}
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
declare state: ProjectedTileLayer<DataT, ExtraPropsT & RasterFadeTileLayerProps>['state'] & {
|
|
239
|
+
tileFades?: WeakMap<Tile2DHeader, TileFadeProps>;
|
|
240
|
+
releasedParentImage?: Texture;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The image that a tile's layer takes in place of its ancestor's image when the fade ends. The
|
|
245
|
+
* layer then releases the texture it made from the ancestor's image. It cannot take `null`:
|
|
246
|
+
* deck.gl's `image` prop type makes a texture of each new value, and fails on `null`. deck.gl
|
|
247
|
+
* does not delete a texture that a layer receives, so the tile layer owns this one.
|
|
248
|
+
*/
|
|
249
|
+
private getReleasedParentImage(): Texture {
|
|
250
|
+
this.state.releasedParentImage ||= this.context.device.createTexture({
|
|
251
|
+
id: `${this.id}-released-parent-image`,
|
|
252
|
+
width: RELEASED_PARENT_IMAGE_SIZE,
|
|
253
|
+
height: RELEASED_PARENT_IMAGE_SIZE
|
|
254
|
+
});
|
|
255
|
+
return this.state.releasedParentImage;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
finalizeState(): void {
|
|
259
|
+
super.finalizeState();
|
|
260
|
+
this.state?.releasedParentImage?.destroy();
|
|
261
|
+
if (this.state) {
|
|
262
|
+
this.state.releasedParentImage = undefined;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** Whether tiles fade: a positive duration, on a device that runs the fade's shader. */
|
|
267
|
+
private fades(): boolean {
|
|
268
|
+
return this.props.rasterFadeDuration! > 0 && isRasterFadeSupported(this as unknown as Layer);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
getSubLayerPropsByTile(tile: Tile2DHeader): Partial<LayerProps> | null {
|
|
272
|
+
if (!tile.content || !this.fades()) {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
this.state.tileFades ||= new WeakMap();
|
|
276
|
+
let fade = this.state.tileFades.get(tile);
|
|
277
|
+
if (!fade) {
|
|
278
|
+
const ancestor = getFadingAncestor(tile);
|
|
279
|
+
fade = {
|
|
280
|
+
rasterFade: {
|
|
281
|
+
start: this.props.rasterFadeClock!(),
|
|
282
|
+
parent: ancestor
|
|
283
|
+
? getRasterFadeParent(tile.index as TileIndex, ancestor.index as TileIndex)
|
|
284
|
+
: null
|
|
285
|
+
},
|
|
286
|
+
rasterFadeParentImage: ancestor ? ancestor.content : null
|
|
287
|
+
};
|
|
288
|
+
this.state.tileFades.set(tile, fade);
|
|
289
|
+
} else if (
|
|
290
|
+
fade.rasterFade.parent &&
|
|
291
|
+
this.props.rasterFadeClock!() - fade.rasterFade.start >= this.props.rasterFadeDuration!
|
|
292
|
+
) {
|
|
293
|
+
// Keep the fade record so `isLoaded` still checks what the sublayers last drew.
|
|
294
|
+
fade = {
|
|
295
|
+
rasterFade: {start: fade.rasterFade.start, parent: null},
|
|
296
|
+
rasterFadeParentImage: this.getReleasedParentImage()
|
|
297
|
+
};
|
|
298
|
+
this.state.tileFades.set(tile, fade);
|
|
299
|
+
}
|
|
300
|
+
return fade as Partial<LayerProps>;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Whether the selected tiles are loaded and their fades have been drawn to completion. */
|
|
304
|
+
get isLoaded(): boolean {
|
|
305
|
+
if (!super.isLoaded) {
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
if (!this.fades()) {
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
return (this.state.tileset?.selectedTiles || []).every(tile => {
|
|
312
|
+
const layers = tile.layers;
|
|
313
|
+
return (
|
|
314
|
+
!this.state.tileFades?.has(tile) ||
|
|
315
|
+
Boolean(layers?.length && layers.every(layer => layer.state?.rasterFadeDrawnFadedIn === true))
|
|
316
|
+
);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// tangram-layers
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {RejectRasterTile} from './raster-tile-loader';
|
|
6
|
+
|
|
7
|
+
/** The SHA-256 digest of the bytes of a raster tile, in hexadecimal. */
|
|
8
|
+
export async function getRasterTileFingerprint(arrayBuffer: ArrayBuffer): Promise<string> {
|
|
9
|
+
const digest = await crypto.subtle.digest('SHA-256', arrayBuffer);
|
|
10
|
+
return Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, '0')).join('');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a `rejectRasterTile` function that matches the tiles whose bytes have one of
|
|
15
|
+
* `fingerprints` (see {@link getRasterTileFingerprint}): the placeholder images of a tile service,
|
|
16
|
+
* which are the same bytes for every tile without data. `fingerprints` can be a promise, for
|
|
17
|
+
* fingerprints that are measured at run time from a tile that is known to have no data. A promise
|
|
18
|
+
* that rejects matches no tile.
|
|
19
|
+
*/
|
|
20
|
+
export function createRasterTileFingerprintFilter(
|
|
21
|
+
fingerprints: Iterable<string> | Promise<Iterable<string>>
|
|
22
|
+
): RejectRasterTile {
|
|
23
|
+
const known = Promise.resolve(fingerprints).then(
|
|
24
|
+
values => new Set(values),
|
|
25
|
+
() => new Set<string>()
|
|
26
|
+
);
|
|
27
|
+
return async ({arrayBuffer}) => {
|
|
28
|
+
const set = await known;
|
|
29
|
+
return set.has(await getRasterTileFingerprint(arrayBuffer));
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// tangram-layers
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {_getURLFromTemplate as getURLFromTemplate} from '@deck.gl/geo-layers';
|
|
6
|
+
import type {_TileLoadProps as TileLoadProps} from '@deck.gl/geo-layers';
|
|
7
|
+
import {parse} from '@loaders.gl/core';
|
|
8
|
+
import {getBasemapResourceFetch} from './basemap-request';
|
|
9
|
+
import type {BasemapLoadOptions, BasemapSource} from './style-resolver';
|
|
10
|
+
import {BasemapFetchError} from './basemap-error';
|
|
11
|
+
import {getSourceTileTemplates} from './source-tiles';
|
|
12
|
+
|
|
13
|
+
/** The position of a tile in the tile pyramid. */
|
|
14
|
+
export type RasterTileIndex = {x: number; y: number; z: number};
|
|
15
|
+
|
|
16
|
+
/** A raster tile as the tile service returned it, before it is decoded. */
|
|
17
|
+
export type RasterTileResponse = {
|
|
18
|
+
/** The URL the tile was requested from. */
|
|
19
|
+
url: string;
|
|
20
|
+
/** The id of the style source of the tile. */
|
|
21
|
+
sourceId: string;
|
|
22
|
+
/** The position of the tile in the tile pyramid. */
|
|
23
|
+
index: RasterTileIndex;
|
|
24
|
+
/** The bytes of the response body. */
|
|
25
|
+
arrayBuffer: ArrayBuffer;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns `true` for a raster tile that has no map data, such as the placeholder image that some
|
|
30
|
+
* tile services return with status 200 where they have no coverage.
|
|
31
|
+
*/
|
|
32
|
+
export type RejectRasterTile = (tile: RasterTileResponse) => boolean | Promise<boolean>;
|
|
33
|
+
|
|
34
|
+
/** A decoded tile image, as deck.gl draws it. */
|
|
35
|
+
type TileImage = ImageBitmap | HTMLImageElement | HTMLCanvasElement | OffscreenCanvas;
|
|
36
|
+
|
|
37
|
+
/** The part of an image to draw, in pixels. */
|
|
38
|
+
export type ImageCrop = {x: number; y: number; width: number; height: number};
|
|
39
|
+
|
|
40
|
+
/** Options of a {@link RasterTileLoader}. */
|
|
41
|
+
export type RasterTileLoaderOptions = {
|
|
42
|
+
/** The tile URL templates of the source. */
|
|
43
|
+
tiles: string[];
|
|
44
|
+
/** The id of the style source. */
|
|
45
|
+
sourceId: string;
|
|
46
|
+
/** The lowest zoom the source has tiles at. */
|
|
47
|
+
minZoom?: number;
|
|
48
|
+
/** Tiles for which this returns `true` have no data. */
|
|
49
|
+
rejectTile: RejectRasterTile;
|
|
50
|
+
/** The `fetch`, `fetchOptions` and `transformRequest` that tiles are requested with. */
|
|
51
|
+
loadOptions?: BasemapLoadOptions;
|
|
52
|
+
/** Decodes the bytes of a tile. Default: the image loader that deck.gl registers. */
|
|
53
|
+
decodeImage?: (arrayBuffer: ArrayBuffer) => Promise<TileImage>;
|
|
54
|
+
/** Cuts a part out of a decoded tile. Default: `createImageBitmap`. */
|
|
55
|
+
cropImage?: (image: TileImage, crop: ImageCrop) => Promise<TileImage>;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/** The most ancestor tiles kept decoded for the tiles that draw a part of them. */
|
|
59
|
+
const MAX_CACHED_ANCESTORS = 32;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The part of an ancestor tile's image that covers the tile `index`, `levels` zooms below it. The
|
|
63
|
+
* part is one pixel at least: a tile far below its ancestor is drawn in the color of that pixel.
|
|
64
|
+
*/
|
|
65
|
+
export function getAncestorCrop(
|
|
66
|
+
index: RasterTileIndex,
|
|
67
|
+
levels: number,
|
|
68
|
+
width: number,
|
|
69
|
+
height: number
|
|
70
|
+
): ImageCrop {
|
|
71
|
+
const scale = 2 ** levels;
|
|
72
|
+
const cropWidth = Math.max(1, Math.floor(width / scale));
|
|
73
|
+
const cropHeight = Math.max(1, Math.floor(height / scale));
|
|
74
|
+
return {
|
|
75
|
+
x: Math.floor(((index.x % scale) * width) / scale),
|
|
76
|
+
y: Math.floor(((index.y % scale) * height) / scale),
|
|
77
|
+
width: cropWidth,
|
|
78
|
+
height: cropHeight
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The ancestor of the tile `index`, `levels` zooms above it. */
|
|
83
|
+
export function getAncestorIndex(index: RasterTileIndex, levels: number): RasterTileIndex {
|
|
84
|
+
const scale = 2 ** levels;
|
|
85
|
+
return {x: Math.floor(index.x / scale), y: Math.floor(index.y / scale), z: index.z - levels};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Decodes a tile as deck.gl decodes the tiles it loads: with the image loader it registers, and
|
|
90
|
+
* with the `loadOptions` of the layer, so that decoder options such as
|
|
91
|
+
* `imagebitmap.colorSpaceConversion` apply as they do to the tiles that deck.gl loads.
|
|
92
|
+
*/
|
|
93
|
+
function decodeTileImage(
|
|
94
|
+
arrayBuffer: ArrayBuffer,
|
|
95
|
+
loadOptions?: BasemapLoadOptions
|
|
96
|
+
): Promise<TileImage> {
|
|
97
|
+
return parse(arrayBuffer, loadOptions || undefined) as Promise<TileImage>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Cuts a part out of a tile, with the colors as stored, as deck.gl decodes its images. */
|
|
101
|
+
function cropTileImage(image: TileImage, crop: ImageCrop): Promise<TileImage> {
|
|
102
|
+
return createImageBitmap(image, crop.x, crop.y, crop.width, crop.height, {
|
|
103
|
+
premultiplyAlpha: 'none'
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Loads the tiles of a raster source that returns an image where it has no data. A tile that
|
|
109
|
+
* `rejectTile` matches is drawn from the nearest ancestor tile that has data, enlarged, as
|
|
110
|
+
* MapLibre GL JS draws a tile that failed to load.
|
|
111
|
+
*/
|
|
112
|
+
export class RasterTileLoader {
|
|
113
|
+
/** Decoded ancestor tiles, or `null` for those without data, most recently used last. */
|
|
114
|
+
private ancestors = new Map<string, Promise<TileImage | null>>();
|
|
115
|
+
|
|
116
|
+
constructor(readonly options: RasterTileLoaderOptions) {}
|
|
117
|
+
|
|
118
|
+
/** The `getTileData` of the source's `TileLayer`. */
|
|
119
|
+
getTileData = async (tile: TileLoadProps): Promise<TileImage | null> => {
|
|
120
|
+
const index = tile.index as RasterTileIndex;
|
|
121
|
+
const image = await this.loadTile(index, tile.signal);
|
|
122
|
+
if (image) {
|
|
123
|
+
return image;
|
|
124
|
+
}
|
|
125
|
+
const minZoom = this.options.minZoom ?? 0;
|
|
126
|
+
for (let levels = 1; index.z - levels >= minZoom; levels++) {
|
|
127
|
+
// An ancestor that fails to load has no data to draw: the next one up is tried.
|
|
128
|
+
const ancestor = await this.loadAncestor(getAncestorIndex(index, levels)).catch(() => null);
|
|
129
|
+
if (tile.signal?.aborted) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
if (ancestor) {
|
|
133
|
+
const crop = getAncestorCrop(index, levels, ancestor.width, ancestor.height);
|
|
134
|
+
return (this.options.cropImage || cropTileImage)(ancestor, crop);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/** The decoded tile, or `null` when it has no data. */
|
|
141
|
+
private async loadTile(index: RasterTileIndex, signal?: AbortSignal): Promise<TileImage | null> {
|
|
142
|
+
const {tiles, sourceId, rejectTile, loadOptions, decodeImage} = this.options;
|
|
143
|
+
const url = getURLFromTemplate(tiles, {index, id: `${index.x}-${index.y}-${index.z}`});
|
|
144
|
+
if (!url) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
const fetchFn = getBasemapResourceFetch('Tile', 'image', loadOptions || undefined);
|
|
148
|
+
const response = await fetchFn(url, {...loadOptions?.fetchOptions, signal});
|
|
149
|
+
if (!response.ok) {
|
|
150
|
+
throw new BasemapFetchError(
|
|
151
|
+
`Failed to load raster tile: ${url} (${response.status})`,
|
|
152
|
+
url,
|
|
153
|
+
response.status
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
157
|
+
if (arrayBuffer.byteLength === 0 || (await rejectTile({url, sourceId, index, arrayBuffer}))) {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
return decodeImage ? decodeImage(arrayBuffer) : decodeTileImage(arrayBuffer, loadOptions);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The decoded ancestor tile, loaded once for all of the tiles that draw a part of it. */
|
|
164
|
+
private loadAncestor(index: RasterTileIndex): Promise<TileImage | null> {
|
|
165
|
+
const key = `${index.z}/${index.x}/${index.y}`;
|
|
166
|
+
let ancestor = this.ancestors.get(key);
|
|
167
|
+
this.ancestors.delete(key);
|
|
168
|
+
if (!ancestor) {
|
|
169
|
+
ancestor = this.loadTile(index);
|
|
170
|
+
// A failed request is not kept: the next tile that needs the ancestor requests it again.
|
|
171
|
+
ancestor.catch(() => {
|
|
172
|
+
if (this.ancestors.get(key) === ancestor) {
|
|
173
|
+
this.ancestors.delete(key);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
this.ancestors.set(key, ancestor);
|
|
178
|
+
if (this.ancestors.size > MAX_CACHED_ANCESTORS) {
|
|
179
|
+
const [leastRecentlyUsed] = this.ancestors.keys();
|
|
180
|
+
this.ancestors.delete(leastRecentlyUsed);
|
|
181
|
+
}
|
|
182
|
+
return ancestor;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const rasterTileLoaders = new WeakMap<BasemapSource, RasterTileLoader>();
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The loader of a raster source's tiles. It is kept while the source's `tiles` and the options stay
|
|
190
|
+
* the same, so that the ancestor tiles it decoded are shared by every render.
|
|
191
|
+
*/
|
|
192
|
+
export function getRasterTileLoader(
|
|
193
|
+
source: BasemapSource,
|
|
194
|
+
options: Omit<RasterTileLoaderOptions, 'tiles'>
|
|
195
|
+
): RasterTileLoader {
|
|
196
|
+
let loader = rasterTileLoaders.get(source);
|
|
197
|
+
if (
|
|
198
|
+
!loader ||
|
|
199
|
+
loader.options.tiles !== getSourceTileTemplates(source) ||
|
|
200
|
+
loader.options.rejectTile !== options.rejectTile ||
|
|
201
|
+
loader.options.loadOptions !== options.loadOptions ||
|
|
202
|
+
loader.options.sourceId !== options.sourceId ||
|
|
203
|
+
loader.options.minZoom !== options.minZoom ||
|
|
204
|
+
loader.options.decodeImage !== options.decodeImage ||
|
|
205
|
+
loader.options.cropImage !== options.cropImage
|
|
206
|
+
) {
|
|
207
|
+
loader = new RasterTileLoader({...options, tiles: getSourceTileTemplates(source) as string[]});
|
|
208
|
+
rasterTileLoaders.set(source, loader);
|
|
209
|
+
}
|
|
210
|
+
return loader;
|
|
211
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// tangram-layers
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {ShaderInjection, ShaderModule, ShaderPlugin} from '@luma.gl/shaders';
|
|
6
|
+
|
|
7
|
+
/** Separates a GLSL extension's ordered hook contributions from its uniform module. */
|
|
8
|
+
export function createShaderInjectionPlugin(
|
|
9
|
+
definition: ShaderModule & {inject: Record<string, string | {injection: string; order?: number}>}
|
|
10
|
+
): ShaderPlugin {
|
|
11
|
+
const {inject, ...module} = definition;
|
|
12
|
+
return {
|
|
13
|
+
name: module.name,
|
|
14
|
+
module,
|
|
15
|
+
injections: Object.entries(inject).map(([key, value]): ShaderInjection => {
|
|
16
|
+
const separator = key.indexOf(':');
|
|
17
|
+
return {
|
|
18
|
+
stage: key.startsWith('vs:') ? 'vertex' : 'fragment',
|
|
19
|
+
target: key.slice(separator + 1),
|
|
20
|
+
glsl: typeof value === 'string' ? value : value.injection,
|
|
21
|
+
order: typeof value === 'string' ? undefined : value.order
|
|
22
|
+
};
|
|
23
|
+
})
|
|
24
|
+
};
|
|
25
|
+
}
|