Use emissive surface fog lighting

This commit is contained in:
Meta Vens 2026-05-10 08:16:15 +01:00
commit 5a7f67a349
5 changed files with 123 additions and 25 deletions

View file

@ -41,6 +41,10 @@ static const int FIELD_FLAG_CLOUD = 2;
static const int MEDIUM_FROXEL_ATLAS_COLUMNS = 8;
static const int MEDIUM_FROXEL_ATLAS_ROWS = 4;
static const int MEDIUM_FROXEL_SLICE_COUNT = MEDIUM_FROXEL_ATLAS_COLUMNS * MEDIUM_FROXEL_ATLAS_ROWS;
static const float PI = 3.14159265359;
static const float SELF_RADIUS = 1.12;
static const float3 SELF_CENTER = float3(0.0, 0.0, 2.2);
static const float3 SELF_EMISSIVE_RADIANCE = float3(10.0, 8.7, 4.2);
struct VertexOut
{
@ -187,10 +191,8 @@ float terrainHeight(float2 p)
return gridHeightTexture.SampleLevel(sourceSampler, saturate(gridUv(p)), 0.0).r;
}
float gridFogDensity(float3 p, out float3 scattering)
float gridFogDensity(float3 p)
{
scattering = 0.0;
float2 local = gridLocal(p.xy);
float radialFade = 1.0 - smoothstep(0.96, 1.12, length(local));
float depthBelowGrid = terrainHeight(p.xy) - p.z;
@ -211,10 +213,17 @@ float gridFogDensity(float3 p, out float3 scattering)
float high = triNoise3d(domain * 7.4 - warp * 0.35 - flow.yzx);
float textureWeight = saturate(0.58 + low * 1.42 - high * 0.52);
float deepening = 1.0 - exp(-max(depthBelowGrid, 0.0) * 1.45);
float density = saturate(radialFade * depthRamp * lerp(0.42, 1.0, deepening) * lerp(0.72, 1.55, textureWeight));
return saturate(radialFade * depthRamp * lerp(0.42, 1.0, deepening) * lerp(0.72, 1.55, textureWeight));
}
scattering = float3(0.58, 0.68, 0.24) * density * lerp(0.34, 0.72, textureWeight);
return density;
float gridFogExtinctionApprox(float3 p)
{
float2 local = gridLocal(p.xy);
float radialFade = 1.0 - smoothstep(0.96, 1.12, length(local));
float depthBelowGrid = terrainHeight(p.xy) - p.z;
float depthRamp = smoothstep(0.035, 0.72, depthBelowGrid);
float deepening = 1.0 - exp(-max(depthBelowGrid, 0.0) * 1.45);
return saturate(radialFade * depthRamp * lerp(0.42, 1.0, deepening));
}
float2 rotate2(float2 value, float angle)
@ -238,15 +247,9 @@ float fieldDistance(float3 p, FieldInstance field)
return ellipsoidSdf(local, max(field.radiusAngle.xyz, 0.001));
}
float registeredMediumDensity(float3 p, out float3 scattering)
float registeredMediumDensityOnly(float3 p)
{
float density = 0.0;
scattering = 0.0;
float3 gridFogScattering;
float gridDensity = gridFogDensity(p, gridFogScattering);
density += gridDensity;
scattering += gridFogScattering;
float density = gridFogDensity(p);
[unroll]
for (int i = 0; i < FIELD_INSTANCE_COUNT; i++)
@ -269,14 +272,76 @@ float registeredMediumDensity(float3 p, out float3 scattering)
local /= max(field.radiusAngle.xyz, 0.001);
float erosion = saturate(0.86 + fbm3(local * 3.4) * 0.14);
float core = 1.0 - smoothstep(0.80, 1.05, length(local));
float fieldDensity = shell * core * erosion * field.mediumTerms.w;
density += fieldDensity;
scattering += field.colorIntensity.rgb * fieldDensity * field.mediumTerms.y;
density += shell * core * erosion * field.mediumTerms.w;
}
return saturate(density);
}
float emissiveSphereSolidAngle(float3 p)
{
float3 toEmitter = SELF_CENTER - p;
float distanceToEmitter = max(length(toEmitter), SELF_RADIUS + 0.001);
float sinTheta = saturate(SELF_RADIUS / distanceToEmitter);
float cosTheta = sqrt(saturate(1.0 - sinTheta * sinTheta));
return 2.0 * PI * (1.0 - cosTheta);
}
float henyeyGreenstein(float cosTheta, float g)
{
float g2 = g * g;
return (1.0 - g2) / max(4.0 * PI * pow(1.0 + g2 - 2.0 * g * cosTheta, 1.5), 0.0001);
}
float emissiveSurfaceTransmittance(float3 p)
{
float3 toEmitter = SELF_CENTER - p;
float distanceToEmitter = length(toEmitter);
float pathLength = max(distanceToEmitter - SELF_RADIUS, 0.0);
if (pathLength <= 0.001)
{
return 1.0;
}
float3 lightDirection = toEmitter / max(distanceToEmitter, 0.0001);
float opticalDepth = 0.0;
[loop]
for (int i = 0; i < 4; i++)
{
float travel = ((float)i + 0.5) * pathLength / 4.0;
opticalDepth += gridFogExtinctionApprox(p + lightDirection * travel);
}
opticalDepth *= (pathLength / 4.0) * 0.16;
return exp(-opticalDepth);
}
float3 emissiveSurfaceScattering(float3 p, float3 rayDirection, float density)
{
if (density <= 0.0)
{
return 0.0;
}
float3 toEmitter = SELF_CENTER - p;
float3 lightDirection = normalize(toEmitter);
float solidAngle = emissiveSphereSolidAngle(p);
float cosTheta = dot(-rayDirection, lightDirection);
float phase = henyeyGreenstein(cosTheta, 0.32);
float visibility = smoothstep(SELF_RADIUS * 0.98, SELF_RADIUS * 1.08, length(toEmitter));
float transmittance = emissiveSurfaceTransmittance(p);
return SELF_EMISSIVE_RADIANCE * solidAngle * phase * density * visibility * transmittance;
}
float registeredMediumDensity(float3 p, float3 rayDirection, out float3 scattering)
{
scattering = 0.0;
float density = registeredMediumDensityOnly(p);
scattering = emissiveSurfaceScattering(p, rayDirection, density);
return density;
}
float mediumSliceTravel(int sliceIndex)
{
float t = ((float)sliceIndex + 0.5) / (float)MEDIUM_FROXEL_SLICE_COUNT;
@ -297,7 +362,7 @@ MediumVolumeOut MediumVolumePS(VertexOut input)
float travel = mediumSliceTravel(sliceIndex);
float3 p = cameraPosition + rayDirection * travel;
float3 scattering;
float density = registeredMediumDensity(p, scattering);
float density = registeredMediumDensity(p, rayDirection, scattering);
float mediumBlend = saturate(mediumCompositeIntensity);
density *= mediumBlend;
scattering *= mediumBlend;

View file

@ -55,6 +55,8 @@ static const float TERRAIN_ISOLINE_PIXEL_WIDTH = 0.54;
static const float TERRAIN_FIELD_LINE_PIXEL_WIDTH = 0.38;
static const float3 GRID_COLOR = float3(0.30, 0.90, 0.82);
static const float GRID_ALPHA_SCALE = 0.56;
static const float3 SELF_CENTER = float3(0.0, 0.0, 2.2);
static const float3 SELF_EMISSIVE_RADIANCE = float3(10.0, 8.7, 4.2);
struct VertexOut
{
@ -711,27 +713,47 @@ RayMarchResult traverseRay(float2 uv, float2 screenUv, float3 origin, float3 dir
return result;
}
float emissiveSphereSolidAngle(float3 p, float radius)
{
float distanceToEmitter = max(length(SELF_CENTER - p), radius + 0.001);
float sinTheta = saturate(radius / distanceToEmitter);
float cosTheta = sqrt(saturate(1.0 - sinTheta * sinTheta));
return 2.0 * PI * (1.0 - cosTheta);
}
float3 emissiveSphereIrradiance(float3 p, float3 normal, float radius, out float3 lightDirection, out float solidAngle)
{
float3 toEmitter = SELF_CENTER - p;
lightDirection = normalize(toEmitter);
solidAngle = emissiveSphereSolidAngle(p, radius);
float surfaceFacing = saturate(dot(normal, lightDirection));
float emitterFacing = smoothstep(radius * 0.98, radius * 1.08, length(toEmitter));
return SELF_EMISSIVE_RADIANCE * solidAngle * surfaceFacing * emitterFacing;
}
float3 shadeBody(float3 p, float3 normal, int primitiveId)
{
float3 self = float3(0.0, 0.0, 2.2);
float3 lightDirection = normalize(self - p);
float ndl = saturate(dot(normal, lightDirection));
if (primitiveId == 0)
{
return float3(10.0, 8.7, 4.2);
return SELF_EMISSIVE_RADIANCE;
}
float3 lightDirection;
float solidAngle;
float3 irradiance = emissiveSphereIrradiance(p, normal, SUN_RADIUS, lightDirection, solidAngle);
float irradianceLuma = dot(irradiance, float3(0.2126, 0.7152, 0.0722));
if (primitiveId == CURSOR_PRIMITIVE_ID)
{
static const float MinimumRoughness = 0.045;
static const float CursorRoughness = 0.22;
static const float SelfRadiance = 18.0;
float3 viewDirection = normalize(cameraPosition - p);
float3 halfVector = normalize(lightDirection + viewDirection);
float ndv = saturate(dot(normal, viewDirection));
float ndh = saturate(dot(normal, halfVector));
float vdh = saturate(dot(viewDirection, halfVector));
float ndl = saturate(dot(normal, lightDirection));
float roughness = max(CursorRoughness, MinimumRoughness);
float alpha = roughness * roughness;
float alpha2 = alpha * alpha;
@ -745,12 +767,12 @@ float3 shadeBody(float3 p, float3 normal, int primitiveId)
float3 f0 = float3(0.95, 0.62, 0.26);
float3 fresnel = f0 + (1.0 - f0) * pow(1.0 - vdh, 5.0);
float3 specular = (distribution * geometry) * fresnel / max(4.0 * ndl * ndv, 0.00001);
return specular * ndl * SelfRadiance;
return specular * irradiance;
}
float hue = hash21(float2(primitiveId, 6.3));
float3 albedo = lerp(float3(0.34, 0.42, 0.18), float3(0.70, 0.76, 0.42), hue);
return albedo * (ndl * 1.68);
return albedo * irradianceLuma / PI;
}
SceneOut D3D12ScenePS(VertexOut input)

View file

@ -127,3 +127,4 @@
{"timestamp":"2026-05-10T07:47:46.8566891+01:00","topic":"d3d12-cursor-mathworld-teardrop","lesson":"Use the actual MathWorld teardrop curve for the cursor profile instead of hand-sculpted bulb/taper pieces. With x=cos(t) and y=sin(t)*sin^m(t/2), revolving around the x axis gives radius `sqrt(1 - x^2) * ((1 - x) / 2)^(m/2)`. The current shader uses m=6, avoiding fractional pow while giving the higher-exponent teardrop silhouette.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","state/memory.json","state/scratch.md"],"source":"https://mathworld.wolfram.com/TeardropCurve.html"}
{"timestamp":"2026-05-10T07:56:15.1955843+01:00","topic":"d3d12-cursor-ggx-metal","lesson":"Cursor brass now uses a compact GGX microfacet metal model instead of Blinn-style tinted specular. It has brass F0, Schlick Fresnel, GGX distribution, Smith-Schlick visibility, no diffuse metal term, no glow, and no ambient floor. Until a reflection source exists, Self is the only direct radiance term.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","state/memory.json","state/scratch.md"]}
{"timestamp":"2026-05-10T08:06:35+01:00","topic":"grid-fog-volume","lesson":"Below-Grid pea-soup fog now enters the D3D12 medium path as real density/scattering. The medium pass and post ray-density debug bind the Grid height target at t22, use the actual height surface as the fog ceiling, and apply cheap Aetheria-style triangular-noise domain warping so final/debug views share the same procedural medium term.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl","state/memory.json","state/scratch.md"]}
{"timestamp":"2026-05-10T08:15:20+01:00","topic":"emissive-only-fog-lighting","lesson":"D3D12 fog density no longer contains baked scattering color, and solid shading no longer uses a point-light radiance constant. Self is treated as a finite emissive sphere: medium in-scattering uses solid angle, Henyey-Greenstein phase, and cheap light-path transmittance; surfaces use emissive-sphere irradiance. Full noisy density inside the light-path propagation loop stalled shader pipeline compilation, so propagation uses a cheap extinction approximation while visible samples keep textured density.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl","src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","state/memory.json","state/scratch.md"]}

