# 05. Custom BufferGeometry, explained

**Goal:** build a mesh from raw numbers (vertex positions, colors and triangle indices), then animate the vertices. This is what every built-in geometry does under the hood.

## How three.js stores a mesh

| Attribute | Item size | Meaning |
| --- | --- | --- |
| `position` | 3 | x, y, z of each vertex |
| `normal` | 3 | Direction the surface faces at the vertex (for lighting) |
| `uv` | 2 | Texture coordinates (0..1) |
| `color` | 3 | Per-vertex color (used when `material.vertexColors = true`) |
| index | 1 | Which three vertices form each triangle (lets triangles share vertices) |

## Walkthrough

```js
const vertsPerSide = segments + 1;
const positions = new Float32Array(vertsPerSide * vertsPerSide * 3);
```
A grid of `segments x segments` cells has `(segments + 1)^2` corner vertices. Each vertex needs 3 floats. Typed arrays (`Float32Array`) are what the GPU consumes directly; avoid building big arrays of `Vector3` objects.

```js
const y = Math.sin(x * 1.3) * Math.cos(z * 1.1) * 0.6;
positions.set([x, y, z], i * 3);
```
Height is a simple function of x and z. Replace it with noise or real elevation data for real terrain.

```js
color.setHSL(0.6 - (y + 0.6) * 0.4, 0.7, 0.5);
```
Maps height to hue: low points blue, high points yellow. `Color.setHSL` takes values from 0 to 1.

```js
indices.push(a, c, b, b, c, d);
```
Each grid cell is two triangles. The **winding order** (the order of the three vertices) decides which side is the front: counter-clockwise as seen from the viewer is front-facing. Get it wrong and the surface disappears from one side (set `side: THREE.DoubleSide` while debugging).

```js
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals();
```
`BufferAttribute(array, itemSize)` tells three.js how to slice the flat array. `computeVertexNormals()` averages the face normals around each vertex so lighting works. Without normals, lit materials render black.

```js
new THREE.MeshStandardMaterial({ vertexColors: true, flatShading: true })
```
`vertexColors` makes the material use the `color` attribute. `flatShading` gives a low-poly faceted look by using one normal per triangle.

```js
const wire = new THREE.Mesh(terrain.geometry, new THREE.MeshBasicMaterial({ wireframe: true, transparent: true, opacity: 0.08 }));
```
Two meshes can share one geometry. The faint wireframe shows the triangle structure.

```js
position.setY(i, ...);
position.needsUpdate = true;
terrain.geometry.computeVertexNormals();
```
To animate vertices on the CPU, edit the attribute and set `needsUpdate` so three.js re-uploads it. Recomputing normals each frame is fine for a few thousand vertices; for hundreds of thousands, move the animation into a vertex shader (example 11).

## Try this

1. Change `segments` to 8 and look at the wireframe.
2. Remove `computeVertexNormals()` and see the lighting break.
3. Add a `uv` attribute (`ix / segments`, `iz / segments`) and apply the checker texture from example 04.
4. Use `BufferGeometryUtils.mergeVertices` (from `three/addons/utils/BufferGeometryUtils.js`) on a non-indexed geometry to share vertices.

## Common mistakes

- Typed array length not equal to `count * itemSize`.
- Forgetting `needsUpdate` after editing an attribute.
- Indices pointing past the vertex count (silent rendering garbage).
