Last active
October 4, 2024 20:23
-
-
Save tntmeijs/6a3b4587ff7d38a6fa63e13f9d0ac46d to your computer and use it in GitHub Desktop.
A 3D implementation using the 2D Perlin noise function of Unity3D.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Glad to hear it was useful after all!