Draw a GeoJSON lake in Three.js

A lake polygon gives you the shoreline and any islands enclosed by it. Project those coordinates into metres, pass the island rings to Shape.holes, and you get a water mesh that follows the bank.

An angled map of a turquoise lake with an island cut out of the water mesh
The island occupies a hole in the lake geometry. The land underneath comes from a separate mesh.

Try the lake mesh

Switch to Map view and turn off Cut out island. Water covers the island as soon as Three.js fills the outer ring by itself. Both options use the same shoreline. Run the lake example

Convert coordinates to metres

GeoJSON puts longitude first and latitude second. Pick an origin near the middle of the lake and subtract it from every point. In the example, one latitude degree is about 111 km. One longitude degree covers less ground at this latitude, so the eastward distance is scaled by the cosine of the origin latitude. A shared origin keeps the terrain, lake and other map layers aligned. GeoJSON position format

The northward distance becomes the shape’s second coordinate. Rotating the geometry by -Math.PI / 2 puts east along +X, north along −Z and height along +Y. The example uses a local approximation for a lake about 1.4 km across. For a larger region or data that must match an existing terrain projection, project all the layers into the same local coordinate reference system before building meshes. Equirectangular projection formulas

Cut out islands

A GeoJSON polygon starts with its exterior ring. The following rings are holes. Drop the repeated closing coordinate before making the Three.js path and keep the outer ring and holes wound in opposite directions. The code below projects each ring and makes the outer ring clockwise in shape space. ShapeGeometry then triangulates the shape and leaves out the holes. GeoJSON polygon format · Three.js Shape · ShapeGeometry

A MultiPolygon adds one level to the coordinates array. Run the same conversion on each polygon and keep each set of holes with its own exterior. For a Feature, read feature.geometry first. If an island touches the shoreline or a ring crosses itself, repair the source geometry before triangulating it.

Copy the example

Save this block as index.html and serve it locally. The fictional lake and its terrain are included. Three.js loads from the CDN.

Full example source
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="robots" content="noindex,follow">
  <title>GeoJSON lake in Three.js example</title>
  <link rel="icon" href="data:,">
  <style>
    body { margin: 0; color: #1a1a18; background: #e8ece3; font: 1rem system-ui, sans-serif; }
    canvas { display: block; }
    .controls {
      position: fixed; top: 1rem; left: 1rem; z-index: 1;
      display: flex; flex-wrap: wrap; gap: .5rem;
    }
    button, label {
      min-height: 44px; box-sizing: border-box; padding: .45rem .75rem;
      border: 1px solid #c7cbc2; border-radius: 6px;
      color: inherit; background: #f8f8f1; font: inherit; cursor: pointer;
    }
    label { display: inline-flex; align-items: center; gap: .45rem; }
    input { width: 1.1rem; height: 1.1rem; accent-color: #236567; }
  </style>
</head>
<body>
  <div class="controls">
    <button type="button" id="view">Map view</button>
    <label><input id="island" type="checkbox" checked> Cut out island</label>
  </div>
  <script type="module">
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.184.0/build/three.module.js";

// A fictional lake. The first ring is the shore, the second is an island.
const lake = {
  type: "Polygon",
  coordinates: [
    [[152.8928, -27.4501], [152.8941, -27.4525], [152.8980, -27.4538],
     [152.9007, -27.4526], [152.9030, -27.4531], [152.9059, -27.4516],
     [152.9073, -27.4489], [152.9057, -27.4476], [152.9029, -27.4482],
     [152.9002, -27.4462], [152.8977, -27.4464], [152.8973, -27.4486],
     [152.8949, -27.4483], [152.8928, -27.4501]],
    [[152.8999, -27.4496], [152.9012, -27.4487], [152.9028, -27.4492],
     [152.9026, -27.4502], [152.9013, -27.4508], [152.8999, -27.4496]],
  ],
};

const [originLon, originLat] = [152.9, -27.45];
const radians = Math.PI / 180;
const metresPerDegree = 6371000 * radians;
const metresPerDegreeLon = metresPerDegree * Math.cos(originLat * radians);

function projectRing(ring, clockwise) {
  // GeoJSON repeats the first position at the end of a valid linear ring.
  const points = ring.slice(0, -1).map(([lon, lat]) => new THREE.Vector2(
    (lon - originLon) * metresPerDegreeLon,
    (lat - originLat) * metresPerDegree,
  ));
  return THREE.ShapeUtils.isClockWise(points) === clockwise ? points : points.reverse();
}

const [shore, ...islands] = lake.coordinates.map((ring, i) => projectRing(ring, i === 0));
function makeSurface(shore, holes) {
  const shape = new THREE.Shape(shore);
  shape.holes = holes.map(points => new THREE.Path(points));
  const geometry = new THREE.ShapeGeometry(shape);
  geometry.rotateX(-Math.PI / 2); // East is +X, north is -Z, up is +Y.
  return geometry;
}

const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8ece3);

const cutout = makeSurface(shore, islands);
const filled = makeSurface(shore, []);
const lakeMaterial = new THREE.ShaderMaterial({
  vertexShader: `
    varying vec2 vMetres;
    void main() {
      vMetres = position.xz;
      gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
    }
  `,
  fragmentShader: `
    varying vec2 vMetres;
    void main() {
      float shelf = clamp(0.48 + vMetres.y * 0.00065, 0.0, 1.0);
      float waves = sin(vMetres.y * 0.16 + sin(vMetres.x * 0.019) * 1.2)
                  + 0.55 * sin(dot(vMetres, vec2(0.13, 0.09)));
      vec3 color = mix(vec3(0.025, 0.15, 0.19), vec3(0.061, 0.41, 0.39), shelf);
      gl_FragColor = vec4(color + 0.004 * waves, 1.0);
      #include <colorspace_fragment>
    }
  `,
});
const water = new THREE.Mesh(cutout, lakeMaterial);
scene.add(water);

// Sample terrain for the diagram. Every wet point stays below the water plane.
function contains(x, north, ring) {
  let inside = false;
  for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
    const a = ring[i];
    const b = ring[j];
    if ((a.y > north) !== (b.y > north) &&
        x < (b.x - a.x) * (north - a.y) / (b.y - a.y) + a.x) inside = !inside;
  }
  return inside;
}

