Skip to content

M3 MapLibre bridge: two-way interop between maplibre-gl-js and three.js #3

Closed

Description

@clement-igonet

Tracking issue for M3 MapLibre bridge: two-way interop between maplibre-gl-js and three.js.

Tasks

  • Direction A: three.js scene inside a MapLibre custom layer: shared camera matrix, terrain-aware, a maplibre-first successor to threebox
  • Direction B: MapLibre-rendered basemap as texture/underlay inside a free three.js scene (globe or planar)
  • Coordinate utilities: lng/lat/alt ↔ scene space, mercator scale correction helpers
  • Camera sync contract: one controller drives both, no drift at high zoom (relative-to-eye / RTC precision)
  • Port 3–5 representative MapLibre examples to the bridge (3D model on map, animated line, custom layer)
  • Compare and document against dvt3d/maplibre-three-plugin and threebox; contribute fixes upstream where they fit
  • Typed API, unit tests, demo pages

Activity

  1. changed the title [-]M3 MapLibre bridge — two-way interop between maplibre-gl-js and three.js[/-] [+]M3 MapLibre bridge: two-way interop between maplibre-gl-js and three.js[/+] on Sep 4, 2026
  2. clement-igonet commented on Sep 27, 2026

    @clement-igonet
    OwnerAuthor

    Kickoff plan for M3, now that #2, the vector tiles milestone, is closed.

    What the custom layer actually hands us

    Checked against maplibre-gl-js 5.24.0 source rather than assumed. A custom layer's render receives modelViewProjectionMatrix, projectionMatrix, nearZ, farZ, fov and defaultProjectionData every frame, and renderingMode: "3d" shares the map's depth buffer. MapLibre's own docs say a projection matrix alone is "sufficient for simple custom layers that also only support mercator projection", which is where both existing libraries stop.

    Under globe, defaultProjectionData.mainMatrix projects a unit sphere planet, and MapLibre places a point on that sphere at x = sin(lng)cos(lat), y = sin(lat), z = cos(lng)cos(lat) (src/geo/projection/globe_utils.ts). This engine places it at x = cos(lon)cos(lat), y = sin(lat), z = -sin(lon)cos(lat). The two differ by a fixed 90 degree rotation about Y, so an ECEF scene maps into MapLibre's globe with one swizzle and a 1/R scale, with no shader surgery. MapLibre also hands over clippingPlane, which feeds straight into three.js renderer.clippingPlanes so nothing shows through the planet.

    That is the thing worth building: a bridge that works on the globe, which neither of the two libraries in this space does.

    Prior art, for the comparison task

    engine projection terrain state
    threebox mapbox-gl-js v1.11 / v2.0.1 mercator no three.js r132 era, Mapbox only
    maplibre-three-plugin maplibre-gl-js mercator not documented MapScene, SceneTransform, Sun, Creator

    Threebox is the richer object toolkit (loaders, shadows, drag, CSS2D labels) and is Mapbox only. maplibre-three-plugin is the closest neighbour and is flat map only.

    The PRs

    • PR A, the bridge core. MapLibreLayer implementing CustomLayerInterface with renderingMode: "3d", a three.js renderer on MapLibre's own GL context with state reset per frame. Camera sync from the layer's matrices, mercator and globe, near, far and fov honoured. Scene space in meters around a floating origin, the trick the vector batches already use, so float32 holds at street level. Coordinate helpers both ways plus the mercator scale correction. Demo with a glTF model and MapAnchor on a MapLibre basemap, unit tests on the transforms, screenshots. The globe path is the risk in this milestone, so it is proven here or falls back to mercator with the reason written down.
    • PR B, terrain and ported examples. Anchors follow map.queryTerrainElevation and re-settle as terrain tiles arrive. Three MapLibre examples ported to the bridge (3D model on a map, animated line, custom layer), each a demo page with a screenshot.
    • PR C, direction B. The MapLibre basemap as an underlay inside this engine's own scene, one controller driving both. Two ways to do it, a DOM underlay or its canvas as a texture; the choice comes from measuring alignment error in pixels and the frame cost, not from taste.
    • PR D, the write-up and the API. The comparison above filled in with what was actually built, a typed API surface, docs, and an upstream contribution where one fits, most likely custom layer documentation for 3D content under globe.

    Same working rules as M2: evidence with every PR, the benchmark and budget stay green, and nothing lands without a screenshot or a measurement behind it.

  3. clement-igonet commented on Oct 3, 2026

    @clement-igonet
    OwnerAuthor

    Five boxes ticked. Direction A, the coordinate utilities and the first ported examples landed in #17 (the custom layer, frames, 0.08 px against map.project()) and #20 (terrain, world space, the animated line and custom layer examples); direction B in #21 (MapLibre's map under our flat scene, 0.000 px) and #22 (the datum parameter, the scene on MapLibre's sphere under its globe, 0.000 px to zoom 7.6 and 0.25 px past 12). The camera sync contract is the underlay's: one controller, the map's camera derived from the scene's every frame, measured both ways by bridge-check and underlay-check. Left: the write-up against maplibre-gl-three, maplibre-three-plugin and threebox, and the typed API.

  4. clement-igonet commented on Oct 4, 2026

    @clement-igonet
    OwnerAuthor

    Last two boxes: the typed API and the comparison landed in #23 (types/index.d.ts checked by tsc in CI; the write-up in docs/design-notes.md, "Compared with threebox, maplibre-three-plugin and maplibre-gl-three"). What came out of it for later: a vertical datum (EGM96) for ECEF data, and a documentation contribution upstream on defaultProjectionData under globe, where none of the libraries handles the morph. Closing M3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions