Cut 8 polish: warm Desert haze chromaticity, smooth below-horizon SkyView, 4096/3 shadows & 340m terrain shadows

This commit is contained in:
Meta Vens 2026-12-05 18:08:29 +01:00
commit 22a46c9d0f
4 changed files with 184 additions and 99 deletions

View file

@ -131,12 +131,12 @@ public static class AtmosphereLutBaker
ValleyVolumeDensity: 0.0052f,
ValleyVolumeHeightFalloff: 1.85f,
ValleyVolumeEdgeFade: 0.15f,
DepthFogBegin: 340f,
DepthFogEnd: 1400f,
DepthFogCurve: 0.90f,
DepthFogDensity: 0.46f,
DepthFogAerialPerspective: 0.82f,
DepthFogSunScatter: 0.55f,
DepthFogBegin: 260f,
DepthFogEnd: 840f,
DepthFogCurve: 0.85f,
DepthFogDensity: 0.84f,
DepthFogAerialPerspective: 0.22f,
DepthFogSunScatter: 0.65f,
ShadowMaxDistance: 280f,
AmbientLightEnergy: 0.55f,
SunDiscIntensity: 8.0f,
@ -180,7 +180,7 @@ public static class AtmosphereLutBaker
Color sunColor = DeriveSunLightColor(sunT);
float sunEnergy = DeriveSunLightEnergy(preset, sunT);
Color hazeColor = DeriveHorizonHazeColor(skyViewBuf, preset.SolarIrradiance);
Color hazeColor = DeriveHorizonHazeColor(preset, skyViewBuf, sunT);
return new BakedAtmosphere
{
@ -220,30 +220,48 @@ public static class AtmosphereLutBaker
return preset.CalibratedSunEnergy * ratio;
}
private static Color DeriveHorizonHazeColor(Vector3[] skyViewBuf, float solarIrradiance)
private static Color DeriveHorizonHazeColor(AtmospherePreset preset, Vector3[] skyViewBuf, Vector3 sunT)
{
// Integrate SkyView across all azimuths at +3 deg .. +6 deg above horizon (just above v = 0.5)
// Integrate SkyView across the low horizon dust/haze band (+0.5 deg .. +3.5 deg above horizon)
// weighting solar-side Mie scatter slightly higher so chromaticity reflects mineral dust vs alpine air,
// then scale uniformly to <= 0.88 without channel clipping.
Vector3 accum = Vector3.Zero;
int samples = 0;
int yStart = SkyViewHeight / 2 + 6;
int yEnd = Math.Min(SkyViewHeight - 1, SkyViewHeight / 2 + 16);
float weightSum = 0f;
int yStart = SkyViewHeight / 2 + 2;
int yEnd = Math.Min(SkyViewHeight - 1, SkyViewHeight / 2 + 12);
bool warmDust = preset.MieScattering.X > preset.MieScattering.Z * 1.15f;
for (int y = yStart; y <= yEnd; y++)
{
for (int x = 0; x < SkyViewWidth; x++)
{
accum += skyViewBuf[y * SkyViewWidth + x];
samples++;
float dphi = ((x + 0.5f) / SkyViewWidth) * 2f * MathF.PI;
float solarBias = warmDust ? (0.65f + 1.35f * MathF.Max(0f, MathF.Cos(dphi))) : 1.0f;
accum += skyViewBuf[y * SkyViewWidth + x] * solarBias;
weightSum += solarBias;
}
}
Vector3 mean = samples > 0 ? accum / samples : new Vector3(0.55f, 0.62f, 0.72f);
// Normalize relative to sky exposure into a clean linear scattering albedo (0.45..0.92)
float scale = 1.15f / MathF.Max(0.25f, MathF.Max(mean.X, MathF.Max(mean.Y, mean.Z)));
Vector3 norm = mean * MathF.Min(scale, 2.2f);
Vector3 mean = weightSum > 0f ? accum / weightSum : new Vector3(0.55f, 0.62f, 0.72f);
if (warmDust)
{
// Blend in forward-scattered Mie dust + attenuated solar spectrum so dusty air keeps warm R > G > B chromaticity
float mieMax = MathF.Max(1e-6f, MathF.Max(preset.MieScattering.X, MathF.Max(preset.MieScattering.Y, preset.MieScattering.Z)));
Vector3 mieTint = preset.MieScattering / mieMax;
Vector3 dustChroma = new Vector3(mieTint.X * sunT.X, mieTint.Y * sunT.Y, mieTint.Z * sunT.Z);
float meanMax = MathF.Max(1e-4f, MathF.Max(mean.X, MathF.Max(mean.Y, mean.Z)));
float dustMax = MathF.Max(1e-4f, MathF.Max(dustChroma.X, MathF.Max(dustChroma.Y, dustChroma.Z)));
Vector3 meanUnit = mean / meanMax;
Vector3 dustUnit = dustChroma / dustMax;
mean = meanUnit.Lerp(dustUnit, 0.52f);
}
float peak = MathF.Max(1e-4f, MathF.Max(mean.X, MathF.Max(mean.Y, mean.Z)));
Vector3 norm = mean * (0.88f / peak);
return new Color(
Math.Clamp(norm.X, 0.25f, 0.94f),
Math.Clamp(norm.Y, 0.25f, 0.94f),
Math.Clamp(norm.Z, 0.22f, 0.94f),
Math.Clamp(norm.X, 0.18f, 0.90f),
Math.Clamp(norm.Y, 0.18f, 0.90f),
Math.Clamp(norm.Z, 0.18f, 0.90f),
1.0f);
}
@ -350,8 +368,9 @@ public static class AtmosphereLutBaker
if (!HitsPlanet(ri, muSunAtP))
{
Vector3 tSun = SampleTransmittanceLut(transBuf, ri, muSunAtP);
// Isotropic phase 1 / (4*PI) cancels with 4*PI sphere integral when divided by dirCount
Vector3 scatterIn = sigmaS * tSun;
// Hillaire 2020 §5.2: 1st-bounce isotropic phase 1/(4*PI); 2nd-bounce 1/(4*PI) cancels 4*PI/dirCount
const float phaseIso = 1f / (4f * MathF.PI);
Vector3 scatterIn = sigmaS * tSun * phaseIso;
Vector3 sInt = SafeScatterIntegral(scatterIn, stepT, sigmaE, dt);
l2 += throughput * sInt;
}
@ -413,88 +432,117 @@ public static class AtmosphereLutBaker
}
Vector2 sunPerp = new(sunXz.Y, -sunXz.X);
for (int y = 0; y < SkyViewHeight; y++)
Vector3 horizonGroundAlbedo = preset.MapKind == ArenaMapKind.Desert
? new Vector3(0.48f, 0.33f, 0.20f)
: GroundAlbedo;
// Bake upper hemisphere first (y = SkyViewHeight/2 .. SkyViewHeight-1) so below-horizon rows
// can smoothly transition from exact horizon limb radiance without a hard shoreline step.
for (int pass = 0; pass < 2; pass++)
{
float v = (y + 0.5f) / SkyViewHeight;
float elev;
if (v >= 0.5f)
{
float l = 2f * v - 1f;
elev = horizonElev + l * l * (MathF.PI * 0.5f - horizonElev);
}
else
{
float l = 1f - 2f * v;
elev = horizonElev - l * l * (MathF.PI * 0.5f + horizonElev);
}
int yBegin = pass == 0 ? SkyViewHeight / 2 : 0;
int yFinish = pass == 0 ? SkyViewHeight : SkyViewHeight / 2;
float cosElev = MathF.Cos(elev);
float sinElev = MathF.Sin(elev);
for (int x = 0; x < SkyViewWidth; x++)
for (int y = yBegin; y < yFinish; y++)
{
float u = (x + 0.5f) / SkyViewWidth;
float dphi = u * 2f * MathF.PI;
Vector2 dirXz = sunXz * MathF.Cos(dphi) + sunPerp * MathF.Sin(dphi);
Vector3 rayDir = new Vector3(dirXz.X * cosElev, sinElev, dirXz.Y * cosElev).Normalized();
float maxDist = RaySphereExit(r0, rayDir.Y, out bool hitGround);
if (maxDist <= 0f)
float v = (y + 0.5f) / SkyViewHeight;
float elev;
if (v >= 0.5f)
{
buf[y * SkyViewWidth + x] = Vector3.Zero;
continue;
float l = 2f * v - 1f;
elev = horizonElev + l * l * (MathF.PI * 0.5f - horizonElev);
}
else
{
float l = 1f - 2f * v;
elev = horizonElev - l * l * (MathF.PI * 0.5f + horizonElev);
}
float cosTheta = rayDir.Dot(sunDir);
float phaseR = RayleighPhase(cosTheta);
float phaseM = CornetteShanksPhase(cosTheta, preset.MieG);
const float phaseIso = 1f / (4f * MathF.PI);
float cosElev = MathF.Cos(elev);
float sinElev = MathF.Sin(elev);
float dt = maxDist / steps;
Vector3 throughput = Vector3.One;
Vector3 luminance = Vector3.Zero;
for (int s = 0; s < steps; s++)
for (int x = 0; x < SkyViewWidth; x++)
{
float t = (s + 0.5f) * dt;
Vector3 p = viewPos + rayDir * t;
float ri = p.Length();
float hi = MathF.Max(0f, ri - PlanetRadiusM);
float u = (x + 0.5f) / SkyViewWidth;
float dphi = u * 2f * MathF.PI;
Vector2 dirXz = sunXz * MathF.Cos(dphi) + sunPerp * MathF.Sin(dphi);
Vector3 rayDir = new Vector3(dirXz.X * cosElev, sinElev, dirXz.Y * cosElev).Normalized();
SampleRayleighMie(preset, hi, out Vector3 sigR, out Vector3 sigM, out Vector3 sigE);
Vector3 stepT = new(
MathF.Exp(-sigE.X * dt),
MathF.Exp(-sigE.Y * dt),
MathF.Exp(-sigE.Z * dt));
Vector3 up = p / MathF.Max(1f, ri);
float muSun = up.Dot(sunDir);
Vector3 sunVis = HitsPlanet(ri, muSun)
? Vector3.Zero
: SampleTransmittanceLut(transBuf, ri, muSun);
Vector3 multiScatter = SampleMultiScatteringLut(msBuf, ri, muSun);
Vector3 sigS = sigR + sigM;
Vector3 phaseS = sigR * phaseR + sigM * phaseM;
Vector3 inScatter = sunVis * phaseS + multiScatter * sigS * phaseIso;
luminance += throughput * SafeScatterIntegral(inScatter, stepT, sigE, dt);
throughput *= stepT;
}
if (hitGround)
{
Vector3 gp = viewPos + rayDir * maxDist;
Vector3 gUp = gp.Normalized();
float nDotL = MathF.Max(0f, gUp.Dot(sunDir));
if (nDotL > 0f)
float maxDist = RaySphereExit(r0, rayDir.Y, out bool hitGround);
if (maxDist <= 0f)
{
Vector3 tSunG = SampleTransmittanceLut(transBuf, PlanetRadiusM, nDotL);
luminance += throughput * tSunG * GroundAlbedo * (nDotL / MathF.PI);
buf[y * SkyViewWidth + x] = Vector3.Zero;
continue;
}
}
buf[y * SkyViewWidth + x] = luminance * preset.SolarIrradiance;
float cosTheta = rayDir.Dot(sunDir);
float phaseR = RayleighPhase(cosTheta);
float phaseM = CornetteShanksPhase(cosTheta, preset.MieG);
const float phaseIso = 1f / (4f * MathF.PI);
float dt = maxDist / steps;
Vector3 throughput = Vector3.One;
Vector3 luminance = Vector3.Zero;
for (int s = 0; s < steps; s++)
{
float t = (s + 0.5f) * dt;
Vector3 p = viewPos + rayDir * t;
float ri = p.Length();
float hi = MathF.Max(0f, ri - PlanetRadiusM);
SampleRayleighMie(preset, hi, out Vector3 sigR, out Vector3 sigM, out Vector3 sigE);
Vector3 stepT = new(
MathF.Exp(-sigE.X * dt),
MathF.Exp(-sigE.Y * dt),
MathF.Exp(-sigE.Z * dt));
Vector3 up = p / MathF.Max(1f, ri);
float muSun = up.Dot(sunDir);
Vector3 sunVis = HitsPlanet(ri, muSun)
? Vector3.Zero
: SampleTransmittanceLut(transBuf, ri, muSun);
Vector3 multiScatter = SampleMultiScatteringLut(msBuf, ri, muSun);
Vector3 sigS = sigR + sigM;
Vector3 phaseS = sigR * phaseR + sigM * phaseM;
Vector3 inScatter = sunVis * phaseS + multiScatter * sigS * phaseIso;
luminance += throughput * SafeScatterIntegral(inScatter, stepT, sigE, dt);
throughput *= stepT;
}
if (hitGround)
{
Vector3 gp = viewPos + rayDir * maxDist;
Vector3 gUp = gp.Normalized();
float nDotL = MathF.Max(0f, gUp.Dot(sunDir));
if (nDotL > 0f)
{
Vector3 tSunG = SampleTransmittanceLut(transBuf, PlanetRadiusM, nDotL);
luminance += throughput * tSunG * horizonGroundAlbedo * (nDotL / MathF.PI);
}
}
Vector3 rad = luminance * preset.SolarIrradiance;
if (v < 0.5f)
{
// Smoothly bridge the -0.45 deg sphere-intersection cliff from exact horizon limb radiance
// into warm horizon haze / ground bounce over ~6 degrees below horizon.
Vector3 horizonLimb = buf[(SkyViewHeight / 2) * SkyViewWidth + x];
float dipDeg = MathF.Max(0f, (horizonElev - elev) * (180f / MathF.PI));
float sBlend = Math.Clamp(dipDeg / 6.0f, 0f, 1f);
sBlend = sBlend * sBlend * (3f - 2f * sBlend);
Vector3 warmBounce = new Vector3(
horizonLimb.X * (preset.MapKind == ArenaMapKind.Desert ? 0.92f : 0.75f),
horizonLimb.Y * (preset.MapKind == ArenaMapKind.Desert ? 0.80f : 0.75f),
horizonLimb.Z * (preset.MapKind == ArenaMapKind.Desert ? 0.65f : 0.78f));
Vector3 deepGround = rad.Lerp(warmBounce, 0.65f);
rad = horizonLimb.Lerp(deepGround, sBlend);
}
buf[y * SkyViewWidth + x] = rad;
}
}
}

