diff --git a/docs/hlsl-renderer.md b/docs/hlsl-renderer.md index 3891a81..fcd5579 100644 --- a/docs/hlsl-renderer.md +++ b/docs/hlsl-renderer.md @@ -37,3 +37,20 @@ The Bevy SH/froxel lighting, brick maps, HDR bloom graph, and debug terminal are not directly ported yet. Those need engine systems around the shader rather than more one-pass pixel shader stuffing. The machete remains on the wall, tastefully lit. + +## Missing Grid Pass + +The current Vortice renderer does **not** yet have the deferred gravity/Grid +height render texture that Aetheria and the Bevy prototype both assume. The +single HLSL pass still evaluates analytic Grid height directly while marching. + +That is temporary. The real Grid path should be: + +1. Render gravity/body sources into a Grid-space height target. +2. Sample that height target in the raymarch pass. +3. Build terrain normals from adjacent Grid texels in world space. +4. Keep the Grid target centered on the camera target while body anchors stay in + world space. + +Until that pass exists, the shader approximates the same normal construction by +sampling `terrainHeight` at adjacent world-space Grid texel offsets. diff --git a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl index 709570b..e4b4c89 100644 --- a/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/Aquarium.hlsl @@ -17,6 +17,7 @@ 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 int PLANET_COUNT = 5; +static const float GRID_HEIGHT_TEXEL_COUNT = 128.0; struct VertexOut { @@ -290,12 +291,25 @@ float sceneDistance(float3 p) float3 terrainNormal(float3 p) { - float2 e = float2(max(0.035, gridRadius * 0.0012), 0.0); - float h0 = terrainHeight(p.xy - e.xy); - float h1 = terrainHeight(p.xy + e.xy); - float h2 = terrainHeight(p.xy - e.yx); - float h3 = terrainHeight(p.xy + e.yx); - return normalize(float3(h0 - h1, h2 - h3, e.x * 2.0)); + float texelWorld = max((gridRadius * 2.0) / GRID_HEIGHT_TEXEL_COUNT, 0.01); + float2 dx = float2(texelWorld, 0.0); + float2 dy = float2(0.0, texelWorld); + + float hLeft = terrainHeight(p.xy - dx); + float hRight = terrainHeight(p.xy + dx); + float hDown = terrainHeight(p.xy - dy); + float hUp = terrainHeight(p.xy + dy); + + float3 tangentX = float3(texelWorld * 2.0, 0.0, hRight - hLeft); + float3 tangentY = float3(0.0, texelWorld * 2.0, hUp - hDown); + float3 normal = normalize(cross(tangentX, tangentY)); + + if (normal.z < 0.0) + { + normal = -normal; + } + + return normal; } void nearestBody(float3 p, out float3 center, out float radius, out int index, out bool isSelf)