namespace Sandbox.Utility;
///
/// Provides access to coherent noise utilities.
///
/// All of these functions should return between 0 and 1.
///
public static partial class Noise
{
static FastNoise perlin;
static FastNoise simplex;
static FastNoise fbm;
static Noise()
{
perlin = new FastNoise( 5633 );
perlin.SetNoiseType( FastNoise.NoiseType.Perlin );
perlin.SetFrequency( 0.03f );
simplex = new FastNoise( 5633 );
simplex.SetNoiseType( FastNoise.NoiseType.Simplex );
simplex.SetFrequency( 0.03f );
fbm = new FastNoise( 5633 );
fbm.SetNoiseType( FastNoise.NoiseType.PerlinFractal );
fbm.SetFrequency( 0.03f );
fbm.SetFractalGain( 0.5f );
fbm.SetFractalLacunarity( 2.0f );
}
internal static float ConvertRange( float f ) => (1f + f) * 0.5f;
///
/// 2D Perlin noise function.
/// For a thread-safe alternative with more options, use .
///
/// Input on the X axis.
/// Input on the Y axis.
/// Resulting noise at given coordinates, in range of 0 to 1.
public static float Perlin( float x, float y = 0f ) => ConvertRange( perlin.GetNoise( x, y ) );
///
/// 3D Perlin noise function.
/// For a thread-safe alternative with more options, use .
///
/// Input on the X axis.
/// Input on the Y axis.
/// Input on the Z axis.
/// Resulting noise at given coordinates, in range of 0 to 1.
public static float Perlin( float x, float y, float z ) => ConvertRange( perlin.GetNoise( x, y, z ) );
///
/// 2D Simplex noise function.
/// For a thread-safe alternative with more options, use .
///
/// Input on the X axis.
/// Input on the Y axis.
/// Resulting noise at given coordinates, in range of 0 to 1.
public static float Simplex( float x, float y = 0f ) => ConvertRange( simplex.GetNoise( x, y ) );
///
/// 3D Simplex noise function.
/// For a thread-safe alternative with more options, use .
///
/// Input on the X axis.
/// Input on the Y axis.
/// Input on the Z axis.
/// Resulting noise at given coordinates, in range of 0 to 1.
public static float Simplex( float x, float y, float z ) => ConvertRange( simplex.GetNoise( x, y, z ) );
///
/// Fractional Brownian Motion noise, a.k.a. Fractal Perlin noise.
/// For a thread-safe alternative with more options, use with .
///
/// Number of octaves for the noise. Higher values are slower but produce more detailed results. 3 is a good starting point.
/// Input on the X axis.
/// Input on the Y axis.
/// Input on the Z axis.
/// Resulting noise at given coordinates, in range of 0 to 1.
public static float Fbm( int octaves, float x, float y = 0.0f, float z = 0.0f )
{
fbm.SetFractalOctaves( octaves );
return ConvertRange( fbm.GetNoise( x, y, z ) );
}
///
/// Fractional Brownian Motion noise, a.k.a. Fractal Perlin noise.
///
/// Number of octaves for the noise. Higher values are slower but produce more detailed results. 3 is a good starting point.
/// Input on the X axis.
/// Input on the Y axis.
public static Vector3 FbmVector( int octaves, float x, float y = 0.0f )
{
fbm.SetFractalOctaves( octaves );
return new Vector3( fbm.GetNoise( x, y, 0.0f ), fbm.GetNoise( x, y, 256.0f ), fbm.GetNoise( x, y, 512.0f ) );
}
}