Skip to main content
← 3D on the Web

Lesson 4 of 5

Light and Material

Four knots, the same shape and the same colour, with four different materials. A button turns the lamp off.

What you will learn

  • MeshBasicMaterial
  • MeshStandardMaterial
  • roughness
  • wireframe
  • AmbientLight
  • light intensity
  • sharing a geometry

Before you start

Your First Scene.

What this page does

Four knots turn side by side. They are the same shape and they are painted the same colour. They do not look alike at all. Click Turn the lamp off. Two of them go black. Two do not notice.

The four

MaterialWhat it isLamp off
BasicA flat colour. It ignores light completely.Unchanged
MatteA rough surface. Light scatters everywhere.Black
PolishedA smooth surface. Light reflects in one bright spot.Black
WireframeThe triangles the shape is really made of.Unchanged

Basic and Wireframe are unlit materials. They take no part in lighting at all. That makes MeshBasicMaterial exactly right for a sun, a sky, or an outline you are using to debug something, and exactly wrong for anything meant to look solid.

Roughness

new THREE.MeshStandardMaterial({ color: PAINT, roughness: 1 })
new THREE.MeshStandardMaterial({ color: PAINT, roughness: 1 - POLISH })

Roughness runs from 0 to 1. At 1 the surface scatters light in every direction and there is no highlight anywhere. Near 0 it reflects the lamp in one small bright spot, which slides around as the knot turns. That sliding highlight is most of what makes something look solid rather than painted.

Polish and lamp

Take the polish to 0, then to 1, and watch only the third knot.

0.9

Only the third knot uses it. Watch the bright spot appear.

3.5

Two lights, two different jobs

LightComes fromCan it make a highlight?
DirectionalLightOne direction, like the sunYes
AmbientLightEverywhere at onceNo

Ambient light cannot make a highlight and it cannot make a shadow, because it has no direction to come from. Its only job is to stop the dark side of things being pure black. Real rooms have light bouncing off the walls, and ambient light is a cheap stand-in for that.

Set the ambient intensity to 0 and the shadowed sides go black. Set it to 3 and everything goes flat and the shape disappears. Both extremes are wrong, and looking at both is the fastest way to understand what it is for.

Paint and lamp colour

The lamp colour tints everything it touches. It does not touch Basic or Wireframe at all.

#38bdf8

All four are painted this colour, so any difference is the material.

#fff4e6

A trap worth knowing about

This page uses polish instead, which needs nothing extra. It is worth knowing the metal trap exists, because you will reach for it and it will look broken when it is not.

Why a knot, and why one geometry

A ball looks identical however you turn it, so nothing about the light would ever appear to move. Any shape used to show off a material needs curves facing different ways.

var knot = new THREE.TorusKnotGeometry(0.55, 0.2, 128, 24);

for (var i = 0; i < materials.length; i++) {
    var mesh = new THREE.Mesh(materials[i].geometry || knot, materials[i].material);
    mesh.position.x = i * 1.8 - 2.7;
    scene.add(mesh);
}

One geometry is shared by three of the four meshes. Geometry is the expensive part, and there is no reason to build the same knot three times. The wireframe brings its own much simpler knot, because at this level of detail a wireframe is a solid mess of lines.

Try these changes

Make these changes in the files beside this guide. Each one only uses what this lesson has already covered. Tick each one when it works.

Check yourself

Three quick questions on this lesson. Get them all right to finish the lesson and earn a pack.

Loading your answers

Teacher's versionHow to run this lesson, what to check, and any answers. Students do not need this.

How to teach it

  • Click the lamp off and on several times before saying anything at all. Ask which two changed and take a guess about why before confirming it.
  • The polish slider is the best two minutes in this lesson. Go from 0 to 1 slowly and let them watch the highlight arrive.
  • Both ambient extremes matter. 0 hides the shape in darkness, 3 hides it in flatness. Students remember the pair better than either alone.
  • The metalness trap is worth demonstrating rather than only mentioning, because a student who meets it alone at home concludes their code is broken.
  • Link back to lesson 2 explicitly. A material is a shader that somebody else wrote, and roughness is a number going into it.