Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Shadow acne issues when using shadow atten/shadow mask for toon shader

Discussion in 'Universal Render Pipeline' started by dancliffAuroch, Apr 3, 2023.

  1. dancliffAuroch

    dancliffAuroch

    Joined:
    Oct 17, 2022
    Posts:
    4
    Hi all,

    I've been trying to create a toon shader for our game recently in URP 2022.1.15f1, however, I've been running into issues with shadow acne when it comes to the shadow attenuation as well as sampling the shadow mask.

    I've recreated a basic setup for a toon shader that still has the problem. The trouble is, when I multiply the results by the shadow & distance attenuation there is really bad shadow acne occuring, however, when I don't do this the shader no longer receives shadows.


    If I plug the shadow attenuation into the base colour you can see what is getting remapped to the texture gradient.

    When I multiply by the sampled shadow mask instead of the shadow/distance attenuation I still get similar results.

    While it looks like this fixes it this actually doesn't as this only works when the ramp is split down the middle at a value of 0.5.

    If I change it to a smooth gradient for example you can see that the issue is still there, though it is improved slightly. I use a different ramp for different objects if I use a smooth gradient to as seem below to clearly show the issue or a gradient texture with multiple shading bandings it's very clear the issue. I can adjust it slightly using the URP asset shadow settings as well as the depth/normal bias settings but this doesn't fix the issue.

    This also occurs on additional light shadows as well.
    This is the stripped down code used in the graph example above.
    Code (CSharp):
    1. #ifndef CUSTOM_LIGHTING_DEMO_INCLUDED
    2. #define CUSTOM_LIGHTING_DEMO_INCLUDED
    3. #ifdef UNIVERSAL_LIGHTING_INCLUDED
    4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/RealtimeLights.hlsl"
    5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    6. #endif
    7. void MainLight_float (float3 WorldPos, out float3 Direction, out float3 Color, out float DistanceAtten, out float ShadowAtten){
    8.     #ifdef SHADERGRAPH_PREVIEW
    9.         Direction = float3(0.5, 0.5, 0);
    10.         Color = 1;
    11.         DistanceAtten = 1;
    12.         ShadowAtten = 1;
    13.     #else
    14.         #if SHADOWS_SCREEN
    15.             half4 clipPos = TransformWorldToHClip(WorldPos);
    16.             half4 shadowCoord = ComputeScreenPos(clipPos);
    17.         #else
    18.             half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    19.         #endif
    20.         Light mainLight = GetMainLight(shadowCoord);
    21.         Direction = mainLight.direction;
    22.         Color = mainLight.color;
    23.         DistanceAtten = mainLight.distanceAttenuation;
    24.         //hadowAtten = mainLight.shadowAttenuation;
    25.  
    26.     #if !defined(_MAIN_LIGHT_SHADOWS) || defined(_RECEIVE_SHADOWS_OFF)
    27.     ShadowAtten = 1.0f;
    28.     #endif
    29.  
    30.     #if SHADOWS_SCREEN
    31.     ShadowAtten = SampleScreenSpaceShadowmap(shadowCoord);
    32.     #else
    33.     ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
    34.     float shadowStrength = GetMainLightShadowStrength();
    35.     ShadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_MainLightShadowmapTexture,
    36.     sampler_MainLightShadowmapTexture),
    37.     shadowSamplingData, shadowStrength, false);
    38.     #endif
    39.     #endif
    40.  
    41. }
    42. void Shadowmask_half (float2 lightmapUV, out half4 Shadowmask){
    43.     #ifdef SHADERGRAPH_PREVIEW
    44.         Shadowmask = half4(1,1,1,1);
    45.     #else
    46.         OUTPUT_LIGHTMAP_UV(lightmapUV, unity_LightmapST, lightmapUV);
    47.         Shadowmask = SAMPLE_SHADOWMASK(lightmapUV);
    48.     #endif
    49. }
    50. #ifndef SHADERGRAPH_PREVIEW
    51.     #include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/ShaderPass.hlsl"
    52.     #if (SHADERPASS != SHADERPASS_FORWARD)
    53.         #undef REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR
    54.     #endif
    55. #endif
    56. void MainLightShadows_float (float3 WorldPos, half4 Shadowmask, out float ShadowAtten){
    57.     #ifdef SHADERGRAPH_PREVIEW
    58.         ShadowAtten = 1;
    59.     #else
    60.         float4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    61.         ShadowAtten = MainLightShadow(shadowCoord, WorldPos, Shadowmask, _MainLightOcclusionProbes);
    62.     #endif
    63. }
    64. #endif
    65.  

    Any help is greatly appreciated! I'm sure I must be missing something but I've search everywhere online and can't find a solution anywhere.
     
    ScetticoBlu and DanWeston like this.
  2. ScetticoBlu

    ScetticoBlu

    Joined:
    Nov 26, 2020
    Posts:
    17
    Did you find a solution? I have the same problem in 2021.3.10f
     
  3. Allan-MacDonald

    Allan-MacDonald

    Joined:
    Sep 21, 2015
    Posts:
    77
    I've noticed issues similar to that if I try to get the termination between light and shadow to go too far beyond the natural termination you would get from N dot L
     
  4. NathoSteveo

    NathoSteveo

    Joined:
    Dec 7, 2019
    Posts:
    7
    bump

    I'm getting same results as op:

     
    Last edited: Aug 23, 2023
  5. dancliffAuroch

    dancliffAuroch

    Joined:
    Oct 17, 2022
    Posts:
    4
    Unfortunately, I've still not found a solution so would still love to hear if anyone does make any progress on this issue!
     
  6. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,402
  7. dancliffAuroch

    dancliffAuroch

    Joined:
    Oct 17, 2022
    Posts:
    4
    Ah, I was using that as a reference but that still has/doesn't fix the issue
     
  8. Allan-MacDonald

    Allan-MacDonald

    Joined:
    Sep 21, 2015
    Posts:
    77
    I think it's because you have receive shadows on but are also moving the terminator of your toon shadow back behind the apex of the curve too much