Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Shadow attenuation issue on URP spot light in custom lighting.

Discussion in 'Universal Render Pipeline' started by methusalah999, Jul 10, 2020.

  1. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    640
    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:
    Code (CSharp):
    1. int lightCount = GetAdditionalLightsCount();
    2. for (int i = 0; i < lightCount; ++i)
    3. {
    4.     Light light = GetAdditionalLight(i, position);
    5.     float3 attenuatedLightColor = light.color * light.distanceAttenuation * light.shadowAttenuation;
    6.     color += GetMySpecular(light.direction, attenuatedLightColor);
    7. }
    And here is the result:
    upload_2020-7-10_11-35-47.png

    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?
     
    Last edited: Jul 10, 2020
    The_Swiss_Guy likes this.
  2. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    640
    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.
    Code (CSharp):
    1. float4 shadowCoord = TransformWorldToShadowCoord(position);
    2. Light mainLight = GetMainLight(shadowCoord);
    3.  
    4. ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
    5. half shadowStrength = GetMainLightShadowStrength();
    6. float mlShadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), shadowSamplingData, shadowStrength, false);
    7.  
    8. float attenuatedLightColor = mainLight.color * mainLight.distanceAttenuation * mlShadowAtten;
    9.  
    10. 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.
    Code (CSharp):
    1. int lightCount = GetAdditionalLightsCount();
    2. for (int i = 0; i < lightCount; ++i)
    3. {
    4.     Light light = GetAdditionalLight(i, position);
    5.  
    6.     ShadowSamplingData shadowSamplingData = GetAdditionalLightShadowSamplingData();
    7.     float shadowStrength = GetAdditionalLightShadowStrenth(i);
    8.     float shadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_AdditionalLightsShadowmapTexture, sampler_AdditionalLightsShadowmapTexture), shadowSamplingData, shadowStrength, false);
    9.  
    10.     float3 attenuatedLightColor = light.color * light.distanceAttenuation* shadowAtten;
    11.     color += GetMySpecular(mlDirection, attenuatedLightColor);
    12. }
     
  3. SniperED007

    SniperED007

    Joined:
    Sep 29, 2013
    Posts:
    345
    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
     
  4. methusalah999

    methusalah999

    Joined:
    May 22, 2017
    Posts:
    640
    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?