Terrain feel: biome regions ~10x larger (Voronoi cell size 96->960); terrain height now eases across biome borders via a Voronoi distance blend (smooth, and ~25x cheaper than the old 169-sample box blur); mountains capped to ~2/3 world height and gentled (ridged amplitude/frequency lowered), snow line lowered to match. Centralization: ~27 world-gen/render tuning constants (biome amplitudes & frequencies, climate-noise frequencies/octaves, biome-selection thresholds, blend width, cell size, filler depth, snow line, camera near plane, spawn offset, overlay scale) hoisted from scattered literals into a region-organized, fully-commented Configuration. Configuration is threaded into NoiseSource, the biomes, BiomeRegistry, BiomeMap (+Classify), TerrainSampler, WorldGenerator (+BlockAt filler depth), and Camera via Player. Defaults equal the prior values, so generation is unchanged; worldgen tests updated for the new constructor signatures.
101 lines
3.4 KiB
C#
101 lines
3.4 KiB
C#
namespace VoxelEngine.World.WorldGen;
|
|
|
|
// A jittered-grid Voronoi partition. Pure integer-hash based, fully deterministic.
|
|
public sealed class VoronoiCells
|
|
{
|
|
public readonly record struct Cell(int Id, float CenterX, float CenterZ);
|
|
|
|
private readonly int _seed;
|
|
private readonly float _cellSize;
|
|
|
|
public VoronoiCells(int seed, float cellSize)
|
|
{
|
|
this._seed = seed;
|
|
this._cellSize = cellSize;
|
|
}
|
|
|
|
public Cell Nearest(float x, float z)
|
|
{
|
|
int gx = (int)MathF.Floor(x / this._cellSize);
|
|
int gz = (int)MathF.Floor(z / this._cellSize);
|
|
|
|
float bestDist = float.MaxValue;
|
|
int bestId = 0;
|
|
float bestCx = 0f, bestCz = 0f;
|
|
|
|
for (int oz = -1; oz <= 1; oz++)
|
|
for (int ox = -1; ox <= 1; ox++)
|
|
{
|
|
int cgx = gx + ox;
|
|
int cgz = gz + oz;
|
|
float jitterX = Jitter(cgx, cgz, this._seed);
|
|
float jitterZ = Jitter(cgx, cgz, this._seed + 9871);
|
|
float cx = (cgx + jitterX) * this._cellSize;
|
|
float cz = (cgz + jitterZ) * this._cellSize;
|
|
float dx = cx - x, dz = cz - z;
|
|
float dist = dx * dx + dz * dz;
|
|
if (dist < bestDist)
|
|
{
|
|
bestDist = dist;
|
|
bestId = (int)Hash(cgx, cgz, this._seed);
|
|
bestCx = cx;
|
|
bestCz = cz;
|
|
}
|
|
}
|
|
return new Cell(bestId, bestCx, bestCz);
|
|
}
|
|
|
|
public readonly record struct NearestPair(Cell First, Cell Second, float FirstDist, float SecondDist);
|
|
|
|
// The two nearest cells to (x,z) with their (Euclidean) distances. Used to
|
|
// ease terrain height across the border between adjacent biome cells. Same
|
|
// 3x3 jittered-grid search as Nearest — the two closest centres always fall
|
|
// within that neighbourhood for jitter in [0,1).
|
|
public NearestPair NearestTwo(float x, float z)
|
|
{
|
|
int gx = (int)MathF.Floor(x / this._cellSize);
|
|
int gz = (int)MathF.Floor(z / this._cellSize);
|
|
|
|
float best = float.MaxValue, second = float.MaxValue;
|
|
Cell bestCell = default, secondCell = default;
|
|
|
|
for (int oz = -1; oz <= 1; oz++)
|
|
for (int ox = -1; ox <= 1; ox++)
|
|
{
|
|
int cgx = gx + ox;
|
|
int cgz = gz + oz;
|
|
float jitterX = Jitter(cgx, cgz, this._seed);
|
|
float jitterZ = Jitter(cgx, cgz, this._seed + 9871);
|
|
float cx = (cgx + jitterX) * this._cellSize;
|
|
float cz = (cgz + jitterZ) * this._cellSize;
|
|
float dx = cx - x, dz = cz - z;
|
|
float dist = dx * dx + dz * dz;
|
|
Cell cell = new((int)Hash(cgx, cgz, this._seed), cx, cz);
|
|
if (dist < best)
|
|
{
|
|
second = best; secondCell = bestCell;
|
|
best = dist; bestCell = cell;
|
|
}
|
|
else if (dist < second)
|
|
{
|
|
second = dist; secondCell = cell;
|
|
}
|
|
}
|
|
return new NearestPair(bestCell, secondCell,
|
|
MathF.Sqrt(best), MathF.Sqrt(second));
|
|
}
|
|
|
|
private static float Jitter(int x, int z, int seed)
|
|
=> (Hash(x, z, seed) & 0xFFFFFF) / (float)0x1000000; // [0,1)
|
|
|
|
private static uint Hash(int x, int z, int seed)
|
|
{
|
|
unchecked
|
|
{
|
|
uint h = (uint)(x * 374761393 + z * 668265263 + seed * 362437);
|
|
h = (h ^ (h >> 13)) * 1274126177u;
|
|
return h ^ (h >> 16);
|
|
}
|
|
}
|
|
}
|