Terrain with 3D noise

The key to creating “random” yet smooth terrain is using “noise”.

To create a terrain map, we want to be able to input the x and y coordinate , and return a height value. Therefore we want to utilise 2D noise, and this example uses the function createNoise2D from the package simplex-noise.

How is this done?

In Scene.svelte:

  1. Create noise map

const noise = createNoise2D()

  1. Create plane geometry

const geometry = new PlaneGeometry(10, 10, 100, 100)

Reminder that planes are on their side by default!

i.e. they extend in the x and y directions, and the z value of each vertex is 0

  1. Extract the “position” (vertices) array from the PlaneGeometry

    const geometry = geometry.getAttribute('position').array

  2. Loop over the vertices, setting each z value, using our noise map

for (let i = 0; i < vertices.length; i += 3) {
  const x = vertices[i]
  const y = vertices[i + 1]
  // @ts-ignore
  vertices[i + 2] = noise(x / 4, y / 4)
}

Why i+=3?

The position array is a flat array of vertices, in the recurring format “x y z x y z…” So if we want to set each vertex’s z value from it’s x and y, we need to loop in triplets.

  1. Attach the plane geometry to a mesh, and rotate
<T.Mesh
	{geometry}
	rotation.x={DEG2RAD * -90}
>

This example intentionally used only part of the noise map (the middle 25%), in order to generate gentler terrain.

To “see” the noise map in full, change noise(x/4, y/4) to noise(x, y).

You’ll see that the terrain is much more hilly!