What you'll learn
- What a material does: it decides how light bounces off a shape, so one sphere can be plastic, chrome, glass, or a glowing orb.
- The four materials you will actually use, Basic, Lambert, Phong, and Standard, and the one sentence that tells each apart.
- How to set colors three ways, make a surface glow with emissive, and make it see-through with transparent and opacity.
- Why an object turns black when there is no light, and the two ways to fix it.
The material is the surface
In Part 3 you learned that a geometry is a bare net of triangles with no color and no finish. The material is everything else you see. When a beam of light hits a triangle, the material answers one question: what comes back to the camera? A matte surface scatters light evenly. A shiny one bounces it into a bright spot. A metal one reflects the room. A glowing one makes light of its own. Same triangles, completely different object.
Think of a t-shirt. Cut the same pattern from cotton, from satin, and from the reflective fabric on a safety vest, and you get three shirts that are identical in shape but nothing alike under a streetlight. The cotton looks the same from every angle. The satin has a bright streak that slides as you move. The safety vest throws the light straight back at you. The pattern is the geometry. The fabric is the material.
Look at the hero stage. Those four spheres share one SphereGeometry and one pink color. As the light circles them, three of them change: a soft shading, a sharp highlight, a broad glossy sheen. The leftmost one never changes at all. It is wearing MeshBasicMaterial, the one material that does not answer the light question. It just paints its color and stops. That makes it the odd one out on this page, and also the most useful debugging tool you have.
Simple definition: a material is the set of rules for how a surface reacts to light: its color, whether it is matte or shiny, whether it glows, whether you can see through it. A mesh is a geometry wearing a material. Swap the material and the shape stays exactly where it was, but it turns into a different thing.
The material menu
We keep the setup from Part 1, including its ambient and directional lights, and add one soft HemisphereLight so metal has something to catch. Part 5 explains lights properly. For now, all you need to know is that some light is hitting the sphere from the upper right.
1. Four materials, one geometry
three.js has more than a dozen materials, but four of them cover almost every game object. They go from simplest to most realistic, and each one is a little more expensive for the graphics card than the last.
MeshBasicMaterial
ignores lightsAlways exactly the color you gave it, lit or not. Use it to check that an object is even there, for cartoon looks, and for UI. The only one that works in a scene with no lights.
MeshLambertMaterial
matteReacts to light, so the side facing the light is bright and the far side is dark, but there is no shiny highlight. Cheap. Good for walls, floors, and cloth.
MeshPhongMaterial
shiny highlightLambert plus a bright spot where the light reflects toward you. shininess makes the spot tighter; specular tints it. The classic "plastic" look.
MeshStandardMaterial
physically basedTwo dials copied from the real world: roughness (0 mirror-smooth, 1 chalky) and metalness (0 plastic, 1 metal). The default for anything that should look real.
MeshNormalMaterial
debug rainbowColors every face by the direction it points, no color property and no lights needed. Great for checking that a shape is built right.
The hero stage builds all four on one shared geometry, the trick from Part 3. Notice that the color is identical every time; only the material class changes.
// One geometry shared by all four spheres (Part 3). Only the material changes. const geometry = new THREE.SphereGeometry(0.5, 48, 32); const basic = new THREE.MeshBasicMaterial({ color: 0xf472b6 }); const lambert = new THREE.MeshLambertMaterial({ color: 0xf472b6 }); const phong = new THREE.MeshPhongMaterial({ color: 0xf472b6, shininess: 60 }); const standard = new THREE.MeshStandardMaterial({ color: 0xf472b6, roughness: 0.3, metalness: 0.25 }); // A row of four meshes: same shape, different surface const row = new THREE.Group(); [basic, lambert, phong, standard].forEach((material, i) => { const ball = new THREE.Mesh(geometry, material); ball.position.x = (i - 1.5) * 1.3; // -1.95, -0.65, 0.65, 1.95 row.add(ball); }); scene.add(row); // A soft sky light on top of Part 1's lights, so metal has something to reflect const sky = new THREE.HemisphereLight(0xffffff, 0x334466, 0.8); scene.add(sky);
This is exactly what the hero stage is running. In its animation loop the directional light circles the row and the row sways a little, which is why the highlights slide on three spheres and never on the first.
2. Color, glow, and the Standard dials
Every material with a color stores it as a THREE.Color, and you can write it three ways: a hex number like 0xff8800, a CSS string like '#ff8800', or a CSS color name like 'hotpink'. Hex colors work exactly like CSS: 0xff8800 is the same orange you would get on a web page. To change a color later, call material.color.set(...) on the existing Color object rather than assigning a new value.
color is the light the surface reflects. emissive is the light the surface makes. An emissive color is added on top no matter what the lights are doing, so it is how you build a neon sign, a lava pool, or a power-up that glows in a dark cave. It does not actually light up anything nearby, but the object itself never goes dark. emissiveIntensity scales it.
The two Standard dials are the ones you will tune most. Roughness is how scratched the surface is. At 0 the highlight is a tiny, sharp point, like polished glass; at 1 it spreads out until it vanishes, like chalk. Metalness is whether the surface is a metal. At 0 the surface has its own color, like plastic; at 1 its color mostly disappears and it reflects its surroundings instead.
const material = new THREE.MeshStandardMaterial({ color: '#f472b6' }); // Three ways to say the same color material.color.set(0xf472b6); // hex number material.color.set('#f472b6'); // CSS hex string material.color.set('hotpink'); // CSS color name // The Standard dials material.roughness = 0.5; // 0 = mirror-smooth, 1 = chalky material.metalness = 0.1; // 0 = plastic, 1 = metal // Glow: a color the surface adds on its own, no light needed material.emissive.set('#f472b6'); material.emissiveIntensity = 0.6;
These are the property lines the "Try it" stage echoes as you drag its sliders. Turn the glow off again with material.emissive.set(0x000000): black emissive means "add nothing."
Honest note about metal: a real metal has almost no color of its own. What you see on chrome is a reflection of the room. Our scene has no room, only three lights, so a sphere at metalness: 1 here looks dark with a few bright spots. Real games fix this with an environment map, a picture of the surroundings for the metal to reflect, which is beyond this series. For now, keep metalness under about 0.6 unless you want that moody look.
3. See-through, inside-out, and swapping safely
Two more properties trip up nearly everyone. To make glass, you need both transparent: true and an opacity below 1. The first switches the material into the slower see-through drawing mode; the second says how much to see through. Set one without the other and nothing happens.
And by default three.js only draws the outside of each triangle. That saves half the work, and for a rock you are looking at from outside it is perfect. But put the camera inside a box to make a room and the walls vanish, because you are looking at their backs. side: THREE.DoubleSide draws both sides; THREE.BackSide draws only the inside, which is the cheap choice for a skybox you never leave.
// Glass: transparent must be true, or opacity is ignored const glass = new THREE.MeshStandardMaterial({ color: '#f472b6', transparent: true, opacity: 0.4 }); // A room you stand inside: draw the inside of the faces too const walls = new THREE.MeshLambertMaterial({ color: '#334466', side: THREE.DoubleSide }); const room = new THREE.Mesh(new THREE.BoxGeometry(10, 4, 10), walls); // Swapping a mesh's material: same rule as geometry in Part 3 function setMaterial(newMaterial) { mesh.material.dispose(); // free the old shader program mesh.material = newMaterial; // dress the mesh in the new one } // Turn every light in the scene off at once lights.visible = false;
The "Try it" stage below runs setMaterial every time you click a material button, and its lights live in one THREE.Group called lights so a single visible flag can switch them all off.
Dress a sphere
One sphere, one grid, three lights in a group. The material buttons call setMaterial with a fresh material; the sliders set the properties from tune.js on whichever material is on. Dials that a material does not have are dimmed. The Lights button is the one to watch.
Loading 3D preview…
MeshStandardMaterialmesh.material.dispose(); mesh.material = new THREE.MeshStandardMaterial({ color: '#f472b6' }); material.roughness = 0.50; material.metalness = 0.10; material.emissive.set(0x000000); material.emissiveIntensity = 0.6; material.transparent = false; material.opacity = 1.00; lights.visible = true;
- Click Lights to switch them off, then click through the five materials. Lambert, Phong, and Standard turn pitch black: they only know how to reflect light, and there is none. Basic stays exactly as pink as before, and Normal keeps its rainbow, because neither one asks about light at all. The grid stays visible too; it is drawn with an unlit line material. This is why a "black object" bug is almost always a lighting bug.
- Lights back on, pick Standard, and set roughness 0 with metalness 0. A hard, tiny highlight: polished plastic. Now drag roughness to 1: the highlight melts into a soft, even shade, like clay. Then try roughness 0.3 with metalness 1. The pink mostly drains away and the sphere goes dark with sharp glints. That is the honest note from above: a metal wants a world to reflect, and this scene has none.
- Pick Standard, turn Emissive on, then switch Lights off. Everything else in the scene would go black, but the sphere keeps glowing on its own. Now drag Opacity down to 0.4 and watch the grid show through the glow. Glowing, see-through, unlit: that is a lava pool, a ghost, or a shield bubble, and you just built the material for it with three properties.
Common mistakes
Material bugs almost never throw an error. They show up as an object that is black, invisible, or stubbornly the wrong color.
You gave a MeshStandardMaterial (or Lambert, or Phong) a bright color and it renders as a black silhouette, or as nothing at all against a dark background.
Fix: there is no light in the scene, or the light is behind the object. Add an AmbientLight and a DirectionalLight like Part 1. To check that the object even exists, swap in a MeshBasicMaterial for a moment: if it shows up, the shape is fine and the lights are the problem.
You set opacity: 0.5 and the object is still completely solid.
Fix: add transparent: true. Without it, three.js draws the material in its fast opaque mode and never looks at opacity. The slider on this page sets material.transparent = opacity < 1 for exactly this reason.
You built a big BoxGeometry for a room and moved the camera inside it. The walls disappeared and you can see straight out into the void.
Fix: by default only the outside of each face is drawn. Give the wall material side: THREE.DoubleSide, or THREE.BackSide if the camera never leaves the room. Same fix for a flat PlaneGeometry that vanishes when you look at it from behind.
You assigned a string straight to material.color. The color did not change, and later some other line crashed with color.getHex is not a function.
Fix: material.color is a THREE.Color object, not a string. Change it in place with material.color.set('red'). Passing a string in the constructor, { color: 'red' }, is fine because three.js converts it for you there.
Check your understanding
Your teammate says the game's glowing power-up "turns off" whenever the level's lights go dark. Which two material properties would keep it glowing, and why does that work?
Your answer:
Sample answer: set the power-up's emissive color (and an emissiveIntensity to control how strong it is). Emissive is light the surface makes on its own, so it is added on top no matter what the scene lights do. Its regular color still goes dark, but the glow stays. A second option is to switch that one object to MeshBasicMaterial, which ignores lights entirely, at the cost of the shading that made it look round.
Quick quiz
1. You remove every light from the scene. Which material still shows its color?
2. On a MeshStandardMaterial, what is the difference between roughness: 0 and roughness: 1?
3. You set material.opacity = 0.5 and the object is still completely solid. What is missing?
Keep learning
You can now pick the right material for a job, set its color three ways, make it glow, make it glass, and diagnose a black object in seconds. Every one of those materials except Basic has been waiting for one thing: real lights. That is next.
Part of the Games and Animations with Three.js tutorial series.