SPACE
to play / pauseARROW RIGHT
orL
to go forwardARROW LEFT
orJ
to go backwardARROW UP
to increase volumeARROW DOWN
to decrease volumeF
to toggle fullscreenM
to toggle mute0 to 9
to go to the corresponding part of the videoSHIFT
+,
to decrease playback speedSHIFT
+.
or;
to increase playback speed
Three.js documentation
- AmbientLight
- PointLight
- MeshLambertMaterial
- MeshPhongMaterial
- MeshToonMaterial
- MeshStandardMaterial
- MeshBasicMaterial
- DirectionalLight
- HemisphereLight
- RectAreaLight
- Vector3
- MeshPhysicalMaterial
- SpotLight
- Object3D
- HemisphereLightHelper
- DirectionalLightHelper
- PointLightHelper
- RectAreaLightHelper
- SpotLightHelper
- OrbitControls
Shortcuts ⌨️
⚠️ Update
In the latest versions of Three.js, lights require a higher intensity
:
const ambientLight = new THREE.AmbientLight(0xffffff, 1)
The result will look slightly more realistic. Don’t be surprised if it’s not the exact same colors as in the video.
⚠️ Update
Since the light intensity
is higher, the tweak’s maximum should be higher too:
gui.add(ambientLight, 'intensity').min(0).max(3).step(0.001)
⚠️ Update
In the latest versions of Three.js, lights require a higher intensity
:
const directionalLight = new THREE.DirectionalLight(0x00fffc, 0.9)
The result will look slightly more realistic. Don’t be surprised if it’s not the exact same colors as in the video.
⚠️ Update
In the latest versions of Three.js, lights require a higher intensity
:
const hemisphereLight = new THREE.HemisphereLight(0xff0000, 0x0000ff, 0.9)
The result will look slightly more realistic. Don’t be surprised if it’s not the exact same colors as in the video.
⚠️ Update
In the latest versions of Three.js, lights require a higher intensity
:
const pointLight = new THREE.PointLight(0xff9000, 1.5)
The result will look slightly more realistic. Don’t be surprised if it’s not the exact same colors as in the video.
⚠️ Update
In the latest versions of Three.js, lights require a higher intensity
:
const rectAreaLight = new THREE.RectAreaLight(0x4e00ff, 6, 1, 1)
The result will look slightly more realistic. Don’t be surprised if it’s not the exact same colors as in the video.
⚠️ Update
In the latest versions of Three.js, lights require a higher intensity
:
const spotLight = new THREE.SpotLight(0x78ff00, 4.5, 10, Math.PI * 0.1, 0.25, 1)
The result will look slightly more realistic. Don’t be surprised if it’s not the exact same colors as in the video.
⚠️ Update
The process of baking has been added to the course.
It’s the Portal Scene chapter in which you’ll create a scene in Blender, unwrap it, bake it and implement it in Three.js.
Follow along and you’ll get there.
Unlock content 🔓
To get access to 93 hours of video, a members-only Discord server, subtitles, lesson resources, future updates and much more join us for only $95!
Want to learn more? 🤘
That's the end of the free part 😔
To get access to 93 hours of video, a members-only Discord server and future updates, join us for only $95!
Introduction 00:00
As we saw in the previous lesson, adding lights is as simple as adding meshes. You instantiate a light using the proper class, and you add it to the scene.
There are multiple types of lights, and we already discovered the AmbientLight and the PointLight.
In this lesson, we will see all the different classes in detail and how to use them.
Setup 00:35
A scene is already set up in the starter (complete with a sphere, a cube, a torus, and a plane as the floor), but feel free to try this yourself if you want to practice.
Because we are going to use lights, we must use a material that reacts to lights. We could have used MeshLambertMaterial, MeshPhongMaterial or MeshToonMaterial, but instead we will use the MeshStandardMaterial because it's the most realistic one as we saw in the previous lesson. We also reduced the roughness
of the material to 0.4
to see the reflections of the lights.
Once the starter is working remove the AmbientLight and the PointLight to start from scratch. You should get a black render with nothing visible in it:
We are now going to discover the main lights available in Three.js, starting with the one we’ve just removed.
AmbientLight 01:40
The AmbientLight applies omnidirectional lighting on all geometries of the scene. The first parameter is the color
and the second parameter is the intensity
. As for the materials, you can set the properties directly while instantiating the AmbientLight
class:
// Ambient light
const ambientLight = new THREE.AmbientLight(0xffffff, 1)
scene.add(ambientLight)
Or you can change those parameters after as properties (color
and intensity
in our case):
// Ambient light
const ambientLight = new THREE.AmbientLight()
ambientLight.color = new THREE.Color(0xffffff)
ambientLight.intensity = 1
scene.add(ambientLight)
We get the same result.
Note that we had to instantiate a Color when we updated the color
property.
And like we did for the materials, you can add the properties to the Debug UI. We won't do that in the rest of the lesson but feel free to add tweaks if you want to ease the testing:
gui.add(ambientLight, 'intensity').min(0).max(3).step(0.001)
If all you have is an AmbientLight, you'll have the same effect as for a MeshBasicMaterial because all faces of the geometries will be lit equally.
In real life, when you light up an object, the sides of the objects at the opposite of the light won't be totally black because light bounces on the walls and other objects. Light bouncing is not supported in Three.js for performance reasons, but you can use a dim AmbientLight to fake this light bounce.
DirectionalLight 07:39
The DirectionalLight will have a sun-like effect as if the sun rays were traveling in parallel. The first parameter is the color
and the second parameter is the intensity
:
// Directional light
const directionalLight = new THREE.DirectionalLight(0x00fffc, 0.9)
scene.add(directionalLight)
By default, the light will seem to come from above. To change that, you must move the whole light by using the position
property like if it were a common Three.js object.
directionalLight.position.set(1, 0.25, 0)
The distance of the light doesn't matter for now. The rays come from an infinite space and travel in parallel to the infinite opposite.
How to use it 🤔
- Download the Starter pack or Final project
- Unzip it
- Open your terminal and go to the unzip folder
-
Run
npm install
to install dependencies
(if your terminal warns you about vulnerabilities, ignore it) -
Run
npm run dev
to launch the local server
(project should open on your default browser automatically) - Start coding
- The JS is located in src/script.js
- The HTML is located in src/index.html
- The CSS is located in src/style.css