@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.
Files changed (139) hide show
  1. package/README.md +12 -7
  2. package/THIRD-PARTY-NOTICES.md +7 -5
  3. package/dist/geojson-task-worker.js +3166 -0
  4. package/dist/geojson-task-worker.js.map +6 -0
  5. package/dist/geojson-worker.js +1089 -7
  6. package/dist/geojson-worker.js.map +3 -3
  7. package/dist/index.cjs +9588 -2093
  8. package/dist/index.cjs.map +3 -3
  9. package/dist/index.js +9599 -2090
  10. package/dist/index.js.map +3 -3
  11. package/dist/map-style.cjs +456 -20
  12. package/dist/map-style.cjs.map +3 -3
  13. package/dist/map-style.js +456 -19
  14. package/dist/map-style.js.map +2 -2
  15. package/dist/types/basemap-error.d.ts +52 -0
  16. package/dist/types/basemap-layer.d.ts +378 -25
  17. package/dist/types/basemap-protocol.d.ts +91 -0
  18. package/dist/types/basemap-request.d.ts +73 -0
  19. package/dist/types/circle-layer.d.ts +65 -8
  20. package/dist/types/color-relief-layer.d.ts +184 -0
  21. package/dist/types/dem-pixels.d.ts +40 -0
  22. package/dist/types/feature-state.d.ts +55 -0
  23. package/dist/types/fill-extrusion-lighting-extension.d.ts +1 -2
  24. package/dist/types/fill-outline-extension.d.ts +2 -3
  25. package/dist/types/fill-pattern.d.ts +35 -3
  26. package/dist/types/fill-style-extensions.d.ts +11 -59
  27. package/dist/types/geojson-resource.d.ts +18 -0
  28. package/dist/types/geojson-source.d.ts +13 -2
  29. package/dist/types/geojson-task-handler.d.ts +2 -0
  30. package/dist/types/geojson-task.d.ts +26 -0
  31. package/dist/types/geojson-tiler.d.ts +29 -2
  32. package/dist/types/geojson-worker.d.ts +5 -1
  33. package/dist/types/globe-horizon-extension.d.ts +2 -45
  34. package/dist/types/globe-layers.d.ts +141 -45
  35. package/dist/types/globe-line-angle-extension.d.ts +20 -17
  36. package/dist/types/glyph-store.d.ts +10 -1
  37. package/dist/types/heatmap-layer.d.ts +99 -0
  38. package/dist/types/hillshade-layer.d.ts +159 -27
  39. package/dist/types/icon-text-fit.d.ts +92 -0
  40. package/dist/types/image-source-layer.d.ts +142 -0
  41. package/dist/types/index.d.ts +25 -1
  42. package/dist/types/line-blur-extension.d.ts +3 -11
  43. package/dist/types/line-dash-extension.d.ts +53 -0
  44. package/dist/types/line-gradient-extension.d.ts +69 -0
  45. package/dist/types/line-metrics.d.ts +24 -0
  46. package/dist/types/line-pattern-extension.d.ts +50 -0
  47. package/dist/types/line-shape-extension.d.ts +76 -0
  48. package/dist/types/map-style-loader.d.ts +1 -1
  49. package/dist/types/map-style.d.ts +2 -2
  50. package/dist/types/mvt-label-layer.d.ts +111 -20
  51. package/dist/types/pmtiles-source.d.ts +38 -0
  52. package/dist/types/projected-background-layer.d.ts +63 -0
  53. package/dist/types/projected-bitmap-layer.d.ts +58 -0
  54. package/dist/types/projected-tile-data.d.ts +16 -0
  55. package/dist/types/projected-tile-selection.d.ts +72 -0
  56. package/dist/types/projected-tileset.d.ts +92 -0
  57. package/dist/types/projected-viewport.d.ts +76 -0
  58. package/dist/types/raster-color-extension.d.ts +4 -7
  59. package/dist/types/raster-fade.d.ts +155 -0
  60. package/dist/types/raster-tile-fingerprint.d.ts +11 -0
  61. package/dist/types/raster-tile-loader.d.ts +80 -0
  62. package/dist/types/shader-injection-plugin.d.ts +8 -0
  63. package/dist/types/source-features.d.ts +58 -0
  64. package/dist/types/source-tiles.d.ts +32 -0
  65. package/dist/types/sprite.d.ts +37 -3
  66. package/dist/types/stretch-icon-layer.d.ts +28 -0
  67. package/dist/types/style-accessor.d.ts +50 -12
  68. package/dist/types/style-expression.d.ts +22 -2
  69. package/dist/types/style-resolver.d.ts +47 -2
  70. package/dist/types/terrain-query.d.ts +39 -0
  71. package/dist/types/zoom-load-gate.d.ts +22 -0
  72. package/dist/types/zoom-paint-extension.d.ts +80 -11
  73. package/package.json +24 -21
  74. package/src/basemap-error.ts +82 -0
  75. package/src/basemap-layer.ts +1289 -101
  76. package/src/basemap-protocol.ts +353 -0
  77. package/src/basemap-request.ts +212 -0
  78. package/src/circle-layer.ts +430 -54
  79. package/src/color-relief-layer.ts +572 -0
  80. package/src/dem-pixels.ts +174 -0
  81. package/src/feature-state.ts +164 -0
  82. package/src/fill-extrusion-lighting-extension.ts +126 -9
  83. package/src/fill-outline-extension.ts +4 -4
  84. package/src/fill-pattern.ts +87 -9
  85. package/src/fill-style-extensions.ts +114 -44
  86. package/src/geojson-resource.ts +58 -0
  87. package/src/geojson-source.ts +106 -85
  88. package/src/geojson-task-handler.ts +11 -0
  89. package/src/geojson-task-worker.ts +8 -0
  90. package/src/geojson-task.ts +34 -0
  91. package/src/geojson-tiler.ts +270 -4
  92. package/src/geojson-worker.ts +36 -6
  93. package/src/globe-horizon-extension.ts +14 -4
  94. package/src/globe-layers.ts +1905 -406
  95. package/src/globe-line-angle-extension.ts +183 -23
  96. package/src/glyph-store.ts +28 -6
  97. package/src/heatmap-layer.ts +591 -0
  98. package/src/hillshade-layer.ts +309 -88
  99. package/src/icon-text-fit.ts +358 -0
  100. package/src/image-source-layer.ts +380 -0
  101. package/src/index.ts +58 -1
  102. package/src/line-blur-extension.ts +14 -12
  103. package/src/line-dash-extension.ts +303 -0
  104. package/src/line-gradient-extension.ts +191 -0
  105. package/src/line-metrics.ts +90 -0
  106. package/src/line-pattern-extension.ts +173 -0
  107. package/src/line-shape-extension.ts +272 -0
  108. package/src/map-style-loader.ts +5 -4
  109. package/src/map-style.ts +2 -1
  110. package/src/mvt-label-layer.ts +568 -144
  111. package/src/pmtiles-source.ts +276 -0
  112. package/src/projected-background-layer.ts +202 -0
  113. package/src/projected-bitmap-layer.ts +262 -0
  114. package/src/projected-tile-data.ts +132 -0
  115. package/src/projected-tile-selection.ts +605 -0
  116. package/src/projected-tileset.ts +183 -0
  117. package/src/projected-viewport.ts +254 -0
  118. package/src/raster-color-extension.ts +43 -18
  119. package/src/raster-fade.ts +319 -0
  120. package/src/raster-tile-fingerprint.ts +31 -0
  121. package/src/raster-tile-loader.ts +211 -0
  122. package/src/shader-injection-plugin.ts +25 -0
  123. package/src/source-features.ts +170 -0
  124. package/src/source-tiles.ts +102 -0
  125. package/src/sprite.ts +122 -15
  126. package/src/stretch-icon-layer.ts +87 -0
  127. package/src/style-accessor.ts +105 -17
  128. package/src/style-expression.ts +80 -3
  129. package/src/style-resolver.ts +200 -22
  130. package/src/terrain-query.ts +86 -0
  131. package/src/text-layout.ts +2 -3
  132. package/src/zoom-load-gate.ts +57 -0
  133. package/src/zoom-paint-extension.ts +245 -47
  134. package/dist/types/arabic-shaping.d.ts +0 -7
  135. package/dist/types/bidi.d.ts +0 -8
  136. package/dist/types/polylabel.d.ts +0 -17
  137. package/src/arabic-shaping.ts +0 -366
  138. package/src/bidi.ts +0 -199
  139. package/src/polylabel.ts +0 -312
