Skip to content

Instantly share code, notes, and snippets.

@tntmeijs
Last active October 4, 2024 20:23
Show Gist options
  • Select an option

  • Save tntmeijs/6a3b4587ff7d38a6fa63e13f9d0ac46d to your computer and use it in GitHub Desktop.

Select an option

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

FakeMG commented Jan 15, 2023

Copy link
Copy Markdown

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
Copy Markdown
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