three.js Materials & Lighting
Make three.js surfaces look right: pick the correct material, light the scene,
enable shadows, and add image-based lighting. Patterns target r184, verified
against r184 (lighting is physically based by default since r155).
When to use
- Use when a mesh renders black or flat, when choosing a material, adding lights,
enabling shadows, or setting up environment-map reflections (IBL).
- Use when code constructs
MeshStandardMaterial, DirectionalLight, etc., or sets
renderer.shadowMap.enabled or scene.environment.
When not to use: the renderer/camera/loop → threejs-scene-setup. Loading
models (whose PBR materials this complements) → threejs-gltf-loading. Custom
GLSL/ShaderMaterial is its own topic; for the portable concept see
shader-programming.
Core workflow
- Pick a material by need.
MeshStandardMaterial (PBR: roughness,
metalness, reacts to lights/IBL) for realism; MeshPhysicalMaterial for
clearcoat/transmission; MeshBasicMaterial (unlit, ignores lights) for UI/flat;
MeshNormalMaterial/MeshDepthMaterial for debugging.
- Add light, or nothing shows. Lit materials need a light source and/or
scene.environment. Combine a soft fill (AmbientLight/HemisphereLight) with a
key DirectionalLight.
- Mind light intensity. Since r155, lighting is physically based; modern
intensities are higher than old tutorials (a key
DirectionalLight ≈ 1–3).
- Enable shadows in three places.
renderer.shadowMap.enabled = true, the
light's castShadow = true, and each mesh's castShadow/receiveShadow. Then
fit the light's shadow camera to the scene.
- Use an environment map for grounded reflections. Assign an equirectangular or
PMREM-processed texture to
scene.environment; PBR materials pick it up
automatically.
- Verify under real lighting — confirm the surface responds to the key light
(highlights move), shadows land where expected, and reflections look plausible.
Patterns
1. PBR material under a 3-light rig
import * as THREE from 'three';
const material = new THREE.MeshStandardMaterial({
color: 0xcc4444,
roughness: 0.5, // 0 = mirror, 1 = fully matte
metalness: 0.0, // 0 = dielectric (plastic/wood), 1 = metal
});
const mesh = new THREE.Mesh(new THREE.SphereGeometry(1, 32, 16), material);
scene.add(mesh);
// Soft sky/ground fill + a directional key light.
scene.add(new THREE.HemisphereLight(0xbbddff, 0x443322, 1.0)); // sky, ground, intensity
const key = new THREE.DirectionalLight(0xffffff, 2.5);
key.position.set(5, 10, 7);
scene.add(key);
2. Unlit material (no light needed)
// MeshBasicMaterial ignores lights — for flat color, UI, or sprites/labels.
const flat = new THREE.MeshBasicMaterial({ color: 0x44aa88 });
// A textured color map should be tagged sRGB so colors aren't washed out:
const tex = new THREE.TextureLoader().load('assets/logo.png');
tex.colorSpace = THREE.SRGBColorSpace;
const logo = new THREE.MeshBasicMaterial({ map: tex, transparent: true });
3. Shadows (the three required switches + camera fit)
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // softer edges
const sun = new THREE.DirectionalLight(0xffffff, 3);
sun.position.set(8, 12, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048); // default 512; raise for crisp
// DirectionalLight uses an OrthographicCamera — fit it tightly to the scene:
const cam = sun.shadow.camera;
cam.near = 1; cam.far = 40;
cam.left = -15; cam.right = 15; cam.top = 15; cam.bottom = -15;
scene.add(sun);
mesh.castShadow = true;
ground.receiveShadow = true; // a plane to catch the shadow
4. PBR textures on a material
const loader = new THREE.TextureLoader();
const colorMap = loader.load('assets/brick_color.jpg');
colorMap.colorSpace = THREE.SRGBColorSpace; // color maps are sRGB
const normalMap = loader.load('assets/brick_normal.jpg'); // data maps stay linear
const roughMap = loader.load('assets/brick_rough.jpg');
const brick = new THREE.MeshStandardMaterial({
map: colorMap,
normalMap,
roughnessMap: roughMap,
metalness: 0,
});
5. Image-based lighting from an HDR environment
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
new RGBELoader().load('assets/studio.hdr', (hdr) => {
hdr.mapping = THREE.EquirectangularReflectionMapping;
scene.environment = hdr; // lights + reflects all PBR materials
scene.background = hdr; // optional: show it as the backdrop
});
// Optional cinematic tone curve:
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.0;
Pitfalls
- Mesh is pure black → a lit material with no light and no
scene.environment.
Add a light or an environment map; to confirm geometry, temporarily swap to
MeshBasicMaterial/MeshNormalMaterial.
- Scene too dark even with lights → old tutorial intensities. r155+ is physically
based; raise intensities (key light ≈ 2–3) or add an environment map.
- Shadows don't appear → you missed one of the three switches
(
renderer.shadowMap.enabled, light.castShadow, mesh castShadow/
receiveShadow).
- Shadows are cut off or blocky → the
DirectionalLight's orthographic
shadow.camera frustum is too big/small or doesn't cover the scene; tighten
left/right/top/bottom/near/far and raise shadow.mapSize. Visualise it with
new THREE.CameraHelper(light.shadow.camera).
- Shadow acne / peter-panning → adjust
light.shadow.bias (small negative) and
light.shadow.normalBias.
- Colors look washed out / too bright → color (albedo) textures need
texture.colorSpace = THREE.SRGBColorSpace; normal/roughness/metalness maps must
stay linear (leave them as NoColorSpace).
- PointLight shadows tank performance → a point light renders the scene 6 times
(cube map). Prefer one shadow-casting
DirectionalLight; use cheaper fakes
elsewhere.
References
- For the material cheat-sheet (which
Mesh*Material for which look), light types
and their parameters/units, transparency vs alphaTest ordering, and the
PMREMGenerator/RoomEnvironment route to IBL without an HDR file, read
references/materials-lights-table.md.
Related skills
threejs-scene-setup — renderer, camera, and loop (set shadowMap, tone mapping).
threejs-gltf-loading — models arrive with PBR materials this skill tunes.
shader-programming — custom shader effects (engine-agnostic concept).
1---2name: threejs-materials-lighting3description: Light and shade a three.js scene: choose materials (MeshStandardMaterial PBR vs unlit MeshBasicMaterial), add ambient/hemisphere/directional/point/spot lights, turn on shadow maps, and use an environment map (IBL) for realistic reflections. Use when a three.js model looks black, flat, or wrong — when the user mentions three.js materials, MeshStandardMaterial, lights, shadows, envMap, or PBR. For renderer/loop setup use threejs-scene-setup; for loading models use threejs-gltf-loading.4---5
6# three.js Materials & Lighting
7
8Make three.js surfaces look right: pick the correct material, light the scene,
9enable shadows, and add image-based lighting. Patterns target **r184**, verified
10against **r184** (lighting is physically based by default since r155).
11
12## When to use
13
14- Use when a mesh renders black or flat, when choosing a material, adding lights,
15 enabling shadows, or setting up environment-map reflections (IBL).
16- Use when code constructs `MeshStandardMaterial`, `DirectionalLight`, etc., or sets
17 `renderer.shadowMap.enabled` or `scene.environment`.
18
19**When *not* to use:** the renderer/camera/loop → `threejs-scene-setup`. Loading
20models (whose PBR materials this complements) → `threejs-gltf-loading`. Custom
21GLSL/`ShaderMaterial` is its own topic; for the portable concept see
22`shader-programming`.
23
24## Core workflow
25
261. **Pick a material by need.** `MeshStandardMaterial` (PBR: `roughness`,
27 `metalness`, reacts to lights/IBL) for realism; `MeshPhysicalMaterial` for
28 clearcoat/transmission; `MeshBasicMaterial` (unlit, ignores lights) for UI/flat;
29 `MeshNormalMaterial`/`MeshDepthMaterial` for debugging.
302. **Add light, or nothing shows.** Lit materials need a light source and/or
31 `scene.environment`. Combine a soft fill (`AmbientLight`/`HemisphereLight`) with a
32 key `DirectionalLight`.
333. **Mind light intensity.** Since r155, lighting is physically based; modern
34 intensities are higher than old tutorials (a key `DirectionalLight` ≈ 1–3).
354. **Enable shadows in three places.** `renderer.shadowMap.enabled = true`, the
36 light's `castShadow = true`, and each mesh's `castShadow`/`receiveShadow`. Then
37 fit the light's shadow camera to the scene.
385. **Use an environment map for grounded reflections.** Assign an equirectangular or
39 PMREM-processed texture to `scene.environment`; PBR materials pick it up
40 automatically.
416. **Verify under real lighting** — confirm the surface responds to the key light
42 (highlights move), shadows land where expected, and reflections look plausible.
43
44## Patterns
45
46### 1. PBR material under a 3-light rig
47
48```js
49import * as THREE from 'three';
50
51const material = new THREE.MeshStandardMaterial({
52 color: 0xcc4444,
53 roughness: 0.5, // 0 = mirror, 1 = fully matte
54 metalness: 0.0, // 0 = dielectric (plastic/wood), 1 = metal
55});
56const mesh = new THREE.Mesh(new THREE.SphereGeometry(1, 32, 16), material);
57scene.add(mesh);
58
59// Soft sky/ground fill + a directional key light.
60scene.add(new THREE.HemisphereLight(0xbbddff, 0x443322, 1.0)); // sky, ground, intensity
61const key = new THREE.DirectionalLight(0xffffff, 2.5);
62key.position.set(5, 10, 7);
63scene.add(key);
64```
65
66### 2. Unlit material (no light needed)
67
68```js
69// MeshBasicMaterial ignores lights — for flat color, UI, or sprites/labels.
70const flat = new THREE.MeshBasicMaterial({ color: 0x44aa88 });
71// A textured color map should be tagged sRGB so colors aren't washed out:
72const tex = new THREE.TextureLoader().load('assets/logo.png');
73tex.colorSpace = THREE.SRGBColorSpace;
74const logo = new THREE.MeshBasicMaterial({ map: tex, transparent: true });
75```
76
77### 3. Shadows (the three required switches + camera fit)
78
79```js
80renderer.shadowMap.enabled = true;
81renderer.shadowMap.type = THREE.PCFSoftShadowMap; // softer edges
82
83const sun = new THREE.DirectionalLight(0xffffff, 3);
84sun.position.set(8, 12, 6);
85sun.castShadow = true;
86sun.shadow.mapSize.set(2048, 2048); // default 512; raise for crisp
87// DirectionalLight uses an OrthographicCamera — fit it tightly to the scene:
88const cam = sun.shadow.camera;
89cam.near = 1; cam.far = 40;
90cam.left = -15; cam.right = 15; cam.top = 15; cam.bottom = -15;
91scene.add(sun);
92
93mesh.castShadow = true;
94ground.receiveShadow = true; // a plane to catch the shadow
95```
96
97### 4. PBR textures on a material
98
99```js
100const loader = new THREE.TextureLoader();
101const colorMap = loader.load('assets/brick_color.jpg');
102colorMap.colorSpace = THREE.SRGBColorSpace; // color maps are sRGB
103const normalMap = loader.load('assets/brick_normal.jpg'); // data maps stay linear
104const roughMap = loader.load('assets/brick_rough.jpg');
105
106const brick = new THREE.MeshStandardMaterial({
107 map: colorMap,
108 normalMap,
109 roughnessMap: roughMap,
110 metalness: 0,
111});
112```
113
114### 5. Image-based lighting from an HDR environment
115
116```js
117import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
118
119new RGBELoader().load('assets/studio.hdr', (hdr) => {
120 hdr.mapping = THREE.EquirectangularReflectionMapping;
121 scene.environment = hdr; // lights + reflects all PBR materials
122 scene.background = hdr; // optional: show it as the backdrop
123});
124// Optional cinematic tone curve:
125renderer.toneMapping = THREE.ACESFilmicToneMapping;
126renderer.toneMappingExposure = 1.0;
127```
128
129## Pitfalls
130
131- **Mesh is pure black** → a lit material with no light and no `scene.environment`.
132 Add a light or an environment map; to confirm geometry, temporarily swap to
133 `MeshBasicMaterial`/`MeshNormalMaterial`.
134- **Scene too dark even with lights** → old tutorial intensities. r155+ is physically
135 based; raise intensities (key light ≈ 2–3) or add an environment map.
136- **Shadows don't appear** → you missed one of the three switches
137 (`renderer.shadowMap.enabled`, `light.castShadow`, mesh `castShadow`/
138 `receiveShadow`).
139- **Shadows are cut off or blocky** → the `DirectionalLight`'s orthographic
140 `shadow.camera` frustum is too big/small or doesn't cover the scene; tighten
141 `left/right/top/bottom/near/far` and raise `shadow.mapSize`. Visualise it with
142 `new THREE.CameraHelper(light.shadow.camera)`.
143- **Shadow acne / peter-panning** → adjust `light.shadow.bias` (small negative) and
144 `light.shadow.normalBias`.
145- **Colors look washed out / too bright** → color (albedo) textures need
146 `texture.colorSpace = THREE.SRGBColorSpace`; normal/roughness/metalness maps must
147 stay linear (leave them as `NoColorSpace`).
148- **PointLight shadows tank performance** → a point light renders the scene 6 times
149 (cube map). Prefer one shadow-casting `DirectionalLight`; use cheaper fakes
150 elsewhere.
151
152## References
153
154- For the material cheat-sheet (which `Mesh*Material` for which look), light types
155 and their parameters/units, transparency vs `alphaTest` ordering, and the
156 `PMREMGenerator`/`RoomEnvironment` route to IBL without an HDR file, read
157 `references/materials-lights-table.md`.
158
159## Related skills
160
161- `threejs-scene-setup` — renderer, camera, and loop (set `shadowMap`, tone mapping).
162- `threejs-gltf-loading` — models arrive with PBR materials this skill tunes.
163- `shader-programming` — custom shader effects (engine-agnostic concept).