WEEK 08
Web Games Development · 3D with Three.js
Shapes, Lights & Shadows
Materials, spot and point lights, real-time shadows and orbit controls: hunt for gems in a dark room.
geometries
materials
lights
shadows
OrbitControls
Game: Torch Hunt
Today · 90 minutes
Plan for the session
| Start | Segment | Time |
|---|---|---|
| 0:00 | Warm-up review | 10 min |
| 0:10 | This week’s game and objectives | 10 min |
| 0:20 | Concepts: idea, code, line by line, try it | 35 min |
| 0:55 | Common mistakes and check your understanding | 5 min |
| 1:00 | Lab: build the game | 25 min |
| 1:25 | Build-your-own task, recap and exit quiz | 5 min |
Week 8 · Shapes, Lights & Shadows
2 / 35
Warm-up · 5 minutes
Remember Week 7?
1
Name the three things every Three.js page needs.
2
What two things make a mesh?
3
Why multiply speed by dt?
4
Which command starts the dev server for a 3D week?
Week 8 · Shapes, Lights & Shadows
3 / 35
This week’s game
Find the gems in the dark
The room is almost dark. Your torch is a spot light shining down from above; steer where it points with the keyboard.
Six gold gems are hidden around the toys. Hold the beam on a gem for half a second and it glows and floats up.
Find all six before the 60-second countdown runs out. Drag with the mouse to look behind the toys.
HOW TO PLAY
Enter to start.
Arrow keys / WASD aim the torch.
Drag to orbit, scroll to zoom.
YOU WILL BUILD
- torchHunt.html
- package.json
Week 8 · Shapes, Lights & Shadows
4 / 35
Objectives
By the end of Week 8 you can…
01
Use many built-in shapes (plane, sphere, cylinder, cone…).
02
Style surfaces with roughness, metalness and emissive.
03
Light a scene with ambient, point and spot lights.
04
Switch on shadows: shadowMap, castShadow, receiveShadow.
05
Build a scene from many meshes with a helper function.
06
Import an add-on and orbit the camera with the mouse.
07
Move something while keys are held down.
08
Detect closeness with distanceTo; run a countdown.
Week 8 · Shapes, Lights & Shadows
5 / 35
Key words this week
Vocabulary
| Term | Meaning |
|---|---|
| roughness | 0 = smooth and shiny, 1 = matt |
| metalness | 0 = non-metal, 1 = metal |
| emissive | Light a surface gives off by itself (glow) |
| PointLight | Light from one point in every direction |
| SpotLight | A cone of light aimed at a target |
| shadow map | The picture a light uses to work out shadows |
| add-on | An extra Three.js module in three/addons, e.g. OrbitControls |
| distanceTo | The straight-line distance between two points |
Week 8 · Shapes, Lights & Shadows
6 / 35
What the program must do
What a torch game needs
A room
Floor, walls and toys built from many shapes.
Light and dark
A dim room and a bright torch beam with shadows.
Control
Keys steer the torch; the mouse orbits the camera.
A goal and a limit
Six gems to find before the timer hits zero.
Week 8 · Shapes, Lights & Shadows
7 / 35
Big idea 1 · Materials
A material answers: what is this made of?
1
roughness
0 = mirror-smooth, 1 = chalky and matt.
2
metalness
0 = plastic or wood, 1 = metal.
3
emissive
A colour the surface gives off by itself: it glows.
Week 8 · Shapes, Lights & Shadows
8 / 35
Example 1 · Materials
Same shape, four materials
Only the material settings change from ball to ball.
CODE · materials.html
const looks = [
{ color: 0x2563eb, roughness: 1, metalness: 0 },
{ color: 0x2563eb, roughness: 0.2, metalness: 0 },
{ color: 0xd4a017, roughness: 0.3, metalness: 1 },
{ color: 0x222222, emissive: 0xff8800 },
];
looks.forEach(function (look, i) {
const ball = new THREE.Mesh(
new THREE.SphereGeometry(0.7, 48, 24),
new THREE.MeshStandardMaterial(look));
ball.position.x = i * 1.8 - 2.7;
scene.add(ball);
});
OUTPUT · matt, shiny, metal and glowing
Week 8 · Shapes, Lights & Shadows
9 / 35
Line by line · Example 1
Same shape, four materials
{ color: 0x2563eb, roughness: 1 }
Matt blue: light scatters, no sharp highlight.
roughness: 0.2
Smooth: a small, bright highlight.
metalness: 1
Metal: the colour tints the reflections.
emissive: 0xff8800
Glows orange even with no light on it.
new THREE.MeshStandardMaterial(look)
One object holds all the settings.
Week 8 · Shapes, Lights & Shadows
10 / 35
Try it · 5 minutes
Make a gem
- Add a fifth mesh with OctahedronGeometry(0.6).
- Give it color 0xffd34d, metalness 0.6, roughness 0.3.
- Then set its emissive to 0xffaa00.
YOU SHOULD SEE
A gold gem that turns into a glowing gem: exactly what happens when you find one in Torch Hunt.
Week 8 · Shapes, Lights & Shadows
11 / 35
Big idea 2 · Lights
Different lights, different jobs
1
AmbientLight
Everywhere at once, no direction, no shadows.
2
PointLight
A bulb: shines in every direction from one point.
3
SpotLight
A torch: a cone aimed at a target.
Week 8 · Shapes, Lights & Shadows
12 / 35
Example 2 · Lights
A bulb and a torch
The spot light shines at its target; move the target to aim it.
CODE · lights.html
scene.add(new THREE.AmbientLight(0x404a66, 0.25));
const bulb = new THREE.PointLight(0xff8844, 30, 10);
scene.add(bulb);
const torch = new THREE.SpotLight(
0xfff2cc, 300, 30, Math.PI / 10, 0.4);
torch.position.set(0, 8, 3);
scene.add(torch);
scene.add(torch.target); // aim point
// in the loop:
bulb.position.set(Math.cos(t) * 4, 1, Math.sin(t) * 4);
torch.target.position.x = Math.sin(t * 0.7) * 4;
OUTPUT · a moving point light and a spot light
Week 8 · Shapes, Lights & Shadows
13 / 35
Line by line · Example 2
A bulb and a torch
AmbientLight(0x404a66, 0.25)
A faint blue-grey fill so dark areas are not pure black.
PointLight(0xff8844, 30, 10)
Colour, intensity, and the distance where it fades out.
SpotLight(colour, 300, 30, Math.PI / 10, 0.4)
Intensity, range, cone angle and how soft the edge is.
scene.add(torch.target)
The target is an object too; add it so it can move.
torch.target.position.x = …
Aim the torch by moving its target.
Week 8 · Shapes, Lights & Shadows
14 / 35
Try it · 5 minutes
Change the torch
- Make the cone wider: Math.PI / 6.
- Make the edge hard: penumbra 0.
- Turn the ambient light up to 1.
YOU SHOULD SEE
A big, sharp-edged circle of light, and a room so bright the torch hardly matters (so turn it back down!).
Week 8 · Shapes, Lights & Shadows
15 / 35
Big idea 3 · Shadows
Shadows need three switches
1
renderer
renderer.shadowMap.enabled = true
2
light
light.castShadow = true
3
objects
mesh.castShadow / mesh.receiveShadow
Week 8 · Shapes, Lights & Shadows
16 / 35
Example 3 · Shadows
Cast a shadow
The floor receives; the box and ball cast. The light moves, so the shadows move.
CODE · shadows.html
renderer.shadowMap.enabled = true; // 1
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const sun = new THREE.DirectionalLight(0xffffff, 2.5);
sun.castShadow = true; // 2
scene.add(sun);
box.castShadow = true; // 3
ball.castShadow = true;
floor.receiveShadow = true;
// in the loop: move the light, the shadows follow
sun.position.set(Math.cos(t) * 6, 6, Math.sin(t) * 6);
OUTPUT · moving shadows on the floor
Week 8 · Shapes, Lights & Shadows
17 / 35
Line by line · Example 3
Cast a shadow
renderer.shadowMap.enabled = true
Switch 1: the renderer will draw shadows.
THREE.PCFSoftShadowMap
Softer, blurrier shadow edges.
sun.castShadow = true
Switch 2: this light makes shadows.
box.castShadow = true
Switch 3: this object blocks light…
floor.receiveShadow = true
…and the floor shows the shadow.
Week 8 · Shapes, Lights & Shadows
18 / 35
Try it · 5 minutes
Break it, then fix it
- Comment out floor.receiveShadow = true.
- Refresh: where did the shadows go?
- Put it back, then try sun.shadow.mapSize.set(256, 256).
YOU SHOULD SEE
No shadows without the floor switch; with a 256 map the shadow edges become blocky.
Week 8 · Shapes, Lights & Shadows
19 / 35
Big idea 4 · Orbit and find
Look around, then measure how close
1
import
Add-ons come with the three package: "three/addons/…".
2
OrbitControls
Drag to orbit, scroll to zoom, update() each frame.
3
distanceTo
a.distanceTo(b) is the straight-line distance in 3D.
Week 8 · Shapes, Lights & Shadows
20 / 35
Example 4 · Orbit and find
Orbit and detect
The gem lights up while the marker is within 1 unit of it.
CODE · orbit.html
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.maxPolarAngle = Math.PI / 2.2;
// in the loop:
const d = gem.position.distanceTo(marker.position);
if (d < 1.0) {
gem.material.emissive.set(0xffaa00); // found!
} else {
gem.material.emissive.set(0x000000);
}
controls.update();
OUTPUT · auto-orbiting camera; the gem glows when the marker is close
Week 8 · Shapes, Lights & Shadows
21 / 35
Line by line · Example 4
Orbit and detect
import { OrbitControls } from "three/addons/…"
Load the add-on from the three package.
new OrbitControls(camera, renderer.domElement)
Move this camera when the canvas is dragged.
controls.maxPolarAngle = Math.PI / 2.2
Stop the camera going under the floor.
gem.position.distanceTo(marker.position)
How far apart, in world units.
controls.update()
Needed every frame when damping is on.
Week 8 · Shapes, Lights & Shadows
22 / 35
Try it · 5 minutes
Tune the controls
- Set controls.minDistance = 3 and maxDistance = 12.
- Set controls.enablePan = false.
- Try controls.autoRotate = true.
YOU SHOULD SEE
You can no longer zoom through the floor or far away, right-drag no longer slides the view, and the room slowly spins.
Week 8 · Shapes, Lights & Shadows
23 / 35
Common mistakes
When it goes wrong, check these first
Failed to resolve "three/addons/…"
Fix: Check the path: "three/addons/controls/OrbitControls.js" (with controls/), and that three is installed in this folder.
No shadows at all
Fix: Check all three switches: renderer.shadowMap.enabled, light.castShadow, and castShadow / receiveShadow on the meshes.
The torch will not aim
Fix: Add torch.target to the scene, then move torch.target.position (not torch.position).
Everything is far too dark or bright
Fix: Point and spot light intensities are in real units: try 10–50 for a bulb and 100–500 for a torch.
Week 8 · Shapes, Lights & Shadows
24 / 35
Check your understanding · 1 of 4
Which setting makes a surface look like metal?
A
roughness: 1
B
metalness: 1
C
emissive: 1
D
opacity: 1
Week 8 · Shapes, Lights & Shadows
25 / 35
Check your understanding · 2 of 4
Which light is shaped like a cone?
A
AmbientLight
B
PointLight
C
SpotLight
D
DirectionalLight
Week 8 · Shapes, Lights & Shadows
26 / 35
Check your understanding · 3 of 4
Shadows are missing. The renderer and the light are set. What else?
A
scene.shadows = true
B
castShadow / receiveShadow on the meshes
C
A second camera
D
emissive on the floor
Week 8 · Shapes, Lights & Shadows
27 / 35
Check your understanding · 4 of 4
What does a.distanceTo(b) return?
A
An angle
B
A colour
C
The distance between a and b
D
A new Vector3
Week 8 · Shapes, Lights & Shadows
28 / 35
Check your understanding
Answers
| Q | Answer | Why |
|---|---|---|
| Q1 | B · metalness: 1 | metalness: 1 makes the material behave like metal. |
| Q2 | C · SpotLight | A SpotLight shines a cone at its target. |
| Q3 | B · castShadow / receiveShadow on the meshes | Objects must cast, and the floor must receive. |
| Q4 | C · The distance between a and b | It returns one number: the straight-line distance. |
Week 8 · Shapes, Lights & Shadows
29 / 35
Lab time · 25 minutes
Your workflow for 3D files
1
Folder
Create a folder named Week8 and open it in VS Code.
2
Install
In the terminal: npm init -y, npm install three, npm install -D vite.
3
Run
npx vite starts the dev server. Leave it running.
4
Open
http://localhost:5173/ plus the file name, e.g. /first3d.html.
5
Check
Save with Ctrl+S; the page reloads. Open the console (F12).
Each week is an npm project: Vite finds “three” in node_modules, so a double-clicked file shows a blank page. Build the four example files first, then the game; work on a copy named torchHunt_yourname.html.
Week 8 · Shapes, Lights & Shadows
30 / 35
Lab activity · torchHunt.html
Build Torch Hunt in ten steps
| Step | Objectives | Checkpoint |
|---|---|---|
| Scaffold + OrbitControls import | 6 | No errors in the console |
| Renderer with shadows, scene, camera | 4 | A black canvas fills the window |
| OrbitControls | 6 | Ready to drag (nothing to see yet) |
| Floor and walls | 1, 2, 5 | Room appears in the ambient light |
| addToy() + seven toys | 1, 2, 4, 5 | Toys stand on the floor |
| Lights: ambient, lamp, torch | 3, 4 | A torch circle with shadows |
| Gems + game state | 1, 2, 8 | Enter starts the game |
| Keyboard + moveTorch() | 7 | The torch moves with the keys |
| checkGems() | 8 | Gems glow and float when found |
| Loop, timer and resize | 8 | Countdown, win and lose messages |
Week 8 · Shapes, Lights & Shadows
31 / 35
Stuck?
A five-step 3D debugging checklist
- Open the console (F12) and read the first red error.
- Blank page? Check npx vite is running and you opened http://localhost:5173/…, not the file itself.
- “Failed to resolve import three”? Run npm install in the week folder.
- Check the spelling and capitals: THREE.BoxGeometry, not THREE.boxGeometry.
- Nothing visible? Check the camera is not inside or behind the object.
THIS WEEK’S TIP
Lights are invisible. While building, add scene.add(new THREE.SpotLightHelper(torch)) to see the torch cone as lines, then remove it.
Week 8 · Shapes, Lights & Shadows
32 / 35
BUILD YOUR OWN · ALL OBJECTIVES
Make it yours: battery, best time, level 2
Week8\torchHunt_yourname.html
- A torch battery that drains while the torch moves; the cone shrinks as it runs low.
- Save the best time with localStorage.
- Level 2: 10 gems and a darker room.
Week 8 · Shapes, Lights & Shadows
33 / 35
How the build-your-own task is marked
Marking guide
| Criterion | What we look for | Marks |
|---|---|---|
| Runs cleanly | Runs with npx vite with no console errors | 2 |
| Required features | Working battery, saved best time and a second level | 4 |
| Readable code | Indented, sensible names, a comment on each function | 2 |
| Your own touch | New toys, colours, light colours or sound | 2 |
| Total | 10 |
Week 8 · Shapes, Lights & Shadows
34 / 35
Recap · Week 8
What you can do now
- Many built-in geometries; sit them on the floor with the bounding box.
- MeshStandardMaterial: roughness, metalness, emissive.
- Ambient, point and spot lights; aim a spot with its target.
- Shadows: renderer + light + mesh switches.
- Add-ons through the import map; OrbitControls; distanceTo.
NEXT WEEK
Week 9 · Movement & Follow Camera
Drive a car with the keyboard: steering with sin and cos, groups, and a camera that follows behind.
Week 8 · Shapes, Lights & Shadows
35 / 35