684 lines
28 KiB
C#
684 lines
28 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Godot;
|
|
using RustToDust.Core;
|
|
|
|
namespace RustToDust.Client;
|
|
|
|
/// <summary>
|
|
/// Deterministic CPU precomputation of Bruneton / Hillaire planetary atmospheric scattering LUTs
|
|
/// (<c>Transmittance</c> 256x64, <c>MultiScattering</c> 32x32, <c>SkyView</c> 256x128 in
|
|
/// <see cref="Image.Format.Rgbah"/>), plus matching solar irradiance and horizon haze evaluation.
|
|
/// Zero Vulkan compute dependency so headless (<c>Dummy</c>) tests and Forward+ share identical data.
|
|
/// </summary>
|
|
public static class AtmosphereLutBaker
|
|
{
|
|
public const int TransmittanceWidth = 256;
|
|
public const int TransmittanceHeight = 64;
|
|
public const int MultiScatteringSize = 32;
|
|
public const int SkyViewWidth = 256;
|
|
public const int SkyViewHeight = 128;
|
|
|
|
public const float PlanetRadiusM = 6_360_000f;
|
|
public const float AtmosphereRadiusM = 6_460_000f;
|
|
public const float AtmosphereHeightM = AtmosphereRadiusM - PlanetRadiusM;
|
|
|
|
private const float RayleighScaleHeightM = 8_000f;
|
|
private static readonly Vector3 RayleighScattering = new(5.802e-6f, 13.558e-6f, 33.100e-6f);
|
|
private const float MieSingleScatterAlbedo = 0.90f;
|
|
private static readonly Vector3 OzoneAbsorption = new(0.650e-6f, 1.881e-6f, 0.085e-6f);
|
|
private static readonly Vector3 GroundAlbedo = new(0.28f, 0.24f, 0.20f);
|
|
|
|
public sealed record AtmospherePreset(
|
|
ArenaMapKind MapKind,
|
|
float AltitudeMeters,
|
|
float SunPitchDeg,
|
|
float SunYawDeg,
|
|
float MieScaleHeightMeters,
|
|
Vector3 MieScattering,
|
|
float MieG,
|
|
float SolarIrradiance,
|
|
float CalibratedSunEnergy,
|
|
float FogMaxLengthMeters,
|
|
float VolumetricFogDensity,
|
|
float VolumetricFogDetailSpread,
|
|
float VolumetricFogAnisotropy,
|
|
float VolumetricFogEmissionEnergy,
|
|
Vector3 ValleyVolumeSize,
|
|
Vector3 ValleyVolumePosition,
|
|
float ValleyVolumeDensity,
|
|
float ValleyVolumeHeightFalloff,
|
|
float ValleyVolumeEdgeFade,
|
|
float DepthFogBegin,
|
|
float DepthFogEnd,
|
|
float DepthFogCurve,
|
|
float DepthFogDensity,
|
|
float DepthFogAerialPerspective,
|
|
float DepthFogSunScatter,
|
|
float ShadowMaxDistance,
|
|
float AmbientLightEnergy,
|
|
float SunDiscIntensity,
|
|
float MieAureoleStrength);
|
|
|
|
public sealed class BakedAtmosphere
|
|
{
|
|
public required AtmospherePreset Preset { get; init; }
|
|
public required Image TransmittanceImage { get; init; }
|
|
public required Image MultiScatteringImage { get; init; }
|
|
public required Image SkyViewImage { get; init; }
|
|
public required ImageTexture TransmittanceTexture { get; init; }
|
|
public required ImageTexture MultiScatteringTexture { get; init; }
|
|
public required ImageTexture SkyViewTexture { get; init; }
|
|
public required Vector3 SunDirection { get; init; }
|
|
public required Vector3 SunTransmittance { get; init; }
|
|
public required Color SunLightColor { get; init; }
|
|
public required float SunLightEnergy { get; init; }
|
|
public required Color HazeColor { get; init; }
|
|
public required float ZenithTransmittanceMean { get; init; }
|
|
public required float HorizonElevationRad { get; init; }
|
|
}
|
|
|
|
private static readonly Dictionary<ArenaMapKind, BakedAtmosphere> Cache = new();
|
|
|
|
public static AtmospherePreset GetPreset(ArenaMapKind mapKind) => mapKind == ArenaMapKind.Sion
|
|
? new AtmospherePreset(
|
|
MapKind: ArenaMapKind.Sion,
|
|
AltitudeMeters: 490f,
|
|
SunPitchDeg: -36f,
|
|
SunYawDeg: 28f,
|
|
MieScaleHeightMeters: 1_200f,
|
|
MieScattering: new Vector3(3.2e-6f, 3.2e-6f, 3.2e-6f),
|
|
MieG: 0.78f,
|
|
SolarIrradiance: 17.5f,
|
|
CalibratedSunEnergy: 1.28f,
|
|
FogMaxLengthMeters: 460f,
|
|
VolumetricFogDensity: 0.0008f,
|
|
VolumetricFogDetailSpread: 2.4f,
|
|
VolumetricFogAnisotropy: 0.62f,
|
|
VolumetricFogEmissionEnergy: 0.18f,
|
|
ValleyVolumeSize: new Vector3(3100f, 110f, 3100f),
|
|
ValleyVolumePosition: new Vector3(0f, 20f, 0f),
|
|
ValleyVolumeDensity: 0.0038f,
|
|
ValleyVolumeHeightFalloff: 1.65f,
|
|
ValleyVolumeEdgeFade: 0.12f,
|
|
DepthFogBegin: 400f,
|
|
DepthFogEnd: 2950f,
|
|
DepthFogCurve: 0.85f,
|
|
DepthFogDensity: 0.36f,
|
|
DepthFogAerialPerspective: 0.88f,
|
|
DepthFogSunScatter: 0.45f,
|
|
ShadowMaxDistance: 320f,
|
|
AmbientLightEnergy: 0.48f,
|
|
SunDiscIntensity: 8.5f,
|
|
MieAureoleStrength: 0.24f)
|
|
: new AtmospherePreset(
|
|
MapKind: ArenaMapKind.Desert,
|
|
AltitudeMeters: 200f,
|
|
SunPitchDeg: -22f,
|
|
SunYawDeg: 36f,
|
|
MieScaleHeightMeters: 1_800f,
|
|
MieScattering: new Vector3(10.5e-6f, 8.8e-6f, 6.6e-6f),
|
|
MieG: 0.74f,
|
|
SolarIrradiance: 16.0f,
|
|
CalibratedSunEnergy: 1.42f,
|
|
FogMaxLengthMeters: 380f,
|
|
VolumetricFogDensity: 0.0012f,
|
|
VolumetricFogDetailSpread: 2.2f,
|
|
VolumetricFogAnisotropy: 0.68f,
|
|
VolumetricFogEmissionEnergy: 0.22f,
|
|
ValleyVolumeSize: new Vector3(1600f, 56f, 1600f),
|
|
ValleyVolumePosition: new Vector3(0f, 2f, 0f),
|
|
ValleyVolumeDensity: 0.0052f,
|
|
ValleyVolumeHeightFalloff: 1.85f,
|
|
ValleyVolumeEdgeFade: 0.15f,
|
|
DepthFogBegin: 260f,
|
|
DepthFogEnd: 840f,
|
|
DepthFogCurve: 0.85f,
|
|
DepthFogDensity: 0.84f,
|
|
DepthFogAerialPerspective: 0.22f,
|
|
DepthFogSunScatter: 0.65f,
|
|
ShadowMaxDistance: 280f,
|
|
AmbientLightEnergy: 0.55f,
|
|
SunDiscIntensity: 8.0f,
|
|
MieAureoleStrength: 0.30f);
|
|
|
|
public static BakedAtmosphere GetOrBake(ArenaMapKind mapKind)
|
|
{
|
|
if (Cache.TryGetValue(mapKind, out var cached))
|
|
{
|
|
return cached;
|
|
}
|
|
|
|
var baked = BakePreset(GetPreset(mapKind));
|
|
Cache[mapKind] = baked;
|
|
return baked;
|
|
}
|
|
|
|
public static Vector3 ComputeSunDirection(float pitchDeg, float yawDeg)
|
|
{
|
|
Basis basis = Basis.FromEuler(new Vector3(Mathf.DegToRad(pitchDeg), Mathf.DegToRad(yawDeg), 0f), EulerOrder.Yxz);
|
|
// DirectionalLight3D shines along -Z; direction toward the sun is +Z.
|
|
return basis.Z.Normalized();
|
|
}
|
|
|
|
private static BakedAtmosphere BakePreset(AtmospherePreset preset)
|
|
{
|
|
Vector3[] transmittanceBuf = BakeTransmittanceBuffer(preset);
|
|
Vector3[] multiScatterBuf = BakeMultiScatteringBuffer(preset, transmittanceBuf);
|
|
Vector3 sunDir = ComputeSunDirection(preset.SunPitchDeg, preset.SunYawDeg);
|
|
float r0 = PlanetRadiusM + preset.AltitudeMeters;
|
|
float horizonElev = -MathF.Acos(Math.Clamp(PlanetRadiusM / r0, -1f, 1f));
|
|
Vector3[] skyViewBuf = BakeSkyViewBuffer(preset, transmittanceBuf, multiScatterBuf, sunDir, r0, horizonElev);
|
|
|
|
Image transImg = CreateRgbahImage(TransmittanceWidth, TransmittanceHeight, transmittanceBuf);
|
|
Image msImg = CreateRgbahImage(MultiScatteringSize, MultiScatteringSize, multiScatterBuf);
|
|
Image skyImg = CreateRgbahImage(SkyViewWidth, SkyViewHeight, skyViewBuf);
|
|
|
|
Vector3 sunT = SampleTransmittanceLut(transmittanceBuf, r0, sunDir.Y);
|
|
Vector3 zenithT = SampleTransmittanceLut(transmittanceBuf, r0, 1.0f);
|
|
float zenithMean = (zenithT.X + zenithT.Y + zenithT.Z) / 3f;
|
|
|
|
Color sunColor = DeriveSunLightColor(sunT);
|
|
float sunEnergy = DeriveSunLightEnergy(preset, sunT);
|
|
Color hazeColor = DeriveHorizonHazeColor(preset, skyViewBuf, sunT);
|
|
|
|
return new BakedAtmosphere
|
|
{
|
|
Preset = preset,
|
|
TransmittanceImage = transImg,
|
|
MultiScatteringImage = msImg,
|
|
SkyViewImage = skyImg,
|
|
TransmittanceTexture = ImageTexture.CreateFromImage(transImg),
|
|
MultiScatteringTexture = ImageTexture.CreateFromImage(msImg),
|
|
SkyViewTexture = ImageTexture.CreateFromImage(skyImg),
|
|
SunDirection = sunDir,
|
|
SunTransmittance = sunT,
|
|
SunLightColor = sunColor,
|
|
SunLightEnergy = sunEnergy,
|
|
HazeColor = hazeColor,
|
|
ZenithTransmittanceMean = zenithMean,
|
|
HorizonElevationRad = horizonElev
|
|
};
|
|
}
|
|
|
|
private static Color DeriveSunLightColor(Vector3 sunT)
|
|
{
|
|
float maxC = MathF.Max(1e-4f, MathF.Max(sunT.X, MathF.Max(sunT.Y, sunT.Z)));
|
|
Vector3 norm = sunT / maxC;
|
|
return new Color(
|
|
Math.Clamp(norm.X, 0.1f, 1.0f),
|
|
Math.Clamp(norm.Y, 0.1f, 1.0f),
|
|
Math.Clamp(norm.Z, 0.1f, 1.0f),
|
|
1.0f);
|
|
}
|
|
|
|
private static float DeriveSunLightEnergy(AtmospherePreset preset, Vector3 sunT)
|
|
{
|
|
float luma = sunT.X * 0.2126f + sunT.Y * 0.7152f + sunT.Z * 0.0722f;
|
|
float refLuma = preset.MapKind == ArenaMapKind.Sion ? 0.86f : 0.68f;
|
|
float ratio = Math.Clamp(luma / refLuma, 0.92f, 1.08f);
|
|
return preset.CalibratedSunEnergy * ratio;
|
|
}
|
|
|
|
private static Color DeriveHorizonHazeColor(AtmospherePreset preset, Vector3[] skyViewBuf, Vector3 sunT)
|
|
{
|
|
// 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;
|
|
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++)
|
|
{
|
|
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 = 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.18f, 0.90f),
|
|
Math.Clamp(norm.Y, 0.18f, 0.90f),
|
|
Math.Clamp(norm.Z, 0.18f, 0.90f),
|
|
1.0f);
|
|
}
|
|
|
|
private static Vector3[] BakeTransmittanceBuffer(AtmospherePreset preset)
|
|
{
|
|
var buf = new Vector3[TransmittanceWidth * TransmittanceHeight];
|
|
float H = MathF.Sqrt(AtmosphereRadiusM * AtmosphereRadiusM - PlanetRadiusM * PlanetRadiusM);
|
|
const int steps = 36;
|
|
|
|
for (int y = 0; y < TransmittanceHeight; y++)
|
|
{
|
|
float v = (y + 0.5f) / TransmittanceHeight;
|
|
float rho = H * v;
|
|
float r = MathF.Sqrt(rho * rho + PlanetRadiusM * PlanetRadiusM);
|
|
float dMin = AtmosphereRadiusM - r;
|
|
float dMax = rho + H;
|
|
|
|
for (int x = 0; x < TransmittanceWidth; x++)
|
|
{
|
|
float u = (x + 0.5f) / TransmittanceWidth;
|
|
float d = dMin + u * (dMax - dMin);
|
|
float mu = d <= 1e-3f ? 1f : Math.Clamp((H * H - rho * rho - d * d) / (2f * r * d), -1f, 1f);
|
|
|
|
float stepLen = d / steps;
|
|
Vector3 opticalDepth = Vector3.Zero;
|
|
for (int i = 0; i < steps; i++)
|
|
{
|
|
float t = (i + 0.5f) * stepLen;
|
|
float ri = MathF.Sqrt(MathF.Max(PlanetRadiusM * PlanetRadiusM, r * r + t * t + 2f * r * t * mu));
|
|
float hi = MathF.Max(0f, ri - PlanetRadiusM);
|
|
opticalDepth += EvaluateExtinction(preset, hi) * stepLen;
|
|
}
|
|
|
|
buf[y * TransmittanceWidth + x] = new Vector3(
|
|
MathF.Exp(-opticalDepth.X),
|
|
MathF.Exp(-opticalDepth.Y),
|
|
MathF.Exp(-opticalDepth.Z));
|
|
}
|
|
}
|
|
|
|
return buf;
|
|
}
|
|
|
|
private static Vector3[] BakeMultiScatteringBuffer(AtmospherePreset preset, Vector3[] transBuf)
|
|
{
|
|
var buf = new Vector3[MultiScatteringSize * MultiScatteringSize];
|
|
const int dirCount = 64;
|
|
const int marchSteps = 20;
|
|
const float goldenRatio = 1.618033988749895f;
|
|
|
|
Vector3[] dirs = new Vector3[dirCount];
|
|
for (int i = 0; i < dirCount; i++)
|
|
{
|
|
float theta = 2f * MathF.PI * i / goldenRatio;
|
|
float z = 1f - 2f * (i + 0.5f) / dirCount;
|
|
float rxy = MathF.Sqrt(MathF.Max(0f, 1f - z * z));
|
|
dirs[i] = new Vector3(rxy * MathF.Cos(theta), z, rxy * MathF.Sin(theta));
|
|
}
|
|
|
|
for (int y = 0; y < MultiScatteringSize; y++)
|
|
{
|
|
float v = (y + 0.5f) / MultiScatteringSize;
|
|
float r = PlanetRadiusM + v * AtmosphereHeightM;
|
|
Vector3 pos = new(0f, r, 0f);
|
|
|
|
for (int x = 0; x < MultiScatteringSize; x++)
|
|
{
|
|
float u = (x + 0.5f) / MultiScatteringSize;
|
|
float muS = u * 2f - 1f;
|
|
Vector3 sunDir = new(MathF.Sqrt(MathF.Max(0f, 1f - muS * muS)), muS, 0f);
|
|
|
|
Vector3 l2Sum = Vector3.Zero;
|
|
Vector3 fmsSum = Vector3.Zero;
|
|
|
|
for (int d = 0; d < dirCount; d++)
|
|
{
|
|
Vector3 rayDir = dirs[d];
|
|
float maxDist = RaySphereExit(r, rayDir.Y, out bool hitGround);
|
|
if (maxDist <= 0f)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float dt = maxDist / marchSteps;
|
|
Vector3 throughput = Vector3.One;
|
|
Vector3 l2 = Vector3.Zero;
|
|
Vector3 fms = Vector3.Zero;
|
|
|
|
for (int s = 0; s < marchSteps; s++)
|
|
{
|
|
float t = (s + 0.5f) * dt;
|
|
Vector3 p = pos + rayDir * t;
|
|
float ri = p.Length();
|
|
float hi = MathF.Max(0f, ri - PlanetRadiusM);
|
|
|
|
SampleMedia(preset, hi, out Vector3 sigmaS, out Vector3 sigmaE);
|
|
Vector3 stepT = new(
|
|
MathF.Exp(-sigmaE.X * dt),
|
|
MathF.Exp(-sigmaE.Y * dt),
|
|
MathF.Exp(-sigmaE.Z * dt));
|
|
|
|
Vector3 up = p / MathF.Max(1f, ri);
|
|
float muSunAtP = up.Dot(sunDir);
|
|
if (!HitsPlanet(ri, muSunAtP))
|
|
{
|
|
Vector3 tSun = SampleTransmittanceLut(transBuf, ri, muSunAtP);
|
|
// 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;
|
|
}
|
|
|
|
Vector3 fmsInt = SafeScatterIntegral(sigmaS, stepT, sigmaE, dt);
|
|
fms += throughput * fmsInt;
|
|
throughput *= stepT;
|
|
}
|
|
|
|
if (hitGround)
|
|
{
|
|
Vector3 gp = pos + rayDir * maxDist;
|
|
Vector3 gUp = gp.Normalized();
|
|
float nDotL = MathF.Max(0f, gUp.Dot(sunDir));
|
|
if (nDotL > 0f)
|
|
{
|
|
Vector3 tGroundSun = SampleTransmittanceLut(transBuf, PlanetRadiusM, nDotL);
|
|
l2 += throughput * tGroundSun * GroundAlbedo * (nDotL / MathF.PI);
|
|
}
|
|
}
|
|
|
|
l2Sum += l2;
|
|
fmsSum += fms;
|
|
}
|
|
|
|
Vector3 l2Avg = l2Sum / dirCount;
|
|
Vector3 fmsAvg = fmsSum / dirCount;
|
|
Vector3 psi = new(
|
|
l2Avg.X / MathF.Max(0.02f, 1f - MathF.Min(0.98f, fmsAvg.X)),
|
|
l2Avg.Y / MathF.Max(0.02f, 1f - MathF.Min(0.98f, fmsAvg.Y)),
|
|
l2Avg.Z / MathF.Max(0.02f, 1f - MathF.Min(0.98f, fmsAvg.Z)));
|
|
buf[y * MultiScatteringSize + x] = psi;
|
|
}
|
|
}
|
|
|
|
return buf;
|
|
}
|
|
|
|
private static Vector3[] BakeSkyViewBuffer(
|
|
AtmospherePreset preset,
|
|
Vector3[] transBuf,
|
|
Vector3[] msBuf,
|
|
Vector3 sunDir,
|
|
float r0,
|
|
float horizonElev)
|
|
{
|
|
var buf = new Vector3[SkyViewWidth * SkyViewHeight];
|
|
const int steps = 28;
|
|
Vector3 viewPos = new(0f, r0, 0f);
|
|
|
|
Vector2 sunXz = new(sunDir.X, sunDir.Z);
|
|
if (sunXz.LengthSquared() < 1e-6f)
|
|
{
|
|
sunXz = new Vector2(0f, 1f);
|
|
}
|
|
else
|
|
{
|
|
sunXz = sunXz.Normalized();
|
|
}
|
|
Vector2 sunPerp = new(sunXz.Y, -sunXz.X);
|
|
|
|
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++)
|
|
{
|
|
int yBegin = pass == 0 ? SkyViewHeight / 2 : 0;
|
|
int yFinish = pass == 0 ? SkyViewHeight : SkyViewHeight / 2;
|
|
|
|
for (int y = yBegin; y < yFinish; y++)
|
|
{
|
|
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);
|
|
}
|
|
|
|
float cosElev = MathF.Cos(elev);
|
|
float sinElev = MathF.Sin(elev);
|
|
|
|
for (int x = 0; x < SkyViewWidth; x++)
|
|
{
|
|
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)
|
|
{
|
|
buf[y * SkyViewWidth + x] = Vector3.Zero;
|
|
continue;
|
|
}
|
|
|
|
float cosTheta = rayDir.Dot(sunDir);
|
|
float phaseR = RayleighPhase(cosTheta);
|
|
float phaseM = CornetteShanksPhase(cosTheta, preset.MieG);
|
|
|
|
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;
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
return buf;
|
|
}
|
|
|
|
private static Vector3 EvaluateExtinction(AtmospherePreset preset, float altitudeM)
|
|
{
|
|
SampleMedia(preset, altitudeM, out _, out Vector3 sigmaE);
|
|
return sigmaE;
|
|
}
|
|
|
|
private static void SampleMedia(AtmospherePreset preset, float altitudeM, out Vector3 sigmaS, out Vector3 sigmaE)
|
|
{
|
|
SampleRayleighMie(preset, altitudeM, out Vector3 sigR, out Vector3 sigM, out sigmaE);
|
|
sigmaS = sigR + sigM;
|
|
}
|
|
|
|
private static void SampleRayleighMie(
|
|
AtmospherePreset preset,
|
|
float altitudeM,
|
|
out Vector3 sigR,
|
|
out Vector3 sigM,
|
|
out Vector3 sigE)
|
|
{
|
|
float h = MathF.Max(0f, altitudeM);
|
|
float dR = MathF.Exp(-h / RayleighScaleHeightM);
|
|
float dM = MathF.Exp(-h / preset.MieScaleHeightMeters);
|
|
float dO = MathF.Max(0f, 1f - MathF.Abs(h - 25_000f) / 15_000f);
|
|
|
|
sigR = RayleighScattering * dR;
|
|
sigM = preset.MieScattering * dM;
|
|
Vector3 extM = sigM / MieSingleScatterAlbedo;
|
|
Vector3 absO = OzoneAbsorption * dO;
|
|
sigE = sigR + extM + absO;
|
|
}
|
|
|
|
private static float RayleighPhase(float cosTheta)
|
|
{
|
|
return (3f / (16f * MathF.PI)) * (1f + cosTheta * cosTheta);
|
|
}
|
|
|
|
private static float CornetteShanksPhase(float cosTheta, float g)
|
|
{
|
|
float g2 = g * g;
|
|
float num = 3f * (1f - g2) * (1f + cosTheta * cosTheta);
|
|
float denom = 8f * MathF.PI * (2f + g2) * MathF.Pow(MathF.Max(1e-4f, 1f + g2 - 2f * g * cosTheta), 1.5f);
|
|
return num / denom;
|
|
}
|
|
|
|
private static Vector3 SafeScatterIntegral(Vector3 inScatter, Vector3 stepT, Vector3 sigmaE, float dt)
|
|
{
|
|
return new Vector3(
|
|
sigmaE.X > 1e-8f ? inScatter.X * (1f - stepT.X) / sigmaE.X : inScatter.X * dt,
|
|
sigmaE.Y > 1e-8f ? inScatter.Y * (1f - stepT.Y) / sigmaE.Y : inScatter.Y * dt,
|
|
sigmaE.Z > 1e-8f ? inScatter.Z * (1f - stepT.Z) / sigmaE.Z : inScatter.Z * dt);
|
|
}
|
|
|
|
private static bool HitsPlanet(float r, float mu)
|
|
{
|
|
float disc = r * r * (mu * mu - 1f) + PlanetRadiusM * PlanetRadiusM;
|
|
return mu < 0f && disc >= 0f;
|
|
}
|
|
|
|
private static float RaySphereExit(float r, float mu, out bool hitGround)
|
|
{
|
|
float discGround = r * r * (mu * mu - 1f) + PlanetRadiusM * PlanetRadiusM;
|
|
if (mu < 0f && discGround >= 0f)
|
|
{
|
|
hitGround = true;
|
|
return MathF.Max(0f, -r * mu - MathF.Sqrt(MathF.Max(0f, discGround)));
|
|
}
|
|
|
|
hitGround = false;
|
|
float discAtmo = r * r * (mu * mu - 1f) + AtmosphereRadiusM * AtmosphereRadiusM;
|
|
return MathF.Max(0f, -r * mu + MathF.Sqrt(MathF.Max(0f, discAtmo)));
|
|
}
|
|
|
|
private static Vector3 SampleTransmittanceLut(Vector3[] buf, float r, float mu)
|
|
{
|
|
float clampedR = Math.Clamp(r, PlanetRadiusM, AtmosphereRadiusM);
|
|
float H = MathF.Sqrt(AtmosphereRadiusM * AtmosphereRadiusM - PlanetRadiusM * PlanetRadiusM);
|
|
float rho = MathF.Sqrt(MathF.Max(0f, clampedR * clampedR - PlanetRadiusM * PlanetRadiusM));
|
|
float v = rho / H;
|
|
|
|
float disc = clampedR * clampedR * (mu * mu - 1f) + AtmosphereRadiusM * AtmosphereRadiusM;
|
|
float d = MathF.Max(0f, -clampedR * mu + MathF.Sqrt(MathF.Max(0f, disc)));
|
|
float dMin = AtmosphereRadiusM - clampedR;
|
|
float dMax = rho + H;
|
|
float u = (d - dMin) / MathF.Max(1e-4f, dMax - dMin);
|
|
|
|
return SampleBilinear(buf, TransmittanceWidth, TransmittanceHeight, u, v);
|
|
}
|
|
|
|
private static Vector3 SampleMultiScatteringLut(Vector3[] buf, float r, float muS)
|
|
{
|
|
float u = 0.5f + 0.5f * Math.Clamp(muS, -1f, 1f);
|
|
float v = Math.Clamp((r - PlanetRadiusM) / AtmosphereHeightM, 0f, 1f);
|
|
return SampleBilinear(buf, MultiScatteringSize, MultiScatteringSize, u, v);
|
|
}
|
|
|
|
private static Vector3 SampleBilinear(Vector3[] buf, int w, int h, float u, float v)
|
|
{
|
|
float fx = Math.Clamp(u, 0f, 1f) * w - 0.5f;
|
|
float fy = Math.Clamp(v, 0f, 1f) * h - 0.5f;
|
|
int x0 = Math.Clamp((int)MathF.Floor(fx), 0, w - 1);
|
|
int y0 = Math.Clamp((int)MathF.Floor(fy), 0, h - 1);
|
|
int x1 = Math.Min(x0 + 1, w - 1);
|
|
int y1 = Math.Min(y0 + 1, h - 1);
|
|
float tx = Math.Clamp(fx - x0, 0f, 1f);
|
|
float ty = Math.Clamp(fy - y0, 0f, 1f);
|
|
|
|
Vector3 c00 = buf[y0 * w + x0];
|
|
Vector3 c10 = buf[y0 * w + x1];
|
|
Vector3 c01 = buf[y1 * w + x0];
|
|
Vector3 c11 = buf[y1 * w + x1];
|
|
Vector3 c0 = c00.Lerp(c10, tx);
|
|
Vector3 c1 = c01.Lerp(c11, tx);
|
|
return c0.Lerp(c1, ty);
|
|
}
|
|
|
|
private static Image CreateRgbahImage(int w, int h, Vector3[] pixels)
|
|
{
|
|
Image img = Image.CreateEmpty(w, h, false, Image.Format.Rgbah);
|
|
for (int y = 0; y < h; y++)
|
|
{
|
|
int row = y * w;
|
|
for (int x = 0; x < w; x++)
|
|
{
|
|
Vector3 c = pixels[row + x];
|
|
img.SetPixel(x, y, new Color(
|
|
MathF.Max(0f, c.X),
|
|
MathF.Max(0f, c.Y),
|
|
MathF.Max(0f, c.Z),
|
|
1.0f));
|
|
}
|
|
}
|
|
return img;
|
|
}
|
|
}
|