From 07d204fd6631ae0d0bfec16c8d057cd39c5ab810 Mon Sep 17 00:00:00 2001 From: yum Date: Tue, 3 Jun 2025 22:37:12 -0700 Subject: Continue work on fog Fix screen uv calculation --- fog.cginc | 58 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 18 deletions(-) (limited to 'fog.cginc') diff --git a/fog.cginc b/fog.cginc index c0800bc..db90f8c 100644 --- a/fog.cginc +++ b/fog.cginc @@ -10,6 +10,7 @@ struct FogParams { float steps; float density; + float y_cutoff; texture2D dithering_noise; texture3D density_noise; float4 density_noise_scale; @@ -17,31 +18,52 @@ struct FogParams { struct FogResult { float4 color; + float depth; }; -float getRayLengthWorld() +FogResult raymarched_fog(v2f i, FogParams p) { - float2 screen_uv; - float perspective_factor; - { - float3 full_vec_eye_to_geometry = i.worldPos - _WorldSpaceCameraPos; - float3 world_dir = normalize(i.worldPos - _WorldSpaceCameraPos); - float perspective_divide = 1.0 / i.pos.w; - perspective_factor = length(full_vec_eye_to_geometry * perspective_divide); - screen_uv = i.screenPos.xy * perspective_divide; + float3 ro = _WorldSpaceCameraPos; + float3 rd = normalize(i.eyeVec.xyz); + + ro += rd * 1E-3; + + float2 screen_uv = (i.pos.xy + 0.5) / _ScreenParams.xy; + float zDepthFromMap = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, screen_uv); + float linearZ = + GetLinearZFromZDepth_WorksWithMirrors(zDepthFromMap, screen_uv); + linearZ = min(1E3, linearZ); + + // Get intersection with plane at elevation y. + float plane_y = -10; + float distance_to_y = 1E3; + if (abs(rd.y) > 1E-6) { + float t = (plane_y - ro.y) / rd.y; + if (t > 0) { + distance_to_y = min(t, 1E3); + } } - float eye_depth_world = - GetLinearZFromZDepth_WorksWithMirrors( - SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, screen_uv), - screen_uv) * perspective_factor; -} + linearZ = min(linearZ, distance_to_y); -FogResult raymarched_fog(v2f i, FogParams params) -{ - float3 ro = _WorldSpaceCameraPos; - float3 rd = normalize(i.worldPos - ray_pos); + float step_size = linearZ / p.steps; + float3 pp = ro; + float d = 0; + for (uint ii = 0; ii < p.steps; ++ii) { + pp += step_size * rd; + float cur_d = p.density_noise.SampleLevel(linear_repeat_s, + pp * p.density_noise_scale.xyz, 0); + cur_d *= p.density * step_size; + d = d + (1 - d) * cur_d; + } + FogResult r; + r.color.rgb = 1; + r.color.rgb = saturate(log(linearZ) / 5.0); + //r.color.a = saturate(d); + r.color.a = 1; + r.depth = 0.0001; // Very small depth value to render in front + return r; } #endif // _RAYMARCHED_FOG -- cgit v1.2.3