Add derivative-aware Grid linework

This commit is contained in:
Meta Vens 2026-05-08 20:25:15 +01:00
commit d9dab13b08
3 changed files with 66 additions and 5 deletions

View file

@ -72,6 +72,14 @@ The target currently stores height in `.r` only. The next useful expansion is to
store packed gradients in `.g/.b` during the Grid pass so the raymarcher can use
one texture fetch for height and a cheaper normal path when fidelity allows it.
Grid linework is derivative-aware in the final raymarch pass. Cartesian
Gridlines use screen-space `fwidth` against their world-domain coordinate so
minor and major lines stay the same pixel width across zoom distances. Terrain
height isolines and gradient-angle field lines follow the older Aetheria UI
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.
## Overlay Text
Readable overlay text is handled by `DirectWriteOverlay`, using DirectWrite for

View file

@ -20,6 +20,13 @@ static const float3 SUN_POSITION = float3(0.0, 0.0, 2.2);
static const float SUN_RADIUS = 1.12;
static const float GRID_WEATHER_WORLD_SCALE = 42.0;
static const float GRID_LINE_WORLD_CELL = 2.0;
static const float GRID_MAJOR_LINE_WORLD_CELL = GRID_LINE_WORLD_CELL * 5.0;
static const float GRID_LINE_PIXEL_WIDTH = 1.15;
static const float GRID_MAJOR_LINE_PIXEL_WIDTH = 1.65;
static const float GRID_LINE_PIXEL_FADE = 1.25;
static const float TERRAIN_ISOLINE_SPACING = 0.12;
static const float TERRAIN_ISOLINE_PIXEL_WIDTH = 1.05;
static const float TERRAIN_FIELD_LINE_PIXEL_WIDTH = 0.72;
static const int PLANET_COUNT = 5;
static const int PRIMITIVE_COUNT = PLANET_COUNT + 1;
static const float GRID_HEIGHT_TEXEL_COUNT = 128.0;
@ -662,11 +669,50 @@ float3 surfaceNormal(float3 p, int materialId)
return planetNormal(p);
}
float periodicLineMask(float coordinate, float pixelWidth, float pixelFade)
{
float distanceToLine = min(frac(coordinate), 1.0 - frac(coordinate));
float coordinatePerPixel = max(fwidth(coordinate), 0.00001);
float distancePixels = distanceToLine / coordinatePerPixel;
return 1.0 - smoothstep(pixelWidth, pixelWidth + pixelFade, distancePixels);
}
float gridLine(float2 p)
{
float2 cell = abs(frac(p / GRID_LINE_WORLD_CELL) - 0.5);
float lineDistance = min(cell.x, cell.y);
return (1.0 - smoothstep(0.018, 0.042, lineDistance * GRID_LINE_WORLD_CELL)) * terrainMask(p);
float2 minorDomain = p / GRID_LINE_WORLD_CELL;
float2 majorDomain = p / GRID_MAJOR_LINE_WORLD_CELL;
float minor = max(
periodicLineMask(minorDomain.x, GRID_LINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE),
periodicLineMask(minorDomain.y, GRID_LINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE));
float major = max(
periodicLineMask(majorDomain.x, GRID_MAJOR_LINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE),
periodicLineMask(majorDomain.y, GRID_MAJOR_LINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE));
return saturate(minor * 0.72 + major) * terrainMask(p);
}
float terrainIsolines(float height)
{
float contourDomain = height / TERRAIN_ISOLINE_SPACING;
float contour = periodicLineMask(contourDomain, TERRAIN_ISOLINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE);
float contourDerivative = max(fwidth(contourDomain), 0.00001);
float slopeFade = smoothstep(0.025, 0.25, contourDerivative);
return contour * slopeFade;
}
float terrainFieldLines(float2 gradient)
{
float slope = length(gradient);
if (slope < 0.0001)
{
return 0.0;
}
float angleDomain = (atan2(gradient.y, gradient.x) / PI + 1.0) * 6.0;
float angleLine = periodicLineMask(angleDomain, TERRAIN_FIELD_LINE_PIXEL_WIDTH, GRID_LINE_PIXEL_FADE);
float slopeStrength = smoothstep(0.015, 0.16, slope);
return angleLine * slopeStrength;
}
bool raymarch(float3 rayOrigin, float3 rayDirection, out float3 hitPosition, out int materialId, out float travel)
@ -771,9 +817,15 @@ float3 shade(float3 position, float3 normal, int materialId, float3 rayDirection
float mask = terrainMask(position.xy);
float gridAmount = gridLine(position.xy);
float contour = 1.0 - smoothstep(0.015, 0.04, abs(frac(terrainHeight(position.xy) * 1.2) - 0.5));
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 lineColor = float3(0.8, 1.0, 0.82) * (gridAmount * 0.85 + contour * 0.16);
float3 gridColor = float3(0.74, 1.0, 0.84) * gridAmount * 0.78;
float3 contourColor = float3(0.98, 1.0, 0.78) * contour * 0.22;
float3 fieldColor = float3(0.36, 0.92, 1.0) * fieldLine * 0.14;
float3 lineColor = gridColor + contourColor + fieldColor;
float diffuse = nDotL * attenuation * 0.85;
float selfGlow = exp(-distance(position, SUN_POSITION) * 0.34) * 0.34;

View file

@ -1,3 +1,4 @@
{"ts":"2026-05-08","topic":"mainline_engine_birth","summary":"Merged the native C# Aquarium Engine line to main and made it the live development branch. Removed branch-lineage memorial docs so the repo describes the current engine instead of carrying prototype-era debris.","files":["README.md","docs/branch-lineage.md"],"verification":["git merge --ff-only codex/port-bevy-features"]}
{"ts":"2026-05-08","topic":"persistent_state_surface","summary":"Added Aquarium-owned persistent state machinery: AGENTS.md for local operating doctrine, state/map.yaml for canonical project map, state/memory.json for durable renderer/state/UI lessons, state/evidence.jsonl for compact lesson entries, and state/scratch.md for disposable working context.","files":["AGENTS.md","state/map.yaml","state/memory.json","state/evidence.jsonl","state/scratch.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4"]}
{"ts":"2026-05-08","topic":"engine_doctrine_distillation","summary":"Distilled only engine-relevant doctrine from the older Aquarium state: camera/Grid invariants, world-space anchors/domains, shader-owned visible world, conservative raymarch broad phase, diegetic lighting, DirectWrite overlay text, CultCache reload state, and hot reload failure behavior.","files":["docs/aquarium-engine-doctrine.md","state/memory.json","state/map.yaml"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4"]}
{"topic":"grid_derivative_linework","summary":"Ported the Aetheria UI isoline idea into the Aquarium Grid shader: height contours and gradient-angle field lines now derive from the deferred Grid height field, and Cartesian Gridlines use fwidth-based masks so line width stays stable in pixels across zoom.","files":["src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl","docs/hlsl-renderer.md"],"verification":["dotnet build Aquarium.Engine.slnx",".\\scripts\\dev-reload.ps1 -Headless -RetainSlots 4"],"ts":"2026-05-08"}