feat: add seeded NoiseSource over FastNoiseLite

This commit is contained in:
MDietermann
2026-05-31 21:46:53 +02:00
parent 99b411bdbd
commit 44f53e8d50
2 changed files with 61 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
using VoxelEngine.World.WorldGen;
using Xunit;
namespace VoxelEngine.Tests.WorldGen;
public class NoiseSourceTests
{
[Fact]
public void SameSeed_SameCoord_IsDeterministic()
{
var a = new NoiseSource(42);
var b = new NoiseSource(42);
Assert.Equal(a.Continent(10f, 20f), b.Continent(10f, 20f));
Assert.Equal(a.Temperature(10f, 20f), b.Temperature(10f, 20f));
}
[Fact]
public void Output_InMinusOneToOneRange()
{
var n = new NoiseSource(7);
for (int i = 0; i < 200; i++)
{
float c = n.Continent(i * 3.1f, i * 1.7f);
float t = n.Temperature(i * 2.3f, i * 0.9f);
Assert.InRange(c, -1.001f, 1.001f);
Assert.InRange(t, -1.001f, 1.001f);
}
}
}

View File

@@ -0,0 +1,32 @@
namespace VoxelEngine.World.WorldGen;
// Seeded wrapper over FastNoiseLite. All Get* methods return roughly [-1, 1].
public sealed class NoiseSource
{
private readonly FastNoiseLite _continent;
private readonly FastNoiseLite _temperature;
private readonly FastNoiseLite _forest;
public NoiseSource(int seed)
{
this._continent = new FastNoiseLite(seed);
this._continent.SetNoiseType(FastNoiseLite.NoiseType.OpenSimplex2);
this._continent.SetFractalType(FastNoiseLite.FractalType.FBm);
this._continent.SetFractalOctaves(4);
this._continent.SetFrequency(0.0035f);
this._temperature = new FastNoiseLite(seed + 1);
this._temperature.SetNoiseType(FastNoiseLite.NoiseType.OpenSimplex2);
this._temperature.SetFractalType(FastNoiseLite.FractalType.FBm);
this._temperature.SetFractalOctaves(3);
this._temperature.SetFrequency(0.0015f);
this._forest = new FastNoiseLite(seed + 2);
this._forest.SetNoiseType(FastNoiseLite.NoiseType.OpenSimplex2);
this._forest.SetFrequency(0.02f);
}
public float Continent(float x, float z) => this._continent.GetNoise(x, z);
public float Temperature(float x, float z) => this._temperature.GetNoise(x, z);
public float Forest(float x, float z) => this._forest.GetNoise(x, z);
}