@@ -48,9 +48,259 @@ export type EncodedTile = {
48
48
  lengths: Uint32Array;
49
49
  };
50
50
 
51
- /** Builds the tile index of a GeoJSON source's data. Throws if the data is not valid GeoJSON. */
52
- export function createTileIndex(data: unknown, options: GeoJSONVTOptions): GeoJSONVT {
53
- return new GeoJSONVT(data as GeoJSON.GeoJSON, options);
51
+ /**
52
+ * Builds the tile index of a GeoJSON source's data, and counts the features that
53
+ * `clipToMercatorRange` dropped or cut (see `clipToMercatorRange`). Throws if the data is not
54
+ * valid GeoJSON.
55
+ */
56
+ export function createTileIndex(
57
+ data: unknown,
58
+ options: GeoJSONVTOptions,
59
+ clipToMercatorRange = false
60
+ ): {index: GeoJSONVT; outside: number} {
61
+ const clipped = clipToMercatorRange
62
+ ? clipFeaturesToMercatorRange(data, options)
63
+ : {data, outside: 0};
64
+ // Cut data carries the ids that `generateId` gave the features of the data as it was.
65
+ const indexOptions =
66
+ clipped.data !== data && options.generateId ? {...options, generateId: false} : options;
67
+ return {index: new GeoJSONVT(clipped.data as GeoJSON.GeoJSON, indexOptions), outside: clipped.outside};
68
+ }
69
+
70
+ /** The latitude of the north edge of the Web Mercator world, 85.0511287798 degrees. */
71
+ const MERCATOR_LATITUDE = (Math.atan(Math.sinh(Math.PI)) * 180) / Math.PI;
72
+
73
+ /**
74
+ * Web Mercator y of a latitude, 0 at the north edge of the world and 1 at the south edge, and
75
+ * not clamped to them. Latitudes are taken to within 1e-4 degrees of the poles, where y is finite.
76
+ */
77
+ function getMercatorY(latitude: number): number {
78
+ const sin = Math.sin((Math.max(-89.9999, Math.min(89.9999, latitude)) * Math.PI) / 180);
79
+ return 0.5 - (0.25 * Math.log((1 + sin) / (1 - sin))) / Math.PI;
80
+ }
81
+
82
+ type Position = number[];
83
+
84
+ /** How far past an edge of the world, in Web Mercator y, a position on the edge can round to. */
85
+ const EDGE_TOLERANCE = 1e-12;
86
+
87
+ /** Whether a Web Mercator y is in the world, or on its edge within rounding. */
88
+ const isInsideY = (y: number) => y >= -EDGE_TOLERANCE && y <= 1 + EDGE_TOLERANCE;
89
+
90
+ /** Whether a position is in the Web Mercator world, where geojson-vt does not move it. */
91
+ function isInside(position: Position): boolean {
92
+ return Math.abs(position[1]) <= MERCATOR_LATITUDE;
93
+ }
94
+
95
+ /**
96
+ * The point at `t` of the segment from `a` to `b`, which is straight in Web Mercator as geojson-vt
97
+ * draws it, on the edge of the world at `edge` (0 or 1).
98
+ */
99
+ function interpolate(a: Position, b: Position, t: number, edge: number): Position {
100
+ return [a[0] + t * (b[0] - a[0]), edge === 0 ? MERCATOR_LATITUDE : -MERCATOR_LATITUDE];
101
+ }
102
+
103
+ /** A part of a cut line, and where it starts and ends along the whole line, from 0 to 1. */
104
+ type LinePart = {line: Position[]; start: number; end: number};
105
+
106
+ /**
107
+ * The parts of a line inside the Web Mercator world. A segment that crosses an edge of the world
108
+ * ends where it crosses it, as geojson-vt cuts lines at tile edges. Each part keeps where it lies
109
+ * along the whole line, measured as geojson-vt measures `line-progress`: in the Web Mercator
110
+ * world square with latitudes clamped to it, here with the points where the line is cut.
111
+ */
112
+ function clipLine(line: Position[]): LinePart[] {
113
+ const points: Position[] = [];
114
+ for (let i = 0; i < line.length; i++) {
115
+ if (i > 0) {
116
+ const a = line[i - 1];
117
+ const ya = getMercatorY(a[1]);
118
+ const dy = getMercatorY(line[i][1]) - ya;
119
+ const crossings = [0, 1]
120
+ .map(edge => ({edge, t: (edge - ya) / dy}))
121
+ .filter(({t}) => t > 0 && t < 1)
122
+ .sort((p, q) => p.t - q.t);
123
+ for (const {edge, t} of crossings) points.push(interpolate(a, line[i], t, edge));
124
+ }
125
+ points.push(line[i]);
126
+ }
127
+ const ys = points.map(point => getMercatorY(point[1]));
128
+ const distances = [0];
129
+ for (let i = 1; i < points.length; i++) {
130
+ const dx = (points[i][0] - points[i - 1][0]) / 360;
131
+ const dy = Math.min(1, Math.max(0, ys[i])) - Math.min(1, Math.max(0, ys[i - 1]));
132
+ distances.push(distances[i - 1] + Math.hypot(dx, dy));
133
+ }
134
+ const length = distances[distances.length - 1] || 1;
135
+ const parts: LinePart[] = [];
136
+ let first = -1;
137
+ for (let i = 0; i < points.length; i++) {
138
+ // A segment is inside when its middle is: its ends are inside or on an edge.
139
+ const middle = i < points.length - 1 ? (ys[i] + ys[i + 1]) / 2 : NaN;
140
+ const inside = isInsideY(middle);
141
+ if (inside && first < 0) {
142
+ first = i;
143
+ } else if (!inside && first >= 0) {
144
+ parts.push({line: points.slice(first, i + 1), start: distances[first] / length, end: distances[i] / length});
145
+ first = -1;
146
+ }
147
+ }
148
+ return parts;
149
+ }
150
+
151
+ /**
152
+ * A polygon ring cut at the edges of the Web Mercator world (Sutherland-Hodgman), closed, or
153
+ * `null` if less than a triangle is left.
154
+ */
155
+ function clipRing(ring: Position[]): Position[] | null {
156
+ let points = ring;
157
+ for (const edge of [0, 1]) {
158
+ const inside = (y: number) =>
159
+ edge === 0 ? y >= -EDGE_TOLERANCE : y <= 1 + EDGE_TOLERANCE;
160
+ const clipped: Position[] = [];
161
+ for (let i = 0; i < points.length; i++) {
162
+ const a = points[i];
163
+ const b = points[(i + 1) % points.length];
164
+ const ya = getMercatorY(a[1]);
165
+ const yb = getMercatorY(b[1]);
166
+ if (inside(ya)) clipped.push(a);
167
+ if (inside(ya) !== inside(yb)) {
168
+ clipped.push(interpolate(a, b, (edge - ya) / (yb - ya), edge));
169
+ }
170
+ }
171
+ points = clipped;
172
+ }
173
+ const [first] = points;
174
+ const last = points[points.length - 1];
175
+ if (first && (first[0] !== last[0] || first[1] !== last[1])) {
176
+ points.push(first);
177
+ }
178
+ return points.length >= 4 ? points : null;
179
+ }
180
+
181
+ /** A polygon's rings cut at the edges of the world, or `null` if its outer ring is gone. */
182
+ function clipPolygon(rings: Position[][]): Position[][] | null {
183
+ const [outer, ...holes] = rings.map(ring => (ring.every(isInside) ? ring : clipRing(ring)));
184
+ return outer ? [outer, ...holes.filter((hole): hole is Position[] => hole !== null)] : null;
185
+ }
186
+
187
+ type Geometry = {type: string; coordinates?: any; geometries?: Geometry[]};
188
+
189
+ /** Whether all of a geometry's positions are in the Web Mercator world. */
190
+ function isGeometryInside(geometry: Geometry): boolean {
191
+ if (geometry.type === 'GeometryCollection') {
192
+ return (geometry.geometries || []).every(isGeometryInside);
193
+ }
194
+ const visit = (value: any): boolean =>
195
+ !Array.isArray(value) || (typeof value[0] === 'number' ? isInside(value) : value.every(visit));
196
+ return visit(geometry.coordinates);
197
+ }
198
+
199
+ const getLines = (parts: LinePart[]) => parts.map(part => part.line);
200
+
201
+ /**
202
+ * The properties that carry where a part of a cut line lies along the whole line, from 0 to 1,
203
+ * for `line-progress` (see `cutTile`).
204
+ */
205
+ const LINE_PART_START = 'tangram_line_part_start';
206
+ const LINE_PART_END = 'tangram_line_part_end';
207
+
208
+ /** The `line-progress` tags of geojson-vt, along the line it was given. */
209
+ const LINE_CLIP_START = 'geojsonvt_clip_start';
210
+ const LINE_CLIP_END = 'geojsonvt_clip_end';
211
+
212
+ /** A geometry cut at the edges of the Web Mercator world, or `null` if none of it is left. */
213
+ function clipGeometry(geometry: Geometry): Geometry | null {
214
+ const {type, coordinates} = geometry;
215
+ const multi = (multiType: string, parts: unknown[], singleType: string): Geometry | null =>
216
+ parts.length === 0
217
+ ? null
218
+ : parts.length === 1
219
+ ? {type: singleType, coordinates: parts[0]}
220
+ : {type: multiType, coordinates: parts};
221
+ switch (type) {
222
+ case 'Point':
223
+ return isInside(coordinates) ? geometry : null;
224
+ case 'MultiPoint':
225
+ return multi('MultiPoint', coordinates.filter(isInside), 'Point');
226
+ case 'LineString':
227
+ return multi('MultiLineString', getLines(clipLine(coordinates)), 'LineString');
228
+ case 'MultiLineString':
229
+ return multi('MultiLineString', getLines(coordinates.flatMap(clipLine)), 'LineString');
230
+ case 'Polygon':
231
+ return multi('MultiPolygon', [clipPolygon(coordinates)].filter(Boolean), 'Polygon');
232
+ case 'MultiPolygon':
233
+ return multi('MultiPolygon', coordinates.map(clipPolygon).filter(Boolean), 'Polygon');
234
+ case 'GeometryCollection': {
235
+ const geometries = (geometry.geometries || [])
236
+ .map(child => (isGeometryInside(child) ? child : clipGeometry(child)))
237
+ .filter((child): child is Geometry => child !== null);
238
+ return geometries.length ? {type, geometries} : null;
239
+ }
240
+ default:
241
+ return geometry;
242
+ }
243
+ }
244
+
245
+ /**
246
+ * GeoJSON data without what lies past the edges of the Web Mercator world, at +-85.0511 degrees,
247
+ * and how many features it dropped or cut. geojson-vt clamps latitudes past them onto them: a
248
+ * point at -89.5 degrees is drawn at -85.0511 degrees, which only a Web Mercator view cannot show
249
+ * apart. Here points past them are dropped, and lines and polygons end where they cross them, as
250
+ * straight lines in Web Mercator, as geojson-vt draws them. Features all inside are kept as they
251
+ * are, and data all inside is returned as it is.
252
+ *
253
+ * With `generateId`, each feature of cut data carries the id that geojson-vt gives it in the data
254
+ * as it was, its index: index the cut data without `generateId`. With `lineMetrics`, each part of a
255
+ * cut line is a feature of its own that carries where it lies along the whole line, so that its
256
+ * `line-progress` goes on along the line (see `cutTile`).
257
+ */
258
+ export function clipFeaturesToMercatorRange(
259
+ data: unknown,
260
+ {generateId = false, lineMetrics = false}: {generateId?: boolean; lineMetrics?: boolean} = {}
261
+ ): {data: unknown; outside: number} {
262
+ let outside = 0;
263
+ const clipFeature = (feature: any): any[] => {
264
+ const geometry = feature?.geometry as Geometry | null | undefined;
265
+ if (!geometry || isGeometryInside(geometry)) {
266
+ return [feature];
267
+ }
268
+ outside++;
269
+ const isLine = geometry.type === 'LineString' || geometry.type === 'MultiLineString';
270
+ if (lineMetrics && isLine) {
271
+ const lines: Position[][] =
272
+ geometry.type === 'LineString' ? [geometry.coordinates] : geometry.coordinates;
273
+ return lines.flatMap(clipLine).map(({line, start, end}) => ({
274
+ ...feature,
275
+ geometry: {type: 'LineString', coordinates: line},
276
+ properties: {...feature.properties, [LINE_PART_START]: start, [LINE_PART_END]: end}
277
+ }));
278
+ }
279
+ const clipped = clipGeometry(geometry);
280
+ return clipped ? [{...feature, geometry: clipped}] : [];
281
+ };
282
+ const withIds = (features: any[], index: number) =>
283
+ generateId ? features.map(feature => feature && {...feature, id: index}) : features;
284
+ const value = data as {type?: string; features?: unknown[]};
285
+ if (value?.type === 'FeatureCollection' && Array.isArray(value.features)) {
286
+ const features = value.features.map(clipFeature);
287
+ return outside
288
+ ? {data: {...value, features: features.flatMap(withIds)}, outside}
289
+ : {data, outside};
290
+ }
291
+ if (value?.type === 'Feature') {
292
+ const features = clipFeature(value);
293
+ return outside ? {data: {type: 'FeatureCollection', features: withIds(features, 0)}, outside} : {data, outside};
294
+ }
295
+ if (value && typeof value === 'object' && 'type' in value) {
296
+ const geometry = value as Geometry;
297
+ if (isGeometryInside(geometry)) {
298
+ return {data, outside};
299
+ }
300
+ const features = clipFeature({type: 'Feature', properties: {}, geometry});
301
+ return {data: {type: 'FeatureCollection', features: withIds(features, 0)}, outside};
302
+ }
303
+ return {data, outside};
54
304
  }
55
305
 
56
306
  /** A ring's signed area in tile units, as `@mapbox/vector-tile` computes it. */
@@ -110,6 +360,22 @@ function getGeometry(feature: GeoJSONVTFeature): {type: GeometryType; coordinate
110
360
  : {type: 'MultiPolygon', coordinates: polygons};
111
361
  }
112
362
 
363
+ /**
364
+ * A tile feature's properties. A part of a line cut at the edges of the Web Mercator world (see
365
+ * `clipFeaturesToMercatorRange`) gets its `line-progress` along the whole line.
366
+ */
367
+ function getProperties(tags: Record<string, unknown> | null | undefined): Record<string, unknown> {
368
+ if (!tags || !(LINE_PART_START in tags)) {
369
+ return tags || {};
370
+ }
371
+ const {[LINE_PART_START]: start, [LINE_PART_END]: end, ...properties} = tags as Record<string, any>;
372
+ if (LINE_CLIP_START in properties) {
373
+ properties[LINE_CLIP_START] = start + properties[LINE_CLIP_START] * (end - start);
374
+ properties[LINE_CLIP_END] = start + properties[LINE_CLIP_END] * (end - start);
375
+ }
376
+ return properties;
377
+ }
378
+
113
379
  /**
114
380
  * Cuts one tile from a tile index, as MapLibre GL JS cuts it, and encodes its features. Its typed
115
381
  * arrays' buffers can be transferred.
@@ -136,7 +402,7 @@ export function cutTile(index: GeoJSONVT, z: number, x: number, y: number): Enco
136
402
  continue;
137
403
  }
138
404
  writeCoordinates(geometry.coordinates, GEOMETRY_DEPTHS[geometry.type]);
139
- const encoded: EncodedFeature = {type: geometry.type, properties: feature.tags || {}};
405
+ const encoded: EncodedFeature = {type: geometry.type, properties: getProperties(feature.tags)};
140
406
  if (feature.id !== undefined) {
141
407
  encoded.id = feature.id;
142
408
  }
@@ -14,13 +14,23 @@
14
14
 
15
15
  import type {GeoJSONVT, GeoJSONVTOptions} from '@maplibre/geojson-vt';
16
16
  import {createTileIndex, cutTile} from './geojson-tiler';
17
+ import {installGeoJsonTaskHandler} from './geojson-task-handler';
17
18
 
18
19
  /** A message from the main thread. Messages of one source arrive in the order they are sent. */
19
20
  export type GeoJsonWorkerRequest =
20
21
  /** Features of a FeatureCollection, sent in batches before its `index` message. */
21
22
  | {type: 'features'; source: number; features: unknown[]}
22
- /** Builds a source's index from `data`, or from the features sent before it. */
23
- | {type: 'index'; source: number; data?: unknown; options: GeoJSONVTOptions}
23
+ /**
24
+ * Builds a source's index from `data`, or from the features sent before it, without what lies
25
+ * past the edges of the Web Mercator world with `clipToMercatorRange` (see `createTileIndex`).
26
+ */
27
+ | {
28
+ type: 'index';
29
+ source: number;
30
+ data?: unknown;
31
+ options: GeoJSONVTOptions;
32
+ clipToMercatorRange?: boolean;
33
+ }
24
34
  | {type: 'tile'; source: number; request: number; z: number; x: number; y: number}
25
35
  | {type: 'remove'; source: number};
26
36
 
@@ -37,11 +47,28 @@ const pendingFeatures = new Map<number, unknown[]>();
37
47
  const indexes = new Map<number, GeoJSONVT | Error>();
38
48
 
39
49
  const scope = globalThis as unknown as {
40
- onmessage: ((event: MessageEvent<GeoJsonWorkerRequest>) => void) | null;
50
+ addEventListener: (type: 'message', listener: (event: MessageEvent<unknown>) => void) => void;
41
51
  postMessage: (message: GeoJsonWorkerResponse, transfer: Transferable[]) => void;
42
52
  };
43
53
 
44
- scope.onmessage = ({data: message}) => {
54
+ let taskInstalled = false;
55
+ let taskReady: Promise<void> | undefined;
56
+ const pendingTaskMessages: unknown[] = [];
57
+
58
+ scope.addEventListener('message', event => {
59
+ const {data} = event;
60
+ if (data && typeof data === 'object' && 'source' in data && data.source === 'loaders.gl') {
61
+ if (!taskInstalled) {
62
+ event.stopImmediatePropagation();
63
+ pendingTaskMessages.push(data);
64
+ taskReady ??= installGeoJsonTaskHandler().then(() => {
65
+ taskInstalled = true;
66
+ for (const data of pendingTaskMessages.splice(0)) globalThis.dispatchEvent(new MessageEvent('message', {data}));
67
+ });
68
+ }
69
+ return;
70
+ }
71
+ const message = data as GeoJsonWorkerRequest;
45
72
  switch (message.type) {
46
73
  case 'features': {
47
74
  const features = pendingFeatures.get(message.source);
@@ -61,7 +88,10 @@ scope.onmessage = ({data: message}) => {
61
88
  };
62
89
  pendingFeatures.delete(message.source);
63
90
  try {
64
- indexes.set(message.source, createTileIndex(data, message.options));
91
+ indexes.set(
92
+ message.source,
93
+ createTileIndex(data, message.options, message.clipToMercatorRange).index
94
+ );
65
95
  } catch (error) {
66
96
  indexes.set(message.source, error instanceof Error ? error : new Error(String(error)));
67
97
  }
@@ -95,4 +125,4 @@ scope.onmessage = ({data: message}) => {
95
125
  break;
96
126
  default:
97
127
  }
98
- };
128
+ });
@@ -4,6 +4,7 @@
4
4
 
5
5
  import {LayerExtension, project} from '@deck.gl/core';
6
6
  import type {Layer} from '@deck.gl/core';
7
+ import {createShaderInjectionPlugin} from './shader-injection-plugin';
7
8
 
8
9
  /**
9
10
  * Hides every vertex of an instance whose anchor (`geometry.worldPosition`) lies beyond the
@@ -19,15 +20,24 @@ import type {Layer} from '@deck.gl/core';
19
20
  * point away from the viewer. The fourth row of an orthographic view-projection matrix is
20
21
  * `(0, 0, 0, 1)`, and its third row is the direction in which depth grows.
21
22
  */
22
- const globeHorizonModule = {
23
+ const globeHorizonModule = createShaderInjectionPlugin({
23
24
  name: 'globeHorizon',
24
25
  dependencies: [project],
25
26
  inject: {
27
+ 'vs:#decl': /* glsl */ `
28
+ bool tangramBillboardOccluded(vec3 commonPosition) {
29
+ vec3 wRow = vec3(project.viewProjectionMatrix[0][3],
30
+ project.viewProjectionMatrix[1][3], project.viewProjectionMatrix[2][3]);
31
+ return dot(wRow, wRow) > 0.0 && project_globe_is_occluded(commonPosition);
32
+ }
33
+ // The native test is perspective-only; the hook below tests parallel rays instead.
34
+ #define project_globe_is_occluded tangramBillboardOccluded
35
+ `,
26
36
  'vs:DECKGL_FILTER_GL_POSITION': {
27
37
  order: 1,
28
38
  injection: /* glsl */ `
29
39
  if (project.projectionMode == PROJECTION_MODE_GLOBE) {
30
- vec3 globeHorizon_normal = normalize(project_position(geometry.worldPosition));
40
+ vec3 globeHorizon_normal = normalize(geometry.position.xyz);
31
41
  mat4 globeHorizon_m = project.viewProjectionMatrix;
32
42
  vec3 globeHorizon_wRow = vec3(globeHorizon_m[0][3], globeHorizon_m[1][3], globeHorizon_m[2][3]);
33
43
  bool globeHorizon_hidden = dot(globeHorizon_wRow, globeHorizon_wRow) == 0.0
@@ -42,7 +52,7 @@ const globeHorizonModule = {
42
52
  `
43
53
  }
44
54
  }
45
- } as const;
55
+ });
46
56
 
47
57
  /**
48
58
  * Hides labels and icons whose anchor is on the far side of the globe in a `GlobeView`, in the
@@ -59,6 +69,6 @@ export class GlobeHorizonExtension extends LayerExtension {
59
69
  if (this.context.device.type === 'webgpu') {
60
70
  return {};
61
71
  }
62
- return {modules: [globeHorizonModule]};
72
+ return {plugins: [globeHorizonModule]};
63
73
  }
64
74
  }