// HOW TO IMPLEMENT A PERLIN NOISE FONCTION TO GET A TERRAIN HEIGHT IN UNITY
using UnityEngine;
public static class NoiseCreator
{
/// scale : The scale of the "perlin noise" view
/// heightMultiplier : The maximum height of the terrain
/// octaves : Number of iterations (the more there is, the more detailed the terrain will be)
/// persistance : The higher it is, the rougher the terrain will be (this value should be between 0 and 1 excluded)
/// lacunarity : The higher it is, the more "feature" the terrain will have (should be strictly positive)
public static float GetNoiseAt(int x, int z, float scale, float heightMultiplier, int octaves, float persistance, float lacunarity)
{
float PerlinValue = 0f;
float amplitude = 1f;
float frequency = 1f;
for(int i = 0; i < octaves; i++)
{
// Get the perlin value at that octave and add it to the sum
PerlinValue += Mathf.PerlinNoise(x * frequency, z * frequency) * amplitude;
// Decrease the amplitude and the frequency
amplitude *= persistance;
frequency *= lacunarity;
}
// Return the noise value
return PerlinValue * heightMultiplier;
}
}
public static float Perlin3D(float x, float y, float z, float density, float scale){
float XY = Mathf.PerlinNoise(x, y);
float YZ = Mathf.PerlinNoise(y, z);
float ZX = Mathf.PerlinNoise(z, x);
float YX = Mathf.PerlinNoise(y, z);
float ZY = Mathf.PerlinNoise(z, y);
float XZ = Mathf.PerlinNoise(x, z);
float val = (XY + YZ + ZX + YX + ZY + XZ)/6f;
return val * scale;
}
public static float PerlinNoise3D(float x, float y, float z)
{
float xy = Mathf.PerlinNoise(x, y);
float xz = Mathf.PerlinNoise(x, z);
float yz = Mathf.PerlinNoise(y, z);
float yx = Mathf.PerlinNoise(y, x);
float zx = Mathf.PerlinNoise(z, x);
float zy = Mathf.PerlinNoise(z, y);
return (xy + xz + yz + yx + zx + zy) / 6;
}