Search Unity

Receive shadows for custom HLSL shader in URP?

Discussion in 'Universal Render Pipeline' started by scoat2, Feb 6, 2020.

  1. scoat2

    scoat2

    Joined:
    Jun 21, 2018
    Posts:
    7
    Hi,
    I'm making a custom shader in URP and I want it to receive shadows. Is there any #include I need to use in order to get shadows working? And if so what are the macros needed?
     
  2. scoat2

    scoat2

    Joined:
    Jun 21, 2018
    Posts:
    7
    MlleBun likes this.
  3. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    In the upcoming 7.2.0 we have written an upgrade guide. sampling shadow is unified and easier now as we always resolve shadows in light space. (no more screen space shadow resolve).

    Here's the excerpt of the Upgrade Guide:

    ## Sampling shadows from the Main Light
    In previous versions of URP, if shadow cascades were enabled for the main Light, shadows would be resolved in a screen space pass. The pipeline now always resolves shadows while rendering opaques or transparent objects. This allows for consistency and solved many issues regarding shadows.
    If have custom hlsl shaders and sample `_ScreenSpaceShadowmapTexture` texture, you must upgrade them to sample shadows by using the `GetMainLight` function instead.
    For example:

    Code (CSharp):
    1. float4 shadowCoord = TransformWorldToShadowCoord(positionWorldSpace);
    2. Light mainLight = GetMainLight(inputData.shadowCoord);
    3. // now you can use shadow to apply realtime occlusion
    4. half shadow = mainLight.shadowAttenuation;
    You must also define the following in your .shader file to make sure your custom shader can receive shadows correctly:

    Code (CSharp):
    1. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    2. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
     
  4. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    314
    is this guide available somewhere to this date?
     
  5. Bovine

    Bovine

    Joined:
    Oct 13, 2010
    Posts:
    195
    What if you're trying to obtain shadow data from additional lights? I can get this working with the main light but not others.
     
  6. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    187
    This seems to be returning incorrect results for me when using shadow cascades.
    I'm using all of these, but I'm still getting strange artifiacts.

    Code (CSharp):
    1.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    2.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    3.             #pragma multi_compile _ _SHADOWS_SOFT
     
  7. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    187
  8. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    187
    Ah sorry for spamming this thread but I just figured it out.

    The template above calculates the shadow coordinates in the vertex shader.

    However, it only works for me if I calculate them in the fragment shader. I was confused because phil_lira's code sample calls GetMainLight(inputData.shadowCoord), so I assumed the shadowCoord just defined was implicitly in the vertex shader!
     
  9. Bovine

    Bovine

    Joined:
    Oct 13, 2010
    Posts:
    195
    For me I found I had to provide the shadow cast to the lighting method (not sure which one right now) otherwise it doesn't pass through the relevant code path to generate shadow data.
     
  10. HaruLee932001

    HaruLee932001

    Joined:
    Dec 25, 2019
    Posts:
    12
    Code (CSharp):
    1. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    2. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    How to define these pragma in the file? I have no Idea how to do that.