Three.js Voxel Terrain Editor (Chunked Meshing)

Editable voxel world in Three.js with procedural terrain, caves and water, chunked face-culled meshing, DDA voxel picking and dig/build brushes that rebuild only the chunks they touch.

Key topics: three.js voxel, voxel terrain, minecraft style, chunk meshing, voxel editor, dda raycast

What this code does

- Voxel Grid: a 64×40×64 `Uint8Array` stores the material per cell, generated from fBm height plus 3D-noise caves.
- Face Culling: only faces between solid voxels and air become quads, so hidden interior faces cost nothing.
- Chunks: the world is split into 16-wide columns, and an edit rebuilds only its chunk and any bordering neighbours.
- DDA Picking: the Amanatides–Woo traversal walks the grid along the ray and returns the hit voxel and face normal.
- Brushes: dig clears a sphere of voxels, and build fills from the face you click, with a chosen material.
- Controls: tool, build material, brush radius and new terrain (click to edit, drag to orbit).

Continue Learning

JavaScript Implementation

ES6+
// Face-culled voxel meshing: emit a quad only where a solid voxel touches air
const W = 32, H = 16, D = 32
const solid = (x, y, z) => x >= 0 && z >= 0 && x < W && z < D && y >= 0 && y < 4 + Math.round(3 * Math.sin(x / 5) * Math.cos(z / 6))
const positions = []
const normals = []
const faces = [[1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1]]
for (let y = 0; y < H; y++) for (let z = 0; z < D; z++) for (let x = 0; x < W; x++) {
  if (!solid(x, y, z)) continue
  for (const [nx, ny, nz] of faces) {
    if (solid(x + nx, y + ny, z + nz)) continue // hidden face
    const quad = new THREE.PlaneGeometry(1, 1).lookAt(new THREE.Vector3(nx, ny, nz)).translate(x + 0.5 + nx / 2, y + 0.5 + ny / 2, z + 0.5 + nz / 2)
    positions.push(...quad.toNonIndexed().attributes.position.array)
    for (let i = 0; i < 6; i++) normals.push(nx, ny, nz)
  }
}
const geometry = new THREE.BufferGeometry()
geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3))
geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3))
scene.add(new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: 0x5fa83a })))