Search Unity

Question Shadow Acne Not Influenced At All By Shadow Bias

Discussion in 'Shaders' started by Nofer, Oct 27, 2022.

  1. Nofer

    Nofer

    Joined:
    Jan 19, 2021
    Posts:
    2
    I'm writing a cel shader and the shadow acne issue occurred.
    upload_2022-10-26_22-56-58.png
    The problem is that both the normal and depth bias make no difference at all on the shadow, while they do reduced the acne on other objects.
    I once copied & pasted someone else's cel shader into the project for testing purposes and the acne appears as well. So probably it is a project/pipeline setting issue instead of a shader issue, but I'm not sure.
    I'm using urp and Unity 2020.3.23. Code attached below.
    Code (CSharp):
    1. Shader "CustomShaders/CelShader"
    2. {
    3.     Properties
    4.     {
    5.         _SurfaceColor ("Surface Color", Color) = (1, 1, 1, 1)
    6.         _Smoothness ("Smoothness Coefficient", Range(0.0, 1.0)) = 1.0
    7.         _RimCoe ("Rim Coefficient", Range(0.0, 1.0)) = 1.0
    8.  
    9.         _DiffuseThreshold ("Diffuse Threshold",  Range(0.0, 1.0)) = 0.5
    10.         _SpecularThreshold ("Specular Threshold",  Range(0.0, 1.0)) = 0.5
    11.         _RimThreshold ("Rim Threshold",  Range(0.0, 1.0)) = 0.5
    12.         _ShadowThreshold ("Shadow Threshold",  Range(0.0, 1.0)) = 0.5
    13.  
    14.         //_OutlineScaler ("Outline Scaler",  Range(0.0, 1.0)) = 0.5
    15.         //_OutlineColor ("Outline Color", Color) = (0, 0, 0, 1)
    16.     }
    17.  
    18.     HLSLINCLUDE
    19.     ENDHLSL
    20.  
    21.     SubShader
    22.     {
    23.         Tags { "RenderType"="Opaque" }
    24.         LOD 100
    25.  
    26.         // Cel shader
    27.         Pass
    28.         {
    29.             Cull Back
    30.  
    31.             Tags
    32.             {
    33.                 "LightMode" = "UniversalForward"
    34.             }
    35.  
    36.             HLSLPROGRAM
    37.             #pragma vertex vert
    38.             #pragma fragment frag
    39.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    40.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    41.  
    42.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    43.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    44.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
    45.    
    46.             struct a2v
    47.             {
    48.                 float4 vertexOS : POSITION;
    49.                 float3 normalOS : NORMAL;
    50.                 float2 uv : TEXCOORD0;
    51.             };
    52.  
    53.             struct v2f
    54.             {
    55.                 float4 vertexCS : SV_POSITION;
    56.                 float4 vertexWS : TEXCOORD0;
    57.                 float3 normalWS : TEXCOORD1;
    58.                 //SHADOW_COORDS(2)
    59.             };
    60.  
    61.             float4 _SurfaceColor;
    62.             float _Smoothness;
    63.             float _RimCoe;
    64.  
    65.             float _DiffuseThreshold;
    66.             float _SpecularThreshold;
    67.             float _RimThreshold;
    68.             float _ShadowThreshold;
    69.  
    70.             v2f vert (a2v v)
    71.             {
    72.                 v2f o;
    73.                 o.vertexCS = mul(UNITY_MATRIX_MVP, v.vertexOS);
    74.                 o.vertexWS = mul(UNITY_MATRIX_M, v.vertexOS);
    75.                 o.normalWS = mul((float3x3)UNITY_MATRIX_M, v.normalOS);
    76.  
    77.                 return o;
    78.             }
    79.  
    80.             float4 frag (v2f i) : SV_Target
    81.             {
    82.                 // Get light
    83.                 Light l = GetMainLight(TransformWorldToShadowCoord(i.vertexWS.xyz));
    84.  
    85.                 // Calculate shadow
    86.                 float shadow = step(_ShadowThreshold, saturate(l.shadowAttenuation));
    87.  
    88.                 // Calculate diffuse
    89.                 float diffuse = saturate(dot(i.normalWS, l.direction));
    90.                 diffuse *= shadow;
    91.                 diffuse = step(_DiffuseThreshold, diffuse);
    92.  
    93.                 // Get view direction
    94.                 float3 viewDirWS = normalize(_WorldSpaceCameraPos.xyz - i.vertexWS.xyz);
    95.                 float3 h = normalize(l.direction + viewDirWS);
    96.                 // Calculate specular
    97.                 float specular = pow(saturate(dot(i.normalWS, h)), exp2(10 * _Smoothness + 1));
    98.                 specular *= diffuse * _Smoothness;
    99.                 specular = step(_SpecularThreshold, specular);
    100.  
    101.                 // Calculate rim light
    102.                 float rim = 1 - dot(viewDirWS, i.normalWS);
    103.                 rim *= pow(diffuse, _RimCoe);
    104.                 rim = step(_RimThreshold, rim);
    105.  
    106.                 // Get ambient
    107.                 //float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
    108.                 float3 ambient = float3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
    109.  
    110.                 float4 outColor = float4(l.color.rgb * (diffuse + max(specular, rim)) + ambient, 1) * _SurfaceColor;
    111.  
    112.                 return float4(shadow, shadow, shadow, 1);
    113.                 //return outColor;
    114.             }
    115.  
    116.             ENDHLSL
    117.         }
    118.  
    119.         // Shadow
    120.         UsePass "VertexLit/SHADOWCASTER"
    121.     }
    122. }
    123.  
     

    Attached Files: