Overview
Threebox handles three different coordinate systems:- Geographic coordinates (longitude/latitude)
- Meters (real-world metric units)
- World units (Three.js scene units)
Coordinate System Types
1. Geographic Coordinates (lnglat)
Format:[longitude, latitude, altitude]
- Longitude: -180 to 180 (west to east)
- Latitude: -90 to 90 (south to north)
- Altitude: meters above sea level (optional)
2. Meters
Real-world metric measurements for object dimensions:3. World Units
Three.js scene coordinates using Mercator projection:- Based on Spherical Mercator projection
- World size:
536870912units (2^29) - Centered at [0, 0]
Key Constants
From source (constants.js):Converting Coordinates
lnglat → World Units
UseprojectToWorld(coords) to convert geographic coordinates to Three.js world coordinates.
From source (utils.js:98-118):
World Units → lnglat
UseunprojectFromWorld(worldUnits) to convert Three.js world coordinates back to geographic coordinates.
From source (utils.js:151-166):
Units Per Meter Scaling
Projected Units Per Meter
Convert meters to world units at a specific latitude:Scaling Vertices to Meters
From source (utils.js:132-141):Object Positioning
Using ‘meters’ Units
When creating objects withunits: 'meters', Threebox automatically handles scaling:
- Convert lnglat to world units
- Scale the object based on latitude
- Update scale when zooming to maintain real-world size
Using ‘scene’ Units
Withunits: 'scene', objects use raw Three.js units without latitude scaling:
- Non-geographic objects
- Custom coordinate systems
- Objects that shouldn’t scale with latitude
Altitude Handling
Altitude in Meters
Altitudes are always specified in meters above sea level:Mercator Z from Altitude
From source (utils.js:128-130):Positioning Objects
setCoords Method
All Threebox objects have asetCoords method:
- Converts lnglat to world units
- Positions the object in the Three.js scene
- Stores coordinates in
obj.coordinates
Getting Object Position
Feature Center Calculation
For GeoJSON features, calculate the center point: From source (utils.js:188-214):Object Height on Floor
Calculate altitude for objects on building levels: From source (utils.js:216-225):Coordinate System Conversions Summary
| From | To | Method |
|---|---|---|
| lnglat | World units | tb.projectToWorld(coords) |
| World units | lnglat | tb.unprojectFromWorld(vector3) |
| Meters | World units | meters * tb.projectedUnitsPerMeter(lat) |
| World units | Meters | worldUnits / tb.projectedUnitsPerMeter(lat) |
| Altitude | Mercator Z | tb.utils.mercatorZfromAltitude(altitude, lat) |
Practical Examples
Example 1: Place Object at Address
Example 2: Create Grid of Objects
Example 3: Convert Screen to World Coordinates
Example 4: Measure Distance Between Objects
Common Issues
Best Practices
-
Always use meters for real-world objects
-
Store original coordinates
-
Use helper methods
-
Account for latitude scaling
Next Steps
Threebox Instance
Learn about Threebox initialization and configuration
Custom Layers
Integrate Threebox with Mapbox custom layers
Camera Synchronization
Understand camera synchronization between Three.js and Mapbox