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 --- 2ner.cginc | 8 +++++++- 2ner.shader | 9 +++++++++ audiolink.cginc | 2 +- features.cginc | 4 ++++ fog.cginc | 33 ++++++++++++++++++++++++++++++--- globals.cginc | 5 +++++ 6 files changed, 56 insertions(+), 5 deletions(-) diff --git a/2ner.cginc b/2ner.cginc index d78e3db..bd5e743 100644 --- a/2ner.cginc +++ b/2ner.cginc @@ -196,8 +196,14 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace _Raymarched_Fog_Density, _Raymarched_Fog_Y_Cutoff, _Raymarched_Fog_Dithering_Noise, + _Raymarched_Fog_Dithering_Noise_TexelSize, _Raymarched_Fog_Density_Noise, - _Raymarched_Fog_Density_Noise_Scale + _Raymarched_Fog_Density_Noise_Scale, + #if defined(_RAYMARCHED_FOG_HEIGHT_DENSITY) + _Raymarched_Fog_Height_Density_Min, + _Raymarched_Fog_Height_Density_Max, + _Raymarched_Fog_Height_Density_Power, + #endif }; FogResult fog_result = raymarched_fog(i, fog_params); depth = fog_result.depth; diff --git a/2ner.shader b/2ner.shader index d176ea7..bff1cb0 100644 --- a/2ner.shader +++ b/2ner.shader @@ -820,11 +820,19 @@ Shader "yum_food/2ner" [HideInInspector] m_start_Raymarched_Fog("Raymarched fog", Float) = 0 [ThryToggle(_RAYMARCHED_FOG)] _Raymarched_Fog_Enabled("Enable", Float) = 0 _Raymarched_Fog_Steps("Steps", Range(1, 32)) = 32 + _Raymarched_Fog_Color("Color", Color) = (0.3, 0.3, 0.3, 1) _Raymarched_Fog_Density("Density", Float) = 1.0 _Raymarched_Fog_Dithering_Noise("Dithering noise", 2D) = "black" {} _Raymarched_Fog_Density_Noise("Density noise", 3D) = "black" {} _Raymarched_Fog_Density_Noise_Scale("Density noise scale", Vector) = (1, 1, 1, 0) _Raymarched_Fog_Y_Cutoff("Y cutoff", Float) = -1000 + + [HideInInspector] m_start_Raymarched_Fog_Height_Density("Height density", Float) = 0 + [ThryToggle(_RAYMARCHED_FOG_HEIGHT_DENSITY)] _Raymarched_Fog_Height_Density_Enabled("Enable", Float) = 0 + _Raymarched_Fog_Height_Density_Min("Height density min", Float) = 0 + _Raymarched_Fog_Height_Density_Max("Height density max", Float) = 100 + _Raymarched_Fog_Height_Density_Power("Height density power", Float) = 1 + [HideInInspector] m_end_Raymarched_Fog_Height_Density("Height density", Float) = 0 [HideInInspector] m_end_Raymarched_Fog("Raymarched fog", Float) = 0 //endex @@ -1806,6 +1814,7 @@ Shader "yum_food/2ner" [HideInInspector] m_start_Shadow_Casting("Cast shadows", Float) = 0 [ThryToggle(_)] _Cast_Shadows_Enabled("Enable", Float) = 1 [HideInInspector] m_end_Shadow_Casting("Cast shadows", Float) = 0 + //endex //ifex _Wrapped_Lighting_Enabled==0 [HideInInspector] m_start_WrappedLighting("Wrapped lighting", Float) = 0 [ThryToggle(_WRAPPED_LIGHTING)] _Wrapped_Lighting_Enabled("Enable", Float) = 1 diff --git a/audiolink.cginc b/audiolink.cginc index 68ddb60..d9c167a 100644 --- a/audiolink.cginc +++ b/audiolink.cginc @@ -1,7 +1,7 @@ #ifndef __AUDIOLINK #define __AUDIOLINK -#if defined(_SHATTER_WAVE_AUDIOLINK) || defined(_VERTEX_DOMAIN_WARPING_AUDIOLINK) || defined(_SSAO) +#if defined(_SHATTER_WAVE_AUDIOLINK) || defined(_VERTEX_DOMAIN_WARPING_AUDIOLINK) || defined(_SSAO) || defined(_RAYMARCHED_FOG) #include "Third_Party/AudioLink.cginc" #endif diff --git a/features.cginc b/features.cginc index 40ed4a1..59a3ff3 100644 --- a/features.cginc +++ b/features.cginc @@ -315,5 +315,9 @@ #pragma shader_feature_local _RAYMARCHED_FOG //endex +//ifex _Raymarched_Fog_Height_Density_Enabled==0 +#pragma shader_feature_local _RAYMARCHED_FOG_HEIGHT_DENSITY +//endex + #endif // __FEATURES_INC 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; diff --git a/globals.cginc b/globals.cginc index 22d707b..341e08b 100644 --- a/globals.cginc +++ b/globals.cginc @@ -536,12 +536,17 @@ float _SSAO_Bias; #endif // _SSAO #if defined(_RAYMARCHED_FOG) +float3 _Raymarched_Fog_Color; float _Raymarched_Fog_Steps; float _Raymarched_Fog_Density; texture2D _Raymarched_Fog_Dithering_Noise; +float4 _Raymarched_Fog_Dithering_Noise_TexelSize; texture3D _Raymarched_Fog_Density_Noise; float4 _Raymarched_Fog_Density_Noise_Scale; float _Raymarched_Fog_Y_Cutoff; +float _Raymarched_Fog_Height_Density_Min; +float _Raymarched_Fog_Height_Density_Max; +float _Raymarched_Fog_Height_Density_Power; #endif // _RAYMARCHED_FOG #endif // __GLOBALS_INC -- cgit v1.2.3