Shadow attenuation issue on URP spot light in custom lighting.

Hi there !

I am making a URP shader for HairStudio, with custom lighting and unlit master node. I’m facing an issue with shadow attenuation that does not seem to be taken into account.

I have a custom node with this code:

int lightCount = GetAdditionalLightsCount();
for (int i = 0; i < lightCount; ++i)
{
    Light light = GetAdditionalLight(i, position);
    float3 attenuatedLightColor = light.color * light.distanceAttenuation * light.shadowAttenuation;
    color += GetMySpecular(light.direction, attenuatedLightColor);
}

And here is the result:

As we can see, the shadow attenuation works well on all surfaces, but not at all on the hair using this custom lighting. It only concerns shadow attenuation, because if I move the spot light back and forth, I can see that the distance attenuation is taken into account on both the hair and surfaces.

Is there something I’m doing wrong?

1 Like

The shadow was not working with the main directional light either, so I changed the code to add shadow sampling.

This code allows me to receive shadows from the main light with unlit master node.

float4 shadowCoord = TransformWorldToShadowCoord(position);
Light mainLight = GetMainLight(shadowCoord);

ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
half shadowStrength = GetMainLightShadowStrength();
float mlShadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), shadowSamplingData, shadowStrength, false);
  
float attenuatedLightColor = mainLight.color * mainLight.distanceAttenuation * mlShadowAtten;
  
color = GetMySpecular(mlDirection, attenuatedLightColor);

I don’t use the mainLight.shadowAttenuation because it is always at 1. As I understand things, this is due to the fact that I use the unlit master node. Using this node, I’m telling that I don’t need light data, therefore shadows are not ready to use for performance purpose. Calling the SampleShadowmap manually is mandatory to create the data I need.

(If I’m right, then it would be great to have a CustomLighting master node, with main light and additionnal light ready to use.)

Anyway I tried that for additionnal lights as well, but the shadow attenuation still has no effect after a manual sampling. This code leads to the same result as in the original post.

int lightCount = GetAdditionalLightsCount();
for (int i = 0; i < lightCount; ++i)
{
    Light light = GetAdditionalLight(i, position);

    ShadowSamplingData shadowSamplingData = GetAdditionalLightShadowSamplingData();
    float shadowStrength = GetAdditionalLightShadowStrenth(i);
    float shadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_AdditionalLightsShadowmapTexture, sampler_AdditionalLightsShadowmapTexture), shadowSamplingData, shadowStrength, false);

    float3 attenuatedLightColor = light.color * light.distanceAttenuation* shadowAtten;
    color += GetMySpecular(mlDirection, attenuatedLightColor);
}

For me, for some reason, light.distanceAttenuation does not work when using LIT shader when I do a build (but it does work whilst in the Editor)

Unity 2019.4.2f1
URP 7.4.1

So, is there a way to obtain additionnal lights shadow attenuation with unlit master node?

Or another master node that can allow a custom lighting?