@tangram.gl/tangram-renderer 0.0.0-stage → 0.0.0

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 (321) hide show
  1. package/README.md +59 -2
  2. package/dist/core.js +36923 -0
  3. package/dist/core.js.map +6 -0
  4. package/dist/index.js +32 -0
  5. package/dist/loaders-gl-worker.js +82 -0
  6. package/dist/loaders-gl-worker.js.map +6 -0
  7. package/dist/map-logic.js +2 -0
  8. package/dist/map-logic.js.map +6 -0
  9. package/dist/projected-basemaps-worker.js +3 -0
  10. package/dist/projected-basemaps-worker.js.map +6 -0
  11. package/dist/shader-modules.js +141 -0
  12. package/dist/shader-modules.js.map +6 -0
  13. package/dist/style-schema.js +126 -0
  14. package/dist/style-schema.js.map +6 -0
  15. package/dist/tangram-style.schema.json +882 -0
  16. package/dist/tangram.debug.mjs +37139 -0
  17. package/dist/tangram.debug.mjs.map +6 -0
  18. package/dist/tangram.min.mjs +4950 -0
  19. package/dist/types/builders/earcut.d.ts +15 -0
  20. package/dist/types/core.d.ts +29 -0
  21. package/dist/types/gl/constants.d.ts +2 -0
  22. package/dist/types/gl/globe_mesh.d.ts +48 -0
  23. package/dist/types/gl/glsl.d.ts +17 -0
  24. package/dist/types/gl/vertex-constants.d.ts +4 -0
  25. package/dist/types/gl/vertex-types.d.ts +34 -0
  26. package/dist/types/gl/vertex_data.d.ts +32 -0
  27. package/dist/types/gl/vertex_elements.d.ts +8 -0
  28. package/dist/types/gl/vertex_layout.d.ts +32 -0
  29. package/dist/types/glsl.d.ts +14 -0
  30. package/dist/types/gpu/luma_device_renderer.d.ts +66 -0
  31. package/dist/types/gpu/resource_lifecycle.d.ts +4 -0
  32. package/dist/types/gpu/tangram_gpu_backend.d.ts +151 -0
  33. package/dist/types/index.d.ts +155 -0
  34. package/dist/types/lights/light-definitions.d.ts +121 -0
  35. package/dist/types/map-logic/draped-terrain-bounds.d.ts +41 -0
  36. package/dist/types/map-logic/globe-terrain-preparation.d.ts +85 -0
  37. package/dist/types/map-logic/globe-terrain.d.ts +54 -0
  38. package/dist/types/map-logic/index.d.ts +33 -0
  39. package/dist/types/map-logic/label-identity.d.ts +22 -0
  40. package/dist/types/map-logic/label-placement.d.ts +32 -0
  41. package/dist/types/map-logic/screen-bounds.d.ts +15 -0
  42. package/dist/types/map-logic/terrain-data.d.ts +55 -0
  43. package/dist/types/map-logic/terrain-error.d.ts +51 -0
  44. package/dist/types/map-logic/terrain-ground-topology.d.ts +26 -0
  45. package/dist/types/map-logic/terrain-height-field.d.ts +59 -0
  46. package/dist/types/map-logic/terrain-navigation.d.ts +27 -0
  47. package/dist/types/map-logic/terrain-neighborhood.d.ts +5 -0
  48. package/dist/types/map-logic/terrain-query.d.ts +33 -0
  49. package/dist/types/map-logic/terrain-resource-budget.d.ts +51 -0
  50. package/dist/types/map-logic/terrain-transition-topology.d.ts +23 -0
  51. package/dist/types/map-logic/tile-build-queue.d.ts +51 -0
  52. package/dist/types/map-logic/tile-cache-policy.d.ts +46 -0
  53. package/dist/types/map-logic/tile-residency.d.ts +17 -0
  54. package/dist/types/map-logic/triangle-query-index.d.ts +87 -0
  55. package/dist/types/procedures/js-yaml.d.ts +15 -0
  56. package/dist/types/procedures/mesh-projector.d.ts +107 -0
  57. package/dist/types/procedures/mvt-dependencies.d.ts +20 -0
  58. package/dist/types/procedures/projected-coordinate-transform.d.ts +27 -0
  59. package/dist/types/procedures/projection-batch-executor.d.ts +53 -0
  60. package/dist/types/procedures/scene-yaml-legacy.d.ts +6 -0
  61. package/dist/types/procedures/terrain-draping.d.ts +165 -0
  62. package/dist/types/procedures/terrain-globe.d.ts +6 -0
  63. package/dist/types/procedures/terrain-styled-topology.d.ts +34 -0
  64. package/dist/types/procedures/web-mercator-legacy.d.ts +4 -0
  65. package/dist/types/procedures/web-mercator-math.d.ts +4 -0
  66. package/dist/types/scene/globals.d.ts +11 -0
  67. package/dist/types/scene/ground_footprint.d.ts +16 -0
  68. package/dist/types/scene/host_frame.d.ts +54 -0
  69. package/dist/types/scene/projection_math.d.ts +52 -0
  70. package/dist/types/scene/scene-resource-types.d.ts +177 -0
  71. package/dist/types/scene/scene_bundle.d.ts +69 -0
  72. package/dist/types/scene/visibility_adapter.d.ts +106 -0
  73. package/dist/types/selection/draped-terrain-surface.d.ts +78 -0
  74. package/dist/types/selection/terrain_surface.d.ts +149 -0
  75. package/dist/types/shader-modules/globe-horizon.d.ts +15 -0
  76. package/dist/types/shader-modules/height-decode.d.ts +13 -0
  77. package/dist/types/shader-modules/hillshade.d.ts +13 -0
  78. package/dist/types/shader-modules/index.d.ts +6 -0
  79. package/dist/types/shader-modules/planar.d.ts +12 -0
  80. package/dist/types/shader-modules/sphere-map.d.ts +13 -0
  81. package/dist/types/shader-modules/triplanar.d.ts +15 -0
  82. package/dist/types/sources/decoded_tile_store.d.ts +95 -0
  83. package/dist/types/sources/tile_source_metadata.d.ts +32 -0
  84. package/dist/types/styles/text/fontfaceobserver.d.ts +10 -0
  85. package/dist/types/tile/tile_id.d.ts +37 -0
  86. package/dist/types/tile/tile_traversal_adapter.d.ts +63 -0
  87. package/dist/types/types.d.ts +293 -0
  88. package/dist/types/utils/geo.d.ts +57 -0
  89. package/dist/types/utils/hash.d.ts +1 -0
  90. package/dist/types/utils/log.d.ts +27 -0
  91. package/dist/types/utils/props.d.ts +6 -0
  92. package/dist/types/utils/subscribe.d.ts +10 -0
  93. package/dist/types/utils/thread.d.ts +6 -0
  94. package/dist/types/utils/urls.d.ts +26 -0
  95. package/dist/types/utils/utils.d.ts +26 -0
  96. package/dist/types/utils/version.d.ts +2 -0
  97. package/dist/types/utils/worker-types.d.ts +68 -0
  98. package/dist/types/utils/worker_broker.d.ts +3 -0
  99. package/package.json +103 -3
  100. package/src/builders/common.ts +41 -0
  101. package/src/builders/earcut.d.ts +15 -0
  102. package/src/builders/geometry-types.ts +60 -0
  103. package/src/builders/points.ts +115 -0
  104. package/src/builders/polygons.ts +225 -0
  105. package/src/builders/polylines.ts +682 -0
  106. package/src/builders/wireframe.ts +31 -0
  107. package/src/core.d.ts +29 -0
  108. package/src/core.ts +33 -0
  109. package/src/experimental/loaders-gl-worker.ts +19 -0
  110. package/src/experimental/projected-basemaps-worker.ts +13 -0
  111. package/src/experimental/projected-mesh-cache.ts +137 -0
  112. package/src/experimental/projected-mesh-domain.ts +110 -0
  113. package/src/experimental/projected-mesh-refinement.ts +193 -0
  114. package/src/experimental/projected-mesh.ts +399 -0
  115. package/src/experimental/projected-normal.ts +26 -0
  116. package/src/experimental/projected-symbol-mesh.ts +74 -0
  117. package/src/gl/constants.ts +18 -0
  118. package/src/gl/context.ts +103 -0
  119. package/src/gl/extensions.ts +30 -0
  120. package/src/gl/globe_mesh.ts +285 -0
  121. package/src/gl/glsl.ts +257 -0
  122. package/src/gl/mesh-types.ts +56 -0
  123. package/src/gl/render_state.ts +111 -0
  124. package/src/gl/shader_program.ts +1310 -0
  125. package/src/gl/texture.ts +814 -0
  126. package/src/gl/uniform_buffer.ts +340 -0
  127. package/src/gl/vao.ts +154 -0
  128. package/src/gl/vbo_mesh.ts +349 -0
  129. package/src/gl/vertex-constants.ts +19 -0
  130. package/src/gl/vertex-types.ts +35 -0
  131. package/src/gl/vertex_data.ts +126 -0
  132. package/src/gl/vertex_elements.ts +54 -0
  133. package/src/gl/vertex_layout.ts +241 -0
  134. package/src/glsl.d.ts +14 -0
  135. package/src/gpu/luma_device_renderer.ts +579 -0
  136. package/src/gpu/pixel_readback.ts +51 -0
  137. package/src/gpu/resource_lifecycle.ts +32 -0
  138. package/src/gpu/tangram_gpu_backend.ts +195 -0
  139. package/src/index.d.ts +155 -0
  140. package/src/index.ts +105 -0
  141. package/src/labels/collision-types.ts +94 -0
  142. package/src/labels/collision.ts +273 -0
  143. package/src/labels/collision_grid.ts +61 -0
  144. package/src/labels/intersect.ts +30 -0
  145. package/src/labels/label-types.ts +91 -0
  146. package/src/labels/label.ts +207 -0
  147. package/src/labels/label_line.ts +801 -0
  148. package/src/labels/label_point.ts +131 -0
  149. package/src/labels/main-pass-types.ts +73 -0
  150. package/src/labels/main_pass.ts +221 -0
  151. package/src/labels/point_anchor.ts +100 -0
  152. package/src/labels/point_placement.ts +150 -0
  153. package/src/labels/projected-pass.ts +259 -0
  154. package/src/labels/repeat_group.ts +72 -0
  155. package/src/lights/ambient_light.glsl +18 -0
  156. package/src/lights/directional_light.glsl +39 -0
  157. package/src/lights/geographic-lights.ts +21 -0
  158. package/src/lights/light-definitions.ts +285 -0
  159. package/src/lights/light.ts +590 -0
  160. package/src/lights/lighting-uniforms.ts +80 -0
  161. package/src/lights/lighting-wgsl.ts +116 -0
  162. package/src/lights/material.glsl +237 -0
  163. package/src/lights/material.ts +145 -0
  164. package/src/lights/native-falloff.ts +37 -0
  165. package/src/lights/point_light.glsl +96 -0
  166. package/src/lights/spot_light.glsl +111 -0
  167. package/src/map-logic/draped-terrain-bounds.ts +71 -0
  168. package/src/map-logic/globe-terrain-preparation.ts +217 -0
  169. package/src/map-logic/globe-terrain.ts +296 -0
  170. package/src/map-logic/index.ts +37 -0
  171. package/src/map-logic/label-identity.ts +36 -0
  172. package/src/map-logic/label-placement.ts +117 -0
  173. package/src/map-logic/screen-bounds.ts +34 -0
  174. package/src/map-logic/terrain-data.ts +107 -0
  175. package/src/map-logic/terrain-error.ts +134 -0
  176. package/src/map-logic/terrain-ground-topology.ts +164 -0
  177. package/src/map-logic/terrain-height-field.ts +254 -0
  178. package/src/map-logic/terrain-navigation.ts +50 -0
  179. package/src/map-logic/terrain-neighborhood.ts +43 -0
  180. package/src/map-logic/terrain-query.ts +51 -0
  181. package/src/map-logic/terrain-resource-budget.ts +124 -0
  182. package/src/map-logic/terrain-transition-topology.ts +164 -0
  183. package/src/map-logic/tile-build-queue.ts +110 -0
  184. package/src/map-logic/tile-cache-policy.ts +89 -0
  185. package/src/map-logic/tile-residency.ts +50 -0
  186. package/src/map-logic/triangle-query-index.ts +265 -0
  187. package/src/procedures/js-yaml.d.ts +15 -0
  188. package/src/procedures/mesh-projector.ts +191 -0
  189. package/src/procedures/mlt-loaders.ts +55 -0
  190. package/src/procedures/mvt-dependencies.d.ts +20 -0
  191. package/src/procedures/mvt-legacy.ts +150 -0
  192. package/src/procedures/mvt-loaders.ts +53 -0
  193. package/src/procedures/mvt-parser.ts +48 -0
  194. package/src/procedures/mvt-properties.ts +36 -0
  195. package/src/procedures/mvt-tile-provider.ts +72 -0
  196. package/src/procedures/pmtiles-loader.ts +75 -0
  197. package/src/procedures/projected-coordinate-transform.ts +79 -0
  198. package/src/procedures/projection-batch-executor.ts +140 -0
  199. package/src/procedures/scene-yaml-legacy.ts +16 -0
  200. package/src/procedures/scene-yaml-loaders.ts +10 -0
  201. package/src/procedures/terrain-draping.ts +713 -0
  202. package/src/procedures/terrain-globe.ts +32 -0
  203. package/src/procedures/terrain-styled-topology.ts +184 -0
  204. package/src/procedures/web-mercator-legacy.ts +24 -0
  205. package/src/procedures/web-mercator-math.ts +50 -0
  206. package/src/scene/camera.ts +298 -0
  207. package/src/scene/camera_base.ts +116 -0
  208. package/src/scene/classic_scene.ts +20 -0
  209. package/src/scene/external_camera.ts +101 -0
  210. package/src/scene/globals.ts +120 -0
  211. package/src/scene/ground_footprint.ts +139 -0
  212. package/src/scene/host_frame.ts +326 -0
  213. package/src/scene/projected_tile_lod.ts +125 -0
  214. package/src/scene/projection_math.ts +142 -0
  215. package/src/scene/projection_shaders.ts +183 -0
  216. package/src/scene/renderer.ts +516 -0
  217. package/src/scene/scene-resource-types.ts +174 -0
  218. package/src/scene/scene.ts +2378 -0
  219. package/src/scene/scene_bundle.ts +304 -0
  220. package/src/scene/scene_debug.ts +129 -0
  221. package/src/scene/scene_loader.ts +402 -0
  222. package/src/scene/scene_worker.ts +528 -0
  223. package/src/scene/view.ts +683 -0
  224. package/src/scene/visibility_adapter.ts +319 -0
  225. package/src/scene/worker_url.ts +15 -0
  226. package/src/selection/draped-terrain-surface.ts +377 -0
  227. package/src/selection/selection-types.ts +79 -0
  228. package/src/selection/selection.ts +542 -0
  229. package/src/selection/selection_fragment.glsl +18 -0
  230. package/src/selection/selection_globals.glsl +9 -0
  231. package/src/selection/selection_pixels.ts +59 -0
  232. package/src/selection/selection_vertex.glsl +15 -0
  233. package/src/selection/terrain_surface.ts +298 -0
  234. package/src/shader-modules/globe-horizon.ts +52 -0
  235. package/src/shader-modules/height-decode.ts +41 -0
  236. package/src/shader-modules/hillshade.ts +70 -0
  237. package/src/shader-modules/index.ts +10 -0
  238. package/src/shader-modules/planar.ts +24 -0
  239. package/src/shader-modules/sphere-map.ts +38 -0
  240. package/src/shader-modules/triplanar.ts +45 -0
  241. package/src/sources/aligned_tile_source.ts +122 -0
  242. package/src/sources/data_source.ts +655 -0
  243. package/src/sources/decoded_tile_store.ts +245 -0
  244. package/src/sources/geojson.ts +300 -0
  245. package/src/sources/mvt.ts +247 -0
  246. package/src/sources/raster.ts +341 -0
  247. package/src/sources/shared_tile_source_adapter.ts +117 -0
  248. package/src/sources/sources.ts +10 -0
  249. package/src/sources/tile_source_adapter.ts +84 -0
  250. package/src/sources/tile_source_metadata.ts +65 -0
  251. package/src/sources/tile_source_state.ts +112 -0
  252. package/src/sources/topojson.ts +90 -0
  253. package/src/styles/feature_annotations.ts +16 -0
  254. package/src/styles/filter.ts +190 -0
  255. package/src/styles/geometry-style-types.ts +209 -0
  256. package/src/styles/globe_visibility_wgsl.ts +22 -0
  257. package/src/styles/layer-types.ts +45 -0
  258. package/src/styles/layer.ts +586 -0
  259. package/src/styles/lines/dasharray.ts +49 -0
  260. package/src/styles/lines/lines.ts +707 -0
  261. package/src/styles/lines/lines_wgsl.ts +224 -0
  262. package/src/styles/points/point-runtime-types.ts +125 -0
  263. package/src/styles/points/points.ts +1079 -0
  264. package/src/styles/points/points_fragment.glsl +126 -0
  265. package/src/styles/points/points_vertex.glsl +230 -0
  266. package/src/styles/points/points_wgsl.ts +189 -0
  267. package/src/styles/polygons/polygons.ts +268 -0
  268. package/src/styles/polygons/polygons_fragment.glsl +173 -0
  269. package/src/styles/polygons/polygons_vertex.glsl +213 -0
  270. package/src/styles/polygons/polygons_wgsl.ts +127 -0
  271. package/src/styles/property-types.ts +125 -0
  272. package/src/styles/raster/raster.ts +33 -0
  273. package/src/styles/raster/raster_globals.glsl +46 -0
  274. package/src/styles/style-mixing-types.ts +101 -0
  275. package/src/styles/style-runtime-types.ts +220 -0
  276. package/src/styles/style-schema.ts +133 -0
  277. package/src/styles/style.ts +1033 -0
  278. package/src/styles/style_globals.glsl +69 -0
  279. package/src/styles/style_manager.ts +464 -0
  280. package/src/styles/style_parser.ts +620 -0
  281. package/src/styles/text/font_manager.ts +180 -0
  282. package/src/styles/text/fontfaceobserver.d.ts +10 -0
  283. package/src/styles/text/text-types.ts +220 -0
  284. package/src/styles/text/text.ts +378 -0
  285. package/src/styles/text/text_canvas.ts +773 -0
  286. package/src/styles/text/text_labels.ts +436 -0
  287. package/src/styles/text/text_segments.ts +142 -0
  288. package/src/styles/text/text_settings.ts +228 -0
  289. package/src/styles/text/text_wgsl.ts +159 -0
  290. package/src/styles/text/text_wrap.ts +194 -0
  291. package/src/tile/globe_tile_preload.ts +40 -0
  292. package/src/tile/tangram_tileset_2d.ts +108 -0
  293. package/src/tile/tile-build-types.ts +155 -0
  294. package/src/tile/tile.ts +807 -0
  295. package/src/tile/tile_id.ts +172 -0
  296. package/src/tile/tile_manager.ts +889 -0
  297. package/src/tile/tile_pyramid.ts +119 -0
  298. package/src/tile/tile_resource_cache.ts +91 -0
  299. package/src/tile/tile_traversal_adapter.ts +132 -0
  300. package/src/types.ts +332 -0
  301. package/src/utils/debounce.ts +17 -0
  302. package/src/utils/debug_settings.ts +43 -0
  303. package/src/utils/errors.ts +12 -0
  304. package/src/utils/functions.ts +95 -0
  305. package/src/utils/geo.ts +364 -0
  306. package/src/utils/hash.ts +14 -0
  307. package/src/utils/log.ts +108 -0
  308. package/src/utils/media_capture.ts +193 -0
  309. package/src/utils/merge.ts +44 -0
  310. package/src/utils/obb.ts +134 -0
  311. package/src/utils/props.ts +43 -0
  312. package/src/utils/slice.ts +14 -0
  313. package/src/utils/subscribe.ts +58 -0
  314. package/src/utils/task.ts +154 -0
  315. package/src/utils/thread.ts +26 -0
  316. package/src/utils/urls.ts +223 -0
  317. package/src/utils/utils.ts +310 -0
  318. package/src/utils/vector.ts +197 -0
  319. package/src/utils/version.ts +8 -0
  320. package/src/utils/worker-types.ts +79 -0
  321. package/src/utils/worker_broker.ts +523 -0