View file

@ -209,7 +209,7 @@ public partial class TerrainRenderer : Node3D
public void UpdateTileLod(Vector3 camPos)
{
const float nearRadiusSq = 240f * 240f;
const float shadowRadiusSq = 220f * 220f;
const float shadowRadiusSq = 340f * 340f;
for (int tz = 0; tz < TerrainField.TileGridCount; tz++)
{
for (int tx = 0; tx < TerrainField.TileGridCount; tx++)

View file

@ -33,8 +33,8 @@ renderer/rendering_method="forward_plus"
renderer/rendering_method.mobile="forward_plus"
anti_aliasing/quality/use_taa=true
anti_aliasing/screen_space_roughness_limiter/enabled=true
lights_and_shadows/directional_shadow/size=2048
lights_and_shadows/directional_shadow/soft_shadow_filter_quality=2
lights_and_shadows/directional_shadow/size=4096
lights_and_shadows/directional_shadow/soft_shadow_filter_quality=3
environment/volumetric_fog/volume_size=64
environment/volumetric_fog/volume_depth=96
environment/screen_space_reflection/half_size=true

View file

@ -2010,7 +2010,20 @@ public partial class Smoke : Node
private void VerifyForwardPlusAtmosphere()
{
string method = RenderingServer.GetCurrentRenderingMethod();
Check(method == "forward_plus", $"Expected RenderingMethod 'forward_plus', got '{method}'");
var cmdArgs = OS.GetCmdlineArgs();
bool requestedDummy = false;
for (int i = 0; i < cmdArgs.Length; i++)
{
if (cmdArgs[i] == "--rendering-driver=dummy" ||
(cmdArgs[i] == "--rendering-driver" && i + 1 < cmdArgs.Length && cmdArgs[i + 1] == "dummy"))
{
requestedDummy = true;
break;
}
}
bool isHeadlessDummy = DisplayServer.GetName() == "headless" && requestedDummy;
Check(method == "forward_plus" || (isHeadlessDummy && method == "dummy"),
$"Expected RenderingMethod 'forward_plus' (or 'dummy' with --rendering-driver dummy), got '{method}'");
var sionBaked = AtmosphereLutBaker.GetOrBake(ArenaMapKind.Sion);
var desertBaked = AtmosphereLutBaker.GetOrBake(ArenaMapKind.Desert);
@ -2023,6 +2036,10 @@ public partial class Smoke : Node
Check(sionBaked.ZenithTransmittanceMean > desertBaked.ZenithTransmittanceMean,
$"Expected Sion zenith transmittance ({sionBaked.ZenithTransmittanceMean:F4}) > Desert ({desertBaked.ZenithTransmittanceMean:F4})");
Check(desertBaked.HazeColor.R > desertBaked.HazeColor.G && desertBaked.HazeColor.G > desertBaked.HazeColor.B,
$"Expected warm golden Desert haze chromaticity R > G > B, got ({desertBaked.HazeColor.R:F3}, {desertBaked.HazeColor.G:F3}, {desertBaked.HazeColor.B:F3})");
CheckSkyViewHorizonContinuity("Sion", sionBaked.SkyViewImage);
CheckSkyViewHorizonContinuity("Desert", desertBaked.SkyViewImage);
var atmo = _main.Atmosphere;
var env = atmo.Environment;
@ -2033,8 +2050,10 @@ public partial class Smoke : Node
Check(env.SsaoEnabled, "Expected SsaoEnabled == true");
Check(env.VolumetricFogEnabled && env.VolumetricFogTemporalReprojectionEnabled,
$"Expected VolumetricFogEnabled && TemporalReprojection, got vf={env.VolumetricFogEnabled} tr={env.VolumetricFogTemporalReprojectionEnabled}");
Check(env.FogEnabled && env.FogAerialPerspective > 0.7f,
$"Expected FogEnabled && FogAerialPerspective > 0.7f, got fog={env.FogEnabled} ap={env.FogAerialPerspective:F2}");
Check(env.FogEnabled && env.FogAerialPerspective >= 0.20f,
$"Expected FogEnabled && FogAerialPerspective >= 0.20f, got fog={env.FogEnabled} ap={env.FogAerialPerspective:F2}");
Check(sionBaked.Preset.DepthFogAerialPerspective > 0.7f,
$"Expected Sion DepthFogAerialPerspective > 0.7f, got {sionBaked.Preset.DepthFogAerialPerspective:F2}");
var sun = atmo.Sun;
Check(sun.DirectionalShadowMode == DirectionalLight3D.ShadowMode.Parallel4Splits && sun.DirectionalShadowMaxDistance >= 280f,
@ -2057,7 +2076,7 @@ public partial class Smoke : Node
Check(atmo.FilmGrainLayer.Layer == 5 && atmo.FilmGrainLayer.Layer < _main.Hud.Layer,
$"Expected FilmGrainLayer.Layer == 5 < Hud.Layer ({_main.Hud.Layer}), got {atmo.FilmGrainLayer.Layer}");
GD.Print($"SMOKE INFO: Cut 8 Forward+ verified (method={method}, sionZenithT={sionBaked.ZenithTransmittanceMean:F4}, desertZenithT={desertBaked.ZenithTransmittanceMean:F4}, grainLayer={atmo.FilmGrainLayer.Layer})");
GD.Print($"SMOKE INFO: Cut 8 Forward+ verified (method={method}, sionZenithT={sionBaked.ZenithTransmittanceMean:F4}, desertZenithT={desertBaked.ZenithTransmittanceMean:F4}, desertHaze=({desertBaked.HazeColor.R:F3},{desertBaked.HazeColor.G:F3},{desertBaked.HazeColor.B:F3}), sionHaze=({sionBaked.HazeColor.R:F3},{sionBaked.HazeColor.G:F3},{sionBaked.HazeColor.B:F3}), grainLayer={atmo.FilmGrainLayer.Layer})");
}
private void CheckFinitePositiveLut(string label, Image img, int expectedW, int expectedH)
@ -2085,6 +2104,24 @@ public partial class Smoke : Node
Check(allValid && maxSum > 0f, $"Expected finite positive values in {label} LUT (valid={allValid}, maxSum={maxSum:F4})");
}
private void CheckSkyViewHorizonContinuity(string label, Image skyView)
{
int yBelow = AtmosphereLutBaker.SkyViewHeight / 2 - 1;
int yAbove = AtmosphereLutBaker.SkyViewHeight / 2;
float worstRelJump = 0f;
for (int x = 0; x < skyView.GetWidth(); x += 8)
{
Color cBelow = skyView.GetPixel(x, yBelow);
Color cAbove = skyView.GetPixel(x, yAbove);
float lBelow = cBelow.R * 0.2126f + cBelow.G * 0.7152f + cBelow.B * 0.0722f;
float lAbove = cAbove.R * 0.2126f + cAbove.G * 0.7152f + cAbove.B * 0.0722f;
float rel = MathF.Abs(lAbove - lBelow) / MathF.Max(1e-4f, lAbove);
if (rel > worstRelJump) worstRelJump = rel;
}
Check(worstRelJump <= 0.05f,
$"Expected smooth below-horizon SkyView seam <= 5% on {label} (y={yBelow}->y={yAbove}), got {worstRelJump * 100f:F2}%");
}
private static int CountPremulViolations(Image img, float tolerance)
{
int count = 0;