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;
}
@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