In Unity 6.0, how to sample shadow in shader code?
I am using the HLSL code below to sample shadow for a custom lighting shader:
// 1) Get shadow coordinates
float4 shadowCoord;
#if SHADOWS_SCREEN
shadowCoord = ComputeScreenPos(positionCS);
#else
shadowCoord = TransformWorldToShadowCoord(positionWS);
#endif
// 2) Get the main light
// `GetMainLight` is Supposed to be located in `Universal RP/ShaderLibrary/Lighting.hlsl`
// but I cannot find it there nor in other files
Light light = GetMainLight(shadowCoord, positionWS, 1);
// 3) Do stuff
float3 radiance = light.color * light.shadowAttenuation;
// ...
float3 color = albedo * radiance * (diffuse + specular);
return color;
I am also setting the boolean keyword _MAIN_LIGHT_SHADOWS in the shader graph that calls the code above as a custom function. This keyword is set to “multi-compile” and the default is true.
The code compiles but the shadow is not visible in the resulting game view.
All this comes from this tutorial (also available as a video) which I tried to reproduce in a new project using the “SRP Universal 3D” template and the Unity version 6000.0.35f1.
As this tutorial is already more than 3 years old, I would expect things to have changed since then and thus the way I am reproducing it now to be wrong in Unity 6. However, after having looked at the library in Universal RP/ShaderLibrary/ I cannot seem to find how to effectively sample shadow with the new version. I see in Lighting.hlsl that now GetMainLight seems to be called with different arguments, on line 291:
Light mainLight = GetMainLight(inputData, shadowMask, aoFactor);
I tried to reproduce this by recreating the “inputData” structure and the other arguments but without success.
Thanks for reading, any help would be appreciated!
