I've been recently studying procedurally generated terrain using two dimensional Perlin Noise. I'm using Unreal Engine 4 blueprints as a way to prototype, but will soon transfer to C++.
I have an array of vertices that construct a two dimensional plane. I run Perlin Noise over each vertex to get a random noise value, and then assign it to that vertex's Z value. This has yielded very nice looking terrain; but it is only a plane. There are no cave systems or anything of the type. This piqued my interest.
I researched a little about 3D Perlin Noise, but I really don't understand how it works. The 3D Perlin Noise function takes the X, Y, and Z positions of the element's location, and outputs a value. But what does this value do? The 2D Perlin Noise assigned the value to the third dimension, so does the 3D function assign it to a fourth dimension? How is this value actually applied to the terrain?
I am fairly new to this area of programming, any advice or knowledge would be greatly appreciated.
Values in 3D perlin noise don't intrinsically mean anything on their own. Just as 2D perlin noise value doesn't "do" anything, you only choose to interpret that as a height value.
One way to use 3D perlin noise underground could be that any value over some threshold (such as 0.8
) would be considered "empty".
Here's one implementation of 2d perlin + 3d perlin random terrain in a voxel system (pseudocode)
bool IsVoxelEmpty(int x, int y, int z)
{
float flatPerlin = Get2DPerlin(x, z);
float terrainHeight = perlinToHeight(flatPerlin)
float thickPerlin = Get3DPerlin(x, y, z);
float threshold = 0.8f;
return (y > terrainHeight) || (thickPerlin > threshold);
}
One downside to using something like this would be that there would be many "craters" and caverns open to the surface. You could change threshold
so that it gets higher as yPerlin
approaches flatPerlin
so that the number of holes in the surface diminishes and caverns are typically only found after you breach the surface.
A problem with using mere perlin noise for cave generation is that you would get isolated pockets of caverns, and they would often be separate from each other. An alternative could be using perlin worms to generate a cave network.