Search Unity

URP lit shader will not receive shadows while transparency is enabled

Discussion in 'Universal Render Pipeline' started by Centripetal, Dec 8, 2019.

  1. gamer2300

    gamer2300

    Joined:
    Jan 16, 2013
    Posts:
    31
    for me unity3d team think that who use unity is really stupid. they create bugs at each version and until using unity 2021 this problem is not solved. i try even amplify shader (that i buy on asset store with real money..) so this is only a unity question. they do with URP what they done with UNET, its only a big fake, otherwise, who don't need shadows on trasparent objects?
     
  2. The_L3rNa3aN

    The_L3rNa3aN

    Joined:
    May 14, 2020
    Posts:
    3
    Didn't work for me unfortunately. I also tried to change that line of code in the package and Unity apparently rebuilt the thing, removing the changes I made.
     
  3. DiceMaster5

    DiceMaster5

    Joined:
    Nov 19, 2015
    Posts:
    12
    This bug is still present for me in Unity 2021.2.0f1
    Is this a shader issue or a built in Unity graphics issue?
    Are there any fixes or workarounds?
     
  4. pseudocolor

    pseudocolor

    Joined:
    Dec 15, 2019
    Posts:
    4
    just use surface type: opaque with alpha clipping, fixed for me
     
  5. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    Are there some repro steps for this to break on recent Unity versions? I tried this on 2021.3's URP using it's own Lit - transparent and also using Lit SG and I always got shadows on transparent materials.
     
  6. rocket5tim

    rocket5tim

    Joined:
    May 19, 2009
    Posts:
    242
    For shadows to appear on transparent objects, assign a URP Asset in the Quality Settings and use the Universal Render Pipeline/Lit or Simple Lit shader with Surface Type set to Transparent. If you don't have the URP assets assigned and you're just using the Standard shader you won't get any shadows.
     
  7. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    13,348
    I have a custom URP Opaque shader that received shadows fine in Unity 2019 and now does not in Unity 2021.3, is there something changed in the shader libs for that case ?

    EDIT:

    I found the cause, in the below code seems defaults to the second line than the first, seems _MAIN_LIGHT_SHADOWS fails to register or something, even though i have it in multi compile etc

    Code (csharp):
    1.  
    2. #if _MAIN_LIGHT_SHADOWS
    3.                 mainLight = GetMainLight(TransformWorldToShadowCoord(positionWS));
    4. #else
    5.                 mainLight = GetMainLight();
    6. #endif
    7.  
    Is there something i am missing ? If i use the below code works

    Code (csharp):
    1.  
    2. #if _MAIN_LIGHT_SHADOWS
    3.                 mainLight = GetMainLight(TransformWorldToShadowCoord(positionWS));
    4. #else
    5.                 mainLight = GetMainLight(TransformWorldToShadowCoord(positionWS));
    6. #endif
    7.  
     
    Last edited: May 9, 2022
  8. bbsuuo

    bbsuuo

    Joined:
    Jul 21, 2016
    Posts:
    16
    you can copy Lit shader and C# ShaderGUI (LitShader and LitDetailGUI , change namespace youself)
    and change copied LitShader function
    public override void ValidateMaterial(Material material)
    {
    SetMaterialKeywords(material, LitGUI.SetMaterialKeywords, LitShaderDetailGUI.SetMaterialKeywords);
    material.SetShaderPassEnabled("ShadowCaster", true);
    }
    and change copied Lit shader
    CustomEditor " your shader gui nameapce.name"

    and you can don't have to change the source code frequently


    urp author seems to think transparent objects shouldn't create shadows. That's the code he wrote

    // Cast Shadows
    bool castShadows = true;
    if (material.HasProperty(Property.CastShadows))
    {
    castShadows = (material.GetFloat(Property.CastShadows) != 0.0f);
    }
    else
    {
    if (isShaderGraph)
    {
    // Lit.shadergraph or Unlit.shadergraph, but no material control defined
    // enable the pass in the material, so shader can decide...
    castShadows = true;
    }
    else
    {
    // Lit.shader or Unlit.shader -- set based on transparency
    castShadows = Rendering.Universal.ShaderGUI.LitGUI.IsOpaque(material);
    }
    }
    material.SetShaderPassEnabled("ShadowCaster", castShadows);
     
    yokitc likes this.
  9. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    766
    Hi, some keywords have changed in 2021.2, see this thread.

    You can replace:
    #if _MAIN_LIGHT_SHADOWS


    With this:
    #if defined(_MAIN_LIGHT_SHADOWS) || defined(_MAIN_LIGHT_SHADOWS_CASCADE) || defined(_MAIN_LIGHT_SHADOWS_SCREEN)


    By the way, we can disable main light shadow in URP, so maybe adding another keyword:
    && !defined(_RECEIVE_SHADOWS_OFF)


    Or replace with this (the same with URP's Lit shader):
    #if defined(MAIN_LIGHT_CALCULATE_SHADOWS)


    "MAIN_LIGHT_CALCULATE_SHADOWS" will be defined somewhere (*.hlsl) when the shadow is enabled.

    Don't forget to change the pragma:
    #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
     
    Last edited: Jun 25, 2022
    nasos_333 likes this.
  10. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    13,348
    Thanks, very useful :)
     
    wwWwwwW1 likes this.
  11. olvasztar

    olvasztar

    Joined:
    Jun 21, 2022
    Posts:
    1
    Thanks for the clear explanation! Finally it casts shadows!
     
  12. yokitc

    yokitc

    Joined:
    Sep 25, 2019
    Posts:
    6
    Solved with this!
    Thank you very much!
     
  13. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    13,348
    The issue with this is that is no longer true transparent item, so will have more hard edges borders, i suppose could use 2 different passes, one for color with true transparency and one for shadow with the cutoff.
     
  14. StripeGuy

    StripeGuy

    Joined:
    Dec 30, 2016
    Posts:
    52
    Any fixes for this? The code change doesn't work anymore. It's no longer true or false, and they seem to imply that there's a "castshadow" checkbox in the Lit material (there isn't). Even if you want to force it to true, you can't cause it gets reverted immediatly on save.

    I have a meterial that needs to "fade" out, and the shadow fades out with it.
     
    hirama-nobuhiro likes this.
  15. michelecipriani80

    michelecipriani80

    Joined:
    May 21, 2021
    Posts:
    10
    This is still happening with URP v14.0.8 on Unity 2022.3.6f1 LTS
    is there any workaround to make an object fade?
     
  16. lecramr

    lecramr

    Joined:
    Jun 22, 2023
    Posts:
    7
    Same issue
     
  17. fleity

    fleity

    Joined:
    Oct 13, 2015
    Posts:
    345
    as far as I know
    receiving shadows on transparent objects: not a problem, as pointed to above activate that on the urp asset
    casting shadows from transparent objects if the shadow can be fully opaque: again easy, a custom shadergraph without any major changes has that Cast Shadow toggle.

    Now Fading Shadows from the receiving end (aka fading all shadows on an object) easy as well if you are willing write the lighting code yourself.

    But Fading Shadows from the casting end... very difficult. Look at the URP .shader files. All shaders have a shadowcaster pass which write a fragments depth value into the current rendertarget, the current rendertarget at that moment being the shadow map. A depth value can not be faded, it can only be blended but that is not 50% shadow on that pixel but rather a different position for the shadow.
    Your best chance to get this imo is dithering the shadow caster. You want the shader to support alpha clipping but keep the clip value at 1 in the base pass and only in the shadow caster pass do the alpha clipping. Which can be done with a custom function node in shadergraph

    something like this
    Code (CSharp):
    1. #ifdef SHADERPASS_SHADOWCASTER
    2. alpha = someInputDitherPattern;
    3. alphaClip = fadeValue;
    4. #else
    5. alpha = fadeValue;
    6. alphaClip = 1;
    7. #endif