function distanceToRing(x, north, ring) {
  let nearest = Infinity;
  for (let i = 0; i < ring.length; i++) {
    const a = ring[i];
    const b = ring[(i + 1) % ring.length];
    const east = b.x - a.x;
    const up = b.y - a.y;
    const t = THREE.MathUtils.clamp(
      ((x - a.x) * east + (north - a.y) * up) / (east * east + up * up), 0, 1,
    );
    nearest = Math.min(nearest, Math.hypot(x - a.x - t * east, north - a.y - t * up));
  }
  return nearest;
}

const tileWidth = 1780;
const tileLength = 1140;
const ground = new THREE.PlaneGeometry(tileWidth, tileLength, 170, 115);
ground.rotateX(-Math.PI / 2);
const points = ground.getAttribute("position");
const colours = [];
const low = new THREE.Color(0xc9c198);
const high = new THREE.Color(0x64846a);
const shade = new THREE.Color();

for (let i = 0; i < points.count; i++) {
  const x = points.getX(i);
  const north = -points.getZ(i);
  const island = islands.some(ring => contains(x, north, ring));
  const wet = contains(x, north, shore) && !island;
  const ridge = Math.exp(-(((x + 480) / 580) ** 2) - ((north - 450) / 290) ** 2);
  const hill = Math.exp(-(((x - 570) / 420) ** 2) - ((north + 470) / 330) ** 2);
  const relief = 16 + ridge * 110 + hill * 84 + Math.sin(x / 170 + north / 145) * 13;
  const inland = THREE.MathUtils.smoothstep(distanceToRing(x, north, shore), 18, 112);
  const edge = Math.min(tileWidth / 2 - Math.abs(x), tileLength / 2 - Math.abs(north));
  const edgeBlend = THREE.MathUtils.smoothstep(edge, 0, 98);
  const height = wet ? -22 : island ? -1.5 : -10 + relief * inland;
  points.setY(i, THREE.MathUtils.lerp(8, height, edgeBlend));
  const band = Math.min(1, Math.floor(relief / 13) / 9);
  shade.copy(low).lerp(high, island ? 0.62 : band * 0.88);
  colours.push(shade.r, shade.g, shade.b);
}

ground.setAttribute("color", new THREE.Float32BufferAttribute(colours, 3));
ground.computeVertexNormals();
scene.add(new THREE.Mesh(ground, new THREE.MeshStandardMaterial({
  vertexColors: true,
  roughness: 1,
})));
const sides = new THREE.MeshStandardMaterial({ color: 0xaba48b, roughness: 1 });
const top = new THREE.MeshBasicMaterial({ visible: false });
const tile = new THREE.Mesh(new THREE.BoxGeometry(tileWidth, 64, tileLength),
  [sides, sides, top, sides, sides, sides]);
tile.position.y = -24;
scene.add(tile);

scene.add(new THREE.HemisphereLight(0xf8fbf3, 0x7c816f, 2.0));
const sun = new THREE.DirectionalLight(0xfff5de, 1.65);
sun.position.set(-500, 800, 460);
scene.add(sun);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.domElement.setAttribute("aria-hidden", "true");
document.body.append(renderer.domElement);
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 1, 5500);
const view = document.querySelector("#view");
const island = document.querySelector("#island");
let overhead = false;

function draw() {
  const width = window.innerWidth;
  const height = window.innerHeight;
  const aspect = width / height;
  const frame = Math.max(652, 1110 / aspect);
  camera.left = -frame * aspect;
  camera.right = frame * aspect;
  camera.top = frame;
  camera.bottom = -frame;
  camera.position.set(overhead ? 0 : 560, 1450, overhead ? 0 : 1500);
  camera.up.set(0, overhead ? 0 : 1, overhead ? -1 : 0);
  camera.lookAt(0, 0, 0);
  camera.updateProjectionMatrix();
  renderer.setSize(width, height);
  renderer.render(scene, camera);
}

view.addEventListener("click", () => {
  overhead = !overhead;
  view.textContent = overhead ? "Angled view" : "Map view";
  draw();
});
island.addEventListener("change", () => {
  water.geometry = island.checked ? cutout : filled;
  draw();
});
window.addEventListener("resize", draw);
draw();
  </script>
</body>
</html>

Add a water material

The example gives the mesh a colour and a small ripple pattern based on the horizontal position. Keep the geometry when you change materials. Every triangle still belongs to the footprint, so a new shader can change the water’s appearance without repainting the island.

The outline locates the shore. For shoaling waves, you also need the bed elevation inside it. Sample the terrain or a bathymetry grid, then subtract each bed elevation from the water level to build Tidewater’s signed depth field. Use the same vertical datum for both. Build a signed depth field · Build a bed mesh from depth samples