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

Resolved Access shadow map in URP

Discussion in 'Universal Render Pipeline' started by valkidy, Jul 15, 2020.

  1. valkidy

    valkidy

    Joined:
    Jul 22, 2014
    Posts:
    2
    Hello guys,

    I have some problem when calculating the attenuation through shadow map.

    My goal is to implement light scattering (volumetric light) through calculating light attenuation in the ray marching step, and here's the final snapshot in the built-in render system.


    And then I write some code in shader to calculate light attenuation only, and the result looks like this :

    And this is how I access shadow map in built-in render system.
    Code (CSharp):
    1.      
    2.  float4 cascadeWeights = GetCascadeWeights_SplitSpheres(wpos);
    3.  bool inside = dot(cascadeWeights, float4(1, 1, 1, 1)) < 4;
    4.  float4 samplePos = GetCascadeShadowCoord(float4(wpos, 1), cascadeWeights);
    5.  atten = inside ? UNITY_SAMPLE_SHADOW(_CascadeShadowMapTexture, samplePos.xyz) : 1.0f;
    6.  atten = _LightShadowData.r + atten * (1 - _LightShadowData.r);
    7.  
    But I got a quite different result in URP, and I used another way to access shadow map. I'm pretty sure those two scenes (transform of scene objects, camera transforms, FOV ....etc) are in same condition.

    And this is how I access shadow map in URP, and this shader executed as a Blit material.
    Code (CSharp):
    1.  
    2. half cascadeIndex = ComputeCascadeIndex(wpos);
    3. float4 coords = mul(_MainLightWorldToShadow[cascadeIndex], float4(wpos, 1.0));
    4.                            
    5. ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
    6. half4 shadowParams = GetMainLightShadowParams();
    7. atten = inside ? SampleShadowmap(TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), coords, shadowSamplingData, shadowParams, false) : 1.0f;
    8. atten = shadowParams.r + atten * (1 - shadowParams.r);
    9.  
    The main camera is set in perspective mode (and I know the _MainLightShadowmapTexture built in orthogonal mode), and the shader is from Here.
    Is there anything wrong during these steps ? any advice would be appreciated.
    [Edit] I just want to know how to sample shadow map in URP and the equivalent way with the built-in method.

    Have a nice day !
     

    Attached Files:

    Last edited: Jul 15, 2020
  2. valkidy

    valkidy

    Joined:
    Jul 22, 2014
    Posts:
    2
    Finally, I found the answer.

    The thing is the default shadow map between Built-in and URP are slightly different.
    (The Left is from Built-in, and the Right is from URP)


    And in order to make same result, you have to change some parameters in shadows settings, so the code snippet works for sampling the default shadow map.
     

    Attached Files:

    Last edited: Jul 17, 2020
    trapazza likes this.
  3. mattdevv

    mattdevv

    Joined:
    Dec 28, 2019
    Posts:
    12
    For anyone else coming across this, you can sample the shadow map in a URP shader by adding this to your pass:
    Code (CSharp):
    1. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    2. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    3.  
    4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    And then in the fragment function use this to get the shadow attenuation:
    Code (CSharp):
    1. half ShadowAtten(float3 worldPosition)
    2. {
    3.         return MainLightRealtimeShadow(TransformWorldToShadowCoord(worldPosition));
    4. }
    This will work with all shadow cascade amounts and is based on this tutorial for shader graph but I cut out the parts that don't relate to attenuation: https://blogs.unity3d.com/pt/2019/0...n-shader-graph-expanding-your-graphs-in-2019/
     
    Last edited: Jan 25, 2021
    dtaddis, fherbst, NotaNaN and 2 others like this.
  4. voxelltech

    voxelltech

    Joined:
    Oct 8, 2019
    Posts:
    44
    Hi there wanted to ask if you got the volumetric light working? may I ask if you have that repo open sourced? Thanks! I was trying to achieve volumetric lighting too and would be glad to reference it from you :)
     
  5. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595
    Code (CSharp):
    1. half ShadowAtten(float3 worldPosition)
    2. {
    3.         return MainLightRealtimeShadow(TransformWorldToShadowCoord(worldPosition));
    4. }
    This works great in the Editor but when built for WebGL it just appears to return 0
     
  6. krifx

    krifx

    Joined:
    Nov 21, 2017
    Posts:
    7
    I had a similar problem with shadows in a custom shader, in the editor it worked but in the windows build it did not show the shadows.
    A single line of shadow pragma instead of multiple solved the problem.
    Code (CSharp):
    1. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE
    It was on Unity 2021.3.26f1 URP 12.1.11.