@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
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
// tangram-layers
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
Layer,
|
|
7
|
+
log,
|
|
8
|
+
project32,
|
|
9
|
+
type Accessor,
|
|
10
|
+
type DefaultProps,
|
|
11
|
+
type LayerProps,
|
|
12
|
+
type UpdateParameters
|
|
13
|
+
} from '@deck.gl/core';
|
|
14
|
+
import type {Framebuffer, Texture} from '@luma.gl/core';
|
|
15
|
+
import {Geometry, Model} from '@luma.gl/engine';
|
|
16
|
+
import type {ShaderModule} from '@luma.gl/shaders';
|
|
17
|
+
import {lngLatToMercatorWorld, mercatorWorldToLngLat, MERCATOR_WORLD_SIZE} from './projected-viewport';
|
|
18
|
+
import {getStyleAccessor, type StyleAccessor} from './style-accessor';
|
|
19
|
+
import {
|
|
20
|
+
compileStylePropertyValue,
|
|
21
|
+
getCompiledStyleProperty,
|
|
22
|
+
getStylePropertyDefault,
|
|
23
|
+
type CompiledStyleProperty,
|
|
24
|
+
type StyleFeature
|
|
25
|
+
} from './style-expression';
|
|
26
|
+
import type {ResolvedBasemapStyleLayer as BasemapStyleLayer} from './style-resolver';
|
|
27
|
+
|
|
28
|
+
const DEFAULT_RADIUS = getStylePropertyDefault('heatmap-radius') as number;
|
|
29
|
+
const DEFAULT_WEIGHT = getStylePropertyDefault('heatmap-weight') as number;
|
|
30
|
+
const DEFAULT_INTENSITY = getStylePropertyDefault('heatmap-intensity') as number;
|
|
31
|
+
const DEFAULT_OPACITY = getStylePropertyDefault('heatmap-opacity') as number;
|
|
32
|
+
|
|
33
|
+
/** The texels of a color ramp, as MapLibre GL JS renders `heatmap-color`. */
|
|
34
|
+
export const COLOR_RAMP_SIZE = 256;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The density texture is a quarter of the drawing buffer wide and high, as MapLibre GL JS draws
|
|
38
|
+
* it, and filtered linearly when it is colored.
|
|
39
|
+
*/
|
|
40
|
+
const DENSITY_TEXTURE_SCALE = 4;
|
|
41
|
+
|
|
42
|
+
/** A point of a heatmap: its longitude/latitude position and the feature it comes from. */
|
|
43
|
+
export type HeatmapPoint = {
|
|
44
|
+
position: [number, number];
|
|
45
|
+
feature: StyleFeature;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const kernelUniformBlock = /* glsl */ `\
|
|
49
|
+
layout(std140) uniform heatmapKernelUniforms {
|
|
50
|
+
float intensity;
|
|
51
|
+
} heatmapKernel;
|
|
52
|
+
`;
|
|
53
|
+
|
|
54
|
+
type HeatmapKernelProps = {intensity: number};
|
|
55
|
+
|
|
56
|
+
const heatmapKernelUniforms = {
|
|
57
|
+
name: 'heatmapKernel',
|
|
58
|
+
vs: kernelUniformBlock,
|
|
59
|
+
fs: kernelUniformBlock,
|
|
60
|
+
uniformTypes: {intensity: 'f32'}
|
|
61
|
+
} as const satisfies ShaderModule<HeatmapKernelProps>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* MapLibre GL JS's kernel (`heatmap.vertex.glsl`): a Gaussian of standard deviation
|
|
65
|
+
* `heatmap-radius / 3` pixels on the map plane, drawn on a square that ends where the kernel
|
|
66
|
+
* falls below `ZERO`. Kernels whose peak is below `ZERO` are not drawn.
|
|
67
|
+
*/
|
|
68
|
+
const kernelVs = /* glsl */ `\
|
|
69
|
+
#version 300 es
|
|
70
|
+
#define SHADER_NAME basemap-heatmap-kernel-vertex-shader
|
|
71
|
+
|
|
72
|
+
in vec3 positions;
|
|
73
|
+
in vec3 instancePositions;
|
|
74
|
+
in vec3 instancePositions64Low;
|
|
75
|
+
in float instanceWeights;
|
|
76
|
+
in float instanceRadius;
|
|
77
|
+
|
|
78
|
+
out vec2 vExtrude;
|
|
79
|
+
out float vWeight;
|
|
80
|
+
|
|
81
|
+
const float ZERO = 1.0 / 255.0 / 16.0;
|
|
82
|
+
const float GAUSS_COEF = 0.3989422804014327;
|
|
83
|
+
|
|
84
|
+
void main(void) {
|
|
85
|
+
geometry.worldPosition = instancePositions;
|
|
86
|
+
vWeight = instanceWeights;
|
|
87
|
+
float S = sqrt(-2.0 * log(ZERO / instanceWeights / heatmapKernel.intensity / GAUSS_COEF)) / 3.0;
|
|
88
|
+
if (!(S > 0.0) || !(instanceRadius > 0.0)) {
|
|
89
|
+
gl_Position = vec4(0.0, 0.0, 2.0, 1.0);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
vExtrude = S * positions.xy;
|
|
93
|
+
vec3 offset = vec3(project_pixel_size(vExtrude * instanceRadius), 0.0);
|
|
94
|
+
gl_Position = project_position_to_clipspace(
|
|
95
|
+
instancePositions, instancePositions64Low, offset, geometry.position
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
`;
|
|
99
|
+
|
|
100
|
+
const kernelFs = /* glsl */ `\
|
|
101
|
+
#version 300 es
|
|
102
|
+
#define SHADER_NAME basemap-heatmap-kernel-fragment-shader
|
|
103
|
+
|
|
104
|
+
precision highp float;
|
|
105
|
+
|
|
106
|
+
in vec2 vExtrude;
|
|
107
|
+
in float vWeight;
|
|
108
|
+
|
|
109
|
+
out vec4 fragColor;
|
|
110
|
+
|
|
111
|
+
const float GAUSS_COEF = 0.3989422804014327;
|
|
112
|
+
|
|
113
|
+
void main(void) {
|
|
114
|
+
float d = -0.5 * 3.0 * 3.0 * dot(vExtrude, vExtrude);
|
|
115
|
+
float density = vWeight * heatmapKernel.intensity * GAUSS_COEF * exp(d);
|
|
116
|
+
fragColor = vec4(density, 1.0, 1.0, 1.0);
|
|
117
|
+
}
|
|
118
|
+
`;
|
|
119
|
+
|
|
120
|
+
const compositeUniformBlock = /* glsl */ `\
|
|
121
|
+
layout(std140) uniform heatmapColorUniforms {
|
|
122
|
+
float opacity;
|
|
123
|
+
} heatmapColor;
|
|
124
|
+
`;
|
|
125
|
+
|
|
126
|
+
type HeatmapColorProps = {opacity: number};
|
|
127
|
+
|
|
128
|
+
const heatmapColorUniforms = {
|
|
129
|
+
name: 'heatmapColor',
|
|
130
|
+
vs: compositeUniformBlock,
|
|
131
|
+
fs: compositeUniformBlock,
|
|
132
|
+
uniformTypes: {opacity: 'f32'}
|
|
133
|
+
} as const satisfies ShaderModule<HeatmapColorProps>;
|
|
134
|
+
|
|
135
|
+
const compositeVs = /* glsl */ `\
|
|
136
|
+
#version 300 es
|
|
137
|
+
#define SHADER_NAME basemap-heatmap-color-vertex-shader
|
|
138
|
+
|
|
139
|
+
in vec2 positions;
|
|
140
|
+
out vec2 vTexCoord;
|
|
141
|
+
|
|
142
|
+
void main(void) {
|
|
143
|
+
vTexCoord = positions * 0.5 + 0.5;
|
|
144
|
+
gl_Position = vec4(positions, 0.0, 1.0);
|
|
145
|
+
}
|
|
146
|
+
`;
|
|
147
|
+
|
|
148
|
+
/** MapLibre GL JS's `heatmap_texture.fragment.glsl`: the density, colored by the ramp. */
|
|
149
|
+
const compositeFs = /* glsl */ `\
|
|
150
|
+
#version 300 es
|
|
151
|
+
#define SHADER_NAME basemap-heatmap-color-fragment-shader
|
|
152
|
+
|
|
153
|
+
precision highp float;
|
|
154
|
+
|
|
155
|
+
uniform sampler2D densityTexture;
|
|
156
|
+
uniform sampler2D colorRamp;
|
|
157
|
+
|
|
158
|
+
in vec2 vTexCoord;
|
|
159
|
+
out vec4 fragColor;
|
|
160
|
+
|
|
161
|
+
void main(void) {
|
|
162
|
+
float density = texture(densityTexture, vTexCoord).r;
|
|
163
|
+
fragColor = texture(colorRamp, vec2(density, 0.5)) * heatmapColor.opacity;
|
|
164
|
+
}
|
|
165
|
+
`;
|
|
166
|
+
|
|
167
|
+
/** Additive blending, which sums the kernels into a density. */
|
|
168
|
+
const KERNEL_PARAMETERS = {
|
|
169
|
+
blend: true,
|
|
170
|
+
blendColorOperation: 'add',
|
|
171
|
+
blendColorSrcFactor: 'one',
|
|
172
|
+
blendColorDstFactor: 'one',
|
|
173
|
+
blendAlphaOperation: 'add',
|
|
174
|
+
blendAlphaSrcFactor: 'one',
|
|
175
|
+
blendAlphaDstFactor: 'one',
|
|
176
|
+
depthWriteEnabled: false,
|
|
177
|
+
depthCompare: 'always',
|
|
178
|
+
cullMode: 'none'
|
|
179
|
+
} as const;
|
|
180
|
+
|
|
181
|
+
/** The ramp is premultiplied, so the colored density is blended as premultiplied color. */
|
|
182
|
+
const COMPOSITE_PARAMETERS = {
|
|
183
|
+
blend: true,
|
|
184
|
+
blendColorOperation: 'add',
|
|
185
|
+
blendColorSrcFactor: 'one',
|
|
186
|
+
blendColorDstFactor: 'one-minus-src-alpha',
|
|
187
|
+
blendAlphaOperation: 'add',
|
|
188
|
+
blendAlphaSrcFactor: 'one',
|
|
189
|
+
blendAlphaDstFactor: 'one-minus-src-alpha',
|
|
190
|
+
depthWriteEnabled: false,
|
|
191
|
+
depthCompare: 'always',
|
|
192
|
+
cullMode: 'none'
|
|
193
|
+
} as const;
|
|
194
|
+
|
|
195
|
+
/** Props of {@link BasemapHeatmapLayer}. */
|
|
196
|
+
export type BasemapHeatmapLayerProps<DataT = HeatmapPoint> = {
|
|
197
|
+
data: DataT[];
|
|
198
|
+
/** The longitude/latitude of a point. */
|
|
199
|
+
getPosition?: Accessor<DataT, [number, number]>;
|
|
200
|
+
/** `heatmap-weight`: a point's share of the density. */
|
|
201
|
+
getWeight?: Accessor<DataT, number>;
|
|
202
|
+
/** `heatmap-radius`, in pixels. */
|
|
203
|
+
getRadius?: Accessor<DataT, number>;
|
|
204
|
+
/** `heatmap-intensity`: multiplies the density of every point. */
|
|
205
|
+
intensity?: number;
|
|
206
|
+
/**
|
|
207
|
+
* `heatmap-color`, as `COLOR_RAMP_SIZE` premultiplied RGBA texels, from density 0 to 1 (see
|
|
208
|
+
* `getHeatmapColorRamp`). The layer's `opacity` is `heatmap-opacity`.
|
|
209
|
+
*/
|
|
210
|
+
colorRamp: Uint8Array;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
type DensityTarget = {framebuffer: Framebuffer; texture: Texture};
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Draws a `heatmap` style layer as MapLibre GL JS does: each point adds a Gaussian kernel to a
|
|
217
|
+
* density texture a quarter of the drawing buffer's size, with additive blending, and the density
|
|
218
|
+
* is then colored by `heatmap-color` over the whole view. Kernels are on the map plane: on a globe
|
|
219
|
+
* they lie on the sphere, and in a projected view on the plane of the view's projection. WebGL
|
|
220
|
+
* only; on WebGPU the layer draws nothing.
|
|
221
|
+
*/
|
|
222
|
+
export class BasemapHeatmapLayer<DataT = HeatmapPoint> extends Layer<
|
|
223
|
+
Required<BasemapHeatmapLayerProps<DataT>> & LayerProps
|
|
224
|
+
> {
|
|
225
|
+
static layerName = 'BasemapHeatmapLayer';
|
|
226
|
+
static defaultProps: DefaultProps<BasemapHeatmapLayerProps<any>> = {
|
|
227
|
+
getPosition: {type: 'accessor', value: (point: HeatmapPoint) => point.position},
|
|
228
|
+
getWeight: {type: 'accessor', value: DEFAULT_WEIGHT},
|
|
229
|
+
getRadius: {type: 'accessor', value: DEFAULT_RADIUS},
|
|
230
|
+
intensity: {type: 'number', value: DEFAULT_INTENSITY},
|
|
231
|
+
colorRamp: {type: 'object', value: null, compare: true}
|
|
232
|
+
} as any;
|
|
233
|
+
|
|
234
|
+
declare state: {
|
|
235
|
+
model?: Model;
|
|
236
|
+
compositeModel?: Model;
|
|
237
|
+
colorRampTexture?: Texture;
|
|
238
|
+
/** The density texture of each view, by viewport id. */
|
|
239
|
+
densityTargets: Map<string, DensityTarget>;
|
|
240
|
+
densityFormat: 'rgba16float' | 'rgba8unorm';
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
getShaders() {
|
|
244
|
+
return super.getShaders({
|
|
245
|
+
vs: kernelVs,
|
|
246
|
+
fs: kernelFs,
|
|
247
|
+
modules: [project32, heatmapKernelUniforms]
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
initializeState(): void {
|
|
252
|
+
const {device} = this.context;
|
|
253
|
+
this.state = {densityTargets: new Map(), densityFormat: 'rgba16float'};
|
|
254
|
+
if (device.type === 'webgpu') {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
// MapLibre GL JS accumulates the density in half floats.
|
|
258
|
+
if (!device.isTextureFormatRenderable('rgba16float')) {
|
|
259
|
+
log.warn(
|
|
260
|
+
`${this.id}: rendering to half-float textures is not supported, heatmap densities are clamped`
|
|
261
|
+
)();
|
|
262
|
+
this.state.densityFormat = 'rgba8unorm';
|
|
263
|
+
}
|
|
264
|
+
this.getAttributeManager()!.addInstanced({
|
|
265
|
+
instancePositions: {
|
|
266
|
+
size: 3,
|
|
267
|
+
type: 'float64',
|
|
268
|
+
fp64: this.use64bitPositions(),
|
|
269
|
+
accessor: 'getPosition',
|
|
270
|
+
...this.usePositionTransforms()
|
|
271
|
+
},
|
|
272
|
+
instanceWeights: {size: 1, accessor: 'getWeight', defaultValue: DEFAULT_WEIGHT},
|
|
273
|
+
instanceRadius: {size: 1, accessor: 'getRadius', defaultValue: DEFAULT_RADIUS}
|
|
274
|
+
});
|
|
275
|
+
this.state.model = this.getKernelModel();
|
|
276
|
+
this.state.compositeModel = new Model(device, {
|
|
277
|
+
id: `${this.props.id}-color`,
|
|
278
|
+
vs: compositeVs,
|
|
279
|
+
fs: compositeFs,
|
|
280
|
+
modules: [heatmapColorUniforms],
|
|
281
|
+
geometry: new Geometry({
|
|
282
|
+
topology: 'triangle-strip',
|
|
283
|
+
attributes: {positions: {size: 2, value: new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1])}}
|
|
284
|
+
}),
|
|
285
|
+
parameters: COMPOSITE_PARAMETERS
|
|
286
|
+
});
|
|
287
|
+
this.state.colorRampTexture = device.createTexture({
|
|
288
|
+
format: 'rgba8unorm',
|
|
289
|
+
width: COLOR_RAMP_SIZE,
|
|
290
|
+
height: 1,
|
|
291
|
+
mipLevels: 1,
|
|
292
|
+
sampler: {
|
|
293
|
+
minFilter: 'linear',
|
|
294
|
+
magFilter: 'linear',
|
|
295
|
+
addressModeU: 'clamp-to-edge',
|
|
296
|
+
addressModeV: 'clamp-to-edge'
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
private getKernelModel(): Model {
|
|
302
|
+
return new Model(this.context.device, {
|
|
303
|
+
...this.getShaders(),
|
|
304
|
+
id: this.props.id,
|
|
305
|
+
bufferLayout: this.getAttributeManager()!.getBufferLayouts(),
|
|
306
|
+
geometry: new Geometry({
|
|
307
|
+
topology: 'triangle-strip',
|
|
308
|
+
attributes: {
|
|
309
|
+
positions: {size: 3, value: new Float32Array([-1, -1, 0, 1, -1, 0, -1, 1, 0, 1, 1, 0])}
|
|
310
|
+
}
|
|
311
|
+
}),
|
|
312
|
+
isInstanced: true
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
updateState(params: UpdateParameters<this>): void {
|
|
317
|
+
super.updateState(params);
|
|
318
|
+
if (!this.state.model) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (params.changeFlags.extensionsChanged) {
|
|
322
|
+
this.state.model.destroy();
|
|
323
|
+
this.state.model = this.getKernelModel();
|
|
324
|
+
this.getAttributeManager()!.invalidateAll();
|
|
325
|
+
}
|
|
326
|
+
const {colorRamp} = params.props;
|
|
327
|
+
if (colorRamp && colorRamp !== params.oldProps.colorRamp) {
|
|
328
|
+
this.state.colorRampTexture!.writeData(colorRamp);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
finalizeState(context: any): void {
|
|
333
|
+
super.finalizeState(context);
|
|
334
|
+
this.state.compositeModel?.destroy();
|
|
335
|
+
this.state.colorRampTexture?.destroy();
|
|
336
|
+
for (const target of this.state.densityTargets.values()) {
|
|
337
|
+
target.framebuffer.destroy();
|
|
338
|
+
target.texture.destroy();
|
|
339
|
+
}
|
|
340
|
+
this.state.densityTargets.clear();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** The density texture of the current view, a quarter of its size in device pixels. */
|
|
344
|
+
private getDensityTarget(width: number, height: number): DensityTarget {
|
|
345
|
+
const {viewport, device} = this.context;
|
|
346
|
+
const key = viewport.id || 'default';
|
|
347
|
+
let target = this.state.densityTargets.get(key);
|
|
348
|
+
if (target && (target.texture.width !== width || target.texture.height !== height)) {
|
|
349
|
+
target.framebuffer.destroy();
|
|
350
|
+
target.texture.destroy();
|
|
351
|
+
target = undefined;
|
|
352
|
+
}
|
|
353
|
+
if (!target) {
|
|
354
|
+
const texture = device.createTexture({
|
|
355
|
+
id: `${this.props.id}-density`,
|
|
356
|
+
format: this.state.densityFormat,
|
|
357
|
+
width,
|
|
358
|
+
height,
|
|
359
|
+
mipLevels: 1,
|
|
360
|
+
sampler: {
|
|
361
|
+
minFilter: 'linear',
|
|
362
|
+
magFilter: 'linear',
|
|
363
|
+
addressModeU: 'clamp-to-edge',
|
|
364
|
+
addressModeV: 'clamp-to-edge'
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
const framebuffer = device.createFramebuffer({width, height, colorAttachments: [texture]});
|
|
368
|
+
target = {framebuffer, texture};
|
|
369
|
+
this.state.densityTargets.set(key, target);
|
|
370
|
+
}
|
|
371
|
+
return target;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
draw({shaderModuleProps}: any): void {
|
|
375
|
+
const {model, compositeModel, colorRampTexture} = this.state;
|
|
376
|
+
const {opacity, intensity, colorRamp} = this.props;
|
|
377
|
+
const instanceCount = this.getNumInstances();
|
|
378
|
+
if (!model || !compositeModel || !colorRamp || !(opacity > 0) || instanceCount === 0) {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (shaderModuleProps?.picking?.isActive) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const {viewport, device, renderPass} = this.context;
|
|
385
|
+
const pixelRatio = shaderModuleProps?.project?.devicePixelRatio ?? 1;
|
|
386
|
+
const width = Math.max(1, Math.floor((viewport.width * pixelRatio) / DENSITY_TEXTURE_SCALE));
|
|
387
|
+
const height = Math.max(1, Math.floor((viewport.height * pixelRatio) / DENSITY_TEXTURE_SCALE));
|
|
388
|
+
const target = this.getDensityTarget(width, height);
|
|
389
|
+
|
|
390
|
+
model.shaderInputs.setProps({heatmapKernel: {intensity}});
|
|
391
|
+
model.setInstanceCount(instanceCount);
|
|
392
|
+
// deck.gl gives the layer's models the layer's parameters before each draw.
|
|
393
|
+
model.setParameters(KERNEL_PARAMETERS);
|
|
394
|
+
// The density is drawn in a render pass of its own; on WebGL the current pass resumes after it.
|
|
395
|
+
const densityPass = device.beginRenderPass({
|
|
396
|
+
id: `${this.props.id}-density`,
|
|
397
|
+
framebuffer: target.framebuffer,
|
|
398
|
+
parameters: {viewport: [0, 0, width, height]},
|
|
399
|
+
clearColor: [0, 0, 0, 0],
|
|
400
|
+
clearDepth: false,
|
|
401
|
+
clearStencil: false
|
|
402
|
+
});
|
|
403
|
+
try {
|
|
404
|
+
model.draw(densityPass);
|
|
405
|
+
} finally {
|
|
406
|
+
densityPass.end();
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
compositeModel.shaderInputs.setProps({heatmapColor: {opacity}});
|
|
410
|
+
compositeModel.setBindings({densityTexture: target.texture, colorRamp: colorRampTexture!});
|
|
411
|
+
compositeModel.draw(renderPass);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const colorRampCache = new WeakMap<CompiledStyleProperty, Uint8Array>();
|
|
416
|
+
let defaultColor: CompiledStyleProperty | null | undefined;
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* `heatmap-color` as `COLOR_RAMP_SIZE` premultiplied RGBA texels, as MapLibre GL JS renders the
|
|
420
|
+
* ramp (`renderColorRamp`): the color at density `i / 255`, rounded to 8 bits unpremultiplied and
|
|
421
|
+
* then premultiplied, as WebGL premultiplies the ramp it uploads. The same array is returned while
|
|
422
|
+
* the property is unchanged.
|
|
423
|
+
*/
|
|
424
|
+
export function getHeatmapColorRamp(styleLayer: BasemapStyleLayer): Uint8Array {
|
|
425
|
+
let property = getCompiledStyleProperty(styleLayer, 'heatmap-color');
|
|
426
|
+
if (!property) {
|
|
427
|
+
defaultColor ??= compileStylePropertyValue(
|
|
428
|
+
'heatmap-color',
|
|
429
|
+
getStylePropertyDefault('heatmap-color')
|
|
430
|
+
);
|
|
431
|
+
property = defaultColor!;
|
|
432
|
+
}
|
|
433
|
+
let ramp = colorRampCache.get(property);
|
|
434
|
+
if (!ramp) {
|
|
435
|
+
ramp = new Uint8Array(COLOR_RAMP_SIZE * 4);
|
|
436
|
+
for (let index = 0; index < COLOR_RAMP_SIZE; index++) {
|
|
437
|
+
const color = property.evaluateWithGlobals({
|
|
438
|
+
zoom: 0,
|
|
439
|
+
heatmapDensity: index / (COLOR_RAMP_SIZE - 1)
|
|
440
|
+
}) as number[] | null;
|
|
441
|
+
const alpha = Math.round((color?.[3] ?? 0) * 255);
|
|
442
|
+
for (let channel = 0; channel < 3; channel++) {
|
|
443
|
+
const straight = alpha > 0 ? Math.round(color![channel]) : 0;
|
|
444
|
+
ramp[index * 4 + channel] = Math.round((straight * alpha) / 255);
|
|
445
|
+
}
|
|
446
|
+
ramp[index * 4 + 3] = alpha;
|
|
447
|
+
}
|
|
448
|
+
colorRampCache.set(property, ramp);
|
|
449
|
+
}
|
|
450
|
+
return ramp;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
type TileIndex = {x: number; y: number; z: number};
|
|
454
|
+
|
|
455
|
+
const tilePointCache = new WeakMap<any[], Map<string, HeatmapPoint[]>>();
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* The points of a tile's features (each point of a multipoint) that lie in the tile, with their
|
|
459
|
+
* longitude/latitude. A tile's features include a buffer around it, which its neighbors also hold;
|
|
460
|
+
* MapLibre GL JS leaves out the points outside a tile, so that each point adds to the density once.
|
|
461
|
+
* `features` are tile-local, from 0 to 1 across the tile, or in longitude/latitude with `lngLat`.
|
|
462
|
+
*/
|
|
463
|
+
export function getHeatmapPoints(features: any[], index: TileIndex, lngLat: boolean): HeatmapPoint[] {
|
|
464
|
+
const key = `${index.x}/${index.y}/${index.z}/${lngLat}`;
|
|
465
|
+
let cache = tilePointCache.get(features);
|
|
466
|
+
if (!cache) {
|
|
467
|
+
cache = new Map();
|
|
468
|
+
tilePointCache.set(features, cache);
|
|
469
|
+
}
|
|
470
|
+
let points = cache.get(key);
|
|
471
|
+
if (!points) {
|
|
472
|
+
points = [];
|
|
473
|
+
const scale = 2 ** index.z;
|
|
474
|
+
const add = (feature: StyleFeature, coordinates: number[]) => {
|
|
475
|
+
let x: number;
|
|
476
|
+
let y: number;
|
|
477
|
+
let position: [number, number];
|
|
478
|
+
if (lngLat) {
|
|
479
|
+
const [worldX, worldY] = lngLatToMercatorWorld([coordinates[0], coordinates[1]]);
|
|
480
|
+
x = (worldX / MERCATOR_WORLD_SIZE) * scale - index.x;
|
|
481
|
+
y = (1 - worldY / MERCATOR_WORLD_SIZE) * scale - index.y;
|
|
482
|
+
position = [coordinates[0], coordinates[1]];
|
|
483
|
+
} else {
|
|
484
|
+
[x, y] = coordinates;
|
|
485
|
+
position = mercatorWorldToLngLat([
|
|
486
|
+
(MERCATOR_WORLD_SIZE * (index.x + x)) / scale,
|
|
487
|
+
MERCATOR_WORLD_SIZE * (1 - (index.y + y) / scale)
|
|
488
|
+
]) as [number, number];
|
|
489
|
+
}
|
|
490
|
+
if (x >= 0 && x < 1 && y >= 0 && y < 1) {
|
|
491
|
+
points!.push({position, feature});
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
for (const feature of features) {
|
|
495
|
+
const geometry = feature.geometry;
|
|
496
|
+
if (geometry?.type === 'Point') {
|
|
497
|
+
add(feature, geometry.coordinates);
|
|
498
|
+
} else if (geometry?.type === 'MultiPoint') {
|
|
499
|
+
for (const coordinates of geometry.coordinates) {
|
|
500
|
+
add(feature, coordinates);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
cache.set(key, points);
|
|
505
|
+
}
|
|
506
|
+
return points;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* The tiles of `tiles` that no other tile of `tiles` covers. Where a tile and its ancestor are both
|
|
511
|
+
* drawn, as while the tile's siblings load, MapLibre GL JS draws the heatmap of the ancestor only.
|
|
512
|
+
*/
|
|
513
|
+
export function getTilesWithoutDrawnAncestor<T extends {index: TileIndex}>(tiles: T[]): T[] {
|
|
514
|
+
const drawn = new Set(tiles.map(({index}) => `${index.x}/${index.y}/${index.z}`));
|
|
515
|
+
return tiles.filter(({index}) => {
|
|
516
|
+
for (let z = index.z - 1; z >= 0; z--) {
|
|
517
|
+
const scale = 2 ** (index.z - z);
|
|
518
|
+
if (drawn.has(`${Math.floor(index.x / scale)}/${Math.floor(index.y / scale)}/${z}`)) {
|
|
519
|
+
return false;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return true;
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/** A style accessor of features as an accessor of the points that carry them. */
|
|
527
|
+
function getPointAccessor<T>(accessor: StyleAccessor<T>): StyleAccessor<T> {
|
|
528
|
+
const {value} = accessor;
|
|
529
|
+
if (typeof value !== 'function') {
|
|
530
|
+
return accessor;
|
|
531
|
+
}
|
|
532
|
+
const evaluate = value as (feature: StyleFeature) => T;
|
|
533
|
+
return {...accessor, value: ((point: HeatmapPoint) => evaluate(point.feature)) as any};
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* The layer of a `heatmap` style layer, drawing the points of all of its run's drawn tiles (see
|
|
538
|
+
* `getHeatmapPoints`). `heatmap-radius` and `heatmap-weight` are evaluated per feature;
|
|
539
|
+
* `heatmap-intensity` and `heatmap-opacity` at the style zoom. Nothing is drawn at
|
|
540
|
+
* `heatmap-opacity: 0`, as in MapLibre GL JS.
|
|
541
|
+
*/
|
|
542
|
+
export function createHeatmapSubLayer({
|
|
543
|
+
props,
|
|
544
|
+
styleLayer,
|
|
545
|
+
points,
|
|
546
|
+
zoom
|
|
547
|
+
}: {
|
|
548
|
+
props: Record<string, any>;
|
|
549
|
+
styleLayer: BasemapStyleLayer;
|
|
550
|
+
points: HeatmapPoint[];
|
|
551
|
+
zoom: number;
|
|
552
|
+
}): BasemapHeatmapLayer | null {
|
|
553
|
+
const radius = getPointAccessor(
|
|
554
|
+
getStyleAccessor(styleLayer, ['heatmap-radius'], zoom, ([value]) =>
|
|
555
|
+
Math.max(0, Number(value ?? DEFAULT_RADIUS) || 0)
|
|
556
|
+
)
|
|
557
|
+
);
|
|
558
|
+
const weight = getPointAccessor(
|
|
559
|
+
getStyleAccessor(styleLayer, ['heatmap-weight'], zoom, ([value]) =>
|
|
560
|
+
Math.max(0, Number(value ?? DEFAULT_WEIGHT) || 0)
|
|
561
|
+
)
|
|
562
|
+
);
|
|
563
|
+
const intensity = getStyleAccessor(styleLayer, ['heatmap-intensity'], zoom, ([value]) =>
|
|
564
|
+
Math.max(0, Number(value ?? DEFAULT_INTENSITY) || 0)
|
|
565
|
+
).value as number;
|
|
566
|
+
const opacity = getStyleAccessor(styleLayer, ['heatmap-opacity'], zoom, ([value]) =>
|
|
567
|
+
Math.min(1, Math.max(0, Number(value ?? DEFAULT_OPACITY) || 0))
|
|
568
|
+
).value as number;
|
|
569
|
+
if (points.length === 0 || opacity === 0) {
|
|
570
|
+
return null;
|
|
571
|
+
}
|
|
572
|
+
return new BasemapHeatmapLayer({
|
|
573
|
+
...props,
|
|
574
|
+
data: points,
|
|
575
|
+
// Points are in longitude/latitude, whatever the tile layer's coordinates.
|
|
576
|
+
coordinateSystem: 'lnglat',
|
|
577
|
+
coordinateOrigin: [0, 0, 0],
|
|
578
|
+
modelMatrix: null,
|
|
579
|
+
extensions: [],
|
|
580
|
+
pickable: false,
|
|
581
|
+
opacity: (props.opacity ?? 1) * opacity,
|
|
582
|
+
getRadius: radius.value,
|
|
583
|
+
getWeight: weight.value,
|
|
584
|
+
intensity,
|
|
585
|
+
colorRamp: getHeatmapColorRamp(styleLayer),
|
|
586
|
+
updateTriggers: {
|
|
587
|
+
getRadius: radius.updateTrigger,
|
|
588
|
+
getWeight: weight.updateTrigger
|
|
589
|
+
}
|
|
590
|
+
} as any);
|
|
591
|
+
}
|