Unity 6.0, how to sample shadow in shader code?

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!

This is one of the best sources on implementing custom lighting in shader graph.

You can also check my free shadow Receiver URP asset to get a barebones shadow Receiver you can use as a base

Thanks for the link, that worked for me!

The thing I was doing wrong is when setting the keywords in the unlit shader graph.
This is well explained in Cyan’s package you posted:

  • Enum Keyword, Global Multi-Compile _MAIN_LIGHT, with entries :
    • SHADOWS
    • SHADOWS_CASCADE
    • SHADOWS_SCREEN
  • Boolean Keyword, Global Multi-Compile _SHADOWS_SOFT

And once the keyword set, choose the right enum type in the material properties.

Thanks, I was missing exactly that part!