What you'll learn
- What a geometry is (points and triangles), what a material is (the surface), and how a mesh combines them into something you can see.
- The built-in geometries you will use most, and what the numbers in new THREE.SphereGeometry(1, 32, 16) mean.
- Why more segments make a shape smoother but slower, and how to read a geometry's real vertex and triangle counts.
- How to share one geometry across many meshes, and how to swap a mesh's geometry without leaking memory.
It's triangles all the way down
A graphics card is a very fast machine that knows how to do one thing: paint triangles. Round things, bumpy things, smooth things, all of them are triangles, just so many tiny ones that your eye stops noticing. Look at the hero stage above. When the white lines fade in, you are seeing the real shape: a net of triangles wrapped around empty space.
Three words describe this net. A vertex is a single point, three numbers (x, y, z) like the positions from Part 2. A face is a triangle that connects three vertices. And a geometry is the whole list: every vertex, plus which three vertices make each triangle. A geometry has no color and no surface. It is the shape and nothing else, like a wire sculpture.
Geometry
the shapeA list of vertices and the triangles that connect them. BoxGeometry, SphereGeometry, and friends build these lists for you.
Material
the surfaceHow the triangles look: color, shininess, whether light matters. Part 4 is all about materials.
Mesh
geometry + materialThe finished object. It has a position, a rotation, and a scale, and it is the thing you scene.add.
Think of a piñata. The papier-mâché frame is the geometry: it decides the shape, but on its own it is bare and gray. The paint and tissue paper are the material. The finished piñata, frame plus decoration, is the mesh, the thing you actually hang up.
Here is the part that matters for games: one geometry can be shared by many meshes. Ten coins do not need ten copies of the coin shape. They need one CylinderGeometry and ten meshes that all point at it. The graphics card stores the triangles once, and each mesh only adds its own position, rotation, and scale. The hero stage does exactly this: the solid knot and the white wireframe are two meshes sharing a single geometry.
Simple definition: a geometry is the list of points and triangles that make a shape. A mesh is a geometry wearing a material, placed somewhere in the scene. Change the geometry and the shape changes; change the material and the look changes; the mesh is what you move around.
Geometries, meshes, and segments
We reuse the setup from Part 1 (scene, camera, renderer, and two lights) and focus on what goes on stage. You have already written new THREE.Mesh(geometry, material) twice in this series. Now we slow down and look at each piece.
1. From geometry to mesh
Making a visible object is always the same three steps: build a geometry, build a material, hand both to a Mesh. The wireframe "x-ray" in the hero stage is a second mesh that reuses the same geometry with a see-through wireframe material, scaled up two percent so its lines sit just above the solid surface. Then the coin loop shows sharing at game scale: one geometry, one material, five meshes.
// 1. geometry (the shape), 2. material (the surface), 3. mesh (the object) const geometry = new THREE.TorusKnotGeometry(0.7, 0.22, 96, 16); const material = new THREE.MeshStandardMaterial({ color: 0xa78bfa }); const knot = new THREE.Mesh(geometry, material); scene.add(knot); // An x-ray of the SAME geometry: a second mesh, a see-through wireframe material const xray = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true, transparent: true, opacity: 0.5 })); xray.scale.setScalar(1.02); // 2% bigger so the lines float just above the surface scene.add(xray); // Sharing at game scale: ONE coin geometry, FIVE coin meshes const coinGeometry = new THREE.CylinderGeometry(0.4, 0.4, 0.08, 32); const coinMaterial = new THREE.MeshStandardMaterial({ color: 0xfbbf24 }); for (let i = 0; i < 5; i++) { const coin = new THREE.Mesh(coinGeometry, coinMaterial); coin.rotation.x = Math.PI / 2; // turn the flat side toward the camera coin.position.x = i - 2; // -2, -1, 0, 1, 2: a row across the screen scene.add(coin); }
The first two blocks are exactly what the hero stage is running (its wireframe opacity is animated between 0 and 0.6). The coin loop is not on a stage on this page. Paste it into your Part 1 file and you get five coins that cost the graphics card one shape.
2. The built-in geometries
You almost never type vertices by hand. three.js ships with ready-made geometry classes, and each one takes a few numbers: first the size, then how many segments to cut it into. Here are the ones this page's demo uses, plus the plane every game needs for a floor.
BoxGeometry
(width, height, depth)A box or cube. The numbers are its size along x, y, and z. Six flat faces, twelve triangles.
SphereGeometry
(radius, widthSegments, heightSegments)A ball. The segments say how many slices around and how many rings top to bottom.
CylinderGeometry
(radiusTop, radiusBottom, height, radialSegments)A can, a coin, or a pillar. Two radii means the top can be a different size from the bottom.
ConeGeometry
(radius, height, radialSegments)A cone or pyramid: a cylinder whose top radius is zero. Three radial segments makes a pyramid.
TorusGeometry
(radius, tube, radialSegments, tubularSegments)A donut. radius is the size of the ring, tube is how thick the dough is.
TorusKnotGeometry
(radius, tube, tubularSegments, radialSegments)A donut tied in a knot. Great for showing off; expensive in triangles.
IcosahedronGeometry
(radius, detail)A 20-sided die at detail 0. Each higher detail cuts every face into more triangles, so it rounds into a sphere fast.
PlaneGeometry
(width, height)A flat rectangle: two triangles. Rotate it by -Math.PI / 2 on x and it is your floor.
3. Segments: smoothness vs. cost
A sphere made of triangles is never truly round. It fakes it. With 8 segments you get a disco ball with visible flat panels. With 48 segments the panels are so small it looks smooth. The catch is how fast the count grows: 8 by 8 segments is 112 triangles, while 48 by 48 is 4,512. Forty times the work for the graphics card, for one object.
// Same radius, same material. The only difference is the segment counts. const ballMaterial = new THREE.MeshStandardMaterial({ color: 0xa78bfa }); const rough = new THREE.Mesh(new THREE.SphereGeometry(0.9, 8, 8), ballMaterial); rough.position.x = -1.2; // 112 triangles: a disco ball const smooth = new THREE.Mesh(new THREE.SphereGeometry(0.9, 48, 48), ballMaterial); smooth.position.x = 1.2; // 4,512 triangles: looks round scene.add(rough, smooth); // The x-ray switch: draw only the triangle edges ballMaterial.wireframe = true;
Try this in your own file, then flip wireframe on and off. Wireframe is the x-ray of 3D graphics: instead of filling each triangle, the material draws only its three edges, so you can count them.
Games budget triangles the way you budget money. A phone might draw a few hundred thousand triangles per frame comfortably; a gaming PC, many millions. A hero character can afford thousands. A pebble the player never looks at closely should be cheap. You will see this trade-off live in the "Try it" stage, where the chip in the corner reports the true counts.
4. Swapping a geometry safely
A mesh's geometry is just a property, so you can replace it: mesh.geometry = new THREE.TorusKnotGeometry(...). But there is a catch. When three.js first draws a geometry, it uploads a copy of the vertices to the graphics card's own memory. Reassigning the property makes JavaScript forget the old geometry, but the graphics card does not know that and keeps its copy forever. Do this every time a player picks a hat and the memory quietly fills up. dispose() is how you tell the graphics card to let go.
The same code shows how to count triangles. Most built-in geometries are indexed: they store each vertex once, plus an index list saying which three vertices make each triangle, so the triangle count is index.count / 3. A few, like IcosahedronGeometry, store three separate vertices per triangle instead, so there it is position.count / 3.
// One mesh whose geometry we swap. The material stays the same. const material = new THREE.MeshStandardMaterial({ color: 0xa78bfa }); const mesh = new THREE.Mesh(new THREE.BoxGeometry(1.4, 1.4, 1.4, 1, 1, 1), material); scene.add(mesh); function setGeometry(newGeometry) { mesh.geometry.dispose(); // free the old triangles on the graphics card mesh.geometry = newGeometry; // hand the mesh its new shape } // For example, when the Torus Knot button is clicked: setGeometry(new THREE.TorusKnotGeometry(0.6, 0.2, 64, 16)); // How big is that? Ask the geometry. function countTriangles(geometry) { const position = geometry.attributes.position; return geometry.index ? geometry.index.count / 3 : position.count / 3; } console.log(mesh.geometry.attributes.position.count); // vertices: 1105 console.log(countTriangles(mesh.geometry)); // triangles: 2048 // And in the animation loop, the spin from Part 1: mesh.rotation.y += 0.01;
This is exactly what the "Try it" stage below is running. Every shape button and every nudge of the segments slider calls setGeometry, so the old geometry is disposed each time, and the chip shows the two counts.
Pick a shape
This stage is running swap.js. The buttons choose which geometry class to build, the slider fills in its segment numbers, and the chip in the corner reports the real vertex and triangle counts straight from the geometry. Watch the "live code" box: it shows the exact constructor call the stage just ran.
Loading 3D preview…
vertices: 24 · triangles: 12mesh.geometry.dispose(); mesh.geometry = new THREE.BoxGeometry(1.4, 1.4, 1.4, 1, 1, 1); material.wireframe = false; // 24 vertices, 12 triangles
- Pick Sphere and drag the slider from 3 all the way to 64. At 3 it is a spiky 12-triangle blob, the same count as the box. At 64 it is 8,064 triangles and perfectly round. But notice where it stopped looking better: somewhere around 24 (about 1,100 triangles) the panels vanish. Everything past that is triangles you are paying for and cannot see.
- Pick Box and turn on Wireframe. Each flat face is exactly two triangles, 12 in total, no matter how big the box is. (Why 24 vertices for a shape with 8 corners? Each corner is stored once per face that touches it, so the lighting knows which way each face points.) Now drag the segments up. The box gets sliced into more triangles, but the shape does not change one bit. Segments only help curved shapes; flat things should stay at 1.
- Pick Torus Knot and drag the slider to the top. One object, 8,192 triangles, and the wireframe is nearly solid white. Compare that with the box's 12. Now imagine a game with 1,000 rocks. As knots, that is over 8 million triangles every frame; as boxes, 12,000. This is why game artists build "low-poly" shapes and spend detail only on what is close to the camera.
Common mistakes
Geometry bugs rarely crash. They show up as a game that gets slower and slower, or a browser tab that quietly eats memory.
Every sphere in the scene is SphereGeometry(1, 128, 128) "so it looks nice." That is 32,512 triangles per ball, and the frame rate drops as soon as there are a few dozen of them.
Fix: the defaults (32 by 16 for a sphere) already look smooth. Spend big counts only on one or two hero objects, and use 8 to 16 for things that are small or far away.
You wrote mesh.geometry = new THREE.BoxGeometry(size, size, size) inside animate to make a box grow. That builds and uploads a fresh geometry 60 times a second, stutters, and leaks the old ones.
Fix: build geometries once, outside the loop. To change size, change mesh.scale instead (Part 6). Only swap geometries when the shape really changes, and dispose when you do.
You reassign mesh.geometry and everything looks right, so it feels fine. But the graphics card still holds every geometry you ever assigned, and memory climbs each time.
Fix: call mesh.geometry.dispose() right before assigning the new one, like setGeometry does. The same rule applies to materials and textures later in the series.
You copied new THREE.Geometry() and geometry.vertices.push(...) from a tutorial and got THREE.Geometry is not a constructor.
Fix: Geometry was removed in r125. Everything is BufferGeometry now, and all the built-ins on this page already are, so just use them. If a tutorial mentions geometry.faces, it is too old to trust.
Check your understanding
You want 200 identical trees in a forest. Do you create 200 geometries or 1? Explain why, and say what each tree still needs its own copy of.
Your answer:
Sample answer: one geometry (and usually one material too), shared by 200 meshes. Every tree has the same shape, so the graphics card only needs one copy of the triangles; storing 200 copies would waste memory for no visual difference. Each tree still needs its own mesh, because the mesh is what holds a position, rotation, and scale. Part 14 shows an even faster way to draw hundreds of copies with a single InstancedMesh.
Quick quiz
1. What two things does a THREE.Mesh combine?
2. You change a sphere from SphereGeometry(1, 8, 8) to SphereGeometry(1, 64, 64). What is the trade-off?
3. Why do you call oldGeometry.dispose() before swapping in a new geometry?
Keep learning
You can now look at any 3D object and see the triangles underneath, pick the right built-in geometry, and choose a segment count on purpose instead of by accident. Next up is the other half of every mesh: materials, and why some of them ignore your lights completely.
Part of the Games and Animations with Three.js tutorial series.