TExcelTAD

Lights

Lit materials (Lambert, Phong, Standard, Physical, Toon) need light to be visible. Three.js has a small set of light types, and each one you add costs GPU time on every lit pixel, so game scenes use a few well-chosen lights rather than many.

Light Models Main parameters Casts shadows
AmbientLight light from everywhere, equally color, intensity no
HemisphereLight sky color from above, ground color from below skyColor, groundColor, intensity no
DirectionalLight the sun: parallel rays from one direction intensity, position → target yes
PointLight a bulb or torch: all directions from a point intensity (candela), distance, decay yes (6 shadow maps: expensive)
SpotLight a cone: flashlight, stage light angle, penumbra, intensity (candela), target yes
RectAreaLight a glowing rectangle: windows, panels width, height, intensity no; Standard/Physical materials only, needs RectAreaLightUniformsLib.init() from the addons

Physically based units

Since r155, Three.js uses physically based light units by default, and the old "legacy" mode has since been removed. That changed what intensity numbers mean, which is why lights copied from older tutorials often look far too dark:

  • PointLight and SpotLight intensity is in candela, and the light falls off with the inverse square of distance (decay = 2, the default and the physically correct value). A point light 5 m away needs an intensity in the tens to light a surface as brightly as a directional light of intensity 1–3.
  • DirectionalLight intensity is an illuminance (roughly lux, scaled for display); values of 1–5 are typical.
  • AmbientLight and HemisphereLight are fill lights; 0.2–3 is typical.
  • distance (point, spot) is an optional hard cut-off: 0 means "no limit", and anything else fades the light to zero at that range, which can save work but is not physical.
js · try it yourself
// How bright is a point light's contribution at different distances? (inverse-square, decay = 2)
const intensity = 40;                         // candela
for (const d of [1, 2, 3, 5, 8, 12]) {
  const lux = intensity / (d * d);
  console.log(`${String(d).padStart(2)} m: ${lux.toFixed(2).padStart(6)}  ${'#'.repeat(Math.round(lux))}`);
}
 1 m:  40.00  ########################################
 2 m:  10.00  ##########
 3 m:   4.44  ####
 5 m:   1.60  ##
 8 m:   0.63  #
12 m:   0.28  

Doubling the distance quarters the light. That is why a torch lights its own wall brightly and the far end of the room barely at all, and why game levels add many lights (or bake them) to cover a space evenly.

Note: If a scene from an old tutorial looks dark in r186, don't reach for useLegacyLights; it is gone. Raise point and spot intensities (often by a factor of tens) and check the other lights by eye.

The lights in action

Toggle each light type in this little courtyard. The directional "sun" and the hemisphere fill give the base; the point light is a flickering torch; the spot light sweeps like a guard's lantern.

html · try it yourself
<style>html, body { margin: 0; height: 100%; overflow: hidden } canvas { display: block }
#ui { position: fixed; left: 8px; top: 8px; display: flex; flex-wrap: wrap; gap: 4px }
#ui label { background: rgba(0,0,0,.55); color: #fff; padding: 3px 7px; border-radius: 4px; font: 12px system-ui, sans-serif }</style>
<div id="ui"></div>
<script type="module">
import * as THREE from 'three';

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color('#0b0e16');
const camera = new THREE.PerspectiveCamera(50, innerWidth / innerHeight, 0.1, 100);
camera.position.set(0, 6, 10);
camera.lookAt(0, 0.5, 0);
addEventListener('resize', () => {
  camera.aspect = innerWidth / innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(innerWidth, innerHeight);
});

const stone = new THREE.MeshStandardMaterial({ color: '#9e9a92', roughness: 0.9 });
const floor = new THREE.Mesh(new THREE.PlaneGeometry(14, 14), stone);
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
for (const [x, z, w, d] of [[0, -6, 14, 1], [-6.5, 0, 1, 12], [6.5, 0, 1, 12]]) {
  const wall = new THREE.Mesh(new THREE.BoxGeometry(w, 3, d), stone);
  wall.position.set(x, 1.5, z);
  scene.add(wall);
}
const shapes = [new THREE.SphereGeometry(0.7, 32, 16), new THREE.BoxGeometry(1.2, 1.2, 1.2), new THREE.TorusKnotGeometry(0.45, 0.15, 80, 12)];
shapes.forEach((g, i) => {
  const m = new THREE.Mesh(g, new THREE.MeshStandardMaterial({ color: ['#ef5350', '#42a5f5', '#ffca28'][i], roughness: 0.4 }));
  m.position.set((i - 1) * 3, 0.8, 0);
  scene.add(m);
});

