Three.js Geometry & Material — Creative Web

Mesh = geometry + material, then scene.add. CylinderGeometry(rTop, rBottom, height, segments, heightSegments, openEnded, thetaStart): segments decide the…

Raka has a new product, the Coffee Print Mug ($65). In this lesson we build it in 3D: a cylinder body, a half-doughnut handle, and the coffee print from the Canvas API Basics lesson as its texture.

Picture making a mug in a ceramics workshop: the clay mould decides the shape, the glaze and the print decide the surface. three.js splits it the same way. Every object that shows up is a Mesh = a geometry (its shape: points and triangles) + a material (its surface: colour, texture, shine). And as in a product photo, even the best glaze looks black in a dark room: a realistic material needs light.

Geometry: the mug's shape

The scene, camera and renderer setup is the same as in the Three.js Setup lesson; the difference is that the camera is raised a little and pointed at the middle with camera.lookAt(0, 0, 0), so the coffee's surface shows.

// Body: CylinderGeometry(radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart)
const body = new THREE.CylinderGeometry(0.8, 0.8, 1.6, 48, 1, false, Math.PI);

// Handle: TorusGeometry(radius, tube, radialSegments, tubularSegments, arc)
const handleGeo = new THREE.TorusGeometry(0.42, 0.1, 16, 32, Math.PI);

Material: Basic or Standard

const mug = new THREE.Mesh(body, [
  new THREE.MeshStandardMaterial({ map: texture, roughness: 0.5 }),  // 0: side, printed
  new THREE.MeshStandardMaterial({ color: '#c47a3d', roughness: 0.3 }), // 1: top, coffee
  new THREE.MeshStandardMaterial({ color: '#1c1917', roughness: 0.5 }), // 2: bottom
]);
const handle = new THREE.Mesh(handleGeo, new THREE.MeshStandardMaterial({ color: '#1c1917', roughness: 0.5 }));
handle.rotation.z = -Math.PI / 2;
handle.position.x = 0.8;   // sticks to the right side of the body
mug.add(handle);           // turns together with the mug
scene.add(mug);
MeshBasicMaterial MeshStandardMaterial
Computes light no, its colour shows as it is yes, PBR (physically based rendering)
With no lights still coloured black
Typical parameters color, map, wireframe color, map, roughness (0 smooth, 1 rough), metalness (0 non-metal, 1 metal)
Good for wireframes, debugging, UI, a flat style products that must look real

A red sphere with MeshBasicMaterial({ color: 0xff0000 }) shows flat red from every side, with or without lights. Basic is lighter on the GPU because it doesn't compute light at all.

Light

scene.add(new THREE.AmbientLight('#ffffff', 0.8));    // from every direction, even
const light = new THREE.DirectionalLight('#ffffff', 2.5); // parallel rays, like the sun
light.position.set(3, 4, 5);                           // comes from top right front toward (0, 0, 0)
scene.add(light);

MeshStandardMaterial with no light at all reflects nothing: the mug becomes a solid black silhouette on the cream background. No error; the background still shows because scene.background isn't an object that gets lit.

Since r155, light intensities in three.js are physical. A DirectionalLight of intensity 1 shining straight on gives only about a third of the colour's brightness (1/π, computed in linear colour space), so tutorials written before r155 look dark. The migration guide: multiply old ambient and directional intensities by π (1 becomes about 3). Here, 2.5 and 0.8.

A texture from a canvas

We put the 300 × 300 coffee print on a canvas whose proportions match the mug's circumference: the circumference is 2π × 0.8 = 5.03 and the height 1.6, so the width is π times the height: 942 × 300.

const canvas = document.createElement('canvas');
canvas.width = 942;
canvas.height = 300;
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#1c1917';
ctx.fillRect(0, 0, 942, 300);
ctx.translate(321, 0);        // the 300 × 300 print in the middle: (942 - 300) / 2
drawCoffeePrint(ctx);         // the steps from the Canvas API Basics lesson

const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;

Colour spaces

three.js computes light in a linear colour space, while hex values like #c47a3d and canvas pixels use sRGB. A hex in color is converted automatically (ColorManagement has been on since r152). A texture's content can't be guessed, so you mark it: texture.colorSpace = THREE.SRGBColorSpace for a colour texture (map). Without it, its values are taken as already linear and come out too bright on screen: with MeshBasicMaterial, shirt black #1c1917 comes out as grey #5d5855 and the print fades. Data textures such as normalMap or roughnessMap stay without a colorSpace. (This is also why the three.js triangle in the WebGL Concepts lesson is a little lighter in the middle: the blend is computed in linear.)

dispose(): cleaning up GPU memory

Geometries, materials and textures already sent to the GPU are not cleaned up by the garbage collector while the renderer still holds them. When swapping a geometry:

function swapSegments(n) {
  mug.geometry.dispose();   // drop the old one from GPU memory
  mug.geometry = new THREE.CylinderGeometry(0.8, 0.8, 1.6, n, 1, false, Math.PI);
  renderer.render(scene, camera);
}
console.log(renderer.info.memory.geometries); // 2: the body and the handle

Without dispose(), renderer.info.memory.geometries goes from 2 to 4 after two segment swaps, and keeps climbing. Materials and textures have dispose() too.

⚠️ A black mug, a faded print, leaking memory

TL;DR: Mesh = geometry + material, then scene.add. CylinderGeometry(rTop, rBottom, height, segments, heightSegments, openEnded, thetaStart): segments decide the smoothness (48 segments = 192 triangles, 8 = an octagon), group 0 the side, 1 the top, 2 the bottom; TorusGeometry(radius, tube, radialSeg, tubularSeg, arc), Math.PI for half a circle; BoxGeometry(w, h, d). MeshBasicMaterial computes no light (debugging, flat, UI); MeshStandardMaterial (roughness, metalness) needs light and turns black without it. AmbientLight is even, DirectionalLight runs parallel from its position to (0, 0, 0); physical intensities since r155 (old × π). The canvas print becomes a CanvasTexture with colorSpace = THREE.SRGBColorSpace. Swapping a geometry or material? dispose() the old one, check renderer.info.memory.

⚡ Mesh = shape + surface

Write:

const handle = new THREE.Mesh(handleGeo, new THREE.MeshStandardMaterial({ color: '#1c1917', roughness: 0.5 }));
mug.add(handle);           // turns together with the mug
scene.add(mug);

Meaning: Every object that shows up is a Mesh = a geometry (its shape: points and triangles) + a material (its surface: colour, texture, shine), then put in with scene.add.

⚡ Segments decide the smoothness

Write:

const body = new THREE.CylinderGeometry(0.8, 0.8, 1.6, 48, 1, false, Math.PI);

Meaning: 48 segments make 192 triangles and look round, 8 segments turn the mug octagonal. thetaStart = Math.PI turns the texture's start to the back, so the middle of the print faces the camera.

⚡ Standard needs light

Write:

scene.add(new THREE.AmbientLight('#ffffff', 0.8));
const light = new THREE.DirectionalLight('#ffffff', 2.5);
light.position.set(3, 4, 5);
scene.add(light);
Guess first: a mug with MeshStandardMaterial and no lights at all. What shows up?

A solid black silhouette, with no error. The material reflects nothing.

Meaning: MeshBasicMaterial computes no light, MeshStandardMaterial needs lights. Since r155 intensities are physical: multiply an old tutorial's numbers by π.

⚡ A texture from a canvas

Meaning: The canvas print becomes a CanvasTexture, marked with texture.colorSpace = THREE.SRGBColorSpace. Without it, shirt black #1c1917 turns grey and the print fades.

What you will learn