@tangram.gl/basemap-layers 0.0.1 → 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
package/src/hillshade-layer.ts
CHANGED
|
@@ -71,12 +71,17 @@
|
|
|
71
71
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
72
72
|
*/
|
|
73
73
|
|
|
74
|
-
import {COORDINATE_SYSTEM, log} from '@deck.gl/core';
|
|
75
74
|
import type {Layer, LayersList, UpdateParameters} from '@deck.gl/core';
|
|
76
|
-
import {SharedTile2DLayer, SharedTileset2D} from '@deck.gl-community/geo-layers';
|
|
77
|
-
import type {SharedTile2DLayerProps, SharedTileset2DProps} from '@deck.gl-community/geo-layers';
|
|
78
|
-
import {
|
|
75
|
+
import {SharedTile2DLayer, SharedTileset2D} from '@deck.gl-community/layers/geo-layers';
|
|
76
|
+
import type {SharedTile2DLayerProps, SharedTileset2DProps} from '@deck.gl-community/layers/geo-layers';
|
|
77
|
+
import {ProjectedBitmapLayer} from './projected-bitmap-layer';
|
|
78
|
+
import {basemapTilesetAdapter} from './projected-tileset';
|
|
79
|
+
import {getSourceTileTemplates} from './source-tiles';
|
|
80
|
+
import {isProjectedViewport} from './projected-viewport';
|
|
81
|
+
import {getBasemapResourceFetch} from './basemap-request';
|
|
79
82
|
import type {BasemapLoadOptions, BasemapSource} from './style-resolver';
|
|
83
|
+
import {BasemapFetchError} from './basemap-error';
|
|
84
|
+
import {createDemImageBitmap, readDemPixels} from './dem-pixels';
|
|
80
85
|
|
|
81
86
|
/**
|
|
82
87
|
* `hillshade` style layers over `raster-dem` sources, drawn as MapLibre GL JS draws them.
|
|
@@ -147,19 +152,14 @@ export function decodeDemTile(
|
|
|
147
152
|
export type DemNeighbors = (dx: number, dy: number) => DemTile | null;
|
|
148
153
|
|
|
149
154
|
/**
|
|
150
|
-
* The
|
|
151
|
-
* tile
|
|
152
|
-
*
|
|
153
|
-
* neighbor is missing, from the tile's nearest edge pixel, as MapLibre fills them.
|
|
154
|
-
*
|
|
155
|
-
* `zoom` is the tile's zoom, also where it is scaled up past its source's `maxzoom`. MapLibre
|
|
156
|
-
* scales the slopes by it, and exaggerates them below zoom 15.
|
|
155
|
+
* The elevation at a pixel `(x, y)` of a DEM tile or of a border around it, up to one tile wide.
|
|
156
|
+
* Pixels past the tile's edges come from its loaded neighbors (of the same size), and where a
|
|
157
|
+
* neighbor is missing, from the tile's nearest edge pixel, as MapLibre GL JS backfills them.
|
|
157
158
|
*/
|
|
158
|
-
export function
|
|
159
|
+
export function getBorderedElevations(
|
|
159
160
|
dem: DemTile,
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
): Uint8ClampedArray<ArrayBuffer> {
|
|
161
|
+
getNeighbor: DemNeighbors
|
|
162
|
+
): (x: number, y: number) => number {
|
|
163
163
|
const {dim} = dem;
|
|
164
164
|
const neighbors: (DemTile | null)[] = [];
|
|
165
165
|
for (let dy = -1; dy <= 1; dy++) {
|
|
@@ -168,8 +168,7 @@ export function prepareHillshadeTexture(
|
|
|
168
168
|
neighbors.push(neighbor && neighbor.dim === dim ? neighbor : null);
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
|
-
|
|
172
|
-
const getElevation = (x: number, y: number): number => {
|
|
171
|
+
return (x, y) => {
|
|
173
172
|
const dx = x < 0 ? -1 : x >= dim ? 1 : 0;
|
|
174
173
|
const dy = y < 0 ? -1 : y >= dim ? 1 : 0;
|
|
175
174
|
const neighbor = neighbors[(dy + 1) * 3 + dx + 1];
|
|
@@ -180,10 +179,32 @@ export function prepareHillshadeTexture(
|
|
|
180
179
|
const clampedY = Math.min(Math.max(y, 0), dim - 1);
|
|
181
180
|
return dem.elevations[clampedY * dim + clampedX];
|
|
182
181
|
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The slope texture of MapLibre GL JS's hillshade "prepare" pass, `dim + 2` pixels square: the
|
|
186
|
+
* tile and a one-pixel border, RGBA, with the scaled east-west and north-south slopes in red and
|
|
187
|
+
* green (128 is flat). Pixels past the tile's edges come from its loaded neighbors, and where a
|
|
188
|
+
* neighbor is missing, from the tile's nearest edge pixel, as MapLibre fills them.
|
|
189
|
+
*
|
|
190
|
+
* `zoom` is the tile's zoom, also where it is scaled up past its source's `maxzoom`. MapLibre
|
|
191
|
+
* scales the slopes by it, and exaggerates them below zoom 15. `bodyRadius` is the radius, in
|
|
192
|
+
* meters, of the body the tile covers (see `getBodyRadius`): a tile pixel spans less ground on a
|
|
193
|
+
* smaller body, so the same elevations make steeper slopes. The default is the Earth's, as MapLibre.
|
|
194
|
+
*/
|
|
195
|
+
export function prepareHillshadeTexture(
|
|
196
|
+
dem: DemTile,
|
|
197
|
+
zoom: number,
|
|
198
|
+
getNeighbor: DemNeighbors = () => null,
|
|
199
|
+
bodyRadius: number = EARTH_RADIUS
|
|
200
|
+
): Uint8ClampedArray<ArrayBuffer> {
|
|
201
|
+
const {dim} = dem;
|
|
202
|
+
const getElevation = getBorderedElevations(dem, getNeighbor);
|
|
183
203
|
|
|
184
204
|
const exaggerationFactor = zoom < 2 ? 0.4 : zoom < 4.5 ? 0.35 : 0.3;
|
|
185
205
|
const exaggeration = zoom < 15 ? (zoom - 15) * exaggerationFactor : 0;
|
|
186
|
-
|
|
206
|
+
// 28.2562 is 3 plus log2 of the Earth's circumference in meters, the ground a zoom 0 tile spans.
|
|
207
|
+
const scale = ((dim / 2 ** (exaggeration + (28.2562 - zoom)) / 8) * EARTH_RADIUS) / bodyRadius;
|
|
187
208
|
const flat = 128 / 255;
|
|
188
209
|
|
|
189
210
|
const size = dim + 2;
|
|
@@ -216,6 +237,20 @@ export function prepareHillshadeTexture(
|
|
|
216
237
|
return texture;
|
|
217
238
|
}
|
|
218
239
|
|
|
240
|
+
/** The radius, in meters, of the Earth that MapLibre's slope scale assumes. */
|
|
241
|
+
const EARTH_RADIUS = 6378137;
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The radius, in meters, of the body a viewport draws: in a projected view the semi-major axis of
|
|
245
|
+
* its CRS, which can be the Moon or Mars, and otherwise the Earth's.
|
|
246
|
+
*/
|
|
247
|
+
export function getBodyRadius(viewport: unknown): number {
|
|
248
|
+
const radius = isProjectedViewport(viewport)
|
|
249
|
+
? (viewport as {crs?: {ellipsoid?: {semiMajorAxis?: number}}}).crs?.ellipsoid?.semiMajorAxis
|
|
250
|
+
: undefined;
|
|
251
|
+
return typeof radius === 'number' && radius > 0 ? radius : EARTH_RADIUS;
|
|
252
|
+
}
|
|
253
|
+
|
|
219
254
|
/** `hillshade-method`, as the shader's `method` uniform. */
|
|
220
255
|
const HILLSHADE_METHODS: Record<string, number> = {
|
|
221
256
|
standard: 0,
|
|
@@ -475,6 +510,147 @@ vec4 hillshade_getColor(sampler2D slopes, vec2 uv) {
|
|
|
475
510
|
}
|
|
476
511
|
return hillshade_standard(deriv);
|
|
477
512
|
}
|
|
513
|
+
`,
|
|
514
|
+
source: /* wgsl */ `\
|
|
515
|
+
struct HillshadeUniforms {
|
|
516
|
+
accent: vec4<f32>,
|
|
517
|
+
altitudes: vec4<f32>,
|
|
518
|
+
azimuths: vec4<f32>,
|
|
519
|
+
shadows: mat4x4<f32>,
|
|
520
|
+
highlights: mat4x4<f32>,
|
|
521
|
+
latRange: vec2<f32>,
|
|
522
|
+
exaggeration: f32,
|
|
523
|
+
method: f32,
|
|
524
|
+
lightCount: f32,
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
@group(0) @binding(auto) var<uniform> hillshade: HillshadeUniforms;
|
|
528
|
+
|
|
529
|
+
// The GLSL functions above, in WGSL.
|
|
530
|
+
const HILLSHADE_PI: f32 = 3.141592653589793;
|
|
531
|
+
|
|
532
|
+
// GLSL's \`mod\`, which rounds down. WGSL's \`%\` rounds toward zero.
|
|
533
|
+
fn hillshade_mod(x: f32, y: f32) -> f32 {
|
|
534
|
+
return x - y * floor(x / y);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
fn hillshade_getAspect(deriv: vec2<f32>) -> f32 {
|
|
538
|
+
if (deriv.x != 0.0) {
|
|
539
|
+
return atan2(deriv.y, -deriv.x);
|
|
540
|
+
}
|
|
541
|
+
return HILLSHADE_PI / 2.0 * select(-1.0, 1.0, deriv.y > 0.0);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
fn hillshade_igor(derivIn: vec2<f32>) -> vec4<f32> {
|
|
545
|
+
let deriv = derivIn * hillshade.exaggeration * 2.0;
|
|
546
|
+
let aspect = hillshade_getAspect(deriv);
|
|
547
|
+
let azimuth = hillshade.azimuths[0] + HILLSHADE_PI;
|
|
548
|
+
let slopeStrength = atan(length(deriv)) * 2.0 / HILLSHADE_PI;
|
|
549
|
+
let aspectStrength = 1.0 - abs(hillshade_mod((aspect + azimuth) / HILLSHADE_PI + 0.5, 2.0) - 1.0);
|
|
550
|
+
let shadowStrength = slopeStrength * aspectStrength;
|
|
551
|
+
let highlightStrength = slopeStrength * (1.0 - aspectStrength);
|
|
552
|
+
return hillshade.shadows[0] * shadowStrength + hillshade.highlights[0] * highlightStrength;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
fn hillshade_standard(deriv: vec2<f32>) -> vec4<f32> {
|
|
556
|
+
let azimuth = hillshade.azimuths[0] + HILLSHADE_PI;
|
|
557
|
+
let slope = atan(0.625 * length(deriv));
|
|
558
|
+
let aspect = hillshade_getAspect(deriv);
|
|
559
|
+
let intensity = hillshade.exaggeration;
|
|
560
|
+
let base = 1.875 - intensity * 1.75;
|
|
561
|
+
let maxValue = 0.5 * HILLSHADE_PI;
|
|
562
|
+
var scaledSlope = slope;
|
|
563
|
+
if (intensity != 0.5) {
|
|
564
|
+
scaledSlope = ((pow(base, slope) - 1.0) / (pow(base, maxValue) - 1.0)) * maxValue;
|
|
565
|
+
}
|
|
566
|
+
let accent = cos(scaledSlope);
|
|
567
|
+
let accentColor = (1.0 - accent) * hillshade.accent * clamp(intensity * 2.0, 0.0, 1.0);
|
|
568
|
+
let shade = abs(hillshade_mod((aspect + azimuth) / HILLSHADE_PI + 0.5, 2.0) - 1.0);
|
|
569
|
+
let shadeColor = mix(hillshade.shadows[0], hillshade.highlights[0], shade) *
|
|
570
|
+
sin(scaledSlope) * clamp(intensity * 2.0, 0.0, 1.0);
|
|
571
|
+
return accentColor * (1.0 - shadeColor.a) + shadeColor;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
fn hillshade_basic(derivIn: vec2<f32>) -> vec4<f32> {
|
|
575
|
+
let deriv = derivIn * hillshade.exaggeration * 2.0;
|
|
576
|
+
let azimuth = hillshade.azimuths[0] + HILLSHADE_PI;
|
|
577
|
+
let cosAz = cos(azimuth);
|
|
578
|
+
let sinAz = sin(azimuth);
|
|
579
|
+
let cosAlt = cos(hillshade.altitudes[0]);
|
|
580
|
+
let sinAlt = sin(hillshade.altitudes[0]);
|
|
581
|
+
let cang = (sinAlt - (deriv.y * cosAz * cosAlt - deriv.x * sinAz * cosAlt)) /
|
|
582
|
+
sqrt(1.0 + dot(deriv, deriv));
|
|
583
|
+
let shade = clamp(cang, 0.0, 1.0);
|
|
584
|
+
if (shade > 0.5) {
|
|
585
|
+
return hillshade.highlights[0] * (2.0 * shade - 1.0);
|
|
586
|
+
}
|
|
587
|
+
return hillshade.shadows[0] * (1.0 - 2.0 * shade);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
fn hillshade_multidirectional(derivIn: vec2<f32>) -> vec4<f32> {
|
|
591
|
+
let deriv = derivIn * hillshade.exaggeration * 2.0;
|
|
592
|
+
var color = vec4<f32>(0.0);
|
|
593
|
+
for (var i = 0; i < 4; i++) {
|
|
594
|
+
if (f32(i) >= hillshade.lightCount) {
|
|
595
|
+
break;
|
|
596
|
+
}
|
|
597
|
+
let cosAlt = cos(hillshade.altitudes[i]);
|
|
598
|
+
let sinAlt = sin(hillshade.altitudes[i]);
|
|
599
|
+
let cosAz = -cos(hillshade.azimuths[i]);
|
|
600
|
+
let sinAz = -sin(hillshade.azimuths[i]);
|
|
601
|
+
let cang = (sinAlt - (deriv.y * cosAz * cosAlt - deriv.x * sinAz * cosAlt)) /
|
|
602
|
+
sqrt(1.0 + dot(deriv, deriv));
|
|
603
|
+
let shade = clamp(cang, 0.0, 1.0);
|
|
604
|
+
var lightColor = hillshade.shadows[i] * (1.0 - 2.0 * shade);
|
|
605
|
+
if (shade > 0.5) {
|
|
606
|
+
lightColor = hillshade.highlights[i] * (2.0 * shade - 1.0);
|
|
607
|
+
}
|
|
608
|
+
color += lightColor / hillshade.lightCount;
|
|
609
|
+
}
|
|
610
|
+
return color;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
fn hillshade_combined(derivIn: vec2<f32>) -> vec4<f32> {
|
|
614
|
+
let deriv = derivIn * hillshade.exaggeration * 2.0;
|
|
615
|
+
let azimuth = hillshade.azimuths[0] + HILLSHADE_PI;
|
|
616
|
+
let cosAz = cos(azimuth);
|
|
617
|
+
let sinAz = sin(azimuth);
|
|
618
|
+
let cosAlt = cos(hillshade.altitudes[0]);
|
|
619
|
+
let sinAlt = sin(hillshade.altitudes[0]);
|
|
620
|
+
let cang = clamp(
|
|
621
|
+
acos((sinAlt - (deriv.y * cosAz * cosAlt - deriv.x * sinAz * cosAlt)) /
|
|
622
|
+
sqrt(1.0 + dot(deriv, deriv))),
|
|
623
|
+
0.0,
|
|
624
|
+
HILLSHADE_PI / 2.0
|
|
625
|
+
);
|
|
626
|
+
let shade = cang * atan(length(deriv)) * 4.0 / HILLSHADE_PI / HILLSHADE_PI;
|
|
627
|
+
let highlight = (HILLSHADE_PI / 2.0 - cang) * atan(length(deriv)) * 4.0 / HILLSHADE_PI / HILLSHADE_PI;
|
|
628
|
+
return hillshade.shadows[0] * shade + hillshade.highlights[0] * highlight;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// The premultiplied color of a pixel of the slope texture, at \`uv\` in the tile. The slopes are
|
|
632
|
+
// sampled from the first mip level, as MapLibre samples them.
|
|
633
|
+
fn hillshade_getColor(slopes: texture_2d<f32>, slopesSampler: sampler, uv: vec2<f32>) -> vec4<f32> {
|
|
634
|
+
let size = vec2<f32>(textureDimensions(slopes, 0));
|
|
635
|
+
let pixel = textureSampleLevel(slopes, slopesSampler, (uv * (size - 2.0) + 1.0) / size, 0.0);
|
|
636
|
+
let scaleFactor = cos(radians(
|
|
637
|
+
(hillshade.latRange[0] - hillshade.latRange[1]) * (1.0 - uv.y) + hillshade.latRange[1]
|
|
638
|
+
));
|
|
639
|
+
let deriv = (pixel.rg - 128.0 / 255.0) * 8.0 / scaleFactor;
|
|
640
|
+
if (hillshade.method > 3.5) {
|
|
641
|
+
return hillshade_basic(deriv);
|
|
642
|
+
}
|
|
643
|
+
if (hillshade.method > 2.5) {
|
|
644
|
+
return hillshade_multidirectional(deriv);
|
|
645
|
+
}
|
|
646
|
+
if (hillshade.method > 1.5) {
|
|
647
|
+
return hillshade_igor(deriv);
|
|
648
|
+
}
|
|
649
|
+
if (hillshade.method > 0.5) {
|
|
650
|
+
return hillshade_combined(deriv);
|
|
651
|
+
}
|
|
652
|
+
return hillshade_standard(deriv);
|
|
653
|
+
}
|
|
478
654
|
`,
|
|
479
655
|
uniformTypes: {
|
|
480
656
|
accent: 'vec4<f32>',
|
|
@@ -493,7 +669,7 @@ vec4 hillshade_getColor(sampler2D slopes, vec2 uv) {
|
|
|
493
669
|
* The blending of the hillshade's premultiplied colors, as MapLibre blends them (and as
|
|
494
670
|
* `RASTER_COLOR_BLEND_PARAMETERS`).
|
|
495
671
|
*/
|
|
496
|
-
const PREMULTIPLIED_BLEND_PARAMETERS = {
|
|
672
|
+
export const PREMULTIPLIED_BLEND_PARAMETERS = {
|
|
497
673
|
blendColorOperation: 'add',
|
|
498
674
|
blendColorSrcFactor: 'one',
|
|
499
675
|
blendColorDstFactor: 'one-minus-src-alpha',
|
|
@@ -508,11 +684,34 @@ type HillshadeBitmapLayerProps = {
|
|
|
508
684
|
hillshadeLatRange: [number, number];
|
|
509
685
|
};
|
|
510
686
|
|
|
687
|
+
const GLSL_INJECTION = {
|
|
688
|
+
// At the end of main(), where the bitmap's sampler is in scope.
|
|
689
|
+
'fs:#main-end': /* glsl */ `
|
|
690
|
+
if (!bool(picking.isActive)) {
|
|
691
|
+
fragColor = hillshade_getColor(bitmapTexture, geometry.uv) * layer.opacity;
|
|
692
|
+
}
|
|
693
|
+
`
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
const WGSL_INJECTION = {
|
|
697
|
+
// deck.gl's WGSL bitmap shader has no hook after its color, but its textures are module-scope.
|
|
698
|
+
// It premultiplies the filtered color itself, so the premultiplied hillshade is divided by its
|
|
699
|
+
// alpha here. In picking, the shader returns its own color and ignores this one.
|
|
700
|
+
'fs:DECKGL_FILTER_COLOR': /* wgsl */ `
|
|
701
|
+
let hillshadeColor = hillshade_getColor(bitmapTexture, bitmapTextureSampler, geometry.uv) *
|
|
702
|
+
layer.opacity;
|
|
703
|
+
*color = vec4<f32>(
|
|
704
|
+
select(vec3<f32>(0.0), hillshadeColor.rgb / hillshadeColor.a, hillshadeColor.a > 0.0),
|
|
705
|
+
hillshadeColor.a
|
|
706
|
+
);
|
|
707
|
+
`
|
|
708
|
+
};
|
|
709
|
+
|
|
511
710
|
/**
|
|
512
711
|
* A `BitmapLayer` whose image is a tile's slope texture (see `prepareHillshadeTexture`), drawn as
|
|
513
|
-
* MapLibre GL JS shades it
|
|
712
|
+
* MapLibre GL JS shades it, on WebGL and on WebGPU.
|
|
514
713
|
*/
|
|
515
|
-
class HillshadeBitmapLayer extends
|
|
714
|
+
class HillshadeBitmapLayer extends ProjectedBitmapLayer<HillshadeBitmapLayerProps> {
|
|
516
715
|
static layerName = 'HillshadeBitmapLayer';
|
|
517
716
|
|
|
518
717
|
getShaders() {
|
|
@@ -522,12 +721,7 @@ class HillshadeBitmapLayer extends BitmapLayer<HillshadeBitmapLayerProps> {
|
|
|
522
721
|
modules: [...shaders.modules, hillshadeModule],
|
|
523
722
|
inject: {
|
|
524
723
|
...shaders.inject,
|
|
525
|
-
|
|
526
|
-
'fs:#main-end': /* glsl */ `
|
|
527
|
-
if (!bool(picking.isActive)) {
|
|
528
|
-
fragColor = hillshade_getColor(bitmapTexture, geometry.uv) * layer.opacity;
|
|
529
|
-
}
|
|
530
|
-
`
|
|
724
|
+
...(this.context.device.type === 'webgpu' ? WGSL_INJECTION : GLSL_INJECTION)
|
|
531
725
|
}
|
|
532
726
|
};
|
|
533
727
|
}
|
|
@@ -544,54 +738,43 @@ class HillshadeBitmapLayer extends BitmapLayer<HillshadeBitmapLayerProps> {
|
|
|
544
738
|
}
|
|
545
739
|
}
|
|
546
740
|
|
|
547
|
-
/** Reads the pixels of a decoded image. */
|
|
548
|
-
function readImagePixels(image: ImageBitmap): Uint8ClampedArray {
|
|
549
|
-
const {width, height} = image;
|
|
550
|
-
const canvas =
|
|
551
|
-
typeof OffscreenCanvas === 'undefined'
|
|
552
|
-
? Object.assign(document.createElement('canvas'), {width, height})
|
|
553
|
-
: new OffscreenCanvas(width, height);
|
|
554
|
-
const context = canvas.getContext('2d', {willReadFrequently: true}) as
|
|
555
|
-
| CanvasRenderingContext2D
|
|
556
|
-
| OffscreenCanvasRenderingContext2D;
|
|
557
|
-
context.drawImage(image, 0, 0);
|
|
558
|
-
return context.getImageData(0, 0, width, height).data;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
741
|
/**
|
|
562
742
|
* Fetches and decodes a DEM tile. Colors are read as stored, without color space conversion or
|
|
563
|
-
* premultiplication, which would change the elevations
|
|
743
|
+
* premultiplication, which would change the elevations, and without the noise that some browsers
|
|
744
|
+
* add to canvas reads (see `readDemPixels`). An empty response is a tile without data.
|
|
564
745
|
*/
|
|
565
746
|
export async function loadDemTile(
|
|
566
747
|
url: string,
|
|
567
748
|
unpack: DemUnpackVector,
|
|
568
749
|
{signal, loadOptions}: {signal?: AbortSignal; loadOptions?: BasemapLoadOptions} = {}
|
|
569
750
|
): Promise<DemTile | null> {
|
|
570
|
-
const fetchFn = loadOptions
|
|
751
|
+
const fetchFn = getBasemapResourceFetch('Tile', 'image', loadOptions || undefined);
|
|
571
752
|
const response = await fetchFn(url, {...loadOptions?.fetchOptions, signal});
|
|
572
753
|
if (!response.ok) {
|
|
573
|
-
throw new
|
|
754
|
+
throw new BasemapFetchError(
|
|
755
|
+
`Failed to load DEM tile: ${url} (${response.status})`,
|
|
756
|
+
url,
|
|
757
|
+
response.status
|
|
758
|
+
);
|
|
574
759
|
}
|
|
575
760
|
const blob = await response.blob();
|
|
576
761
|
if (blob.size === 0) {
|
|
577
762
|
return null;
|
|
578
763
|
}
|
|
579
|
-
const image = await
|
|
580
|
-
premultiplyAlpha: 'none',
|
|
581
|
-
colorSpaceConversion: 'none'
|
|
582
|
-
});
|
|
764
|
+
const image = await createDemImageBitmap(blob);
|
|
583
765
|
try {
|
|
584
|
-
return decodeDemTile(
|
|
766
|
+
return decodeDemTile(await readDemPixels(image), image.width, image.height, unpack);
|
|
585
767
|
} finally {
|
|
586
768
|
image.close();
|
|
587
769
|
}
|
|
588
770
|
}
|
|
589
771
|
|
|
590
|
-
/** The slope texture of a DEM tile, with the neighbors it was prepared with. */
|
|
772
|
+
/** The slope texture of a DEM tile, with the neighbors and the body radius it was prepared with. */
|
|
591
773
|
const preparedTextures = new WeakMap<DemTile, {key: string; image: ImageData}>();
|
|
592
774
|
|
|
593
775
|
type TileIndex = {x: number; y: number; z: number};
|
|
594
|
-
|
|
776
|
+
/** A tile of a `raster-dem` source's tileset. */
|
|
777
|
+
export type DemTileHeader = {
|
|
595
778
|
id: string;
|
|
596
779
|
index: TileIndex;
|
|
597
780
|
bbox: unknown;
|
|
@@ -601,7 +784,7 @@ type DemTileHeader = {
|
|
|
601
784
|
|
|
602
785
|
/**
|
|
603
786
|
* Creates the tileset of a `raster-dem` source: each tile is fetched and decoded once, and shared
|
|
604
|
-
* by every `
|
|
787
|
+
* by every `DemTileLayer` drawn from it, as the tiles of a vector source are shared by its
|
|
605
788
|
* style runs. `tilesetProps` are the source's tile pyramid (see `getTilePyramidProps`).
|
|
606
789
|
*/
|
|
607
790
|
export function createSharedDemTileset(
|
|
@@ -612,11 +795,12 @@ export function createSharedDemTileset(
|
|
|
612
795
|
const unpack = getDemUnpackVector(source);
|
|
613
796
|
// Expands the source's URL template for a tile, as the layer does for its own tileset.
|
|
614
797
|
const loader = new SharedTile2DLayer<DemTile | null>({
|
|
615
|
-
data: source
|
|
798
|
+
data: getSourceTileTemplates(source) as string[],
|
|
616
799
|
getTileData: ({url, signal}) => (url ? loadDemTile(url, unpack, {signal, loadOptions}) : null)
|
|
617
800
|
});
|
|
618
801
|
return new SharedTileset2D<DemTile | null>({
|
|
619
802
|
...loader._getTilesetOptions(),
|
|
803
|
+
adapter: basemapTilesetAdapter,
|
|
620
804
|
...tilesetProps,
|
|
621
805
|
tileSize: source.tileSize || 512,
|
|
622
806
|
getTileData: tile => loader.getTileData(tile)
|
|
@@ -624,34 +808,34 @@ export function createSharedDemTileset(
|
|
|
624
808
|
}
|
|
625
809
|
|
|
626
810
|
/**
|
|
627
|
-
* A tileset shared by the `
|
|
628
|
-
*
|
|
811
|
+
* A tileset shared by the `DemTileLayer`s of one `raster-dem` source, finalized when the last of
|
|
812
|
+
* them is. `BasemapLayer` owns its sources' tilesets itself; this is for the layers that
|
|
629
813
|
* `getBasemapLayers` creates without it.
|
|
630
814
|
*/
|
|
631
815
|
export type DemTilesetOwner = {tileset: SharedTileset2D; consumers: Set<object>};
|
|
632
816
|
|
|
633
|
-
/** Props of a `
|
|
634
|
-
export type
|
|
635
|
-
hillshadeParameters: HillshadeParameters;
|
|
636
|
-
/** Extra props of every tile's bitmap layer, such as `parameters`. */
|
|
637
|
-
hillshadeSubLayerProps?: Record<string, unknown>;
|
|
817
|
+
/** Props of a `DemTileLayer`. */
|
|
818
|
+
export type DemTileLayerProps = SharedTile2DLayerProps<DemTile | null> & {
|
|
638
819
|
/** The owner of a shared `data` tileset that the layer releases when finalized. */
|
|
639
820
|
demTilesetOwner?: DemTilesetOwner | null;
|
|
640
821
|
};
|
|
641
822
|
|
|
823
|
+
/** The loaded neighbors of a DEM tile, and a key that changes when a neighbor loads. */
|
|
824
|
+
export type DemTileNeighbors = {getNeighbor: DemNeighbors; key: string};
|
|
825
|
+
|
|
642
826
|
/**
|
|
643
|
-
* The tiles of a `raster-dem` source,
|
|
644
|
-
* come from the source's tileset, shared with the source's other
|
|
645
|
-
* `createSharedDemTileset`)
|
|
646
|
-
*
|
|
647
|
-
*
|
|
827
|
+
* The tiles of a `raster-dem` source, drawn by one style layer (`hillshade` or `color-relief`).
|
|
828
|
+
* The decoded tiles come from the source's tileset, shared with the source's other such layers
|
|
829
|
+
* (see `createSharedDemTileset`). A tile is drawn from its elevations and the edge pixels of its
|
|
830
|
+
* loaded neighbors, so that tiles join without seams: when a tile loads, its neighbors are drawn
|
|
831
|
+
* again.
|
|
648
832
|
*/
|
|
649
|
-
export class
|
|
650
|
-
|
|
833
|
+
export abstract class DemTileLayer<
|
|
834
|
+
PropsT extends DemTileLayerProps = DemTileLayerProps
|
|
835
|
+
> extends SharedTile2DLayer<DemTile | null, PropsT> {
|
|
836
|
+
static layerName = 'DemTileLayer';
|
|
651
837
|
static defaultProps = {
|
|
652
838
|
...SharedTile2DLayer.defaultProps,
|
|
653
|
-
hillshadeParameters: {type: 'object', value: null, compare: false},
|
|
654
|
-
hillshadeSubLayerProps: {type: 'object', value: null, compare: false},
|
|
655
839
|
demTilesetOwner: {type: 'object', value: null, compare: false}
|
|
656
840
|
};
|
|
657
841
|
|
|
@@ -686,6 +870,24 @@ export class HillshadeTileLayer extends SharedTile2DLayer<DemTile | null, Hillsh
|
|
|
686
870
|
}
|
|
687
871
|
}
|
|
688
872
|
|
|
873
|
+
/** Tile traversal that also selects tiles in a projected view (see `basemapTilesetAdapter`). */
|
|
874
|
+
_getTilesetOptions(): SharedTileset2DProps<DemTile | null> {
|
|
875
|
+
return {...super._getTilesetOptions(), adapter: basemapTilesetAdapter};
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
/**
|
|
879
|
+
* In a projected view, tiles are not culled by the longitude/latitude box of the screen corners:
|
|
880
|
+
* a corner can lie outside the projection, and the tile selection has tested each tile already.
|
|
881
|
+
*/
|
|
882
|
+
filterSubLayer(context: any): boolean {
|
|
883
|
+
const {tile} = context.layer.props;
|
|
884
|
+
if (tile && isProjectedViewport(context.viewport)) {
|
|
885
|
+
const view = this.state.tilesetViews?.get(context.viewport?.id || 'default');
|
|
886
|
+
return Boolean(view?.isTileVisible(tile));
|
|
887
|
+
}
|
|
888
|
+
return super.filterSubLayer(context);
|
|
889
|
+
}
|
|
890
|
+
|
|
689
891
|
_onTileLoad(tile: any): void {
|
|
690
892
|
// The tiles around it can now take its edge pixels: their sublayers are prepared again.
|
|
691
893
|
const {x, y, z} = tile.index as TileIndex;
|
|
@@ -705,22 +907,9 @@ export class HillshadeTileLayer extends SharedTile2DLayer<DemTile | null, Hillsh
|
|
|
705
907
|
super._onTileLoad(tile);
|
|
706
908
|
}
|
|
707
909
|
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
log.once(1, '[BasemapLayer] hillshade layers are not drawn on WebGPU')();
|
|
711
|
-
return null;
|
|
712
|
-
}
|
|
713
|
-
return super.renderLayers();
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
renderSubLayers(props: any): Layer | null | LayersList {
|
|
717
|
-
const dem = props.data as DemTile | null;
|
|
718
|
-
const tile = props.tile as DemTileHeader;
|
|
719
|
-
if (!dem || typeof ImageData === 'undefined') {
|
|
720
|
-
return null;
|
|
721
|
-
}
|
|
910
|
+
/** The loaded neighbors of a tile, which wrap around the antimeridian. */
|
|
911
|
+
protected getDemNeighbors(tile: DemTileHeader): DemTileNeighbors {
|
|
722
912
|
const {x, y, z} = tile.index;
|
|
723
|
-
|
|
724
913
|
const loaded = new Map<string, DemTile>();
|
|
725
914
|
for (const other of (this.state.tileset?.tiles || []) as DemTileHeader[]) {
|
|
726
915
|
if (other.index.z === z && other.isLoaded && other.content) {
|
|
@@ -731,15 +920,47 @@ export class HillshadeTileLayer extends SharedTile2DLayer<DemTile | null, Hillsh
|
|
|
731
920
|
const getNeighbor: DemNeighbors = (dx, dy) =>
|
|
732
921
|
loaded.get(`${(x + dx + count) % count}/${y + dy}`) ?? null;
|
|
733
922
|
// Which neighbors are loaded decides the texture's border.
|
|
734
|
-
const
|
|
923
|
+
const key = [-1, 0, 1]
|
|
735
924
|
.flatMap(dy => [-1, 0, 1].map(dx => ((dx || dy) && getNeighbor(dx, dy) ? 1 : 0)))
|
|
736
925
|
.join('');
|
|
737
|
-
|
|
926
|
+
return {getNeighbor, key};
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
/** Props of a `HillshadeTileLayer`. */
|
|
931
|
+
export type HillshadeTileLayerProps = DemTileLayerProps & {
|
|
932
|
+
hillshadeParameters: HillshadeParameters;
|
|
933
|
+
/** Extra props of every tile's bitmap layer, such as `parameters`. */
|
|
934
|
+
hillshadeSubLayerProps?: Record<string, unknown>;
|
|
935
|
+
};
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* The tiles of a `raster-dem` source, shaded by one `hillshade` style layer (see `DemTileLayer`).
|
|
939
|
+
* Each tile's slopes are prepared from its elevations and those of its loaded neighbors.
|
|
940
|
+
*/
|
|
941
|
+
export class HillshadeTileLayer extends DemTileLayer<HillshadeTileLayerProps> {
|
|
942
|
+
static layerName = 'HillshadeTileLayer';
|
|
943
|
+
static defaultProps = {
|
|
944
|
+
...DemTileLayer.defaultProps,
|
|
945
|
+
hillshadeParameters: {type: 'object', value: null, compare: false},
|
|
946
|
+
hillshadeSubLayerProps: {type: 'object', value: null, compare: false}
|
|
947
|
+
};
|
|
948
|
+
|
|
949
|
+
renderSubLayers(props: any): Layer | null | LayersList {
|
|
950
|
+
const dem = props.data as DemTile | null;
|
|
951
|
+
const tile = props.tile as DemTileHeader;
|
|
952
|
+
if (!dem || typeof ImageData === 'undefined') {
|
|
953
|
+
return null;
|
|
954
|
+
}
|
|
955
|
+
const {getNeighbor, key: neighborKey} = this.getDemNeighbors(tile);
|
|
956
|
+
// The body decides how steep the slopes are: a view can change it.
|
|
957
|
+
const bodyRadius = getBodyRadius(this.context.viewport);
|
|
958
|
+
const key = `${neighborKey}:${bodyRadius}`;
|
|
738
959
|
// Kept with the decoded tile, so the source's hillshade layers prepare its slopes once.
|
|
739
960
|
let prepared = preparedTextures.get(dem);
|
|
740
961
|
if (prepared?.key !== key) {
|
|
741
962
|
const size = dem.dim + 2;
|
|
742
|
-
const pixels = prepareHillshadeTexture(dem, z, getNeighbor);
|
|
963
|
+
const pixels = prepareHillshadeTexture(dem, tile.index.z, getNeighbor, bodyRadius);
|
|
743
964
|
prepared = {key, image: new ImageData(pixels, size, size)};
|
|
744
965
|
preparedTextures.set(dem, prepared);
|
|
745
966
|
}
|
|
@@ -765,7 +986,7 @@ export class HillshadeTileLayer extends SharedTile2DLayer<DemTile | null, Hillsh
|
|
|
765
986
|
},
|
|
766
987
|
data: null,
|
|
767
988
|
// Positions in the tile are Web Mercator, also on a globe.
|
|
768
|
-
_imageCoordinateSystem:
|
|
989
|
+
_imageCoordinateSystem: 'cartesian',
|
|
769
990
|
image: prepared.image,
|
|
770
991
|
bounds: [west, south, east, north],
|
|
771
992
|
hillshadeParameters,
|