View file

@ -52,6 +52,7 @@
"HDR/PBR presentation should follow research/rendering/hdr-pbr-presentation.md: scene-linear lighting, explicit exposure, pre-tonemap bloom/veil, then a display transform. Do not reach for 2000s threshold bloom when the frame needs dynamic range.",
"Aquarium's current HDR presentation pass uses fixed manual exposure plus a three-level half/quarter/eighth pre-tonemap bloom pyramid with firefly-safe downsampling and separable blur. Next HDR work is persisted exposure/display controls, not threshold glow.",
"Future volumetric lighting should be a field sampled consistently by terrain, planets, fog, particles, and diegetic UI. Do not fake only the planet term if the scene is supposed to bounce light.",
"Volumetric density must not self-illuminate through baked color. D3D12 direct fog lighting currently treats Self as a finite emissive sphere: solid angle plus phase for in-scattering, with cheap depth/radial light-path attenuation so shader compilation stays sane. Do not reintroduce point-light constants or ambient fog glow.",
"Volumetric design must start from research/volumetrics/synthesis.md. The first pass should follow Wronski/Hillaire production shape: explicit low-resolution frustum volume, injected density/extinction/scattering, Self as first emitter, front-to-back transmittance/in-scattering integration, then consistent composition.",
"For GPU SDF or newer volumetric storage, consult research/volumetrics/recent-volumetrics-and-gpu-sdf.md. The durable pattern is cascaded sparse bricks, compressed distance/occupancy, separate static/dynamic updates, toroidal partial updates, and learned fields only when runtime evaluation stays cheap and inspectable.",
"For research decks with recordings, pull subtitles/transcripts before distilling. The Dreams transcript changed the lesson: preserve the evaluator/field that serves the image, not the elegant SDF/brick/froxel experiment that drifted away from the art direction.",

View file

@ -76,6 +76,15 @@ reusing field identity/travel/control.
density/scattering below the surface. The post debug ray-density evaluator
uses the same procedural density so Medium Ray Density / Transmittance stay
honest against the final medium atlas.
- Emissive-only lighting pass: the D3D12 fog no longer carries baked pea-soup
glow in its density function, and solid bodies no longer use a point-light
constant. Self is treated as an emissive sphere: the medium pass estimates
finite-surface solid angle, Henyey-Greenstein phase, and a cheap depth/radial
light-path transmittance toward Self; the scene shader shades planets and the
cursor from finite emissive-sphere irradiance. Full noisy density in the
emitter-to-sample march made shader pipeline compilation stall, so light-path
attenuation deliberately uses a cheap extinction approximation while visible
camera samples keep the textured density.
- Cursor locator SDF: `AquariumFrame` carries the mouse cursor projected onto
the Grid XY plane. D3D12 bins a cursor primitive into the existing view-froxel
solid table and traces it as an asymmetric teardrop lobe: local `z = -1`