From 50b0b6c8b292e966a43fe56c6e0bf0a20c1d5b62 Mon Sep 17 00:00:00 2001 From: yum Date: Wed, 4 Jun 2025 15:15:35 -0700 Subject: More ssao and c30 cleanups --- 2ner.cginc | 18 +++++++++++------- cnlohr.cginc | 3 +-- custom30.cginc | 28 ++++++++++++++++++++++------ ssao.cginc | 24 ++++++++++++++++-------- 4 files changed, 50 insertions(+), 23 deletions(-) diff --git a/2ner.cginc b/2ner.cginc index 606262b..820236c 100644 --- a/2ner.cginc +++ b/2ner.cginc @@ -250,13 +250,6 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace i.uv01.xy = eye_effect_00.uv; #endif - float3x3 tangentToWorld = float3x3(i.tangent, i.binormal, i.normal); - float ssao = 1; -#if defined(_SSAO) - float2 debug; - ssao = get_ssao(i, tangentToWorld, debug); -#endif - #if defined(_CUSTOM30) && defined(FORWARD_BASE_PASS) || (!defined(_DEPTH_PREPASS) && defined(SHADOW_CASTER_PASS)) #if defined(_CUSTOM30_BASICCUBE) Custom30Output c30_out = BasicCube(i); @@ -266,11 +259,22 @@ float4 frag(v2f i, uint facing : SV_IsFrontFace Custom30Output c30_out = BasicPlatform(i); #endif i.normal = c30_out.normal; + i.worldPos = mul(unity_ObjectToWorld, float4(c30_out.objPos, 1)); + float4 c30_clipPos = UnityObjectToClipPos(i.objPos); + float4 c30_screenPos = ComputeScreenPos(c30_clipPos); + i.pos = c30_screenPos; #if !defined(_DEPTH_PREPASS) depth = c30_out.depth; #endif #endif + float3x3 tangentToWorld = float3x3(i.tangent, i.binormal, i.normal); + float ssao = 1; +#if defined(_SSAO) + float2 debug; + ssao = get_ssao(i, tangentToWorld, debug); +#endif + tangentToWorld = float3x3(i.tangent, i.binormal, i.normal); YumPbr pbr = GetYumPbr(i, tangentToWorld); pbr.ao *= ssao; diff --git a/cnlohr.cginc b/cnlohr.cginc index cd98f08..20589bc 100644 --- a/cnlohr.cginc +++ b/cnlohr.cginc @@ -84,12 +84,11 @@ void GetScreenUVAndPerspectiveFactor(float3 worldPos, float4 clipPos, out float2 #if defined(_SSAO) float GetDepthOfWorldPos(float3 worldPos, out float2 debug) { - float3 full_vec_eye_to_geometry = worldPos - _WorldSpaceCameraPos; float3 objPos = mul(unity_WorldToObject, float4(worldPos, 1)); float4 clipPos = UnityObjectToClipPos(objPos); float4 screenPos = ComputeScreenPos(clipPos); - const float2 screen_uv = screenPos.xy / screenPos.w; + float zDepthFromMap = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, screen_uv); float linearZ = GetLinearZFromZDepth_WorksWithMirrors(zDepthFromMap, screen_uv); diff --git a/custom30.cginc b/custom30.cginc index be4bd4c..2bf8ec2 100644 --- a/custom30.cginc +++ b/custom30.cginc @@ -58,7 +58,24 @@ float distance_from_hex_comb( float half_period = period * 0.5; float3 which = abs(floor((p_hex + half_period) / period)); - p_hex = glsl_mod(p_hex + half_period, period) - half_period; + + // The original code here was this: + // p_hex = glsl_mod(p_hex + half_period, period) - half_period; + // + // But you can simplify it. Given the definition of glsl_mod: + // #define glsl_mod(x,y) (((x)-(y)*floor((x)/(y)))) + // + // You can plug in terms: + // (p_hex + half_period) - (period) * floor((p_hex + half_period) / period) + // = p_hex + half_period - period * floor(p_hex/period + 0.5) + // + // For all x, + // round(x) = floor(x + 0.5) + // + // Continuing to simplify: + // (p_hex + half_period - period * round(p_hex/period)) - half_period + // = p_hex - period * round(p_hex / period) + p_hex = p_hex - period * round(p_hex / period); p.xy = hex_to_cart(p_hex); @@ -181,13 +198,13 @@ Custom30Output BasicCube(v2f i) { clip(epsilon - d); #endif float3 objPos = ro + rd * d_acc; - o.objPos = objPos; // Transform from SDF space back to object space float3 objSpacePos = objPos + (i.objPos + frag_to_origin); + o.objPos = objSpacePos; float4 clipPos = UnityObjectToClipPos(objSpacePos); o.depth = clipPos.z / clipPos.w; - float3 sdfNormal = BasicCube_normal(objPos) * float3(-1, 1, 1); + float3 sdfNormal = BasicCube_normal(objPos); o.normal = UnityObjectToWorldNormal(sdfNormal); return o; @@ -242,9 +259,9 @@ Custom30Output BasicWedge(v2f i) { clip(epsilon - d); #endif float3 objPos = ro + rd * d_acc; - o.objPos = objPos; // Transform from SDF space back to object space float3 objSpacePos = objPos + (i.objPos + frag_to_origin); + o.objPos = objSpacePos; float4 clipPos = UnityObjectToClipPos(objSpacePos); o.depth = clipPos.z / clipPos.w; @@ -327,9 +344,8 @@ Custom30Output BasicPlatform(v2f i) { clip(epsilon - d); #endif float3 objPos = ro + rd * d_acc; - o.objPos = objPos; - // Transform from SDF space back to object space float3 objSpacePos = objPos + (i.objPos + frag_to_origin); + o.objPos = objSpacePos; float4 clipPos = UnityObjectToClipPos(objSpacePos); o.depth = clipPos.z / clipPos.w; diff --git a/ssao.cginc b/ssao.cginc index 060b2da..ad20a28 100644 --- a/ssao.cginc +++ b/ssao.cginc @@ -7,29 +7,37 @@ #if defined(_SSAO) float get_ssao(v2f i, float3x3 tangentToWorld, out float2 debug) { + float3 objPos = mul(unity_WorldToObject, float4(i.worldPos, 1)); + float4 clipPos = UnityObjectToClipPos(objPos); + float4 screenPos = ComputeScreenPos(clipPos); + const float2 screen_uv = screenPos.xy / screenPos.w; + const float2 screen_px = screen_uv * _ScreenParams.xy; + const float fragment_depth = GetDepthOfWorldPos(i.worldPos, debug); - const float2 screen_px = (i.pos.xy + 0.5); - const float2 screen_uv = (i.pos.xy + 0.5) / _ScreenParams.xy; - const float ssao_theta = _SSAO_Noise.SampleLevel(point_repeat_s, screen_px * _SSAO_Noise_TexelSize.xy, 0) * TAU; + const float3 noise = _SSAO_Noise.SampleLevel(point_repeat_s, screen_px * _SSAO_Noise_TexelSize.xy, 0); + float ssao_theta = noise.x; + const float frame = ((float) AudioLinkData(ALPASS_GENERALVU + int2(1, 0)).x); + ssao_theta += frame * PHI; + ssao_theta *= TAU; const float2x2 ssao_rot = float2x2( cos(ssao_theta), -sin(ssao_theta), sin(ssao_theta), cos(ssao_theta)); - const float frame = ((float) AudioLinkData(ALPASS_GENERALVU + int2(1, 0)).x); - float ssao_occlusion = 0; const float ssao_eps = 1E-5; [loop] - for (uint ii = 0; ii < _SSAO_Samples; ii++) { + for (uint ii = 0; ii < round(_SSAO_Samples); ii++) { // Compute random vector in tangent space. // Get the index of the current pixels, on the range [0, screen_width] x // [0, screen_height]. // Map that onto the noise texture's pixels. Add an offset for each // index. float2 noise_uv = (float2(ii % _ScreenParams.x, ii * (_ScreenParams.z - 1.0f))) * _SSAO_Noise_TexelSize.xy; +#if 1 float3 sample_point = _SSAO_Noise.SampleLevel(point_repeat_s, noise_uv, 0).rgb; - // Use a low discrepancy sequence to transform each sample over time. - //sample_point = frac(sample_point + PHI * frame); +#else + float3 sample_point = frac(noise + PHI * ii); +#endif sample_point.xy = 2.0 * sample_point.xy - 1.0; sample_point.xy = mul(ssao_rot, sample_point.xy); -- cgit v1.2.3