From 96f8c227eb803a3132294833aa219b79edeb9ca1 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 10 May 2026 08:08:01 +0100 Subject: [PATCH] Add below-grid fog medium --- src/Aquarium.Engine/Render/D3D12Renderer.cs | 9 ++ .../Render/Shaders/D3D12Medium.hlsl | 86 +++++++++++++++++++ .../Render/Shaders/D3D12Post.hlsl | 77 ++++++++++++++++- state/evidence.jsonl | 1 + state/memory.json | 1 + state/scratch.md | 7 ++ 6 files changed, 180 insertions(+), 1 deletion(-) diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index 8e8a902..27a85db 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -1054,6 +1054,7 @@ public sealed class D3D12Renderer : IAquariumRenderer context.CommandList.SetGraphicsRootDescriptorTable(15, frameResources.SceneEventMetadataDescriptor.Gpu); context.CommandList.SetGraphicsRootDescriptorTable(16, frameResources.HistoryEventColorDescriptor.Gpu); context.CommandList.SetGraphicsRootDescriptorTable(17, frameResources.HistoryEventMetadataDescriptor.Gpu); + context.CommandList.SetGraphicsRootDescriptorTable(18, frameResources.GridHeightDescriptor.Gpu); context.CommandList.RSSetViewports(viewport); context.CommandList.RSSetScissorRects(scissorRect); @@ -1138,6 +1139,7 @@ public sealed class D3D12Renderer : IAquariumRenderer activeCommandList.SetGraphicsRootSignature(fullscreenRootSignature); activeCommandList.SetGraphicsRootDescriptorTable(0, frameResources.FrameConstantsDescriptor.Gpu); activeCommandList.SetGraphicsRootDescriptorTable(4, frameResources.FieldInstanceDescriptor.Gpu); + activeCommandList.SetGraphicsRootDescriptorTable(18, frameResources.GridHeightDescriptor.Gpu); activeCommandList.RSSetViewports(mediumViewport); activeCommandList.RSSetScissorRects(mediumScissorRect); activeCommandList.OMSetRenderTargets( @@ -1892,6 +1894,12 @@ public sealed class D3D12Renderer : IAquariumRenderer 21, 0, D3D12.DescriptorRangeOffsetAppend); + var gridHeightMediumRange = new DescriptorRange( + DescriptorRangeType.ShaderResourceView, + 1, + 22, + 0, + D3D12.DescriptorRangeOffsetAppend); var rootParameters = new[] { new RootParameter(new RootDescriptorTable([constantBufferRange]), ShaderVisibility.All), @@ -1912,6 +1920,7 @@ public sealed class D3D12Renderer : IAquariumRenderer new RootParameter(new RootDescriptorTable([currentEventMetadataRange]), ShaderVisibility.Pixel), new RootParameter(new RootDescriptorTable([historyEventColorRange]), ShaderVisibility.Pixel), new RootParameter(new RootDescriptorTable([historyEventMetadataRange]), ShaderVisibility.Pixel), + new RootParameter(new RootDescriptorTable([gridHeightMediumRange]), ShaderVisibility.Pixel), }; var staticSamplers = new[] { diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl index 49a394d..84258af 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12Medium.hlsl @@ -33,6 +33,8 @@ struct FieldInstance }; StructuredBuffer fieldInstances : register(t12); +Texture2D gridHeightTexture : register(t22); +SamplerState sourceSampler : register(s0); static const int FIELD_INSTANCE_COUNT = 10; static const int FIELD_FLAG_CLOUD = 2; @@ -136,6 +138,85 @@ float fbm3(float3 p) return clamp(value, -1.0, 1.0); } +float tri(float x) +{ + return abs(frac(x) - 0.5); +} + +float3 tri3(float3 p) +{ + return float3( + tri(p.z + tri(p.y)), + tri(p.z + tri(p.x)), + tri(p.y + tri(p.x))); +} + +float triNoise3d(float3 p) +{ + float z = 1.4; + float value = 0.001; + float3 basePoint = p; + + [unroll] + for (int i = 0; i < 2; i++) + { + float3 dg = tri3(basePoint * 2.0); + p += dg + timeSeconds * 0.055; + basePoint *= 1.8; + z *= 1.5; + p *= 1.2; + value += tri(p.z + tri(p.x + tri(p.y))) / z; + basePoint += 0.14; + } + + return value; +} + +float2 gridLocal(float2 p) +{ + return (p - gridCenter) / max(gridRadius, 0.001); +} + +float2 gridUv(float2 p) +{ + return gridLocal(p) * 0.5 + 0.5; +} + +float terrainHeight(float2 p) +{ + return gridHeightTexture.SampleLevel(sourceSampler, saturate(gridUv(p)), 0.0).r; +} + +float gridFogDensity(float3 p, out float3 scattering) +{ + 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; + float depthRamp = smoothstep(0.035, 0.72, depthBelowGrid); + if (radialFade <= 0.0 || depthRamp <= 0.0) + { + return 0.0; + } + + float3 domain = float3(p.xy - gridCenter, p.z * 1.7) * 0.18; + float3 flow = float3(0.11, -0.07, 0.05) * timeSeconds; + float3 warp = float3( + triNoise3d(domain + flow + 13.1), + triNoise3d(domain.yzx - flow + 27.7), + triNoise3d(domain.zxy + flow * 0.63 + 41.3)) * 2.0 - 1.0; + + float low = triNoise3d(domain * 1.85 + warp * 0.82 + flow); + 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)); + + scattering = float3(0.58, 0.68, 0.24) * density * lerp(0.34, 0.72, textureWeight); + return density; +} + float2 rotate2(float2 value, float angle) { float s = sin(angle); @@ -162,6 +243,11 @@ float registeredMediumDensity(float3 p, out float3 scattering) float density = 0.0; scattering = 0.0; + float3 gridFogScattering; + float gridDensity = gridFogDensity(p, gridFogScattering); + density += gridDensity; + scattering += gridFogScattering; + [unroll] for (int i = 0; i < FIELD_INSTANCE_COUNT; i++) { diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl index 6dcf272..0ac4c9d 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12Post.hlsl @@ -37,6 +37,7 @@ Texture2D currentEventColorTexture : register(t18); Texture2D currentEventMetadataTexture : register(t19); Texture2D historyEventColorTexture : register(t20); Texture2D historyEventMetadataTexture : register(t21); +Texture2D gridHeightTexture : register(t22); SamplerState sourceSampler : register(s0); struct FieldInstance @@ -364,6 +365,80 @@ float fbm3(float3 p) return clamp(value, -1.0, 1.0); } +float tri(float x) +{ + return abs(frac(x) - 0.5); +} + +float3 tri3(float3 p) +{ + return float3( + tri(p.z + tri(p.y)), + tri(p.z + tri(p.x)), + tri(p.y + tri(p.x))); +} + +float triNoise3d(float3 p) +{ + float z = 1.4; + float value = 0.001; + float3 basePoint = p; + + [unroll] + for (int i = 0; i < 2; i++) + { + float3 dg = tri3(basePoint * 2.0); + p += dg + timeSeconds * 0.055; + basePoint *= 1.8; + z *= 1.5; + p *= 1.2; + value += tri(p.z + tri(p.x + tri(p.y))) / z; + basePoint += 0.14; + } + + return value; +} + +float2 gridLocal(float2 p) +{ + return (p - gridCenter) / max(gridRadius, 0.001); +} + +float2 gridUv(float2 p) +{ + return gridLocal(p) * 0.5 + 0.5; +} + +float terrainHeight(float2 p) +{ + return gridHeightTexture.SampleLevel(sourceSampler, saturate(gridUv(p)), 0.0).r; +} + +float gridFogDensity(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); + if (radialFade <= 0.0 || depthRamp <= 0.0) + { + return 0.0; + } + + float3 domain = float3(p.xy - gridCenter, p.z * 1.7) * 0.18; + float3 flow = float3(0.11, -0.07, 0.05) * timeSeconds; + float3 warp = float3( + triNoise3d(domain + flow + 13.1), + triNoise3d(domain.yzx - flow + 27.7), + triNoise3d(domain.zxy + flow * 0.63 + 41.3)) * 2.0 - 1.0; + + float low = triNoise3d(domain * 1.85 + warp * 0.82 + flow); + 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); + return saturate(radialFade * depthRamp * lerp(0.42, 1.0, deepening) * lerp(0.72, 1.55, textureWeight)); +} + float2 rotate2(float2 value, float angle) { float s = sin(angle); @@ -387,7 +462,7 @@ float fieldDistance(float3 p, FieldInstance field) float registeredMediumDensity(float3 p) { - float density = 0.0; + float density = gridFogDensity(p); [unroll] for (int i = 0; i < FIELD_INSTANCE_COUNT; i++) diff --git a/state/evidence.jsonl b/state/evidence.jsonl index b338294..fd06912 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -126,3 +126,4 @@ {"timestamp":"2026-05-10T07:43:40.3417470+01:00","topic":"d3d12-cursor-round-bulb-polish","lesson":"To avoid pagoda shelves and make the cursor bulb read more spherical, derive the lower profile from a sphere around local z=-0.46, blend into a strengthened cubic taper starting near local z=-0.10, and reduce ripple amplitude. Brass should read polished: use a tight high-power specular core plus a smaller shoulder, with no ambient diffuse floor.","files":["src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl","state/memory.json","state/scratch.md"]} {"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"]} diff --git a/state/memory.json b/state/memory.json index 1836824..b6fdf51 100644 --- a/state/memory.json +++ b/state/memory.json @@ -33,6 +33,7 @@ "The camera target is the Grid center on XY. Grid radius derives from zoom distance. Pitch, yaw, FOV, and screen aspect do not resize the Grid.", "Body anchors are world-space simulation state. Moving the Grid visibility window must not drag bodies with it.", "Registered volumetric brush anchors are world-space simulation state too. Do not derive cloud/medium field centers from `frame.Grid.Center`; the Grid can pan under/through them, but the medium bodies must stay fixed unless simulation state moves them.", + "Below-Grid fog is a medium field, not a post-process tint. D3D12 binds the Grid height target into the medium/debug density paths and uses the actual height surface as the fog ceiling; keep final medium and ray-debug density evaluators coherent whenever procedural medium terms are added.", "Grid texture, weather, line, and future fog domains should sample world-space coordinates so panning reveals the field instead of sliding a texture under the camera.", "Aquarium/Aetheria clouds are local SDF media: above, below, around, and sometimes enclosing the camera. Do not build the core cloud path on skybox, hemisphere, paraboloid, or far-only assumptions.", "Cursor and camera interactions should project onto the same XY surface used by the Grid contract." diff --git a/state/scratch.md b/state/scratch.md index b00b156..7763e7a 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -69,6 +69,13 @@ reusing field identity/travel/control. panning. D3D12 now stores fixed world-space `CloudCenters`; Grid can remain a moving visibility window, but medium bodies must not ride it unless simulation state explicitly moves them. +- Grid fog volume: the first below-Grid pea-soup volume is a real D3D12 medium + contribution, not a post tint. The medium pass binds the Grid height target + through a dedicated `t22` SRV, samples the actual height surface as the fog + ceiling, and adds animated Aetheria-style triangular-noise domain warping to + 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. - 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`