Skip to content

Instantly share code, notes, and snippets.

@tntmeijs
Last active February 19, 2024 01:52
Show Gist options
  • Save tntmeijs/6a3b4587ff7d38a6fa63e13f9d0ac46d to your computer and use it in GitHub Desktop.
Save tntmeijs/6a3b4587ff7d38a6fa63e13f9d0ac46d to your computer and use it in GitHub Desktop.
A 3D implementation using the 2D Perlin noise function of Unity3D.
public static float Noise3D(float x, float y, float z, float frequency, float amplitude, float persistence, int octave, int seed)
{
float noise = 0.0f;
for (int i = 0; i < octave; ++i)
{
// Get all permutations of noise for each individual axis
float noiseXY = Mathf.PerlinNoise(x * frequency + seed, y * frequency + seed) * amplitude;
float noiseXZ = Mathf.PerlinNoise(x * frequency + seed, z * frequency + seed) * amplitude;
float noiseYZ = Mathf.PerlinNoise(y * frequency + seed, z * frequency + seed) * amplitude;
// Reverse of the permutations of noise for each individual axis
float noiseYX = Mathf.PerlinNoise(y * frequency + seed, x * frequency + seed) * amplitude;
float noiseZX = Mathf.PerlinNoise(z * frequency + seed, x * frequency + seed) * amplitude;
float noiseZY = Mathf.PerlinNoise(z * frequency + seed, y * frequency + seed) * amplitude;
// Use the average of the noise functions
noise += (noiseXY + noiseXZ + noiseYZ + noiseYX + noiseZX + noiseZY) / 6.0f;
amplitude *= persistence;
frequency *= 2.0f;
}
// Use the average of all octaves
return noise / octave;
}
@Llama-W-2Ls
Copy link

Amazing, thank you so much!

@fguillen
Copy link

+1 :)

@FakeMG
Copy link

FakeMG commented Jan 14, 2023

Could you explain why we do it that way please?

@tntmeijs
Copy link
Author

@FakeMG what exactly would you like me to explain? I don't quite recall all the implementation details as I created this snippet almost 5 years ago...

@FakeMG
Copy link

FakeMG commented Jan 14, 2023

@tntmeijs I don't understand why we need to reverse of the permutations of noise?

@tntmeijs
Copy link
Author

tntmeijs commented Jan 14, 2023

@FakeMG I don't quite remember why, unfortunately. It has been a long time since I did gamedev so my noise / vector math is a bit fuzzy. As far as I know, back then I based myself on various resources I found on the internet and just used it as-is.

It'd be fun to just delete the reverse permutations and observe the results. I'd be very interested in it! Please do post here if you end up doing this quick experiment.

Based on some things I read online, I think that this noise might actually have some tiling artefacts if you were to copy-paste the code. Rather than dividing by 6.0 and averaging the values, it might be better to mod it instead. Just a hunch, but it might be worth experimenting with.

Alternatively, you could try to implement a better noise yourself. I found various resources on Perlin / Simplex noise that looked very promising, so that could also be a viable alternative if this code snippet doesn't suit your needs. 😊

@FakeMG
Copy link

FakeMG commented Jan 15, 2023

I deleted the reverse permutations and the noise didn't change much. I still don't understand what it's for. It's fine if you don't remember, it's not too important, the code works just fine.
Thanks for your time!

@tntmeijs
Copy link
Author

Glad to hear it was useful after all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment