TExcelTAD
Home › 3D Web Games with Three.js › Geometry, Materials & Meshes

Geometry, Materials & Meshes

A visible object in Three.js is usually a Mesh, and a mesh is just two things put together:

  • a geometry: the shape, stored as arrays of vertex data (positions, normals, texture coordinates) and triangles;
  • a material: how each pixel of that shape is colored (a flat color, lit PBR surface, texture, wireframe …).
js
const geometry = new THREE.SphereGeometry(1, 32, 16);              // shape
const material = new THREE.MeshStandardMaterial({ color: 'tomato' }); // look
const ball = new THREE.Mesh(geometry, material);                    // object in the scene
scene.add(ball);

The mesh adds a transform (position, rotation, scale) and a place in the scene graph. Because geometry and material are separate objects, a hundred meshes can share one geometry and one material, which saves memory and lets Three.js draw them more cheaply.

Built-in geometries

Three.js ships parametric shapes. Their extra arguments are segment counts: more segments give a smoother curve and more triangles.

html · try it yourself
<style>html, body { margin: 0; height: 100%; overflow: hidden } canvas { display: block }</style>
<script type="module">
import * as THREE from 'three';

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color('#181c28');
const camera = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 0.1, 100);
camera.position.set(0, 2.2, 11);
camera.lookAt(0, 0, 0);
scene.add(new THREE.HemisphereLight('#ffffff', '#334', 1.2));
const sun = new THREE.DirectionalLight('#ffffff', 2.5);
sun.position.set(3, 5, 4);
scene.add(sun);

const shapes = {
  Box: new THREE.BoxGeometry(1, 1, 1),
  Sphere: new THREE.SphereGeometry(0.6, 32, 16),
  Cylinder: new THREE.CylinderGeometry(0.5, 0.5, 1.1, 24),
  Cone: new THREE.ConeGeometry(0.6, 1.2, 24),
  Torus: new THREE.TorusGeometry(0.45, 0.18, 16, 40),
  Capsule: new THREE.CapsuleGeometry(0.35, 0.6, 4, 16),
  Icosahedron: new THREE.IcosahedronGeometry(0.6, 0),
  TorusKnot: new THREE.TorusKnotGeometry(0.4, 0.13, 100, 12),
};
const material = new THREE.MeshStandardMaterial({ color: '#4fc3f7', roughness: 0.4 });  // shared by all
const meshes = Object.entries(shapes).map(([name, geo], i) => {
  const mesh = new THREE.Mesh(geo, material);
  mesh.position.set((i % 4) * 2.6 - 3.9, i < 4 ? 1.1 : -1.1, 0);
  scene.add(mesh);
  const tris = (geo.index ? geo.index.count : geo.attributes.position.count) / 3;
  console.log(`${name.padEnd(12)} ${String(geo.attributes.position.count).padStart(5)} vertices ${String(tris).padStart(5)} triangles`);
  return mesh;
});

renderer.setAnimationLoop((t) => {
  for (const m of meshes) m.rotation.set(t / 2000, t / 1500, 0);
  renderer.render(scene, camera);
});
</script>

The console lists the vertex and triangle counts that the GPU actually processes. Notice that a box has 24 vertices, not 8: each corner appears three times, once per face, because each face needs its own normal (the direction it faces, used for lighting). Sharp edges always duplicate vertices.

Geometry Constructor (main arguments) Typical use
BoxGeometry width, height, depth crates, walls, placeholders
SphereGeometry radius, widthSegments, heightSegments balls, planets
PlaneGeometry width, height, wSeg, hSeg floors, water, terrain (with segments)
CylinderGeometry radiusTop, radiusBottom, height, radialSegments pillars, trunks; radiusTop = 0 for a cone
CapsuleGeometry radius, height, capSegments, radialSegments character placeholders (matches physics capsules)
TorusGeometry, TorusKnotGeometry radius, tube, segments rings, test objects
IcosahedronGeometry radius, detail low-poly rocks (detail 0–1), smooth spheres (higher)
ExtrudeGeometry, LatheGeometry, TubeGeometry a 2D shape / profile / curve logos, vases, pipes and rails

