Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Shadow and distance attenuation do not work in URP Unlit Graph

Discussion in 'Universal Render Pipeline' started by CogumeloSoft, Jan 3, 2020.

  1. Bovine

    Bovine

    Joined:
    Oct 13, 2010
    Posts:
    106
    So a related issue for me was that additional light shadow has no attenuation. This was solved for me by looking at the lighting.hlsl which contains two forms of GetAdditionalLight(), one of which accepts a shadow mask and the other does not. Without suppling the shadowmask (i.e. half4(1,1,1,1)) the shadowAttenuation is otherwise 1
     
    Last edited: Jan 12, 2021
    The_Swiss_Guy likes this.
  2. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    593
    Anyone resolved this issue in URP 11.0?
    After trying all of the above I still can't recieve shadows in my unlit graph...
     
  3. CogumeloSoft

    CogumeloSoft

    Joined:
    Dec 28, 2012
    Posts:
    76
    I just give up and have been using lit instead.
     
  4. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    593
    Hi, Actually yesterday I found a blog that gave me a tip.

    Code (CSharp):
    1.     // --- RUDIMENTARY SHADOWs   Keywords: _MAIN_LIGHT_SHADOWS_CASCADE && _SHADOWS_SOFT --- \\  
    2.     float4 shadowCoord = TransformWorldToShadowCoord(WPos);      
    3.     float shadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture),
    4.     GetMainLightShadowSamplingData(), GetMainLightShadowStrength(), false);
    Use this code before the main light function call and multiply the lighting by 'shadowAtten', and don't forget to defing the keywords in the graph.

    This is the final custom function:
    Code (CSharp):
    1. #ifndef CUSTOM_LIGHTING_INCLUDED
    2. #define CUSTOM_LIGHTING_INCLUDED
    3.  
    4. /* IN(5): SpecColor(4), Smoothness(1), WPos(3), WNormal(3), WView(3) */
    5. /* OUT(2): Diffuse(3), Specular(3) NdotL(1) for toon ramp: point,clamp) */
    6.  
    7. void CalculateLights_half(half4 SpecColor, half Smoothness, half3 WPos, half3 WNormal, half3 WView,
    8.                           out half3 Diffuse, out half3 Specular)
    9. {          
    10.     Diffuse = 0;
    11.     Specular = 0;
    12.     #ifndef SHADERGRAPH_PREVIEW
    13.  
    14.     Smoothness = exp2(10 * Smoothness + 1);  // WNormal = normalize(WNormal);    WView = SafeNormalize(WView);
    15.    
    16.     // --- RUDIMENTARY SHADOWs   Keywords: _MAIN_LIGHT_SHADOWS_CASCADE && _SHADOWS_SOFT --- \\  
    17.     float4 shadowCoord = TransformWorldToShadowCoord(WPos);
    18.     float shadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture),
    19.     GetMainLightShadowSamplingData(), GetMainLightShadowStrength(), false);
    20.    
    21.     Light light = GetMainLight(shadowCoord); // Main Pixel Light
    22.     half3 attenCol = light.color * light.distanceAttenuation * shadowAtten;
    23.  
    24.     Diffuse = LightingLambert(attenCol, light.direction, WNormal);     /* LAMBERT */        
    25.     Specular = LightingSpecular(attenCol, light.direction, WNormal, WView, SpecColor, Smoothness); /*Blinn-Phong*/    
    26.  
    27.     #endif
    28. }
    29. #endif
    The shadows and pitch black and not attenuated, maybe I'm still missing something, but it's a start.
     
    CogumeloSoft likes this.
  5. Ologon

    Ologon

    Joined:
    Nov 16, 2014
    Posts:
    13
    After doing a bit of research I've found two solutions. In both cases they work with the function MainLight_half shown here without modifications (You only need to change #if SHADERGRAPH_PREVIEW into #if defined(SHADERGRAPH_PREVIEW)):
    https://blogs.unity3d.com/2019/07/31/custom-lighting-in-shader-graph-expanding-your-graphs-in-2019/

    -> Solution 1: Use the Lit graph
    1) As suggested before, just plug your custom lighting into the "Emission" node, setting to 0
    everything else.
    2) Add _MAIN_LIGHT_SHADOWS and _MAIN_LIGHT_SHADOWS_CASCADE keywords in Shader Graph and set them to multi compile & global.


    -> Solution 2: Use the Unlit graph
    1) Make sure in your URP shadow settings the cascade count is set to 2 or above.
    2) Add _MAIN_LIGHT_SHADOWS and _MAIN_LIGHT_SHADOWS_CASCADE keywords in Shader Graph and set them to multi compile & global.

    The unlit shader will only work with cascade shadows.
    If you want to support non-cascade shadows, then you'll have to make changes to the generated shader as Unity looks for a new Varying called "shadowCoord" which the unlit graph doesn't generate. (In this case, you're probably better off using the Lit graph instead)
     
    Last edited: Feb 9, 2021
  6. CogumeloSoft

    CogumeloSoft

    Joined:
    Dec 28, 2012
    Posts:
    76
    "Solution 1: Use the Lit graph"
    This seems the easier way. That said i found this and they are using Unlit. I'm yet to download it to check
     
  7. Ologon

    Ologon

    Joined:
    Nov 16, 2014
    Posts:
    13
    Interesting, thanks for sharing! I took a look at their project and found out what's going on:
    As I wrote above, the unlit graph can't handle non-cascade shadows as it lacks the "shadowCoord" varying, so I suggested adding the two defines to force the shader to use cascade shadows. (The NG combination of keywords is when _MAIN_LIGHT_SHADOWS is defined but _MAIN_LIGHT_SHADOWS_CASCADE is not. That's when Unity will look for shadowCoord)

    When using a multi_compile keyword I believed Unity would compile all shader combinations, including the error one, but that appears not to be the case. It's only compiling them as necessary.
    Since the above project is using cascade shadows, everything works fine. If you try to set the cascade count to 1 in their URP asset, Boom, the scene turns pink and all their graphs don't work anymore :)

    I updated my solution 2 above to this simpler one
     
    Last edited: Feb 10, 2021
  8. RChrispy

    RChrispy

    Joined:
    Dec 18, 2013
    Posts:
    62
    2021.1 This is also not working :)
    The TransformWorldToShadowCoord seems to not work at all so I tried the
    half cascadeIndex = ComputeCascadeIndex(WorldPos);
    half4 shadowCoord = mul(_MainLightWorldToShadow[cascadeIndex], float4(WorldPos, 1.0));

    But also no shadows from other objects. This is really annoying...
     
  9. RChrispy

    RChrispy

    Joined:
    Dec 18, 2013
    Posts:
    62
  10. brunnoart3d

    brunnoart3d

    Joined:
    May 3, 2020
    Posts:
    1
    I'm using the unity 2020.2.6f1 version, and the URP version 10.3.1.
    I dont know about shader code so, i watched this tutorial in you tube:
    , and i did all the stuffs he did.

    The problem happends when i do the cast shadows (according to the tutorial) and didn't work for me. The material works, but cast shadows doesn't.
    After this, i change the MainLight_half function (that you guys posted), and does not work.
    and i tryed include the:
    #pragma multi_compile _ _MAIN_LIGHT_SHADOW
    #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    and and does not work too.
    can you help me guys ?
     
  11. RChrispy

    RChrispy

    Joined:
    Dec 18, 2013
    Posts:
    62
    Try the settings I posted on my screenshot.
     
unityunity