From 6624e9d39113ab9eae6c9cb4707d61204c194130 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 8 May 2026 21:42:55 +0100 Subject: [PATCH] Render Grid as transparent overlay --- docs/hlsl-renderer.md | 31 +++--- docs/sdf-field-renderer-plan.md | 7 +- .../Render/Shaders/Aquarium.hlsl | 98 +++++++++++++++++-- state/evidence.jsonl | 1 + state/memory.json | 1 + state/scratch.md | 3 + 6 files changed, 118 insertions(+), 23 deletions(-) diff --git a/docs/hlsl-renderer.md b/docs/hlsl-renderer.md index 291c4b2..2af2018 100644 --- a/docs/hlsl-renderer.md +++ b/docs/hlsl-renderer.md @@ -93,26 +93,20 @@ shader pattern: height contours are derived from the Grid height field, while angle lines quantize the terrain gradient direction and fade out on flat surfaces. -## SDF Cloud Prototype +## SDF Cloud Scaffold -`AquariumPS` now includes a first analytic SDF cloud-medium prototype. It defines +`Aquarium.hlsl` still contains an analytic SDF cloud-medium scaffold. It defines four bounded local cloud fields in world/Grid space, including clouds below and -around the Grid, then integrates density and single-scattering along the camera -ray before compositing the nearest solid surface: +around the Grid, but `AquariumPS` no longer composites those clouds into the +scene. The scaffold is retained only as evidence and throwaway machinery while +the waterfall field renderer is built. ```text color = cloud_scattering + cloud_transmittance * surface_color ``` -This is intentionally a prototype inside the fullscreen shader. It proves the -coordinate and composition contract for local SDF clouds, but the final renderer -should move toward the explicit field registry and volume pass described in -`docs/sdf-field-renderer-plan.md`. - -Do not grow this scaffold sideways. The SDF cloud path is now a waterfall -renderer task: field contract, broad phase, explicit volume pass, then -composition and acceleration. Correctness fixes belong here only when needed to -keep the scene inspectable while the real machine is built. +That composition shape belongs to the future explicit field/volume renderer, not +to the current fullscreen shader. Current prototype traits: @@ -128,6 +122,17 @@ Current prototype traits: - no sparse brick storage yet - no debug view yet +## Transparent Grid Overlay + +The Grid now renders as a transparent schematic overlay. `AquariumPS` traces +bodies separately from the Grid surface, shades solid bodies first, then +intersects the Grid height field and blends lines, isolines, field lines, and a +thin weather tint over the scene. The Grid no longer owns the opaque terrain hit +that stops scene composition. + +This keeps the current scene legible while leaving future volumetrics to provide +the real spatial mass. + ## Overlay Text Readable overlay text is handled by `DirectWriteOverlay`, using DirectWrite for diff --git a/docs/sdf-field-renderer-plan.md b/docs/sdf-field-renderer-plan.md index 20a7514..08ecb32 100644 --- a/docs/sdf-field-renderer-plan.md +++ b/docs/sdf-field-renderer-plan.md @@ -183,15 +183,16 @@ If we cannot see why a pixel looks wrong, the renderer is already lying. ## Current Temporary Scaffold -`Aquarium.hlsl` currently contains an analytic local cloud scaffold. It exists to -keep pixels alive while the real field renderer is built, not to define the -architecture. +`Aquarium.hlsl` currently contains an analytic local cloud scaffold, but it is no +longer composited into the scene. It exists as evidence and disposable machinery +while the real field renderer is built, not to define the architecture. Rules for that scaffold: - Fix correctness bugs when they block evaluation. - Do not add new cloud features there. - Do not tune around structural failures. +- Do not re-enable it as scene mass. - Replace it with the field registry, broad phase, and explicit volume pass. - Treat every artifact as evidence for the final machine, not as an invitation to add another little compromise. The compromise pile is how renderers become diff --git a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl index c8c7b27..ba1f90c 100644 --- a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl @@ -1030,6 +1030,69 @@ bool raymarch(float3 rayOrigin, float3 rayDirection, out float3 hitPosition, out return false; } +bool raymarchBodies(float3 rayOrigin, float3 rayDirection, out float3 hitPosition, out int materialId, out float travel) +{ + float entryTravel; + float exitTravel; + float bodyHit = traceNearestBodyInterval( + rayOrigin, + rayDirection, + 0.0, + entryTravel, + travel, + exitTravel, + materialId); + + hitPosition = rayOrigin + rayDirection * travel; + return bodyHit > 0.5 && travel <= farDistance; +} + +bool traceGridSurface(float3 rayOrigin, float3 rayDirection, out float3 hitPosition, out float travel) +{ + travel = 0.0; + float previousTravel = 0.0; + float previousTerrainGap = rayOrigin.z - terrainHeight(rayOrigin.xy); + + [loop] + for (int stepIndex = 0; stepIndex < 96; stepIndex++) + { + hitPosition = rayOrigin + rayDirection * travel; + float fade = terrainMask(hitPosition.xy); + if (fade <= 0.0001 && hitPosition.z < 4.0) + { + return false; + } + + float terrainGap = hitPosition.z - terrainHeight(hitPosition.xy); + float hitEpsilon = max(SURFACE_EPSILON, travel * 0.00035); + if (fade > 0.0001 && (terrainGap <= hitEpsilon || (previousTerrainGap > 0.0 && terrainGap <= 0.0))) + { + float alpha = previousTerrainGap / max(previousTerrainGap - terrainGap, 0.0001); + travel = lerp(previousTravel, travel, saturate(alpha)); + hitPosition = rayOrigin + rayDirection * travel; + return true; + } + + float2 terrainSlope = terrainGradient(hitPosition.xy); + float terrainRate = abs(rayDirection.z - dot(terrainSlope, rayDirection.xy)); + float terrainStep = terrainGap > 0.0 + ? terrainGap / max(terrainRate, 0.22) + : 0.026; + + terrainStep = min(terrainStep * 0.62, max(gridRadius * 0.08, 0.026)); + previousTravel = travel; + previousTerrainGap = terrainGap; + + travel += max(terrainStep, 0.026); + if (travel > farDistance) + { + return false; + } + } + + return false; +} + float3 shade(float3 position, float3 normal, int materialId, float3 rayDirection) { float3 toSun = SUN_POSITION - position; @@ -1070,6 +1133,25 @@ float3 shade(float3 position, float3 normal, int materialId, float3 rayDirection return (baseColor * (diffuse + selfGlow) + lineColor) * mask; } +float4 shadeGridOverlay(float3 position) +{ + float mask = terrainMask(position.xy); + float gridAmount = gridLine(position.xy); + float height = terrainHeight(position.xy); + float2 gradient = terrainGradient(position.xy); + float contour = terrainIsolines(height); + float fieldLine = terrainFieldLines(gradient); + float3 baseColor = gridWeatherColor(position.xy, position.z); + float3 gridColor = float3(0.74, 1.0, 0.84) * gridAmount; + float3 contourColor = float3(0.98, 1.0, 0.78) * contour; + float3 fieldColor = float3(0.36, 0.92, 1.0) * fieldLine; + float lineAlpha = saturate(gridAmount * 0.58 + contour * 0.22 + fieldLine * 0.16); + float fieldAlpha = saturate((0.035 + length(gradient) * 0.16) * mask); + float alpha = saturate((fieldAlpha + lineAlpha) * mask); + float3 overlayColor = baseColor * fieldAlpha * 1.4 + gridColor * 0.9 + contourColor * 0.32 + fieldColor * 0.22; + return float4(overlayColor, alpha); +} + float3 aces(float3 color) { const float a = 2.51; @@ -1096,20 +1178,22 @@ float4 AquariumPS(VertexOut input) : SV_Target int materialId; float travel; float3 color = 0.0; - float maxCloudTravel = farDistance; - if (raymarch(cameraPosition, rayDirection, hitPosition, materialId, travel)) + if (raymarchBodies(cameraPosition, rayDirection, hitPosition, materialId, travel)) { float3 normal = surfaceNormal(hitPosition, materialId); color = shade(hitPosition, normal, materialId, rayDirection); color *= exp(-travel * 0.012); - maxCloudTravel = travel; } - float3 cloudScattering; - float cloudTransmittance; - integrateCloudFields(cameraPosition, rayDirection, maxCloudTravel, cloudScattering, cloudTransmittance); - color = cloudScattering + color * cloudTransmittance; + float3 gridHitPosition; + float gridTravel; + if (traceGridSurface(cameraPosition, rayDirection, gridHitPosition, gridTravel)) + { + float4 gridOverlay = shadeGridOverlay(gridHitPosition); + color = lerp(color, gridOverlay.rgb, gridOverlay.a); + } + color += float3(0.001, 0.003, 0.004); return float4(aces(color), 1.0); } diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 8d8ca18..617b756 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -13,3 +13,4 @@ {"area":"renderer/sdf-fields","lesson":"The first SDF cloud transfer function made nearer ellipsoid edges occlude farther clouds because the SDF boundary produced a positive-distance absorbing veil and too much extinction. Cloud SDFs should provide sculptural containment and empty-space skipping, while density should be eroded interior occupancy with feathered boundaries and low extinction until a real volume pass owns the transfer function.","files":["src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","docs/hlsl-renderer.md","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4"],"ts":"2026-05-08"} {"area":"renderer/sdf-fields","lesson":"Fainter extinction did not fix near-cloud silhouettes because the prototype was still structurally wrong: a global SDF shell march could spend the frame's cloud sample budget inside a nearer ellipsoid and fail to reach farther cloud intervals. The analytic prototype now intersects ray/ellipsoid intervals, jumps empty space to true entries, and samples density only inside container volumes.","files":["src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","docs/hlsl-renderer.md","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4"],"ts":"2026-05-08"} {"area":"renderer/sdf-fields","lesson":"The SDF field/cloud renderer should use a waterfall implementation shape, not a chain of shy compromised prototypes. Define the target machine first: field ABI, broad phase, explicit local volume pass, composition contract, and acceleration gates. The current fullscreen analytic cloud code is temporary scaffolding for inspection only, not a place to accumulate features.","files":["docs/sdf-field-renderer-plan.md","docs/hlsl-renderer.md","state/memory.json","state/scratch.md"],"ts":"2026-05-08"} +{"area":"renderer/composition","lesson":"Removed the analytic SDF cloud scaffold from visible scene composition and made the Grid a transparent schematic overlay. Bodies now trace as solid scene content; the Grid traces separately and blends lines/isolines/field lines/weather tint over the result. This preserves legibility while reserving real scene mass for the future explicit volumetric field renderer.","files":["src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","docs/hlsl-renderer.md","docs/sdf-field-renderer-plan.md","state/memory.json","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4"],"ts":"2026-05-08"} diff --git a/state/memory.json b/state/memory.json index 6e6ca67..b38cede 100644 --- a/state/memory.json +++ b/state/memory.json @@ -45,6 +45,7 @@ "SDF cloud distance should shape and accelerate the medium, not create a positive-distance absorbing shell. Prototype ellipsoid edges occluded farther clouds until density was changed to eroded interior occupancy with lower extinction.", "A bounded cloud prototype should march analytic ray/volume intervals, not global SDF shells. The first cloud artifact persisted until rays jumped between true ellipsoid entry/exit intervals; otherwise near fields can consume the sample budget and masquerade as occluders.", "SDF clouds/solids need waterfall architecture, not agile accretion. Define and build the field contract, broad phase, explicit local volume pass, composition contract, and acceleration gates before adding more cloud features.", + "Until the real field renderer exists, do not composite the analytic SDF cloud scaffold into the Aquarium scene. The Grid should read as a transparent schematic overlay; volumetrics will own scene mass later.", "Bruneton-style atmospheric scattering is the later sky/planet solution, not the first Grid fog pass. Use LUTs, explicit texture mappings, and unit-tested physical terms when Aquarium reaches ground-to-space atmosphere.", "GigaVoxels-style ray-guided sparse bricks are only for large sparse field/world storage. Do not turn early fog into a monolithic persistent volume cache." ], diff --git a/state/scratch.md b/state/scratch.md index 23fb402..31d4692 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -31,3 +31,6 @@ prototype integrated along the camera ray before surface composition. target renderer as a waterfall machine: field contract, broad phase, explicit local volume pass, composition, then acceleration. Current shader cloud code is temporary scaffolding only. +- Current scene correction: analytic cloud scaffold is no longer composited. + Bodies render as solids; the Grid traces separately and blends as a transparent + schematic overlay so future volumetrics can own actual scene mass.