Hey r/mapbox,
I run BetterMap, and I thought this sub might like to see how much of it is Mapbox under the hood. We're an indoor mapping platform: venues upload their floor plans, we turn them into interactive 3D maps, and they publish them to their website, to visitors' phones through a QR code, and to touchscreens in the building. We're live in shopping malls, events and expos, and we're expanding into campuses and hospitals.
The whole platform is built on Mapbox GL JS. Some of it isn't what Mapbox does out of the box, including sloped 3D surfaces, which Mapbox closed as wontfix, so here's what it looks like and a bit about how we built it.
From floor plan to editable map
This is the whole flow: upload a plan, let it process, place it on the map, then edit it. In the video we copy and resize shapes, move points, add 3D models, put an image on a shape, and change a shape's colour, height and style.
Every shape is stored relative to the plan itself (0 to 1 across and down), and turned into real coordinates using three corners of where the plan sits on the map. So if you move or rotate the plan, every shape comes with it.
Shapes are fill-extrusions. Hollow fill is one of the options, but walls are thin extrusions instead, because hollow only works for a single closed shape with no door gaps.
The 3D models live in our own three.js custom layer that shares Mapbox's GL context.
2. The finished map, with directions
This is the same venue from the first video after it's been fully edited, with proper names, categories and directions. Visitors can get directions on one floor or across floors, and choose whether to take the escalators or the lift.
Only the floor you're looking at is loaded into the source. Switching floors swaps the data with setData instead of filtering thousands of features.
When a route goes across floors, the floors lift apart and the route is drawn between them, with a shaft at the lift or escalator. The lifting is just Mapbox's own paint transitions on the extrusion base and height, so there's no animation loop on our side.
Getting the camera into that multi-floor view at an angle was harder than I expected. We test up to 14 camera positions with jumpTo inside a single frame, then do one easeTo to the best one. Mapbox doesn't paint until the next frame, so you never see the tests.
3. Slopes
In Mapbox every extruded 3D shape has a flat top. The recommendation on the closed request is to build a custom mesh, so that's what we did. It ended up doing more than slopes. The same surface can make complex shapes that behave more like terrain, and they're still quick to render and click.
You draw a line inside a shape and set the heights along it, and the shape turns into a ramp. Add more points along the line, or a second line on the other side, and you get a more complex surface, like a roof with a ridge down the middle.
The height at any point depends on how far it is from each line (inverse distance weighting). We use a power of 1 instead of the usual 2 on purpose: with 1, two lines give you a perfectly flat plane between them. With 2, a point that should have been 3.00m came out at 3.60m.
Curved profiles use a monotone curve (Fritsch-Carlson), because Catmull-Rom dipped below the floor on a flat-bottomed valley.
Neighbouring triangles always split their shared edges at the same point, so the mesh has (almost) no cracks. Our first attempt with Delaunay left holes.
The hard part was making it look like it belongs. A three.js mesh doesn't shade like a Mapbox extrusion, so we wrote a shader that copies Mapbox's own extrusion lighting, using the style's actual light values. We test it against pixels Mapbox itself draws, and on our venue style they match.
The guide lines you draw sit on the slope using line-z-offset driven by line-progress, so they follow the surface in 3D.
4. A hospital from a CAD drawing
The last one is raw. It's a hospital, imported from a CAD file. First you see the original drawing, then we hide it to show what was pulled out of it, then the result in 3D, with proper walls instead of boxes, plus 3D models and patterns. What you see is straight out of the extraction, down to the original room codes, before anyone has tidied it up.
Architectural drawings are mostly clutter for a map, things like measurements, shading and furniture. Most of the work is deciding what to keep.
Some shapes are drawn with a tiled texture instead of a flat colour. We wanted one layer where each shape picks its own texture, but fill-extrusion-pattern only ever rendered a single pattern for the whole layer, so we ended up with one layer per pattern.
Our engine takes in images, PDFs and CAD files, and it isn't perfect: a low quality scan comes out badly, and CAD plans still take hours to sort out. That's why the editor exists, and why we're now building an agent that does most of that cleanup itself.
If you want to try it, you can build a map for free at bettermap.com
Happy to answer questions about any of it. That camera fitting in section 2 is the one bit I'd still like to do properly, so if anyone has a cleaner way to fit a camera to a 3D scene like that, I'd like to hear it.