View on GitHub

leaflet-city-mapper

A small 3D rendering engine for leaflet

Source guide

Project README Entry point

Internal architecture and contributor navigation. For the public API, see the project README.

From a layer to a frame

sequenceDiagram
  participant App
  participant Layer as L.Layer / scene
  participant Source as Source loader
  participant Worker
  participant Frame as Shared renderer
  App->>Layer: new L.Layer({source})
  App->>Layer: addTo(map)
  Layer->>Frame: attach root / create renderer
  Layer->>Source: request visible data
  Source->>Worker: decode or assemble (where applicable)
  Worker-->>Source: prepared data
  Source->>Layer: attach result and schedule update
  Layer->>Frame: render next frame

Not every source uses this exact worker path: vector tiles have their own loading and presentation lifecycle, and a native Leaflet layer is synchronized through native. See scene for source routing, shared ownership and removal.

Ownership and debugging

weakmaps holds per-object state; globals also contains shared scratch buffers and queues. On source failures, inspect the layer’s error and progress events.

Where to start a change

Change Follow the code Existing scenarios
Source routing or scene ownership scene → relevant source guide → workers scene, workers
GeoJSON extrusions or feature hits sources → geometry → picking geojson, picking
Tile formats or terrain archives / cog → raster / terrain pmtiles, cog, terrain
Vector tile styles and labels mvt → vector-style → vector-tiles vector tiles, unit cases
Camera or walking camera / controls → walk / collision camera, walk
GPU drawing or lighting rendering → materials / shaders → lighting / shadows webgpu, night lights, sunlight

The build guide explains the normal build and test commands. The test runner builds and runs Cucumber with both Leaflet versions; the feature files above are useful starting points for finding behavior and regressions specific to a change.

Subsystem map

This diagram is an overview, not an exhaustive dependency graph.

flowchart LR
  Leaflet["Leaflet layer"] --> Scene["scene: source routing"]
  Scene --> Sources["sources / vector-tiles"]
  Scene --> Raster["raster / terrain"]
  Sources --> Workers["workers: decoding and geometry"]
  Raster --> Archives["archives / cog"]
  Workers --> Geometry["geometry / models"]
  Geometry --> Render["rendering / GPU"]
  Raster --> Render
  Native["native Leaflet layers"] --> Render
  Camera["camera / controls"] --> Render

Source layout

src/
  index.js           Public entry point and source registration
  globals/           One shared constant or mutable cell per module
  weakmaps/          One private WeakMap per map/layer state field
  mvt/               PBF vector tile decoding and tessellation
  vector-style/      Safe vector style parsing and expressions
  vector-tiles/      Native vector tile lifecycle and worker preparation
  camera/            Leaflet camera API and scene camera state
  controls/          Compass, dragging, touch and resizing
  core/              Leaflet hooks, camera-aware overlays and readiness
  native/            Native Leaflet layers rendered through WebGPU
  layer-controls/    Layer entry placement and progress indicators
  scene/             Source routing, lifecycle and graph nodes
  sources/           GeoJSON and streamed source loading and assembly
  workers/           Geometry, MVT, COG and model tasks sharing one CDN-compatible worker
  picking/           Raycasting, highlighting and feature interaction
  rendering/         Frame stages, draw pipelines and mesh lifecycle
  gpu/               Device initialization, renderer resources and diagnostics
  materials/         Material programs, uniforms, glTF materials and cutouts
  textures/          Image loading, texture upload/binding and roof atlases
  animations/        Camera transitions, zoom synchronization and tile fading
  shadows/           Directional shadow maps and cache
  lighting/          Point lights and spatial light grids
  shaders/           Material shader generation
  geometry/          Mesh construction, bounds, grids, vectors and route geometry
  projections/       Map, screen and local coordinate conversion
  transforms/        Local/world matrices and glTF node transforms
  triangulation/     Polygon triangulation and ring operations
  collision/         Mesh intersection and collision queries
  models/            glTF loading, decoding and scene assembly
  archives/          PMTiles archive reading and decompression
  cog/               Supported GeoTIFF directories, byte ranges and tile decoding
  raster/            Raster tile loading, attachment and lifecycle
  traffic/           Routes, scheduling, simulation, loading and rendering
  vehicles/          Vehicle model geometry
  terrain/           Terrain loading and sampling
  astronomy/         Sun/moon position and observer/date validation
  sunlight/          Sky, celestial display and time controls
  walk/              First-person walking
  colors/            Colour parsing, conversion, interpolation and formatting
  utils/             Shared numeric and no-op helpers

Layer lifecycle and data

Rendering and geometry

Map behavior

Shared state and helpers

Each source directory has a README; the source documentation test checks this invariant for new directories as well.