MapTiler vector styles include building footprints with height data in the building source-layer. The fill-extrusion layer type renders these polygons as 3D shapes using render_height and render_min_height properties from the vector tiles.
The code detects the vector source name at runtime instead of hardcoding it. MapTiler styles may use maptiler_planet, openmaptiles, or other source names depending on the style version.
The layer is inserted before the first label layer so that road/POI labels stay rendered on top of the buildings. The filter ['!=', ['get', 'hide_3d'], true] drops features that the tiles explicitly mark as “not for 3D rendering”: without it, MapLibre 5.x throws a TypeError inside loadTile when processing tiles with mixed 2D/3D hints.
['coalesce', ['get', 'render_height'], 0] also protects against features where render_height is missing or null (returns 0 instead of null, which the shader can handle).
Set pitch above 0 in the constructor to view buildings from an angle. Without pitch, extrusions are invisible because you are looking straight down.
Different MapTiler style versions use different source names. Hardcoding openmaptiles would break with newer styles that use maptiler_planet.
Can I color buildings based on their height?
Yes. Use an interpolate expression on render_height to map heights to colors, e.g., taller buildings get darker shading.
Common mistakes
Hardcoding the vector source name instead of detecting it at runtime
Forgetting to set pitch > 0: extrusions are invisible from directly above
Adding the layer before the map loads: always use map.on('load', ...)
Passing raw ['get', 'render_height'] without coalesce: features with null height crash loadTile in MapLibre 5.x
Adding the layer without the hide_3d filter and without a beforeId: labels and icons end up buried under buildings, and features the tiles marked as not-for-3D still get extruded
auto_awesome
Build maps using MapLibre GL JS with AI
Generate code like this 3d building extrusions with maplibre gl js example using the MapLibre GL JS skill in your IDE.