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

Bug URP. No Shadows in build

Discussion in 'Universal Render Pipeline' started by kbowsher, Jan 12, 2023.

  1. kbowsher

    kbowsher

    Joined:
    Aug 30, 2022
    Posts:
    1
    Hello,

    Some context, we are using Unity 2021.3.5f1 and URP 12.1.7.
    As well, sorry for the very long post but I felt that it would be best to try and explain everything since I looked everywhere for a solution and have tried many things to fix this problem.

    We are facing an issue with missing shadows whenever we build our game. Shadows appear fine when playing in the editor but not in a build. We are not entirely sure why this is happening but we believe it may be to do with shader stripping since when we enable that option in URP Global Settings, the shadows disappear in the build. However, if we disable shader stripping, then the shadows appear but this causes the build times to take an incredibly long time. Essentially, we having a similar issue as this post https://forum.unity.com/threads/no-shadows-in-asset-bundles-or-standalone-builds.1271534/

    We have different URP Assets for different quality settings which a user can change in game. The quality settings range from Fastest to Beautiful with Fastest having nearly every option including shadows disabled and Beautiful having all options enabled.

    When doing a build with shader stripping, since the Fastest quality setting has shadows disabled, this causes all the other quality settings to not have shadows appear even though the option is enabled in their URP assets. Things get a little weird when I enable shadows for the Fastest quality setting and then disable shadows in any other quality settings and create another build. What happens is that any quality setting that has shadows enabled will render shadows and any quality setting that disables shadows won’t render shadows which is to be expected.

    It seems like Unity compiles the shader variants only for the first quality level (which in our case is Fastest) and ignores the other quality levels.
    During a build, does Unity take priority of the first quality level when compiling shader variants?

    We can just enable shadows for all URP Assets and this does somewhat fix our problem but we need the Fastest quality to not have shadows since it's used a lot for CPU performance testing.

    Since the issue seems to be related to shadows. I have looked to see if the shadow keywords are being removed due to shader stripping. I have enabled the “Log Shader Compilation” option to help see which variants are being used in the build. I have two builds with the stripping option enabled and disabled and have compared the differences. What I found is that the _MAIN_LIGHT_SHADOW keyword gets removed when using shader stripping but weirdly, the _MAIN_LIGHT_SHADOW_CASCADE keyword remains for both builds.
    Is there a way of keeping the _MAIN_LIGHT_SHADOW keyword while stripping the unused keywords?

    As well, during my search for a solution to this problem, I came across a few of these posts.
    https://forum.unity.com/threads/urp-simple-lit-shader-variants-missing-in-build-alpha-clip.1182385/
    https://forum.unity.com/threads/understanding-shaders-and-addressables.1293390/
    These posts mention issues related to addressable (which we use) and shader variants. I'm unsure if Addressable are causing the issue that we have but I have tried following all the solutions/suggestions in those posts to try and solve our problem but sadly none of them worked.

    I do want to mention that I'm unfamiliar with addressable and their effects on shader variants.
    Should all materials and shader (including the shader variant collection) be included in the same Addresseable group?. If there not, does this affect how Unity compiles the shader variants?

    Finally, again sorry for the long post. Some of the posts that I looked at also mentioned implementing our own shader stripping script since Unity's version is supposedly very aggressive with shader stripping. If we do implement our own shader stripping, do we have control of what variants we can keep and remove?
     
    SealDev and mezzanine510 like this.
  2. sh0v0r

    sh0v0r

    Joined:
    Nov 29, 2010
    Posts:
    320
    I just ran into the same issue, first time I attempted a build and there are no shadows and possibly other missing rendering features.

    URP 2021.3.18f1
     
  3. mezzanine510

    mezzanine510

    Joined:
    Mar 7, 2018
    Posts:
    5
    I've run into the same issue here.

    I'm using Unity 2022.2.5f1, Forward Renderer.

    When I use Forward+ Renderer, I get no lights, and flashing textures on everything except the terrain. Not sure if this is related, but thought I'd throw it in there.
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,402
    With all issues:
    If you can reproduce them, make a bug report.
    Posting here won't get it fixed
     
  5. SealDev

    SealDev

    Joined:
    Feb 13, 2016
    Posts:
    13
    Same issue! Foliage has shadows in editor, but not in build!

    URP 2021.3.18f1
     
  6. tichise

    tichise

    Joined:
    Apr 23, 2020
    Posts:
    5
    I am experiencing the same problem in my environment.
    Unity 2022.3.20f1
     
  7. arrrky_work

    arrrky_work

    Joined:
    Apr 17, 2023
    Posts:
    2
    In my case - it happened with our custom shaders. Worked fine in editor - no shadows in builds.
    We switched to Unity 2021 (from 2020). A lot of shaders should be fixed.
    What our tech artist did to fix the issues with shadows:

    Old keywords variant:
    #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    New variant:
    #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN

    Also was removed #pragma prefer_ / exclude. But I guess that's not the case.

    So if nothing helps - check shaders that cause troubles.

    Not sure how it works. May be because of keywords stripping etc.
     
  8. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,402
    2021+ uses a lighting enum instead of seperate keywords. This is the same for 2022 and all new versions.
    So this fix is the right one if you use custom shaders
     
  9. naradya

    naradya

    Joined:
    Jun 11, 2022
    Posts:
    1

    Saved my day! Thank you so much!
     
    Coco07 and Mpsteve137 like this.
  10. SachinGanesh

    SachinGanesh

    Joined:
    Jun 28, 2015
    Posts:
    19
    upload_2023-9-22_12-13-32.png

    Unticking downsample in URPRenderer worked for me