Three.js Population Density Hex Map

A 3D hex-bin population map in Three.js over a procedural nation, where Zipf-sized coastal cities grow and sprawl from 1900 to 2025 as extruded hexagonal prisms coloured with viridis on a log or linear scale.

Key topics: population density map, hexbin map, three.js choropleth, demographic visualization, 3d map, viridis

What this code does

- Hex Binning: 3,456 offset-row hexagonal cells (CylinderGeometry with 6 sides) in one InstancedMesh avoid square-grid artefacts.
- Synthetic Geography: fbm noise with radial falloff makes the landmass, and cities are seeded on coastal lowlands with Zipf sizes (1/rank).
- Growth: as the year advances each city's peak density and sprawl radius grow at their own rate, with a rural baseline everywhere.
- Log Scale: density spans orders of magnitude, so the log scale reveals rural structure that a linear scale flattens.
- Viridis: a perceptually uniform, colour-blind-friendly sequential ramp.
- Controls: year, log or linear, height exaggeration, labels and map seed; hover a cell to read it.

Continue Learning

JavaScript Implementation

ES6+
// Hex-bin density map: one instanced hexagonal prism per cell, height & colour = density
const hex = new THREE.CylinderGeometry(r, r, 1, 6).translate(0, 0.5, 0)   // 6 sides → hexagon
const cells = new THREE.InstancedMesh(hex, new THREE.MeshStandardMaterial(), cols * rows)
for (let row = 0; row < rows; row++) for (let col = 0; col < cols; col++) {
  const x = col * Math.sqrt(3) * r + (row % 2) * Math.sqrt(3) / 2 * r, z = row * 1.5 * r // offset rows
  const k = Math.log10(density[row][col]) / Math.log10(maxDensity)                         // log scale
  cells.setMatrixAt(i, new THREE.Matrix4().makeScale(1, k * 3, 1).setPosition(x, 0, z))
  cells.setColorAt(i, viridis(k))
}