# 08. Load a glTF model with realistic lighting, explained

**Goal:** load a real 3D model in the glTF format, light it with an environment map, and automatically frame it with the camera.

## Why glTF?

glTF 2.0 is an open standard from the Khronos Group and is often called "the JPEG of 3D". It stores meshes, PBR materials, textures, skeletons, animations and cameras. `.gltf` is JSON plus separate files; `.glb` is a single binary file. Blender, Maya, 3ds Max, Substance and most online model libraries can export it.

| Format | Loader | Notes |
| --- | --- | --- |
| glTF / GLB | `GLTFLoader` | Recommended. PBR materials and animations map directly to three.js |
| FBX | `FBXLoader` | Common from game tools; convert to glTF when possible |
| OBJ (+ MTL) | `OBJLoader`, `MTLLoader` | Geometry only, old material model |
| STL | `STLLoader` | 3D printing, no materials |
| USDZ | `USDZLoader` | Apple AR format |

## Walkthrough

```js
renderer.toneMapping = THREE.ACESFilmicToneMapping;
```
PBR lighting produces values brighter than the screen can show. Tone mapping compresses them like a camera would. Options include `ACESFilmicToneMapping`, `AgXToneMapping` and `NeutralToneMapping`; try each with your model.

```js
const pmrem = new THREE.PMREMGenerator(renderer);
scene.environment = pmrem.fromScene(new RoomEnvironment(), 0.04).texture;
```
`scene.environment` lights every PBR material with an image of the surroundings (image-based lighting). Metals need it to look like metal. `RoomEnvironment` builds a simple studio procedurally, so no HDR download is needed. For photographic lighting, load an `.hdr` file with `HDRLoader` (named `RGBELoader` in older releases) and pass it through `PMREMGenerator.fromEquirectangular`.

```js
loader.load(url, onLoad, onProgress, onError);
```
Loading is asynchronous. `loadAsync(url)` returns a promise if you prefer `await`. Always handle errors: wrong paths and missing CORS headers are the usual culprits.

```js
const box = new THREE.Box3().setFromObject(model);
const size = box.getSize(new THREE.Vector3()).length();
const center = box.getCenter(new THREE.Vector3());
model.position.sub(center);
```
Models come in any unit and offset (centimeters, meters, huge, tiny). Computing the bounding box lets you center the model and place the camera at a distance relative to its size. Adjusting `near` and `far` to the model size keeps depth precision good.

## Optimizing models for the web

| Problem | Tool or technique |
| --- | --- |
| Large geometry files | Draco or Meshopt compression (`DRACOLoader`, `MeshoptDecoder`) |
| Large textures | KTX2 / Basis Universal textures (`KTX2Loader`), resize to what is visible |
| Many draw calls | Merge meshes and materials in your DCC tool |
| Inspect a file | The glTF Viewer at gltf-viewer.donmccurdy.com and the `gltf-transform` CLI |

## Animations

If the file has animations:

```js
const mixer = new THREE.AnimationMixer(gltf.scene);
mixer.clipAction(gltf.animations[0]).play();
// in the loop: mixer.update(delta);
```

## Try this

1. Swap the URL for `https://threejs.org/examples/models/gltf/Soldier.glb` and play its animations with `AnimationMixer`.
2. Set `renderer.toneMapping = THREE.AgXToneMapping` and compare.
3. Show the environment as the background: `scene.background = scene.environment` and `scene.backgroundBlurriness = 0.5`.
