Geometry and Mesh

Explore built-in geometries and BufferGeometry, and learn how vertices, indices, normals, and UVs form a renderable mesh.

🎯 Introduction

By the end of this article, you will be able to choose common geometries such as Box, Sphere, and Plane, understand vertices, indices, normals, and UVs in BufferGeometry, and build a triangle mesh from scratch.

🧱 Common geometries

Geometry defines a mesh's shape and dimensions, but does not provide color or appear in the scene on its own. Three.js includes many basic shapes. Start by understanding their uses:

ConstructorShapeCommon usesKey parameters
BoxGeometryBox, including a cubeBoxes, building volumes, debug placeholdersWidth, height, depth
SphereGeometrySphereGlobes, dots, spherical equipmentRadius, width segments, height segments
PlaneGeometryRectangular plane with no thicknessGround, posters, image surfacesWidth, height
CylinderGeometryCylinderColumns, pipes, cylindrical equipmentTop radius, bottom radius, height, radial segments
ConeGeometryConeMarkers, arrows, cone-shaped indicatorsRadius, height, radial segments

This code creates five shapes. The variable names identify their shapes; to display them, create a mesh for each and add it to the scene:

const box = new THREE.BoxGeometry(1, 1, 1); // Box with width, height, and depth of 1
const sphere = new THREE.SphereGeometry(0.8, 32, 16); // Sphere with radius 0.8
const plane = new THREE.PlaneGeometry(4, 3); // Plane: width 4, height 3, no thickness
const cylinder = new THREE.CylinderGeometry(0.5, 0.5, 1.5, 32); // Cylinder
const cone = new THREE.ConeGeometry(0.6, 1.2, 32); // Cone

Segment counts determine how finely a sphere or cylinder is divided around its circumference, and thus how many small triangles form its surface. More segments produce smoother silhouettes but also more vertices and triangles. A sphere only a few dozen pixels wide does not need many segments.

Geometry is shape data, and a material defines appearance rules. Mesh combines them into an object the camera can see in the scene:

const material = new THREE.MeshStandardMaterial({ color: 0x44aa88 });
const boxMesh = new THREE.Mesh(box, material); // Create a renderable box
scene.add(boxMesh); // Add the mesh to the scene

Thus, new THREE.BoxGeometry() only prepares box vertex data in memory. Without new THREE.Mesh() and scene.add(), nothing appears on the page.

This demo creates meshes from all five geometries so you can compare their shapes:

🧰 Four key concepts in BufferGeometry

Modern Three.js uses BufferGeometry to store vertex data for the GPU. The GPU does not understand application concepts such as spheres or buildings. It needs vertex positions, triangle connectivity, surface directions, and the locations where an image maps onto a surface.

These four concepts answer those questions:

  • position specifies vertex locations and determines shape. Without it, the GPU does not know what to draw.
  • index specifies which vertices form triangles, reducing duplicate vertex storage. Without it, each consecutive group of three vertices in position forms a triangle.
  • normal specifies surface direction, which lighting uses to determine brightness. It is usually needed with MeshStandardMaterial.
  • uv maps a 3D surface to locations in a 2D image. Textures use these coordinates to place the image. Without UVs, color maps cannot map correctly.

You do not have to supply all four manually for every geometry. A solid-color triangle needs only position; lighting needs normal; images need uv; and index can reduce data when vertices are repeated.

Here is a triangle built manually. Each three numbers in position give one vertex's x, y, and z coordinates. Three vertices form one triangle. Since MeshBasicMaterial does not calculate lighting, this example needs neither normals nor UVs:

const geometry = new THREE.BufferGeometry();
const positions = new Float32Array([
    -1, -1, 0,
    1, -1, 0,
    0, 1, 0,
]);

geometry.setAttribute(
    'position',
    new THREE.BufferAttribute(positions, 3)
);

const material = new THREE.MeshBasicMaterial({
    color: 0xff8844,
    side: THREE.DoubleSide,
});
const triangle = new THREE.Mesh(geometry, material);
scene.add(triangle);

To use MeshStandardMaterial for this triangle, ask Three.js to calculate surface directions from the vertex positions:

geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({ color: 0xff8844 });

To form a rectangle from four vertices, reuse them with index. This example defines four vertices, then uses indices 0, 1, 2 and 0, 2, 3 to form two triangles:

const quadGeometry = new THREE.BufferGeometry();
quadGeometry.setAttribute(
    'position',
    new THREE.BufferAttribute(new Float32Array([
        -1, -1, 0,
        1, -1, 0,
        1, 1, 0,
        -1, 1, 0,
    ]), 3)
);
quadGeometry.setIndex([
    0, 1, 2,
    0, 2, 3,
]);

Geometry usually needs correct normals with MeshStandardMaterial. If you change vertex data, call computeVertexNormals() again. By default, a plane is visible only from the front. Set side: THREE.DoubleSide to show both sides, at an additional rendering cost.

Here is a triangle created using a position attribute:

🧾 Summary

  • Geometry describes shape, Material describes appearance, and Mesh combines them.
  • Box, Sphere, Plane, Cylinder, and Cone are common basic shapes.
  • BufferGeometry stores vertex data in attributes.
  • position defines locations, index organizes triangles, normal affects lighting, and uv handles texture mapping.
  • More segments are not always better. Choose counts based on the size on screen.

❓ Questions and answers

Q1: Why does a square consist of two triangles?

GPUs use triangles as basic drawing primitives, so rectangles are typically split into two triangles.

Q2: Why does lighting look wrong after changing geometry?

Normals may still contain old data after vertex positions change. Recalculate them with computeVertexNormals().

Q3: When should I build BufferGeometry manually?

Set attributes directly when shapes come from API data, particle positions, or custom algorithms that built-in geometries cannot represent.

🧪 Exercise

Use BufferGeometry to create a square from two triangles. Add position and uv attributes, then try switching to MeshStandardMaterial.

// Write your code here

🎉 Models are data organized into vertices and triangles. Next, you will add materials, colors, and lights to their surfaces.