using VoxelEngine.Core; namespace VoxelEngine.World.WorldGen; public sealed class TerrainSampler { private readonly NoiseSource _noise; private readonly BiomeMap _biomes; private readonly Configuration _cfg; public TerrainSampler(NoiseSource noise, BiomeMap biomes, Configuration cfg) { this._noise = noise; this._biomes = biomes; this._cfg = cfg; } public int HeightAt(float worldX, float worldZ) { // Blend the two nearest biomes' heights (each evaluated AT this column) by // how deep inside the nearer cell we are: exactly on the border they mix // 50/50, easing to 100% of the nearer biome over BiomeBlendWidth blocks. BiomeMap.BiomeBlend blend = this._biomes.NearestTwo(worldX, worldZ); float hNear = blend.Near.SampleHeight(this._noise, worldX, worldZ); float hFar = blend.Far.SampleHeight(this._noise, worldX, worldZ); float fracFar = 0.5f * (1f - SmoothStep01(blend.BorderDistance / this._cfg.BiomeBlendWidth)); float avg = hNear * (1f - fracFar) + hFar * fracFar; int h = (int)MathF.Round(avg); return Math.Clamp(h, 0, WorldConstants.WorldHeight - 1); } // Hermite smoothstep on [0,1]. private static float SmoothStep01(float t) { t = Math.Clamp(t, 0f, 1f); return t * t * (3f - 2f * t); } }