# 02. Lights, PBR materials and shadows, explained

**Goal:** light a scene realistically with physically based materials and soft, moving shadows.

## Light types at a glance

| Light | Shape of light | Casts shadows | Typical use |
| --- | --- | --- | --- |
| `AmbientLight` | Equal from everywhere | No | Lift the darkest areas a little |
| `HemisphereLight` | Sky color from above, ground color from below | No | Cheap outdoor fill light |
| `DirectionalLight` | Parallel rays (infinitely far away) | Yes (orthographic shadow camera) | Sun |
| `PointLight` | All directions from a point | Yes (cube shadow map, 6 renders) | Bulbs, candles |
| `SpotLight` | A cone | Yes (perspective shadow camera) | Flashlights, stage lights |
| `RectAreaLight` | A rectangle | No | Soft window or panel light (`MeshStandardMaterial` and `MeshPhysicalMaterial` only) |

## Walkthrough

```js
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
```
Shadows are off by default because they are expensive: each shadow-casting light renders the scene again from its own point of view into a depth texture (the **shadow map**). `PCFSoftShadowMap` filters that map for softer edges.

```js
const hemi = new THREE.HemisphereLight(0xdfefff, 0x5a4632, 1.2);
```
Sky color, ground color, intensity. Surfaces facing up get bluish light, surfaces facing down get brownish bounce light. It is a cheap stand-in for real global illumination.

```js
const sun = new THREE.DirectionalLight(0xffffff, 2.5);
sun.position.set(5, 8, 3);
sun.castShadow = true;
```
A directional light shines from its `position` toward its `target` (the origin by default). Only the direction matters for lighting, but for shadows the position also places the shadow camera.

Intensities: three.js uses physically based light units by default, so useful values for directional lights are roughly 1 to 5. Very old tutorials that used `0.5` with "legacy lights" will look dim now.

```js
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -5; // ...right, top, bottom, near, far
```
The shadow camera for a directional light is an **orthographic box**. Everything inside it can cast and receive shadows. Make it as small as possible around your scene: the same 2048x2048 map spread over a smaller area gives sharper shadows. Use `new THREE.CameraHelper(sun.shadow.camera)` to visualize the box while tuning.

```js
const ground = new THREE.Mesh(new THREE.PlaneGeometry(20, 20), new THREE.MeshStandardMaterial({ color: 0x8fbf7a, roughness: 0.9 }));
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
```
`PlaneGeometry` is created standing up in the XY plane; rotating -90 degrees around X lays it flat. Each object must opt in: `castShadow` on things that block light, `receiveShadow` on things that show shadows.

```js
new THREE.MeshStandardMaterial({ color, roughness, metalness })
```
`MeshStandardMaterial` is the go-to physically based material:
- `roughness` 0 = mirror-sharp highlights, 1 = matte.
- `metalness` 0 = plastic/wood/stone, 1 = metal. Values in between are rarely physically meaningful; use 0 or 1 most of the time.
Metals reflect their surroundings, so the gold knot looks best with an environment map (see example 08).

```js
sun.position.set(Math.cos(t * 0.3) * 6, 8, Math.sin(t * 0.3) * 6);
```
Moving the light each frame updates the shadows automatically.

## Material cheat sheet

| Material | Lighting | Cost | Use for |
| --- | --- | --- | --- |
| `MeshBasicMaterial` | None (unlit) | Lowest | UI, flat colors, emissive-looking objects |
| `MeshLambertMaterial` | Diffuse only | Low | Many objects on weak devices |
| `MeshPhongMaterial` | Diffuse + shiny highlight | Low | Simple shiny plastic |
| `MeshStandardMaterial` | PBR (roughness/metalness) | Medium | Default choice for realistic scenes |
| `MeshPhysicalMaterial` | PBR + clearcoat, transmission, sheen, iridescence | Highest | Car paint, glass, fabric |
| `MeshToonMaterial` | Banded cartoon shading | Low | Stylized looks |

## Try this

1. Add `scene.add(new THREE.CameraHelper(sun.shadow.camera))` and shrink the box until shadows clip.
2. Set `sun.shadow.mapSize.set(256, 256)` to see pixelated shadows.
3. Replace the hemisphere light with `new THREE.AmbientLight(0xffffff, 0.5)` and compare.
4. Swap the cube's material for `MeshPhysicalMaterial({ transmission: 1, roughness: 0.1, thickness: 0.5 })` for a glass look.

## Common mistakes

- **No shadows:** forgot one of `renderer.shadowMap.enabled`, `light.castShadow`, `mesh.castShadow` or `mesh.receiveShadow`.
- **Striped "shadow acne":** adjust `sun.shadow.bias` (small negative values like `-0.0005`) or `normalBias`.
- **Everything black:** a lit material (`Standard`, `Lambert`, `Phong`) with no lights or environment.
