Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question What are Screen Space Shadows?

Discussion in 'Shaders' started by Adelto, Nov 20, 2022.

  1. Adelto

    Adelto

    Joined:
    Feb 23, 2022
    Posts:
    12
    Hi.

    Help me please~~
    While looking at "Shadows.hlsl" file in the URP, there was something I didn't understand, so I googled here and there and failed, so I asked a question.

    I saw a function named MainLightRealtimeShadow(float4), and I saw it samples from _ScreenSpaceShadowmapTexture or _MainLightShadowmapTexture according to writting a keyword named _MAIN_LIGHT_SHADOWS_SCREEN or not.

    What is the difference between _ScreenSpaceShadowmapTexture and _MainLightShadowmapTexture?

    And What are Screen Space Shadows?

    Thanks for reading~
     
    Last edited: Nov 20, 2022
  2. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    761
    Hi, screen space shadow sounds like a screen space effect (no shadows when objects are outside screen), but it's actually not in Unity.

    Shadowmaps are Depth Texures, it stores the scene depth from the light's view. (areas that a light cannot "see" directly should apply shadow)

    Then, the shader will compare the current pixel's depth with the one in shadowmap.


    When you enable Screen Space Shadow, URP will use "_CameraDepthTexture" & "_MainLightShadowmapTexture" to calculate main light shadows and output it to a black&white texture.

    Then, all opaque objects will get the shadow directly from "_ScreenSpaceShadowmapTexture". (transparent objects will not be in "_CameraDepthTexture", so they will calculate shadows during the lighting calculation)

    You can also see URP Documentation - Screen Space Shadow. (with detailed explanation)

    You can use this feature (enable "_MAIN_LIGHT_SHADOWS_SCREEN" globally with your custom renderer feature) to customize URP's main light shadow filtering. (such as PCSS, a more realistic soft shadow filtering)
    HDRP_ShadowFiltering.jpg
     
    Last edited: Nov 20, 2022
    Adelto likes this.
  3. Adelto

    Adelto

    Joined:
    Feb 23, 2022
    Posts:
    12
    Thank you for reply.

    What you said means that _ScreenSpaceShadowmapTexture is made using _CameraDepthTexture and _MainLightShadowmapTexture?
    If this is correct,
    Is _MainLightShadowmapTexture a depth texture relative to the light source(needs to calculate for testing depth)
    and _ScreenSpaceShadowmapTexture just a shadow texture? (no needs to test depth, TT sorry for my English skill..)

    It means:
    If my shader samples from _MainLightShadowmapTexture,
    needs to convert the pixel coordinates in the world space to the light space then compare the depths, and make the pixel darker if the pixel is farther from the light source than the depth of the _MainLightShadowmapTexture, otherwise lighten it.
    However, when sampling from _ScreenSpaceShadowmapTexture, the sampled value is the shadow value itself, so it's just multiplied to the pixel color.

    Am I right?
     
    Last edited: Nov 21, 2022
  4. Adelto

    Adelto

    Joined:
    Feb 23, 2022
    Posts:
    12
    Only now did I understand.

    Thank you ~
     
  5. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    761
    Yes, that's what I mean.

    Correct, and we use screen coordinates to sample the screen space shadowmap.

    Yes, but the actual shadow filtering is more complex than this. (bias to avoid self-shadowing, soft shadows, cascades)

    If you are interested in it, I suggest catlike coding's SRP shadow filtering tutorials.
     
    Adelto likes this.
  6. Adelto

    Adelto

    Joined:
    Feb 23, 2022
    Posts:
    12
    Thank you so much!!

    I got it. I'll study about that more.

    Thanks for your suggestion! I'll see it.
     
    wwWwwwW1 likes this.