Search Unity

Question Cascaded Shadowmaps and custom lighting in shadergraph

Discussion in 'Shader Graph' started by AverageCG, Nov 23, 2020.

  1. AverageCG

    AverageCG

    Joined:
    Nov 13, 2017
    Posts:
    19
    Hey, for a project i'm making a toon shader and following different posts about how to achieve custom lighting with shadergraph. One of the main ressources has been the unity open project (UOP) and the recent video about it's toon shading. In the video they specificly mention that they want cascade compatibility, however it does not work for me. Past the first cascade shadows are dissappearing or appearing out of place.

    for reference here's the custom function code i'm using. the if!defined section currently commented out because I cannot define the keyword even though it seems to work for the uop toon shader (which is also unlit master).

    Code (CSharp):
    1. void MainLight_float(float3 WorldPos, out float3 Direction, out float3 Color, out float DistanceAtten, out float ShadowAtten)
    2. {
    3. #if SHADERGRAPH_PREVIEW
    4.     Direction = float3(0.5, 0.5, 0);
    5.     Color = 1;
    6.     DistanceAtten = 1;
    7.     ShadowAtten = 1;
    8. #else
    9.     float4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    10.  
    11.     Light mainLight = GetMainLight(shadowCoord);
    12.     Direction = mainLight.direction;
    13.     Color = mainLight.color;
    14.     DistanceAtten = mainLight.distanceAttenuation;
    15.  
    16. //#if !defined(_MAIN_LIGHT_SHADOWS) || defined(_RECEIVE_SHADOWS_OFF)
    17. //    ShadowAtten = 1.0h;
    18. //#else
    19.     ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
    20.     float shadowStrength = GetMainLightShadowStrength();
    21.     ShadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_MainLightShadowmapTexture,
    22.         sampler_MainLightShadowmapTexture),
    23.         shadowSamplingData, shadowStrength, false);
    24. //#endif
    25. #endif
    26. }
    here's a screenshot of the issue; on the sing you can see the shadow getting cut off. This line moves as i change the shadow cascade % for the first cascade. I'm a little lost on the whole thing...

    shadowIssue.PNG
     
  2. StuwuStudio

    StuwuStudio

    Joined:
    Feb 4, 2015
    Posts:
    165
    Necro because I stumbled upon this and no one had answered:


    Code (CSharp):
    1.  
    2. void MainLight_float(float3 WorldPos, out float3 Direction, out float3 Color, out float DistanceAtten, out float ShadowAtten
    3. {
    4. #if SHADERGRAPH_PREVIEW
    5.     Direction = float3(0.5, 0.5, 0);
    6.     Color = 1;
    7.     DistanceAtten = 1;
    8.     ShadowAtten = 1;
    9.     debug = (float4)0;
    10. #else
    11.     half cascadeIndex = ComputeCascadeIndex(WorldPos);
    12.     float4 shadowCoord = mul(_MainLightWorldToShadow[cascadeIndex], float4(WorldPos, 1.0));
    13.    
    14.     Light mainLight = GetMainLight(shadowCoord);
    15.     Direction = mainLight.direction;
    16.     Color = mainLight.color;
    17.     DistanceAtten = mainLight.distanceAttenuation;
    18.    
    19.     ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
    20.     half4 shadowParams = GetMainLightShadowParams();
    21.     ShadowAtten = SampleShadowmap(TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), shadowCoord, shadowSamplingData, shadowParams, false);
    22. #endif
    23. }
    NOTE!!! This will break the screen space shadows option, and this won't work with non cascade lights.
     
    Extelen1 and SomethingMauser like this.