-
-
Save tntmeijs/6a3b4587ff7d38a6fa63e13f9d0ac46d to your computer and use it in GitHub Desktop.
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; | |
} |
+1 :)
Could you explain why we do it that way please?
@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...
@tntmeijs I don't understand why we need to reverse of the permutations of noise?
@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. 😊
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!
Glad to hear it was useful after all!
Amazing, thank you so much!