@@ -0,0 +1,528 @@
1
+ // Tangram
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) 2013-2016 Brett Camper and Mapzen
4
+ // Copyright (c) 2026 vis.gl contributors
5
+
6
+
7
+ /*jshint worker: true*/
8
+
9
+ import Utils from '../utils/utils';
10
+ import {compileFunctionStrings, functionStringCache, clearFunctionStringCache} from '../utils/functions';
11
+ import debugSettings, {mergeDebugSettings} from '../utils/debug_settings';
12
+ import log from '../utils/log';
13
+ import mergeObjects from '../utils/merge';
14
+ import WorkerBroker from '../utils/worker_broker'; // jshint ignore:line
15
+ import Tile from '../tile/tile';
16
+ import Geo from '../utils/geo';
17
+ import DataSource from '../sources/data_source';
18
+ import {iterateSourceTiles} from '../sources/tile_source_adapter';
19
+ import SharedTileSourceAdapter from '../sources/shared_tile_source_adapter';
20
+ import {getFeatureRenderedGeneration} from '../styles/feature_annotations';
21
+ import {registerMvtDecoder} from '../procedures/mvt-parser';
22
+ import {registerMvtTileProvider} from '../procedures/mvt-tile-provider';
23
+ import type {TangramTileSourceMetadata} from '../sources/tile_source_metadata';
24
+ import '../sources/sources';
25
+ import FeatureSelection from '../selection/selection';
26
+ import StyleParser from '../styles/style_parser';
27
+ import {StyleManager} from '../styles/style_manager';
28
+ import {parseLayers, FilterOptions, layerCache} from '../styles/layer';
29
+ import {buildFilter} from '../styles/filter';
30
+ import Texture from '../gl/texture';
31
+ import VertexElements from '../gl/vertex_elements';
32
+ import Label from '../labels/label';
33
+ import type {WorkerBuildTile, TileBuildStyle} from '../tile/tile-build-types';
34
+ import type {DecodedTileFeature} from '../tile/tile-build-types';
35
+ import type {LayerTree} from '../styles/layer';
36
+ import type {StyleDefinition} from '../styles/style-mixing-types';
37
+ import type {SceneSourceDefinition} from './scene-resource-types';
38
+ import type {LogLevel} from '../utils/log';
39
+ import type {TerrainDrapingSnapshot} from '../procedures/terrain-draping';
40
+ import type {DebugSettings} from '../utils/debug_settings';
41
+
42
+ /** Configuration serialized by Scene after global/resource normalization. */
43
+ interface WorkerSceneConfiguration extends Record<string, unknown> {
44
+ global: Record<string, unknown>;
45
+ sources: Record<string, SceneSourceDefinition>;
46
+ styles: Record<string, StyleDefinition & import('./scene-resource-types').SceneStyleDefinition>;
47
+ layers: Record<string, import('../styles/layer-types').SceneLayerDefinition>;
48
+ textures?: Record<string, import('./scene-resource-types').SceneResourceDefinition>;
49
+ }
50
+ /** Worker-local ownership installed by init and updateConfig before builds are dispatched. */
51
+ interface WorkerSceneState {
52
+ scene_id: number;
53
+ _worker_id: number;
54
+ num_workers: number;
55
+ generation: number;
56
+ introspection: boolean;
57
+ global: Record<string, unknown>;
58
+ sources: Record<string, DataSource>;
59
+ styles: Record<string, import('../styles/style-mixing-types').ManagedStyle>;
60
+ layers: Record<string, LayerTree>;
61
+ tiles: Record<string, WorkerBuildTile>;
62
+ style_manager: StyleManager;
63
+ sharedTileSources: SharedTileSourceAdapter<WorkerBuildTile>;
64
+ last_config_sources: Record<string, SceneSourceDefinition>;
65
+ config_sources: Record<string, SceneSourceDefinition>;
66
+ syncing_textures: Promise<unknown>;
67
+ configuring: Promise<void>;
68
+ finalizeTileSources(): void;
69
+ importExternalScripts(scripts: string[]): void;
70
+ createDataSources(config: Pick<WorkerSceneConfiguration, 'sources'>): void;
71
+ syncTextures(textures?: Record<string, unknown>): Promise<unknown>;
72
+ getTile(key: string): WorkerBuildTile | undefined;
73
+ awaitConfiguration(): Promise<void>;
74
+ loadTileSourceData(tile: WorkerBuildTile): Promise<unknown>;
75
+ removeTile(key: string): void;
76
+ }
77
+
78
+ /** Match the geometry builder's null-geometry admission before evaluating worker queries. */
79
+ function hasQueryGeometry(feature: DecodedTileFeature): feature is DecodedTileFeature & {geometry: NonNullable<DecodedTileFeature['geometry']>} {
80
+ return feature.geometry !== null;
81
+ }
82
+
83
+ import {registerMeshProjector, clearMeshProjectorPreparation, getMeshProjectorPreparationStatistics,
84
+ getMeshProjectorWorkStatistics} from '../procedures/mesh-projector';
85
+
86
+ /** Bootstrap retains the original own-property shape; init installs the declared lazy state. */
87
+ class WorkerSceneState {
88
+ declare scene_id: number;
89
+ declare _worker_id: number;
90
+ declare num_workers: number;
91
+ declare generation: number;
92
+ declare introspection: boolean;
93
+ declare global: Record<string, unknown>;
94
+ declare style_manager: StyleManager;
95
+ declare last_config_sources: Record<string, SceneSourceDefinition>;
96
+ declare config_sources: Record<string, SceneSourceDefinition>;
97
+ declare syncing_textures: Promise<unknown>;
98
+ declare configuring: Promise<void>;
99
+ sources: WorkerSceneState['sources'] = {};
100
+ styles: WorkerSceneState['styles'] = {};
101
+ layers: WorkerSceneState['layers'] = {};
102
+ tiles: WorkerSceneState['tiles'] = {};
103
+ sharedTileSources = new SharedTileSourceAdapter<WorkerBuildTile>();
104
+ }
105
+
106
+ const SceneWorker = Object.assign(self, new WorkerSceneState(), {
107
+
108
+ FeatureSelection,
109
+ registerMvtDecoder,
110
+ registerMvtTileProvider,
111
+ registerMeshProjector,
112
+
113
+
114
+ // Initialize worker
115
+ init (this: WorkerSceneState, scene_id: number, worker_id: number, num_workers: number, log_level: LogLevel, device_pixel_ratio: number, has_element_index_unit: boolean, external_scripts: string[], maxConcurrentLoads?: number): number {
116
+ const sharedTileSources = new SharedTileSourceAdapter<WorkerBuildTile>(external_scripts.length === 0, {maxConcurrentLoads});
117
+ this.finalizeTileSources();
118
+ this.sharedTileSources = sharedTileSources;
119
+ this.scene_id = scene_id;
120
+ this._worker_id = worker_id;
121
+ this.num_workers = num_workers;
122
+ log.setLevel(log_level);
123
+ Utils.device_pixel_ratio = device_pixel_ratio;
124
+ VertexElements.setElementIndexUint(has_element_index_unit);
125
+ FeatureSelection.setPrefix(this._worker_id);
126
+ this.style_manager = new StyleManager();
127
+ this.importExternalScripts(external_scripts);
128
+ Label.id_prefix = worker_id;
129
+ Label.id_multiplier = num_workers;
130
+ return worker_id;
131
+ },
132
+
133
+ // Import custom external scripts
134
+ importExternalScripts(this: WorkerSceneState, scripts: string[]) {
135
+ if (scripts.length === 0) {
136
+ return;
137
+ }
138
+ log('debug', 'loading custom data source scripts in worker:', scripts);
139
+
140
+ // `window` is already shimmed to allow compatibility with some other libraries (e.g. FontFaceObserver)
141
+ // So there's an extra dance here to look for any additional `window` properties added by these script imports,
142
+ // then add them to the worker `self` scope.
143
+ let prev_names = Object.getOwnPropertyNames(window);
144
+
145
+ importScripts(...scripts);
146
+
147
+ Object.getOwnPropertyNames(window).forEach(prop => {
148
+ if (prev_names.indexOf(prop) === -1) {
149
+ Reflect.set(this, prop, Reflect.get(window, prop)); // new property added to window, also add it to self
150
+ }
151
+ });
152
+ },
153
+
154
+ // Starts a config refresh
155
+ updateConfig (this: WorkerSceneState, { config: serializedConfig, generation, introspection, shader_language = 'glsl', projection_target, terrain, terrainRibbonZoom }: {
156
+ config: string; generation: number; introspection: boolean; shader_language?: 'glsl' | 'wgsl'; projection_target?: string; terrain?: TerrainDrapingSnapshot; terrainRibbonZoom?: number;
157
+ }, debug: Partial<DebugSettings>) {
158
+ // This is the scene-owned broker envelope, not a provider data parser.
159
+ const config = JSON.parse(serializedConfig) as WorkerSceneConfiguration;
160
+ mergeDebugSettings(debug);
161
+
162
+ this.generation = generation;
163
+ this.introspection = introspection;
164
+
165
+ // Expand global properties
166
+ this.global = compileFunctionStrings(config.global);
167
+
168
+ // Create data sources
169
+ this.createDataSources(config);
170
+
171
+ // Expand styles
172
+ config.styles = compileFunctionStrings(config.styles, StyleParser.wrapFunction);
173
+ this.styles = this.style_manager.build(config.styles);
174
+ this.style_manager.initStyles({
175
+ generation: this.generation,
176
+ styles: this.styles,
177
+ sources: this.sources,
178
+ introspection: this.introspection,
179
+ shader_language,
180
+ projection_target,
181
+ terrain,
182
+ terrainRibbonZoom,
183
+ config
184
+ });
185
+
186
+ // Parse each top-level layer as a separate tree
187
+ this.layers = parseLayers(config.layers, this.style_manager.styles);
188
+
189
+ // Sync tetxure info from main thread
190
+ this.syncing_textures = this.syncTextures(config.textures);
191
+
192
+ // Return promise for when config refresh finishes
193
+ this.configuring = this.syncing_textures.then(() => {
194
+ log('debug', 'updated config');
195
+ });
196
+
197
+ return this.configuring;
198
+ },
199
+
200
+ /** Refine view-dependent ribbons without cloning DEMs, reparsing scenes or invalidating decoded source caches. */
201
+ updateTerrainView(this: WorkerSceneState, {generation, ribbonZoom}: {generation: number; ribbonZoom?: number}) {
202
+ if (ribbonZoom !== undefined && (!Number.isFinite(ribbonZoom) || ribbonZoom < 0 || ribbonZoom > 40)) throw new Error('Invalid terrain ribbon view zoom');
203
+ if (generation < this.generation) return;
204
+ this.generation = generation;
205
+ for (const style of Object.values(this.styles)) {
206
+ style.setGeneration(generation);
207
+ style.terrainRibbonZoom = ribbonZoom;
208
+ }
209
+ return this.configuring;
210
+ },
211
+
212
+ // Create data sources and clear tile cache if necessary
213
+ createDataSources (this: WorkerSceneState, config: Pick<WorkerSceneConfiguration, 'sources'>) {
214
+ // Save and compare previous sources
215
+ this.last_config_sources = this.config_sources || {};
216
+ this.config_sources = mergeObjects({}, config.sources); // deep copy the serialized config for the later existence comparison
217
+ let last_sources = this.sources;
218
+ let changed: string[] = [];
219
+
220
+ // Parse new sources
221
+ this.sources = {}; // clear previous sources
222
+ for (let name in config.sources) {
223
+ if (JSON.stringify(this.last_config_sources[name]) === JSON.stringify(config.sources[name])) {
224
+ this.sources[name] = last_sources[name];
225
+ continue;
226
+ }
227
+
228
+ // compile any user-defined JS functions
229
+ config.sources[name] = compileFunctionStrings(config.sources[name]);
230
+
231
+ let source;
232
+ try {
233
+ source = DataSource.create(Object.assign({}, config.sources[name], {name}), this.sources);
234
+ }
235
+ catch(e) {
236
+ continue;
237
+ }
238
+
239
+ if (!source) {
240
+ continue;
241
+ }
242
+ this.sources[name] = source;
243
+ changed.push(name);
244
+ }
245
+
246
+ // Removed or failed replacement sources must release old requests as well.
247
+ for (const name in last_sources) {
248
+ if (this.sources[name] !== last_sources[name]) {
249
+ this.sharedTileSources.invalidateSource(last_sources[name]);
250
+ if (changed.indexOf(name) === -1) changed.push(name);
251
+ }
252
+ }
253
+ // Release source leases, selection entries, and build ownership together.
254
+ changed.forEach(source => {
255
+ for (let t in this.tiles) {
256
+ if (this.tiles[t].source === source) {
257
+ this.removeTile(t);
258
+ }
259
+ }
260
+ });
261
+ for (const name in last_sources) {
262
+ if (this.sources[name] !== last_sources[name]) last_sources[name].dispose?.();
263
+ }
264
+ },
265
+
266
+ // Returns a promise that fulfills when config refresh is finished
267
+ awaitConfiguration (this: WorkerSceneState): Promise<void> {
268
+ return this.configuring;
269
+ },
270
+
271
+ // Build a tile: load from tile source if building for first time, otherwise rebuild with existing data
272
+ buildTile (this: WorkerSceneState, { tile }: {tile: WorkerBuildTile}): Promise<void> | undefined {
273
+ // Tile cached?
274
+ const cachedTile = this.getTile(tile.key);
275
+ if (cachedTile != null) {
276
+ // Already loading?
277
+ if (cachedTile.loading === true) {
278
+ return;
279
+ }
280
+ }
281
+
282
+ // Update tile cache
283
+ tile = this.tiles[tile.key] = Object.assign(this.getTile(tile.key) || {}, tile);
284
+
285
+ // Update config (styles, etc.), then build tile
286
+ return this.awaitConfiguration().then(() => {
287
+ // First time building the tile
288
+ if (tile.loaded !== true) {
289
+
290
+ tile.loading = true;
291
+ tile.loaded = false;
292
+ tile.error = null;
293
+
294
+ this.loadTileSourceData(tile).then(() => {
295
+ if (this.getTile(tile.key) !== tile) {
296
+ log('trace', `stop tile build after data source load because tile was removed: ${tile.key}`);
297
+ return;
298
+ }
299
+
300
+ // Warn and continue on data source error
301
+ if (tile.source_data?.error) {
302
+ log('warn', `tile load error(s) for ${tile.key}: ${tile.source_data.error}`);
303
+ }
304
+
305
+ tile.loading = false;
306
+ tile.loaded = true;
307
+ Tile.buildGeometry(tile, this);
308
+ }).catch((error) => {
309
+ if (this.getTile(tile.key) !== tile) return;
310
+ tile.loading = false;
311
+ tile.loaded = false;
312
+ tile.error = error.stack;
313
+ log('error', `tile load error for ${tile.key}: ${tile.error}`);
314
+
315
+ // Send error to main thread
316
+ WorkerBroker.postMessage(`TileManager_${this.scene_id}.buildTileError`, Tile.slice(tile));
317
+ });
318
+ }
319
+ // Tile already loaded, just rebuild
320
+ else {
321
+ log('trace', `used worker cache for tile ${tile.key}`);
322
+
323
+ // Build geometry
324
+ try {
325
+ Tile.buildGeometry(tile, this);
326
+ }
327
+ catch(error) {
328
+ // Send error to main thread
329
+ tile.error = String(error);
330
+ WorkerBroker.postMessage(`TileManager_${this.scene_id}.buildTileError`, Tile.slice(tile));
331
+ }
332
+ }
333
+ });
334
+ },
335
+
336
+ // Load this tile's data source, or copy from an existing tile's data
337
+ loadTileSourceData (this: WorkerSceneState, tile: WorkerBuildTile) {
338
+ return this.sharedTileSources.loadTile(tile, this.sources[tile.source], () => iterateSourceTiles(this.tiles));
339
+ },
340
+
341
+ /** Internal diagnostics distinguish decoded acquisition from styled mesh/build residency. */
342
+ getTileSourceStatistics(this: WorkerSceneState) {
343
+ const projectionPreparation = getMeshProjectorPreparationStatistics();
344
+ const projectionWork = getMeshProjectorWorkStatistics();
345
+ return {...this.sharedTileSources.store.getStatistics(), sharingEnabled: this.sharedTileSources.sharingEnabled,
346
+ ...(projectionPreparation ? {projectionPreparation} : {}), ...(projectionWork ? {projectionWork} : {})};
347
+ },
348
+
349
+ /** Return current source capabilities; external provider factories are registered only in workers. */
350
+ async getSourceMetadata(this: WorkerSceneState, names: readonly string[], generation: number = this.generation): Promise<Record<string, TangramTileSourceMetadata>> {
351
+ await this.awaitConfiguration();
352
+ if (generation !== this.generation) throw new Error('Worker configuration changed during metadata loading');
353
+ const sources = names.map((name): [string, DataSource] => [name, this.sources[name]]);
354
+ return Object.fromEntries(await Promise.all(sources.map(async ([name, source]) => {
355
+ if (!source) throw new Error(`Source '${name}' is unavailable in this worker`);
356
+ const metadata = await source.getMetadata();
357
+ if (this.sources[name] !== source) throw new Error(`Source '${name}' changed during metadata loading`);
358
+ if (generation !== this.generation) throw new Error('Worker configuration changed during metadata loading');
359
+ return [name, metadata];
360
+ })));
361
+ },
362
+
363
+ /** Explicit cleanup for worker reset; native worker termination also releases its isolated heap. */
364
+ finalizeTileSources(this: WorkerSceneState) {
365
+ clearMeshProjectorPreparation();
366
+ for (const key in this.tiles) this.removeTile(key);
367
+ this.sharedTileSources.finalize();
368
+ for (const source of Object.values(this.sources)) source.dispose?.();
369
+ this.sources = {};
370
+ this.config_sources = {};
371
+ this.last_config_sources = {};
372
+ },
373
+
374
+ getTile(this: WorkerSceneState, key: string): WorkerBuildTile | undefined {
375
+ return this.tiles[key];
376
+ },
377
+
378
+ // Remove tile
379
+ removeTile (this: WorkerSceneState, key: string) {
380
+ var tile = this.tiles[key];
381
+
382
+ if (tile != null) {
383
+ this.sharedTileSources.releaseTile(tile);
384
+ // Cancel if loading
385
+ if (tile.loading === true) {
386
+ log('trace', `cancel tile load for ${key}`);
387
+ tile.loading = false;
388
+ Tile.cancel(tile);
389
+ }
390
+
391
+ // Remove from cache
392
+ FeatureSelection.clearTile(key);
393
+ delete this.tiles[key];
394
+ log('trace', `remove tile from cache for ${key}`);
395
+ }
396
+ },
397
+
398
+ // Query features within visible tiles, with optional filter conditions
399
+ queryFeatures (this: WorkerSceneState, { filter, visible, geometry, tile_keys }: {filter?: unknown; visible?: boolean; geometry?: boolean; tile_keys: string[]}) {
400
+ let features: {type?: string; id?: string | number; properties: Record<string, unknown>; geometry?: import('../utils/geo').Geometry | null}[] = [];
401
+ let tiles = tile_keys
402
+ .map(t => this.tiles[t])
403
+ .filter(t => t && t.loaded);
404
+
405
+ // Compile feature filter
406
+ if (filter != null) {
407
+ filter = typeof filter === 'string' && ['{', '['].indexOf(filter[0]) > -1 ? JSON.parse(filter) : filter; // de-serialize if looks like an object
408
+ filter = compileFunctionStrings(filter, StyleParser.wrapFunction);
409
+ }
410
+ const compiledFilter = buildFilter(filter, FilterOptions);
411
+
412
+ tiles.forEach(tile => {
413
+ const layers = tile.source_data?.layers;
414
+ for (let layer in layers) {
415
+ let data = layers[layer];
416
+
417
+ if (data == null) {
418
+ return;
419
+ }
420
+
421
+ data.features.forEach(feature => {
422
+ if (!hasQueryGeometry(feature)) return;
423
+ // Optionally check if feature is visible (e.g. was rendered for current generation)
424
+ const feature_visible = (getFeatureRenderedGeneration(feature) === this.generation);
425
+ if ((visible === true && !feature_visible) ||
426
+ (visible === false && feature_visible)) {
427
+ return;
428
+ }
429
+
430
+ // Apply feature filter
431
+ let context = StyleParser.getFeatureParseContext(feature, tile, this.global);
432
+ context.source = tile.source; // add data source name
433
+ context.layer = layer; // add data source layer name
434
+ context.id = feature.id; // add feature id
435
+
436
+ if (!compiledFilter(context)) {
437
+ return;
438
+ }
439
+
440
+ // Info to return with each feature
441
+ let subset: (typeof features)[number] = {
442
+ type: feature.type,
443
+ id: feature.id,
444
+ properties: Object.assign({}, feature.properties, {
445
+ $source: context.source,
446
+ $layer: context.layer,
447
+ $geometry: context.geometry,
448
+ $visible: feature_visible
449
+ })
450
+ };
451
+
452
+ // Optionally include geometry in response
453
+ if (geometry === true) {
454
+ // Transform back to lat lng (copy geometry to avoid local modification)
455
+ subset.geometry = Geo.copyGeometry(feature.geometry);
456
+ if (subset.geometry) Geo.tileSpaceToLatlng(subset.geometry, tile.coords.z, tile.min);
457
+ }
458
+
459
+ features.push(subset);
460
+ });
461
+ }
462
+ });
463
+ return features;
464
+ },
465
+
466
+ // Get a feature from the selection map
467
+ getFeatureSelection ({ id, key }: {id?: number; key?: number} = {}) {
468
+ var selection = key === undefined ? undefined : FeatureSelection.map[key];
469
+
470
+ return {
471
+ id: id,
472
+ feature: (selection && selection.feature)
473
+ };
474
+ },
475
+
476
+ // Resets the feature selection state
477
+ resetFeatureSelection (sources: string[] | null = null) {
478
+ FeatureSelection.reset(sources);
479
+ },
480
+
481
+ // Selection map size for this worker
482
+ getFeatureSelectionMapSize () {
483
+ return FeatureSelection.getMapSize();
484
+ },
485
+
486
+ // Texture info needs to be synced from main thread, e.g. width/height, which we only know after the texture loads
487
+ syncTextures (tex_config?: Record<string, unknown>) {
488
+ let textures = [];
489
+ if (tex_config) {
490
+ textures.push(...Object.keys(tex_config));
491
+ }
492
+
493
+ log('trace', 'sync textures to worker:', textures);
494
+ if (textures.length > 0) {
495
+ return Texture.syncTexturesToWorker(textures);
496
+ }
497
+ return Promise.resolve();
498
+ },
499
+
500
+ // Sync device pixel ratio from main thread
501
+ updateDevicePixelRatio (device_pixel_ratio: number) {
502
+ Utils.device_pixel_ratio = device_pixel_ratio;
503
+ },
504
+
505
+ clearFunctionStringCache () {
506
+ clearFunctionStringCache();
507
+ },
508
+
509
+ // Profiling helpers
510
+ profile (this: WorkerSceneState, name: string) {
511
+ console.profile(`worker ${this._worker_id}: ${name}`); // eslint-disable-line no-console
512
+ },
513
+
514
+ profileEnd (this: WorkerSceneState, name: string) {
515
+ console.profileEnd(`worker ${this._worker_id}: ${name}`); // eslint-disable-line no-console
516
+ },
517
+
518
+ debug: {
519
+ debugSettings,
520
+ layerCache,
521
+ functionStringCache
522
+ }
523
+
524
+ });
525
+
526
+ WorkerBroker.addTarget('self', SceneWorker);
527
+
528
+ export default SceneWorker;