How to use a real 3D map in three.js
TerraPrinter makes 3D models of real places for 3D printing, and every model also downloads as a GLB that three.js loads as it is: upright, in metres, with each map layer as its own named part and the map's GPS bounds in the file.
What the GLB gives you
GLB is binary glTF 2.0, the format three.js loads with its GLTFLoader. There is nothing to convert.
- Upright, at its printed size. Y is up and the unit is the metre, as glTF expects, so a 200 mm model is 0.2 units across. It is centred on the origin with its bottom at y = 0, and north points to −Z.
- One named part per layer. Terrain, Buildings, Roads, Water, Parks, Trees and the rest are separate meshes, each in its own colour.
- Where it is. The longitude and latitude of the map's edges, its scale, the vertical exaggeration and the data credits are in the file. In three.js they are in the model's
userData. - Free for any use, including commercial games and apps. Keep the data credits that come in the file.
Download a GLB
- Open TerraPrinter and find your place: search for it, or start from one of the ready-made maps.
- Draw the area and switch on the layers you want, such as buildings, roads, water and trees.
- Press Generate 3D Model.
- In the download window, click GLB, next to STL and OBJ.
Layers you hide in the preview are left out of the file. A frame, a name plate or a stand you add comes as its own part.
Load it in three.js
See it running: Paris, live in three.js, built from this guide.
This is a whole page: a scene, two lights, a camera you can turn with the mouse, and the model. Save it next to your GLB, change the file name to yours, and open it through any local web server, because browsers don't load files straight from the disk.
<!doctype html>
<html>
<body style="margin: 0">
<script type="importmap">
{ "imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.183.1/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.183.1/examples/jsm/"
} }
</script>
<script type="module">
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(devicePixelRatio);
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color('#1b1f24');
scene.add(new THREE.HemisphereLight(0xffffff, 0x445566, 1.5));
const sun = new THREE.DirectionalLight(0xffffff, 2.5);
sun.position.set(-1, 2, 1);
scene.add(sun);
// The model is at its printed size: a 200 mm print is 0.2 units across.
const camera = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 0.001, 100);
camera.position.set(0.2, 0.2, 0.25);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0.01, 0);
const gltf = await new GLTFLoader().loadAsync('paris-terraprinter.com.glb');
scene.add(gltf.scene);
renderer.setAnimationLoop(() => {
controls.update();
renderer.render(scene, camera);
});
</script>
</body>
</html>With a bundler such as Vite, install three with npm install three and leave out the import map. The imports stay the same.
Change or hide a layer
Find each layer by its name. three.js swaps the spaces in names for underscores, so Sports fields is Sports_fields, and keeps the name as TerraPrinter wrote it in userData.name. Parts of the same colour share one material, so give a part its own material before you change its colour.
const model = gltf.scene;
// hide the trees
const trees = model.getObjectByName('Trees');
if (trees) trees.visible = false;
// paint the buildings
const buildings = model.getObjectByName('Buildings');
if (buildings) buildings.material = new THREE.MeshStandardMaterial({ color: '#d9d4c7', roughness: 0.9 });
// every part, by its name in TerraPrinter
model.traverse(o => { if (o.isMesh) console.log(o.userData.name, o.geometry.attributes.position.count); }); Real-world size and heights
The model's root, gltf.scene.children[0], carries what you need in its userData:
boundsandcentre: the longitude and latitude of the map's edges and of its middle.scale, such as 1:10,005, andgroundMetresPerUnit: metres on the ground for one unit of the model.verticalExaggeration: how much the heights were stretched for the print. 1 means true heights.mapBoxInModel: where the map's west, east, north and south edges are in the model.printSizeMm,baseThicknessMm, the data credits and the licence.
To make one unit one metre on the ground, with true heights:
const root = gltf.scene.children[0];
const info = root.userData;
const k = info.groundMetresPerUnit;
root.scale.set(k, k / (info.verticalExaggeration || 1), k);The base is part of the Terrain mesh and grows with it, to tens of metres thick. Keep it under your own ground, or make the base thinner in TerraPrinter before you download. At real size the model is as big as the place, so move the camera out and raise its far plane to match.
Put a GPS point on the model
Longitude and latitude are spread evenly across the map's box, with north towards −Z. This function finds the ground under any longitude and latitude on the model, at any scale:
const raycaster = new THREE.Raycaster();
function groundAt(lon, lat, root) {
const { bounds: b, mapBoxInModel: m } = root.userData;
const x = m.xWest + (lon - b.west) / (b.east - b.west) * (m.xEast - m.xWest);
const z = m.zNorth + (b.north - lat) / (b.north - b.south) * (m.zSouth - m.zNorth);
// a ray straight down onto the terrain
root.updateWorldMatrix(true, true);
const from = root.localToWorld(new THREE.Vector3(x, 1000, z));
const to = root.localToWorld(new THREE.Vector3(x, 0, z));
raycaster.set(from, to.sub(from).normalize());
const hit = raycaster.intersectObject(root.getObjectByName('Terrain'))[0];
return hit ? hit.point : null;
}
// a red marker on the ground under the Eiffel Tower
const marker = new THREE.Mesh(new THREE.SphereGeometry(0.002), new THREE.MeshStandardMaterial({ color: 'red' }));
marker.position.copy(groundAt(2.2945, 48.8583, gltf.scene.children[0]));
scene.add(marker);To land on roofs and treetops as well, cast the ray at the whole root instead of the Terrain part. If you scaled the model to real size, make the marker bigger too.
File size and speed
- A city centre about 2 km across, with every building and tree, comes to 60 to 110 MB. Trees are often most of it, so switch them off in TerraPrinter if you don't need them.
- For a website, compress the file with glTF Transform: install it with
npm install --global @gltf-transform/cli, then rungltf-transform meshopt input.glb output.glb. That compresses the geometry and keeps every part and its name. - A compressed file needs the decoder that comes with three.js:
import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';
const loader = new GLTFLoader();
loader.setMeshoptDecoder(MeshoptDecoder);
const gltf = await loader.loadAsync('output.glb');