const hemi = new THREE.HemisphereLight('#9ec3ff', '#3a2e22', 0.6);
const sun = new THREE.DirectionalLight('#fff1d6', 1.5);
sun.position.set(-5, 8, 4);                  // shines from here toward sun.target (the origin)
const torch = new THREE.PointLight('#ff9a3c', 25, 0, 2);
torch.position.set(-4, 2, -4.5);
const torchFlame = new THREE.Mesh(new THREE.SphereGeometry(0.12), new THREE.MeshBasicMaterial({ color: '#ffb060' }));
torch.add(torchFlame);
const lantern = new THREE.SpotLight('#cfe8ff', 120, 0, THREE.MathUtils.degToRad(22), 0.4, 2);
lantern.position.set(4, 5, 4);
scene.add(lantern.target);                   // the target must be in the scene to update its matrix
scene.add(hemi, sun, torch, lantern);

const ui = document.getElementById('ui');
const on = new Map();                        // light -> switched on?
for (const [name, light] of Object.entries({ hemisphere: hemi, directional: sun, point: torch, spot: lantern })) {
  on.set(light, light.intensity);            // remember the intensity to restore
  const label = document.createElement('label');
  label.innerHTML = `<input type="checkbox" checked> ${name}`;
  label.firstChild.onchange = (e) => { light.userData.off = !e.target.checked; };
  ui.appendChild(label);
}

const timer = new THREE.Timer();
renderer.setAnimationLoop((t) => {
  timer.update(t);
  const time = timer.getElapsed();
  for (const [light, intensity] of on) light.intensity = light.userData.off ? 0 : intensity;   // off = intensity 0
  if (!torch.userData.off) torch.intensity += Math.sin(time * 13) * 3 + Math.sin(time * 7.3) * 2;   // flicker
  torchFlame.visible = !torch.userData.off;
  lantern.target.position.set(Math.sin(time * 0.8) * 4, 0, Math.cos(time * 0.5) * 2);
  renderer.render(scene, camera);
});
</script>

Details worth copying:

  • Directional and spot lights aim at light.target, an Object3D at the origin by default. To aim elsewhere, move the target and add it to the scene (or parent it to something in the scene), otherwise its matrix never updates and the light keeps pointing at the origin.
  • The checkboxes switch a light off by setting its intensity to 0, not by removing it or setting visible = false. Lit materials are compiled for a particular number of lights of each type, and invisible lights are skipped when the renderer collects lights, so adding, removing or hiding a light makes every lit material recompile its shader: a visible hitch the first time it happens. A light at intensity 0 keeps the shaders as they are (it still costs its share of shading work, which is the price).
  • The torch flicker only changes intensity, which is a cheap uniform update.

A lighting recipe for games

A dependable starting setup for an outdoor scene:

js
scene.add(new THREE.HemisphereLight('#bcd7ff', '#4b3b2a', 1.0));   // sky/ground fill: no flat-black shadows
const sun = new THREE.DirectionalLight('#fff4e5', 2.5);              // key light, casts the shadows
sun.position.set(-30, 50, 20);
scene.add(sun, sun.target);
// plus scene.environment (an environment map) for reflections on PBR materials

Then add a few point or spot lights for gameplay meaning: a lamp by the door, a glow on the pickup, a muzzle flash. Each extra dynamic light costs every lit pixel on screen, so on mobile keep it to a handful, use distance to limit their reach, and bake static lighting into textures or vertex colors where you can.

Need Cheap option Expensive option
General fill HemisphereLight or an environment map many point lights
Glowing object emissive color (+ bloom post-processing) a real point light inside it
Static room lighting lightmaps baked in Blender (lightMap on the material) many dynamic lights with shadows
Moving flashlight one SpotLight spot light with shadows on mobile

Test yourself

A point light's illuminance at 2 m is 10. With physically correct decay, what is it at 4 m?

  1. 5
  2. 2.5
  3. 1.25
  4. 10

You set spot.target.position.set(5, 0, 0) but the spotlight still points at the origin. Why?

  1. Spotlights cannot be aimed
  2. The target isn't in the scene, so its world matrix is never updated
  3. You must call spot.lookAt instead
  4. The angle is too small

Why switch a light off with intensity = 0 rather than scene.remove(light) or light.visible = false?

  1. Intensity 0 frees GPU memory
  2. The light count stays the same, so lit materials don't recompile their shaders (no hitch)
  3. Hidden lights still cast shadows
  4. Removing lights is not allowed