HOW TO ADD LIGHTING
TO A THREE.JS SCENE
By Shane Brumback · · Updated · Free Three.js tutorial · Level 1
To light a Three.js scene, add a soft fill light such as AmbientLight or HemisphereLight, one main DirectionalLight for the sun, and PointLight or SpotLight for local glows, then turn on shadows. This campsite uses all four: the sun, sky fill, a flickering campfire and a lamp-post spotlight.
A beginner walkthrough of every common light in Three.js 0.160, built only from simple shapes. The campsite behind this page is the live demo: watch it slide from day to dusk as the fire and lamp take over. The full copy-paste source is below.
LIVE DEMO
The campsite behind this page is a live Three.js scene. Launch the full-screen demo to drag to orbit and scroll to zoom around the tent, the campfire and the lamp post while the sun sets. Launch asks for a free Google sign-in first. Press Esc or Exit to return.
Free Google sign-in · Orbit: drag · Zoom: scroll · Esc to exit
READY FOR ENDLESS TERRAIN?
This Level 1 campsite sits on a small circle of grass under one sun. The Level 3 premium Terrain LOD tutorial lights endless hills with sand, grass, rock and snow blended in an onBeforeCompile shader, with LOD skirts, worker chunks and grass that follows you. Members unlock the full code; sign in to reveal it.
How Lighting Works in Three.js
A light in Three.js is an invisible object that tells materials how bright and what color each surface should be. Materials like MeshStandardMaterial stay black until a light reaches them, while MeshBasicMaterial ignores lights, which is why the flame and the bulb use it to look like they glow.
Real places mix lights, so this demo maps each type to something you already know: AmbientLight and HemisphereLight are the sky, DirectionalLight is the sun, PointLight is the campfire, and SpotLight is the lamp. New to Three.js? Start with the spinning cube starter or rendering a 3D cube, then come back here.
Step-by-Step Code Breakdown
Every excerpt below is copied exactly from the full code further down.
1. Set up the scene, camera and renderer
In plain words: we make an empty 3D world, a camera that looks at it, and a renderer that draws it. The one setting that matters most for lighting is shadowMap.enabled: without it, no light can cast a shadow. Create a Scene, a PerspectiveCamera and a WebGLRenderer, enable PCF soft shadow maps and ACES tone mapping, and add OrbitControls so you can walk around the campsite.
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(50, innerWidth / innerHeight, 0.1, 200);
camera.position.set(9, 5.5, 12);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, isMobile ? 1.5 : 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.style.margin = '0';
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 1.2, 0);
controls.enableDamping = true;
controls.maxPolarAngle = Math.PI * 0.47;
controls.minDistance = 6;
controls.maxDistance = 30;
2. Fill the sky with AmbientLight and HemisphereLight
In plain words: a scene with no light is pitch black, so first we add soft light that comes from everywhere. AmbientLight brightens every surface equally. HemisphereLight is smarter: blue light from the sky above and green bounce light from the grass below. Set a sky-blue background with matching Fog, add a weak AmbientLight as a base, and a HemisphereLight with a sky color and a ground color for natural outdoor fill.
const DAY_SKY = new THREE.Color(0x8ec5f0);
const DUSK_SKY = new THREE.Color(0x2b2f5a);
scene.background = DAY_SKY.clone();
scene.fog = new THREE.Fog(DAY_SKY.clone(), 25, 70);
const ambient = new THREE.AmbientLight(0x6070a0, 0.25);
scene.add(ambient);
const hemi = new THREE.HemisphereLight(0xbfdcff, 0x3a5a28, 1.0);
scene.add(hemi);
3. Add the sun with a DirectionalLight
In plain words: the sun is so far away that all its rays are parallel, which is exactly what a DirectionalLight does. It is the main light here and the one that throws the long tree shadows. Create a DirectionalLight, set castShadow, choose a shadow map size (smaller on phones), and fit its orthographic shadow camera around the campsite so shadows stay sharp.
const sun = new THREE.DirectionalLight(0xfff1d6, 2.5);
sun.position.set(12, 16, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(isMobile ? 1024 : 2048, isMobile ? 1024 : 2048);
sun.shadow.camera.left = sun.shadow.camera.bottom = -14;
sun.shadow.camera.right = sun.shadow.camera.top = 14;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 50;
sun.shadow.bias = -0.0005;
scene.add(sun);
4. Build the ground, trees and rocks
In plain words: lights need something to land on. We build everything from simple shapes: a flat circle for grass, cones stacked on a cylinder for each pine tree, and lumpy dodecahedrons for rocks. Use MeshStandardMaterial (it reacts to every light type) with flatShading for a low-poly look. Set castShadow on objects that block light and receiveShadow on the ground.
const ground = new THREE.Mesh(
new THREE.CircleGeometry(40, 48),
new THREE.MeshStandardMaterial({ color: 0x4f8a34, roughness: 1 })
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const flat = (color) => new THREE.MeshStandardMaterial({ color, roughness: 0.9, flatShading: true });
const trunkMat = flat(0x6b4a2b);
const leafMat = flat(0x2f6b3a);
const rockMat = flat(0x8a8f96);
function addTree(x, z, s) {
const tree = new THREE.Group();
const trunk = new THREE.Mesh(new THREE.CylinderGeometry(0.15, 0.22, 1, 6), trunkMat);
trunk.position.y = 0.5;
tree.add(trunk);
for (let i = 0; i < 3; i++) {
const leaves = new THREE.Mesh(new THREE.ConeGeometry(1.1 - i * 0.25, 1.4, 7), leafMat);
leaves.position.y = 1.3 + i * 0.7;
tree.add(leaves);
}
tree.traverse((o) => { o.castShadow = true; });
tree.position.set(x, 0, z);
tree.scale.setScalar(s);
scene.add(tree);
}
[[-6, -4, 1.4], [-3.5, -7, 1.7], [1, -8, 1.5], [5, -6.5, 1.8], [-8, 1, 1.3],
[7.5, -2, 1.2], [-6.5, 5, 1.6], [3, -11, 2.0], [-10, -6, 1.9]]
.forEach(([x, z, s]) => addTree(x, z, s));
function addRock(x, z, s) {
const rock = new THREE.Mesh(new THREE.DodecahedronGeometry(s, 0), rockMat);
rock.position.set(x, s * 0.5, z);
rock.rotation.set(Math.random(), Math.random(), Math.random());
rock.castShadow = rock.receiveShadow = true;
scene.add(rock);
}
addRock(3.6, 2.2, 0.5);
addRock(4.2, 1.6, 0.3);
addRock(-3, 3.5, 0.6);
addRock(2, -3.5, 0.4);
5. Pitch the tent
In plain words: a four-sided cone makes a perfect tent. Its orange fabric shows off the warm campfire light at dusk. Use an open-ended ConeGeometry with 4 radial segments, rotate it 45 degrees so a flat side faces the fire, and let it both cast and receive shadows.
const tent = new THREE.Mesh(
new THREE.ConeGeometry(1.6, 2, 4, 1, true),
new THREE.MeshStandardMaterial({ color: 0xd9822b, roughness: 0.8, flatShading: true, side: THREE.DoubleSide })
);
tent.position.set(-2.2, 1, -1.6);
tent.rotation.y = Math.PI / 4;
tent.castShadow = tent.receiveShadow = true;
scene.add(tent);
6. Light the campfire with a PointLight
In plain words: a PointLight shines in every direction from one spot, like a bulb or a flame. We put one just above a cone of glowing flame, inside a ring of stones and logs. Create a PointLight with a warm color, a distance of 12 and decay 2 so it fades realistically. The flame uses MeshBasicMaterial so it glows without needing light. Point light shadows cost six renders, so they are off on phones.
const fire = new THREE.Group();
fire.position.set(0.8, 0, 1.2);
const logMat = flat(0x4a3222);
for (let i = 0; i < 4; i++) {
const log = new THREE.Mesh(new THREE.CylinderGeometry(0.08, 0.08, 1, 6), logMat);
log.rotation.set(Math.PI / 2.6, (i / 4) * Math.PI * 2, 0);
log.position.y = 0.2;
log.castShadow = true;
fire.add(log);
}
for (let i = 0; i < 8; i++) {
const a = (i / 8) * Math.PI * 2;
const stone = new THREE.Mesh(new THREE.DodecahedronGeometry(0.16, 0), rockMat);
stone.position.set(Math.cos(a) * 0.6, 0.08, Math.sin(a) * 0.6);
fire.add(stone);
}
const flame = new THREE.Mesh(
new THREE.ConeGeometry(0.28, 0.8, 8),
new THREE.MeshBasicMaterial({ color: 0xffa040 })
);
flame.position.y = 0.55;
fire.add(flame);
const fireLight = new THREE.PointLight(0xff8a2a, 20, 12, 2);
fireLight.position.y = 0.9;
fireLight.castShadow = !isMobile;
fireLight.shadow.mapSize.set(512, 512);
fire.add(fireLight);
scene.add(fire);
7. Hang a SpotLight in the lamp post
In plain words: a SpotLight is a cone of light, like a flashlight or a street lamp. We aim it down from the lamp head to make a bright pool on the grass. Build the post from a cylinder, a box arm and a cone shade. Create a SpotLight with an angle of PI / 6 and a soft penumbra, set its target position, and add both the light and its target to the lamp group.
const lamp = new THREE.Group();
lamp.position.set(4.2, 0, -1.2);
const metal = new THREE.MeshStandardMaterial({ color: 0x2a2d33, metalness: 0.6, roughness: 0.4 });
const pole = new THREE.Mesh(new THREE.CylinderGeometry(0.07, 0.1, 4, 8), metal);
pole.position.y = 2;
const arm = new THREE.Mesh(new THREE.BoxGeometry(0.9, 0.08, 0.08), metal);
arm.position.set(-0.4, 3.95, 0);
const head = new THREE.Mesh(new THREE.ConeGeometry(0.3, 0.3, 8, 1, true), metal);
head.position.set(-0.8, 3.8, 0);
const bulb = new THREE.Mesh(new THREE.SphereGeometry(0.1, 12, 8), new THREE.MeshBasicMaterial({ color: 0xfff2c4 }));
bulb.position.set(-0.8, 3.68, 0);
pole.castShadow = arm.castShadow = head.castShadow = true;
lamp.add(pole, arm, head, bulb);
const spot = new THREE.SpotLight(0xffe7b0, 60, 14, Math.PI / 6, 0.5, 2);
spot.position.set(-0.8, 3.65, 0);
spot.target.position.set(-1.3, 0, 0.6);
spot.castShadow = true;
spot.shadow.mapSize.set(512, 512);
lamp.add(spot, spot.target);
scene.add(lamp);
8. Add drifting low-poly clouds
In plain words: a few puffy clouds make the sky feel alive, and they show the sunset colours beautifully. Each cloud is five squashed icospheres in a row. Use one InstancedMesh with flatShading so every puff is drawn in a single call, move each cloud slowly along x and wrap it around, and tint the material from white by day to warm pink at sunset and dim blue at dusk. Clouds only cast shadows on desktop, and phones get fewer of them.
const CLOUD_DAY = new THREE.Color(0xffffff);
const CLOUD_GLOW = new THREE.Color(0xffa98a);
const CLOUD_NIGHT = new THREE.Color(0x4a5280);
const cloudMat = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 1, flatShading: true, emissive: 0xffffff, emissiveIntensity: 0.3 });
const CLOUDS = isMobile ? 3 : 5;
const PUFFS = 5;
const clouds = new THREE.InstancedMesh(new THREE.IcosahedronGeometry(1, 1), cloudMat, CLOUDS * PUFFS);
clouds.castShadow = !isMobile;
clouds.frustumCulled = false;
scene.add(clouds);
const cloudData = [];
for (let c = 0; c < CLOUDS; c++) {
const puffs = [];
for (let p = 0; p < PUFFS; p++) {
puffs.push({ x: (p - 2) * 1.5, y: Math.random() * 0.5, z: Math.random() - 0.5, s: 2 - Math.abs(p - 2) * 0.4 });
}
cloudData.push({ x: -32 + c * 13, y: 7.5 + (c % 3) * 1.2, z: -18 - (c % 2) * 5, speed: 0.35 + (c % 3) * 0.12, puffs });
}
const dummy = new THREE.Object3D();
function updateClouds(t, dusk) {
cloudData.forEach((c, ci) => {
const x = ((c.x + c.speed * t + 32) % 64) - 32;
c.puffs.forEach((p, pi) => {
dummy.position.set(x + p.x, c.y + p.y, c.z + p.z);
dummy.scale.set(p.s, p.s * 0.55, p.s);
dummy.updateMatrix();
clouds.setMatrixAt(ci * PUFFS + pi, dummy.matrix);
});
});
clouds.instanceMatrix.needsUpdate = true;
if (dusk < 0.6) cloudMat.color.lerpColors(CLOUD_DAY, CLOUD_GLOW, dusk / 0.6);
else cloudMat.color.lerpColors(CLOUD_GLOW, CLOUD_NIGHT, (dusk - 0.6) / 0.4);
cloudMat.emissive.copy(cloudMat.color);
}
9. Animate day to dusk and the flicker
In plain words: every frame we slowly lower the sun, darken the sky, and turn the campfire and lamp up as night falls. The fire flickers by mixing two fast sine waves with a little randomness. Compute a dusk value from 0 to 1 with a cosine over a 24 second cycle, lerp the sun angle, intensity and color, the hemisphere intensity and the sky color, then scale the PointLight and flame by the flicker.
const clock = new THREE.Clock();
const DAY_LENGTH = 24;
function render() {
requestAnimationFrame(render);
const t = clock.getElapsedTime();
const dusk = 0.5 - 0.5 * Math.cos((t / DAY_LENGTH) * Math.PI * 2);
const angle = THREE.MathUtils.lerp(1.0, 0.18, dusk);
sun.position.set(Math.cos(angle) * 18, Math.sin(angle) * 18, 6);
sun.intensity = THREE.MathUtils.lerp(2.5, 0.35, dusk);
sun.color.setHSL(0.1, 0.8, THREE.MathUtils.lerp(0.92, 0.6, dusk));
hemi.intensity = THREE.MathUtils.lerp(1.0, 0.25, dusk);
scene.background.lerpColors(DAY_SKY, DUSK_SKY, dusk);
scene.fog.color.copy(scene.background);
const flicker = 0.8 + Math.sin(t * 13) * 0.1 + Math.sin(t * 29) * 0.07 + Math.random() * 0.08;
fireLight.intensity = THREE.MathUtils.lerp(8, 30, dusk) * flicker;
flame.scale.set(1, flicker, 1);
spot.intensity = THREE.MathUtils.lerp(10, 70, dusk);
updateClouds(t, dusk);
controls.update();
renderer.render(scene, camera);
}
render();
Want to add motion next? See creating and animating simple 3D objects, or grow the meadow with the endless procedural grass tutorial.
The Full Code
This is one complete HTML file (Three.js 0.160.0 from unpkg, no build tools, no models or textures). Save it as index.html and open it, or press Launch Full Code to run it in a new window.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Three.js Lighting Demo: Outdoor Campsite</title>
<script type="importmap">
{"imports":{"three":"https://unpkg.com/three@0.160.0/build/three.module.js","three/addons/":"https://unpkg.com/three@0.160.0/examples/jsm/"}}
</script>
</head>
<body>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const isMobile = matchMedia('(max-width: 768px)').matches;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(50, innerWidth / innerHeight, 0.1, 200);
camera.position.set(9, 5.5, 12);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, isMobile ? 1.5 : 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.style.margin = '0';
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 1.2, 0);
controls.enableDamping = true;
controls.maxPolarAngle = Math.PI * 0.47;
controls.minDistance = 6;
controls.maxDistance = 30;
const DAY_SKY = new THREE.Color(0x8ec5f0);
const DUSK_SKY = new THREE.Color(0x2b2f5a);
scene.background = DAY_SKY.clone();
scene.fog = new THREE.Fog(DAY_SKY.clone(), 25, 70);
const ambient = new THREE.AmbientLight(0x6070a0, 0.25);
scene.add(ambient);
const hemi = new THREE.HemisphereLight(0xbfdcff, 0x3a5a28, 1.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff1d6, 2.5);
sun.position.set(12, 16, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(isMobile ? 1024 : 2048, isMobile ? 1024 : 2048);
sun.shadow.camera.left = sun.shadow.camera.bottom = -14;
sun.shadow.camera.right = sun.shadow.camera.top = 14;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 50;
sun.shadow.bias = -0.0005;
scene.add(sun);
const ground = new THREE.Mesh(
new THREE.CircleGeometry(40, 48),
new THREE.MeshStandardMaterial({ color: 0x4f8a34, roughness: 1 })
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const flat = (color) => new THREE.MeshStandardMaterial({ color, roughness: 0.9, flatShading: true });
const trunkMat = flat(0x6b4a2b);
const leafMat = flat(0x2f6b3a);
const rockMat = flat(0x8a8f96);
function addTree(x, z, s) {
const tree = new THREE.Group();
const trunk = new THREE.Mesh(new THREE.CylinderGeometry(0.15, 0.22, 1, 6), trunkMat);
trunk.position.y = 0.5;
tree.add(trunk);
for (let i = 0; i < 3; i++) {
const leaves = new THREE.Mesh(new THREE.ConeGeometry(1.1 - i * 0.25, 1.4, 7), leafMat);
leaves.position.y = 1.3 + i * 0.7;
tree.add(leaves);
}
tree.traverse((o) => { o.castShadow = true; });
tree.position.set(x, 0, z);
tree.scale.setScalar(s);
scene.add(tree);
}
[[-6, -4, 1.4], [-3.5, -7, 1.7], [1, -8, 1.5], [5, -6.5, 1.8], [-8, 1, 1.3],
[7.5, -2, 1.2], [-6.5, 5, 1.6], [3, -11, 2.0], [-10, -6, 1.9]]
.forEach(([x, z, s]) => addTree(x, z, s));
function addRock(x, z, s) {
const rock = new THREE.Mesh(new THREE.DodecahedronGeometry(s, 0), rockMat);
rock.position.set(x, s * 0.5, z);
rock.rotation.set(Math.random(), Math.random(), Math.random());
rock.castShadow = rock.receiveShadow = true;
scene.add(rock);
}
addRock(3.6, 2.2, 0.5);
addRock(4.2, 1.6, 0.3);
addRock(-3, 3.5, 0.6);
addRock(2, -3.5, 0.4);
const tent = new THREE.Mesh(
new THREE.ConeGeometry(1.6, 2, 4, 1, true),
new THREE.MeshStandardMaterial({ color: 0xd9822b, roughness: 0.8, flatShading: true, side: THREE.DoubleSide })
);
tent.position.set(-2.2, 1, -1.6);
tent.rotation.y = Math.PI / 4;
tent.castShadow = tent.receiveShadow = true;
scene.add(tent);
const fire = new THREE.Group();
fire.position.set(0.8, 0, 1.2);
const logMat = flat(0x4a3222);
for (let i = 0; i < 4; i++) {
const log = new THREE.Mesh(new THREE.CylinderGeometry(0.08, 0.08, 1, 6), logMat);
log.rotation.set(Math.PI / 2.6, (i / 4) * Math.PI * 2, 0);
log.position.y = 0.2;
log.castShadow = true;
fire.add(log);
}
for (let i = 0; i < 8; i++) {
const a = (i / 8) * Math.PI * 2;
const stone = new THREE.Mesh(new THREE.DodecahedronGeometry(0.16, 0), rockMat);
stone.position.set(Math.cos(a) * 0.6, 0.08, Math.sin(a) * 0.6);
fire.add(stone);
}
const flame = new THREE.Mesh(
new THREE.ConeGeometry(0.28, 0.8, 8),
new THREE.MeshBasicMaterial({ color: 0xffa040 })
);
flame.position.y = 0.55;
fire.add(flame);
const fireLight = new THREE.PointLight(0xff8a2a, 20, 12, 2);
fireLight.position.y = 0.9;
fireLight.castShadow = !isMobile;
fireLight.shadow.mapSize.set(512, 512);
fire.add(fireLight);
scene.add(fire);
const lamp = new THREE.Group();
lamp.position.set(4.2, 0, -1.2);
const metal = new THREE.MeshStandardMaterial({ color: 0x2a2d33, metalness: 0.6, roughness: 0.4 });
const pole = new THREE.Mesh(new THREE.CylinderGeometry(0.07, 0.1, 4, 8), metal);
pole.position.y = 2;
const arm = new THREE.Mesh(new THREE.BoxGeometry(0.9, 0.08, 0.08), metal);
arm.position.set(-0.4, 3.95, 0);
const head = new THREE.Mesh(new THREE.ConeGeometry(0.3, 0.3, 8, 1, true), metal);
head.position.set(-0.8, 3.8, 0);
const bulb = new THREE.Mesh(new THREE.SphereGeometry(0.1, 12, 8), new THREE.MeshBasicMaterial({ color: 0xfff2c4 }));
bulb.position.set(-0.8, 3.68, 0);
pole.castShadow = arm.castShadow = head.castShadow = true;
lamp.add(pole, arm, head, bulb);
const spot = new THREE.SpotLight(0xffe7b0, 60, 14, Math.PI / 6, 0.5, 2);
spot.position.set(-0.8, 3.65, 0);
spot.target.position.set(-1.3, 0, 0.6);
spot.castShadow = true;
spot.shadow.mapSize.set(512, 512);
lamp.add(spot, spot.target);
scene.add(lamp);
const CLOUD_DAY = new THREE.Color(0xffffff);
const CLOUD_GLOW = new THREE.Color(0xffa98a);
const CLOUD_NIGHT = new THREE.Color(0x4a5280);
const cloudMat = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 1, flatShading: true, emissive: 0xffffff, emissiveIntensity: 0.3 });
const CLOUDS = isMobile ? 3 : 5;
const PUFFS = 5;
const clouds = new THREE.InstancedMesh(new THREE.IcosahedronGeometry(1, 1), cloudMat, CLOUDS * PUFFS);
clouds.castShadow = !isMobile;
clouds.frustumCulled = false;
scene.add(clouds);
const cloudData = [];
for (let c = 0; c < CLOUDS; c++) {
const puffs = [];
for (let p = 0; p < PUFFS; p++) {
puffs.push({ x: (p - 2) * 1.5, y: Math.random() * 0.5, z: Math.random() - 0.5, s: 2 - Math.abs(p - 2) * 0.4 });
}
cloudData.push({ x: -32 + c * 13, y: 7.5 + (c % 3) * 1.2, z: -18 - (c % 2) * 5, speed: 0.35 + (c % 3) * 0.12, puffs });
}
const dummy = new THREE.Object3D();
function updateClouds(t, dusk) {
cloudData.forEach((c, ci) => {
const x = ((c.x + c.speed * t + 32) % 64) - 32;
c.puffs.forEach((p, pi) => {
dummy.position.set(x + p.x, c.y + p.y, c.z + p.z);
dummy.scale.set(p.s, p.s * 0.55, p.s);
dummy.updateMatrix();
clouds.setMatrixAt(ci * PUFFS + pi, dummy.matrix);
});
});
clouds.instanceMatrix.needsUpdate = true;
if (dusk < 0.6) cloudMat.color.lerpColors(CLOUD_DAY, CLOUD_GLOW, dusk / 0.6);
else cloudMat.color.lerpColors(CLOUD_GLOW, CLOUD_NIGHT, (dusk - 0.6) / 0.4);
cloudMat.emissive.copy(cloudMat.color);
}
const clock = new THREE.Clock();
const DAY_LENGTH = 24;
function render() {
requestAnimationFrame(render);
const t = clock.getElapsedTime();
const dusk = 0.5 - 0.5 * Math.cos((t / DAY_LENGTH) * Math.PI * 2);
const angle = THREE.MathUtils.lerp(1.0, 0.18, dusk);
sun.position.set(Math.cos(angle) * 18, Math.sin(angle) * 18, 6);
sun.intensity = THREE.MathUtils.lerp(2.5, 0.35, dusk);
sun.color.setHSL(0.1, 0.8, THREE.MathUtils.lerp(0.92, 0.6, dusk));
hemi.intensity = THREE.MathUtils.lerp(1.0, 0.25, dusk);
scene.background.lerpColors(DAY_SKY, DUSK_SKY, dusk);
scene.fog.color.copy(scene.background);
const flicker = 0.8 + Math.sin(t * 13) * 0.1 + Math.sin(t * 29) * 0.07 + Math.random() * 0.08;
fireLight.intensity = THREE.MathUtils.lerp(8, 30, dusk) * flicker;
flame.scale.set(1, flicker, 1);
spot.intensity = THREE.MathUtils.lerp(10, 70, dusk);
updateClouds(t, dusk);
controls.update();
renderer.render(scene, camera);
}
render();
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>
Frequently Asked Questions
What are the main light types in Three.js?
Three.js has AmbientLight (even light everywhere), HemisphereLight (sky and ground colors), DirectionalLight (parallel rays like the sun), PointLight (all directions from one point, like a bulb or fire), SpotLight (a cone like a flashlight) and RectAreaLight (a glowing panel).
Why is my Three.js scene completely black?
Either there are no lights, or your material needs light. MeshStandardMaterial, MeshPhongMaterial and MeshLambertMaterial are dark without a light, while MeshBasicMaterial ignores lights. Add an AmbientLight or HemisphereLight plus a DirectionalLight to start.
How do I turn on shadows in Three.js?
Set renderer.shadowMap.enabled to true, set castShadow to true on the light and on the objects that block it, and set receiveShadow to true on the ground. Only DirectionalLight, PointLight and SpotLight can cast shadows.
Why are my PointLight and SpotLight so dim in Three.js r160?
Since r155 Three.js uses physically correct light units by default, so point and spot intensities are in candela and fade with distance squared when decay is 2. Use larger intensities, such as 20 to 70 in this demo, or move the light closer.
How do I make a light flicker like fire?
In the render loop multiply the PointLight intensity by a flicker value built from two fast sine waves plus a small Math.random() amount, and scale the flame mesh by the same value so the glow and the flame move together.
How do I add cheap low-poly clouds in Three.js?
Use one InstancedMesh of flat-shaded IcosahedronGeometry puffs squashed on the y axis, a few puffs per cloud, move each cloud slowly along x and wrap it around, and lerp the material color with the same dusk value that drives the sky so the clouds turn pink at sunset and blue at dusk.
Why do I need to sign in to launch the live demo?
The tutorial and its full code are free to read. The full-screen Live Demo asks for a free Google sign-in first, the same gate used across the site, so I know who is learning from these tutorials.
Play It and Keep Learning
Sun, shadows and fire light like this power Super Soldier Battle, free in your browser. Browse every Three.js tutorial, read the articles, or search the site.
Need Three.js lighting or WebGL work? I take freelance and contract work: contact me.
Play Game