Overview
The Threebox instance is the core object that bridges Three.js and Mapbox GL JS. It manages the Three.js scene, camera synchronization, rendering, and all 3D objects on your map.Constructor
Create a Threebox instance inside a Mapbox GL JS custom layer:Parameters
The Mapbox GL JS map instance
WebGL rendering context from Mapbox. Get it from
map.getCanvas().getContext('webgl') or the gl parameter in onAdd(map, gl)Configuration options for the Threebox instance
Configuration Options
Lighting
Adds default lighting to the scene:
THREE.AmbientLightat 75% intensity- Two
THREE.DirectionalLightsources
Simulates sun position based on map center coordinates and datetime using SunCalc. Creates:
THREE.HemisphereLightTHREE.DirectionalLightpositioned by sun calculations
tb.setSunlight(date, coords)Shows a visual helper for the sun light direction when
realSunlight is enabledRendering
Controls rendering behavior. When
true, renders only when map updates. When false, continuously triggers repaints.Preserves the drawing buffer for screenshots or image exports
Interaction
Enables mouseover and selection of
fill-extrusion layer features. Fires SelectedFeatureChange event.Enables mouseover and selection of 3D objects. Fires
SelectedChange event. Sets bbox option on created objects.Allows dragging selected 3D objects:
- Shift + drag: Move horizontally (x-y plane)
- Ctrl + drag: Move vertically (z axis)
ObjectDragged event with draggedAction: 'translate' or 'altitude'Allows rotating selected 3D objects:
- Alt + drag: Rotate around z-axis
ObjectDragged event with draggedAction: 'rotate'Enables default tooltips on fill-extrusion features and 3D objects
Shows help tooltips during object manipulation (position, rotation, measurements)
Camera
Uses
THREE.OrthographicCamera instead of THREE.PerspectiveCameraField of view for
THREE.PerspectiveCamera in degrees. Has no effect when orthographic: true.Layers
Enables multiple 3D layers. Creates a default layer that automatically manages
tb.update() calls. Recommended to set in constructor if using multiple layers.Adds atmospheric sky layer (Mapbox GL JS v2.0+). Automatically updates with
realSunlight.Adds terrain layer using
mapbox-terrain-dem-v1 (Mapbox GL JS v2.0+). Automatically updates lighting with realSunlight.Usage Examples
Basic Setup (Method 1: Context from onAdd)
Advanced Setup (Method 2: Context from Canvas)
Multi-Layer Setup
Internal Structure
When initialized, Threebox creates:Key Properties
Reference to the Mapbox GL JS map instance
The Three.js scene containing all 3D objects
Group container for all Threebox objects. Access via
tb.world.childrenThree.js camera (PerspectiveCamera or OrthographicCamera)
Three.js WebGL renderer
Light objects:
ambientLight, dirLight, dirLightBack, dirLightHelper, hemiLight, pointLightThreebox version number (currently ‘2.2.7’)
Resource Management
- All 3D objects and geometries
- Materials and textures
- WebGL context
- Mapbox map instance
- Event listeners
Next Steps
Custom Layers
Learn how to integrate Threebox with Mapbox custom layers
Camera Synchronization
Understand how Three.js and Mapbox cameras stay in sync
Coordinate Systems
Master coordinate conversions between lnglat, meters, and units