From d5a6e4d432aea4b5b1dd74579a226e8f6bb4caee Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 9 May 2026 11:56:29 +0100 Subject: [PATCH] Composite Grid as refined event stream --- .../Render/Shaders/Aquarium.hlsl | 93 ++++++++++++------- state/evidence.jsonl | 1 + state/scratch.md | 5 + 3 files changed, 68 insertions(+), 31 deletions(-) diff --git a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl index 6f4cf45..3c0d2a6 100644 --- a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl @@ -1328,11 +1328,12 @@ bool raymarchBodies(float3 rayOrigin, float3 rayDirection, out float3 hitPositio return bodyHit > 0.5 && travel <= farDistance; } -bool traceGridSurface(float3 rayOrigin, float3 rayDirection, out float3 hitPosition, out float travel) +bool traceGridSurfaceSegment(float3 rayOrigin, float3 rayDirection, float startTravel, float maxTravel, out float3 hitPosition, out float travel) { - travel = 0.0; - float previousTravel = 0.0; - float previousTerrainGap = rayOrigin.z - terrainHeight(rayOrigin.xy); + travel = max(startTravel, 0.0); + float previousTravel = travel; + float3 previousPosition = rayOrigin + rayDirection * previousTravel; + float previousTerrainGap = previousPosition.z - terrainHeight(previousPosition.xy); [loop] for (int stepIndex = 0; stepIndex < 96; stepIndex++) @@ -1365,7 +1366,7 @@ bool traceGridSurface(float3 rayOrigin, float3 rayDirection, out float3 hitPosit previousTerrainGap = terrainGap; travel += max(terrainStep, 0.026); - if (travel > farDistance) + if (travel > maxTravel || travel > farDistance) { return false; } @@ -1374,6 +1375,11 @@ bool traceGridSurface(float3 rayOrigin, float3 rayDirection, out float3 hitPosit return false; } +bool traceGridSurface(float3 rayOrigin, float3 rayDirection, out float3 hitPosition, out float travel) +{ + return traceGridSurfaceSegment(rayOrigin, rayDirection, 0.0, farDistance, hitPosition, travel); +} + float3 shade(float3 position, float3 normal, int materialId, float3 rayDirection) { float3 toSun = SUN_POSITION - position; @@ -1444,6 +1450,48 @@ float gridTemporalSupport(float3 position) return saturate(gridAmount * 0.58 + contour * 0.22 + fieldLine * 0.16) * mask; } +void integrateGridEventStream(float3 rayOrigin, float3 rayDirection, float maxTravel, out float3 eventColor, out float eventAlpha, out float eventSupport) +{ + eventColor = 0.0; + eventAlpha = 0.0; + eventSupport = 0.0; + + float transmittance = 1.0; + float startTravel = 0.0; + int eventCount = 0; + + [loop] + for (int eventIndex = 0; eventIndex < 8; eventIndex++) + { + if (transmittance < 0.05 || startTravel >= maxTravel) + { + break; + } + + float3 hitPosition; + float hitTravel; + if (!traceGridSurfaceSegment(rayOrigin, rayDirection, startTravel, maxTravel, hitPosition, hitTravel)) + { + break; + } + + float4 overlay = shadeGridOverlay(hitPosition); + float support = gridTemporalSupport(hitPosition); + float alpha = saturate(overlay.a * 0.58); + eventColor += transmittance * overlay.rgb * alpha; + eventAlpha += transmittance * alpha; + eventSupport = max(eventSupport, support); + transmittance *= 1.0 - alpha; + eventCount++; + + float2 slope = terrainGradient(hitPosition.xy); + float normalRate = max(abs(rayDirection.z - dot(slope, rayDirection.xy)), 0.18); + startTravel = hitTravel + max(0.035, 0.014 / normalRate); + } + + eventAlpha = saturate(eventAlpha); +} + float3 aces(float3 color) { const float a = 2.51; @@ -1596,24 +1644,6 @@ SceneOut AquariumScenePS(VertexOut input) outputCoverage = 1.0; } - float3 gridHitPosition; - float gridTravel; - if (traceGridSurface(cameraPosition, rayDirection, gridHitPosition, gridTravel) && gridTravel < nearestSolidTravel) - { - float4 gridOverlay = shadeGridOverlay(gridHitPosition); - float gridCoverage = saturate(gridOverlay.a); - float gridSupport = gridTemporalSupport(gridHitPosition); - outputTravel = gridTravel; - outputMaterialId = FIELD_ID_GRID; - outputNormal = terrainNormal(gridHitPosition); - outputCoverage = gridSupport; - outputReactive = 0.0; - if (stochasticTransparency(screenUv, gridOverlay.a) > 0.0) - { - color = gridOverlay.rgb; - } - } - float mediumDensityMean; float mediumTransmittance; float3 mediumInScattering; @@ -1751,14 +1781,6 @@ ResolveOut AquariumResolvePS(VertexOut input) float currentMediumDensity = saturate(currentControl.w); bool currentIsGrid = abs(currentFieldId - FIELD_ID_GRID) < 0.25; - if (currentIsGrid && currentTravel <= farDistance) - { - float3 currentRay = rayDirectionForPixel(pixel, jitterPixels, cameraPosition, gridCenter); - float3 worldPosition = cameraPosition + currentRay * currentTravel; - float4 gridOverlay = shadeGridOverlay(worldPosition); - currentColor = gridOverlay.rgb * saturate(gridOverlay.a) + float3(0.001, 0.003, 0.004); - } - float historyWeight = 0.0; float historyAge = 0.0; float3 historyColor = currentColor; @@ -1829,6 +1851,15 @@ ResolveOut AquariumResolvePS(VertexOut input) float blendedMediumTransmittance = lerp(1.0, froxelMediumTransmittance, mediumBlend); float3 resolved = currentColor * blendedMediumTransmittance + froxelMediumInScattering * mediumBlend; + float gridEventMaxTravel = currentTravel <= farDistance && currentFieldId > 0.5 && !currentIsGrid + ? currentTravel + : farDistance; + float3 gridEventColor; + float gridEventAlpha; + float gridEventSupport; + float3 gridEventRay = rayDirectionForPixel(pixel, jitterPixels, cameraPosition, gridCenter); + integrateGridEventStream(cameraPosition, gridEventRay, gridEventMaxTravel, gridEventColor, gridEventAlpha, gridEventSupport); + resolved = resolved * (1.0 - gridEventAlpha) + gridEventColor; float3 exposedColor = exposeSceneColor(resolved); float3 bloomColor = diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 19dd91c..adb2cc5 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -59,3 +59,4 @@ {"area":"renderer/volumetrics","lesson":"Do not compose the final frame from the diagnostic 4x4 medium atlas. If atlas density shows frustum-slice blobs while direct ray density is continuous, final composition should use direct registered-medium ray integration until a real froxel volume replaces the diagnostic atlas. Keep atlas density as an artifact-revealing debug view only.","files":["src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 60"],"ts":"2026-05-09"} {"area":"renderer/volumetrics","lesson":"The medium atlas trial has been replaced by a first real packed froxel volume: independent froxel dimensions, 32 depth slices, 8x4 D3D11 atlas packing, per-froxel density/transmittance/in-scattering, and resolve-side front-to-back composition. Direct ray density/transmittance debug remains the correctness reference; froxel density is the volume inspection view.","files":["src/Aquarium.Engine/Render/D3D11Renderer.cs","src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","docs/hlsl-renderer.md","docs/sdf-field-renderer-plan.md","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4 -HeadlessTimeoutSeconds 60"],"ts":"2026-05-09"} {"area":"renderer/volumetrics","lesson":"The Grid is a transparent schematic overlay, not a participating-medium occluder. Keep Grid travel/field id for temporal stability, but do not clamp fog integration to Grid travel; only solid fields such as Self and planets terminate the froxel composite ray.","files":["src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","state/scratch.md"],"verification":["Aquarium.Engine project build succeeded","Full solution/headless verification blocked by dirty external CultLib changes on E:\\Projects\\CultLib codex/cultcache-attribute-first: CultCache.cs currently fails GameCult.Caching build with System.Text.Json/IsExternalInit errors"],"ts":"2026-05-09"} +{"area":"renderer/grid","lesson":"Transparent Grid composition should be an event stream of refined heightfield crossings, not fixed band sampling and not scene first-hit ownership. Repeated slope-aware terrain-gap tracing gives stable Grid events that can composite front-to-back while opaque solids remain the only depth terminators.","files":["src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","state/scratch.md"],"verification":["dotnet build src\\Aquarium.Engine\\Aquarium.Engine.csproj","manual headless shader check using reusable dev-reload slot 20260509-114238-9a9638cb"],"ts":"2026-05-09"} diff --git a/state/scratch.md b/state/scratch.md index 0c0b8a8..cac9f12 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -206,3 +206,8 @@ prototype integrated along the camera ray before surface composition. stops at Self/planet solids only; Grid pixels integrate medium to the visible far distance so the schematic overlay cannot cut holes through fog or occlude itself. +- Transparent Grid event stream pass: the failed fixed-step band sampler was + reverted. The current pass traces repeated slope-aware `terrainGap` crossings, + refines each crossing through the existing heightfield marcher, and composites + up to eight Grid events front-to-back in resolve. The Grid no longer writes + scene depth/color; solids still bound the event stream.