# 3d Perlin Noise?

Hey everyone,

Been looking into procedural terrain generation for quite some time. Just looking at perlin noise now. However I want my terrain to have overhangs and caves etc (i’m going to create my own meshes instead of using unity’s terrain). In researching this I’ve found I should use 3D Perlin noise, and use the noise value as density (<0 = air >= 0 = ground). Just wondering how in unity I can make 3D perlin noise? Should I still use mathf.perlinnoise in some way? Also how can I make the terrain more believable instead of just a block of holes and rocks?

Chris

Hi,

Things like caves are not explained in the video, but maybe it will help.

I was banging my head trying to get rid of the artefacts I kept seeing in the naive approach, which was found here [Unity] Easy 3D Perlin Noise - YouTube

``````public static float PerlinNoise3D(float x, float y, float z)
{
float xy = Mathf.PerlinNoise(x, y);
float xz = Mathf.PerlinNoise(x, z);
float yz = Mathf.PerlinNoise(y, z);
float yx = Mathf.PerlinNoise(y, x);
float zx = Mathf.PerlinNoise(z, x);
float zy = Mathf.PerlinNoise(z, y);

return (xy + xz + yz + yx + zx + zy) / 6;
}
``````

That approach may work in some cases, but for my work I kept seeing symmetrical lines throughout the data. It turns out that it was mirroring along all three axis! Terrible for a noise function to give you symmetry

Eventually I worked this out:

``````public static float PerlinNoise3D(float x, float y, float z)
{
y += 1;
z += 2;
float xy = _perlin3DFixed(x, y);
float xz = _perlin3DFixed(x, z);
float yz = _perlin3DFixed(y, z);
float yx = _perlin3DFixed(y, x);
float zx = _perlin3DFixed(z, x);
float zy = _perlin3DFixed(z, y);

return xy * xz * yz * yx * zx * zy;
}

static float _perlin3DFixed(float a, float b)
{
return Mathf.Sin(Mathf.PI * Mathf.PerlinNoise(a, b));
}
``````

I haven’t seen any artefacts in it yet.

Hope that helps you out!

EDIT: I’ve seen some case where there are lines again, but I’m sure someone cleverer than me an iron them out.