Note: PlaneGeometry lies in the XY plane, facing +Z. For a floor, rotate it: floor.rotation.x = -Math.PI / 2.

Materials at a glance

Material Lit? Cost Use it for
MeshBasicMaterial no cheapest UI in 3D, unlit stylised art, debugging
MeshNormalMaterial no cheap checking normals and shapes
MeshLambertMaterial yes (diffuse only) low matte surfaces on weak hardware
MeshPhongMaterial yes (+ shiny highlight) low–medium plastic-looking, older style
MeshStandardMaterial yes, PBR (metalness/roughness) medium the default for most games
MeshPhysicalMaterial yes, PBR + clearcoat, transmission, sheen … highest car paint, glass, fabric
MeshToonMaterial yes, banded low cel-shaded looks

The Material Types lesson compares them side by side. A few properties are shared by all mesh materials:

html · try it yourself
<style>html, body { margin: 0; height: 100%; overflow: hidden } canvas { display: block }</style>
<script type="module">
import * as THREE from 'three';

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color('#181c28');
const camera = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 0.1, 100);
camera.position.set(0, 0.5, 9);
scene.add(new THREE.HemisphereLight('#ffffff', '#334', 1.2));
const sun = new THREE.DirectionalLight('#ffffff', 2.5);
sun.position.set(3, 5, 4);
scene.add(sun);

const geo = new THREE.IcosahedronGeometry(0.9, 1);
const variants = [
  ['smooth', new THREE.MeshStandardMaterial({ color: '#ffb74d' })],
  ['flatShading', new THREE.MeshStandardMaterial({ color: '#ffb74d', flatShading: true })],
  ['wireframe', new THREE.MeshBasicMaterial({ color: '#ffb74d', wireframe: true })],
  ['transparent', new THREE.MeshStandardMaterial({ color: '#ffb74d', transparent: true, opacity: 0.4 })],
];
const meshes = variants.map(([label, mat], i) => {
  const m = new THREE.Mesh(geo, mat);
  m.position.x = i * 2.4 - 3.6;
  scene.add(m);
  console.log(label);
  return m;
});
renderer.setAnimationLoop((t) => {
  for (const m of meshes) m.rotation.y = t / 1800;
  renderer.render(scene, camera);
});
</script>
  • flatShading: true uses one normal per triangle, the faceted low-poly look, without changing the geometry.
  • wireframe: true draws triangle edges; useful for checking how dense a mesh is.
  • transparent + opacity blend the object with what is behind it. Transparent objects are sorted and drawn after opaque ones, and many overlapping layers are expensive, especially on phones.
  • side: by default only the front of each triangle is drawn (THREE.FrontSide). Use THREE.DoubleSide for thin things such as leaves or flags.

Changing things at runtime

Mesh properties (position, visible, material) can change every frame for free. Material values (color, opacity, roughness) can change too. But some changes alter the compiled shader, for example turning flatShading on, adding a texture map where there was none, or changing transparent. After those, set material.needsUpdate = true so the shader is rebuilt, and avoid doing it every frame.

Because materials are shared, changing one changes every mesh that uses it. To highlight a single object, give it its own material (mesh.material = mesh.material.clone()) or swap in a prepared "highlight" material.

Cleaning up

Geometries and materials hold GPU memory that JavaScript's garbage collector cannot see. When you remove something for good, free it:

js
scene.remove(mesh);
mesh.geometry.dispose();
mesh.material.dispose();      // also dispose its textures if no one else uses them

Forgetting this is the classic Three.js memory leak, covered in Field Notes.

Test yourself

Why does BoxGeometry(1, 1, 1) have 24 vertices instead of 8?

  1. Three.js adds hidden vertices for shadows
  2. Each corner is repeated per face so every face can have its own normal (and UVs)
  3. It is subdivided by default
  4. WebGL requires 24 vertices per box

Fifty crates share one MeshStandardMaterial. You set crates[0].material.color.set('red'). What happens?

  1. Only crate 0 turns red
  2. All fifty crates turn red
  3. Nothing until needsUpdate is set
  4. An error: shared materials are read-only

Which material ignores lights entirely?

  1. MeshStandardMaterial
  2. MeshLambertMaterial
  3. MeshBasicMaterial
  4. MeshPhysicalMaterial