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 --- 2ner.cginc | 31 ++++++++++++++++++++----------- 2ner.shader | 3 +++ cnlohr.cginc | 3 +-- custom30.cginc | 4 +++- fog.cginc | 58 ++++++++++++++++++++++++++++++++++++++++------------------ globals.cginc | 3 ++- 6 files changed, 69 insertions(+), 33 deletions(-) diff --git a/2ner.cginc b/2ner.cginc index 6e2b6e6..96d997d 100644 --- a/2ner.cginc +++ b/2ner.cginc @@ -3,6 +3,7 @@ #include "UnityCG.cginc" #include "UnityLightingCommon.cginc" +#include "AutoLight.cginc" #include "custom30.cginc" #include "eyes.cginc" @@ -162,13 +163,17 @@ v2f vert(appdata v) { TRANSFER_SHADOW_CASTER_NORMALOFFSET(o); #endif +#if defined(SHADOWS_SCREEN) + TRANSFER_SHADOW(o); +#endif + // Vertex color o.color = v.color; return o; } float4 frag(v2f i, uint facing : SV_IsFrontFace -#if defined(_HARNACK_TRACING) || defined(_SHATTER_WAVE) || defined(_VERTEX_DOMAIN_WARPING) || (defined(_CUSTOM30) && !defined(_DEPTH_PREPASS)) +#if defined(_HARNACK_TRACING) || defined(_SHATTER_WAVE) || defined(_VERTEX_DOMAIN_WARPING) || (defined(_CUSTOM30) && !defined(_DEPTH_PREPASS)) || defined(_RAYMARCHED_FOG) , out float depth : SV_DepthLessEqual #endif ) : SV_Target { @@ -185,15 +190,19 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace i.binormal = UnityObjectToWorldNormal(i.binormal); #if defined(_RAYMARCHED_FOG) - FogParams fog_params = { - _Raymarched_Fog_Steps, - _Raymarched_Fog_Density, - _Raymarched_Fog_Dithering_Noise, - _Raymarched_Fog_Density_Noise, - _Raymarched_Fog_Density_Noise_Scale - }; - FogResult fog_result = raymarched_fog(i, fog_params); - return fog_result.color; + { + FogParams fog_params = { + _Raymarched_Fog_Steps, + _Raymarched_Fog_Density, + _Raymarched_Fog_Dithering_Noise, + _Raymarched_Fog_Density_Noise, + _Raymarched_Fog_Density_Noise_Scale, + _Raymarched_Fog_Y_Cutoff + }; + FogResult fog_result = raymarched_fog(i, fog_params); + depth = fog_result.depth; + return fog_result.color; + } #endif #if defined(_SHATTER_WAVE) || defined(_TESSELLATION_HEIGHTMAP) @@ -230,7 +239,7 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace i.uv01.xy = eye_effect_00.uv; #endif -#if defined(_CUSTOM30) && defined(FORWARD_BASE_PASS) +#if defined(_CUSTOM30) && defined(FORWARD_BASE_PASS) || (!defined(_DEPTH_PREPASS) && defined(SHADOW_CASTER_PASS)) #if defined(_CUSTOM30_BASICCUBE) Custom30Output basic_cube_output = BasicCube(i); i.pos = UnityObjectToClipPos(basic_cube_output.objPos); diff --git a/2ner.shader b/2ner.shader index 439da3b..4f137e7 100644 --- a/2ner.shader +++ b/2ner.shader @@ -824,6 +824,7 @@ Shader "yum_food/2ner" _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_end_Raymarched_Fog("Raymarched fog", Float) = 0 //endex @@ -2173,6 +2174,7 @@ Shader "yum_food/2ner" CGPROGRAM #pragma target 5.0 + #pragma multi_compile_fwdbase #pragma multi_compile_fullshadows #pragma multi_compile_instancing #pragma multi_compile_fog @@ -2330,6 +2332,7 @@ Shader "yum_food/2ner" CGPROGRAM #pragma target 5.0 + #pragma multi_compile_fwdbase #pragma multi_compile_fullshadows #pragma multi_compile_instancing #pragma multi_compile_fog diff --git a/cnlohr.cginc b/cnlohr.cginc index 2a03516..b01bbce 100644 --- a/cnlohr.cginc +++ b/cnlohr.cginc @@ -76,10 +76,9 @@ float GetLinearZFromZDepth_WorksWithMirrors(float zDepthFromMap, float2 screenUV void GetScreenUVAndPerspectiveFactor(float3 worldPos, float4 clipPos, out float2 screen_uv, out float perspective_factor) { float3 full_vec_eye_to_geometry = worldPos - _WorldSpaceCameraPos; - float3 world_dir = normalize(worldPos - _WorldSpaceCameraPos); float perspective_divide = 1.0f / clipPos.w; perspective_factor = length(full_vec_eye_to_geometry * perspective_divide); - screen_uv = clipPos.xy * perspective_divide; + screen_uv = ComputeScreenPos(clipPos).xy; } #if defined(_SSAO) diff --git a/custom30.cginc b/custom30.cginc index 6e81c44..2d407ca 100644 --- a/custom30.cginc +++ b/custom30.cginc @@ -214,7 +214,9 @@ Custom30Output BasicWedge(v2f i) { } Custom30Output o; - //clip(epsilon - d); +#if !defined(_DEPTH_PREPASS) + clip(epsilon - d); +#endif float3 objPos = ro + rd * d_acc; o.objPos = objPos; // Transform from SDF space back to object space 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 diff --git a/globals.cginc b/globals.cginc index 80ba634..22d707b 100644 --- a/globals.cginc +++ b/globals.cginc @@ -3,7 +3,7 @@ #include "features.cginc" -#if defined(_SSAO) +#if defined(_SSAO) || defined(_RAYMARCHED_FOG) UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture); float4 _CameraDepthTexture_TexelSize; #endif @@ -541,6 +541,7 @@ float _Raymarched_Fog_Density; texture2D _Raymarched_Fog_Dithering_Noise; texture3D _Raymarched_Fog_Density_Noise; float4 _Raymarched_Fog_Density_Noise_Scale; +float _Raymarched_Fog_Y_Cutoff; #endif // _RAYMARCHED_FOG #endif // __GLOBALS_INC -- cgit v1.2.3