In this chapter, we will learn about a fun technique that consists of using a 2D Perlin noise to displace the vertex of a mesh to create a terrain. As we mentioned in the first lesson on noise, the noise function is a very useful "procedural texture" primitive from which more complex procedural textures can be created such as for example the fractal or the turbulence pattern. We will use the Perlin noise to start with, and then show an example of a terrain generated using a fractal pattern instead.
This technique will be useful to better understand the importance of computing the derivatives of the Perlin noise function which is the topic of the next chapter.
After reading this chapter, you will be able to reproduce the image below.
The idea behind this technique is very simple and similar to what we call displacement mapping. If you look at the grid from the top, you can more easily see that if you overlay the noise image onto the grid you get a perfect match: to each vertex of the grid corresponds a pixel in the noise image. As you know we can define the coordinates of the pixels in some sort of normal device coordinates (the pixel coordinates are then in the range [0,1]). The same can be done with the grid vertices: these are technically called texture coordinates. Let's look at the function we will be using to create the grid:
The texture coordinate of the vertex is computed line 16. This is also a space in which the coordinates are in the range [0,1]. The vertex in the upper left corner of the grid has the texture coordinates [0,0], while the vertex in the lower-right coordinate has the texture coordinate [1,1]. It becomes thus easy to use the vertex texture coordinates to do a lookup in the noise image.
In case of a height map, we generally use the brightness (the luminance for example) of the pixels' color to control the amplitude of displacement. Generally the brighter the pixel, the greater the displacement though of course you can map the pixel value to displacement in a completely different way if you wish. It all depends on the effect you intend to create. All you need to remember is that you use an image to somehow control the amount by which the vertices of the object are displaced or moved along for example their normal.
Keep in mind that the values of the Perlin noise are in the range [-1,1]. But we remapped these values to the range [0,1] when we stored the values in the image buffer (line 5). Though the values are mapped again back to the range [-1,1] when we displace the vertices later on (line 15) because that way, the mesh will stay centred around the origin along the y-axis. We will either push the vertices upward (if the values are greater than 0) or downward (if the values are lower than 0) and if the value is 0, the vertex y-coordinate will stay 0.
If you render this mesh with the noise image applied on top as a texture map, you should get something similar to the first image of this chapter. Note how the white/bright area of the noise image corresponds to bump in the mesh, while dark areas in the image corresponds to dents or valleys (and note how the amount of displacement is proportional to the pixel values).
As mentioned in the introduction of this chapter, you can use more interesting procedural patterns to displace the mesh vertices, such as a fractal pattern which can be construct as a weighted sum of noise layers. Check the previous lesson on noise to learn now to generate a fractal pattern using the noise function. Here is the code to generate the fractal image that was used to displace the mesh:
A fractal image generally contains details of higher frequency than in a single layer of noise. Thus, to see these details in the displacement, it is likely that you will have to increase the density of the mesh itself. Here is a render of the mesh displaced with a fractal image.
As suggested in the previous lesson, this technique can be used to generate realistic terrains (hopefully the image above is convincing enough). We can also use the noise function to create and animate water surfaces. In this example, the procedural pattern we used (a fractal) is pretty simple. You can play with the parameters a bit to modify the look of the terrain, but we will also learn in another lesson how to add effects such as erosion to increase the realism of the terrain.
As a final note, we haven't computed the normal of the mesh after displacement. How do we do that? This is the topic of our next chapter. We will learn how to use the noise function derivatives to compute a "true" normal at the vertex position after displacement.