Hey there!
I recently was trying to implement sun scattering for Unity, at first I tried to implement it for default render pipeline after I succeded I tried to rewrite this effect for URP however I can’t find any way to get access to the directional light shadow map. Maybe someone already faced this problem and have a solution? In default rendering pipeline it was quite straightforward just attach command buffer to directional light and copy its shadow map texture to another one but this is not working anymore on URP.
Hey @piter00999 , did you end up figuring it out?
Unfortunately no, I ditched this project because of this.
In a post-processing effect or any screen-space pass, you can resolve the directional light’s shadow map, based on the camera’s depth texture. This is similar to how shadows are sampled in all the regular shaders. The difference being that the world position has to be reconstructed from depth first.
//Note: ZW components is normalized screen position
float ResolveShadowMask(float4 uv)
{
float deviceDepth = SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_CameraDepthTexture, uv.xy).r;
#if !UNITY_REVERSED_Z
deviceDepth = deviceDepth * 2.0 - 1.0;
#endif
float3 vpos = ComputeViewSpacePosition(uv.zw, deviceDepth, unity_CameraInvProjection);
float3 wpos = mul(unity_CameraToWorld, float4(vpos, 1)).xyz;
//Fetch shadow coordinates for cascade.
float4 coords = TransformWorldToShadowCoord(wpos);
// Screenspace shadowmap is only used for directional lights which use orthogonal projection.
ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
half4 shadowParams = GetMainLightShadowParams();
return SampleShadowmap(TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), coords, shadowSamplingData, shadowParams, false);
}
Code is based on Unity’s own screen-space shadow pass https://github.com/Unity-Technologies/Graphics/blob/master/com.unity.render-pipelines.universal/Shaders/Utils/ScreenSpaceShadows.shader
It may be faster or more convenient to add the “Screen Space Shadows” render feature before your own, then use half SampleScreenSpaceShadowmap(float4 shadowCoord)(declared in ShaderLibrary/Shadows.hlsl) to sample the shadow map in screen-space, which will already be resolved
Wow, thanks for such a detailed answer
! I guess it’s time to tinker with it again.