diff options
| -rw-r--r-- | late_pbr.cginc | 14 | ||||
| -rwxr-xr-x | lighting.cginc | 8 | ||||
| -rw-r--r-- | ssfd.cginc | 12 |
3 files changed, 21 insertions, 13 deletions
diff --git a/late_pbr.cginc b/late_pbr.cginc index 5b1e324..f748c73 100644 --- a/late_pbr.cginc +++ b/late_pbr.cginc @@ -6,21 +6,9 @@ #include "pbr.cginc" #include "ssfd.cginc" -void apply_ssfd(v2f i, float2 uv, LightData l, inout float3 albedo) { -#if defined(_SSFD) - float ssfd_mask = ssfd(uv, _SSFD_Scale, _SSFD_Max_Fwidth, 0, _SSFD_Noise); - float ssfd_mask_fw = fwidth(ssfd_mask); - // TODO I think anti aliasing is probably broken - float NoL = -dot(i.normal, l.indirect.diffuse_dominant_dir); - float ssfd_threshold = saturate(NoL * 0.5 + 0.5 + _SSFD_Threshold); - ssfd_mask = smoothstep(ssfd_threshold - ssfd_mask_fw * 0.5, ssfd_threshold + ssfd_mask_fw * 0.5, ssfd_mask); - albedo += ssfd_mask * _SSFD_Tint.rgb; -#endif -} - void latePbr(v2f i, LightData l, inout Pbr pbr) { apply_matcaps_and_rim_lighting(i, pbr, l); - apply_ssfd(i, i.uv01.xy, l, pbr.albedo.xyz); + apply_ssfd(i, i.uv01.xy, l, pbr.normal, pbr.albedo.xyz); } #endif // __LATE_PBR_INC diff --git a/lighting.cginc b/lighting.cginc index 0287f51..8b6693c 100755 --- a/lighting.cginc +++ b/lighting.cginc @@ -145,7 +145,15 @@ float4 getIndirectDiffuse(v2f i, Pbr pbr, inout LightData light) { ); // Multiply unit vector by L1 matrix to get vector pointing in direction of // light. + float3 raw_dir = mul(mat, float3(1,1,1)); + float raw_dir_len = length(raw_dir); float3 dom_dir = normalize(mul(mat, float3(1,1,1))); + if (abs(raw_dir_len) < 1e-3) { + dom_dir = light.direct.dir; + } else { + dom_dir = raw_dir; + } + dom_dir = normalize(dom_dir); light.indirect.diffuse_dominant_dir = dom_dir; #endif @@ -54,5 +54,17 @@ float ssfd(float2 uv, float scale, float max_fwidth, float2 uv_offset, texture3D } #endif // _SSFD +void apply_ssfd(v2f i, float2 uv, LightData l, float3 normal, inout float3 albedo) { +#if defined(_SSFD) + float ssfd_mask = ssfd(uv, _SSFD_Scale, _SSFD_Max_Fwidth, 0, _SSFD_Noise); + float ssfd_mask_fw = fwidth(ssfd_mask); + // TODO I think anti aliasing is probably broken + float light_amount = 1.0 - (dot(l.indirect.diffuse_dominant_dir, normal) * 0.5 + 0.5); + float ssfd_threshold = saturate(light_amount + _SSFD_Threshold); + ssfd_mask = smoothstep(ssfd_threshold - ssfd_mask_fw * 0.5, ssfd_threshold + ssfd_mask_fw * 0.5, ssfd_mask); + albedo += ssfd_mask * _SSFD_Tint.rgb; +#endif +} + #endif // __SSFD_INC |
