From 102f53a2402b204d1dc679078aaf8ba5765d97a7 Mon Sep 17 00:00:00 2001 From: yum Date: Tue, 3 Jun 2025 23:49:25 -0700 Subject: Add spatiotemporal dithering to fog --- fog.cginc | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'fog.cginc') diff --git a/fog.cginc b/fog.cginc index bd1c1da..b1e209c 100644 --- a/fog.cginc +++ b/fog.cginc @@ -1,6 +1,7 @@ #ifndef __FOG_INC #define __FOG_INC +#include "audiolink.cginc" #include "cnlohr.cginc" #include "interpolators.cginc" #include "globals.cginc" @@ -12,8 +13,14 @@ struct FogParams { float density; float y_cutoff; texture2D dithering_noise; + float4 dithering_noise_texelsize; texture3D density_noise; float4 density_noise_scale; +#if defined(_RAYMARCHED_FOG_HEIGHT_DENSITY) + float height_density_min; + float height_density_max; + float height_density_power; +#endif }; struct FogResult { @@ -26,7 +33,8 @@ FogResult raymarched_fog(v2f i, FogParams p) float3 ro = _WorldSpaceCameraPos; float3 rd = normalize(i.eyeVec.xyz); - ro += rd * 1E-3; + const float ro_epsilon = 1E-3; + ro += rd * ro_epsilon; float2 screen_uv = (i.pos.xy + 0.5) / _ScreenParams.xy; float zDepthFromMap = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, screen_uv); @@ -44,6 +52,15 @@ FogResult raymarched_fog(v2f i, FogParams p) } } linearZ = min(linearZ, distance_to_y); + linearZ -= ro_epsilon; + + float dither = p.dithering_noise.SampleLevel(point_repeat_s, + screen_uv * _ScreenParams.xy * p.dithering_noise_texelsize.xy, 0).r; + + const float frame = ((float) AudioLinkData(ALPASS_GENERALVU + int2(1, 0)).x); + dither = frac(dither + PHI * frame); + ro += rd * dither; + linearZ -= dither; float step_size = linearZ / p.steps; float3 pp = ro; @@ -53,15 +70,25 @@ FogResult raymarched_fog(v2f i, FogParams p) 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; +#if defined(_RAYMARCHED_FOG_HEIGHT_DENSITY) + // Apply height-based density (branchless) + float t = saturate((pp.y - p.height_density_min) / + (p.height_density_max - p.height_density_min)); + float height_factor = pow(1.0 - t, p.height_density_power); + cur_d *= height_factor; +#endif + + cur_d = saturate(cur_d); d = d + (1 - d) * cur_d; } FogResult r; - r.color.rgb = 1; + r.color.rgb = _Raymarched_Fog_Color; //r.color.rgb = saturate(log(linearZ) / 5.0); - r.color.a = saturate(d); + r.color.a = d; //r.color.a = 1; r.depth = 0.0001; // Very small depth value to render in front return r; -- cgit v1.2.3