Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Problems with getting shadow attenuation from light struct in a custom function node

Discussion in 'Shaders' started by FranFyrhart, Mar 21, 2023.

  1. FranFyrhart

    FranFyrhart

    Joined:
    Jun 8, 2018
    Posts:
    11
    I'm trying to follow this tutorial:



    and have copied the code exactly as shown and I get similar results except for when I start working with shadow attenuation. The video shows that they can get the shadow attenuation data but when I do it, all I get is this:
    upload_2023-3-21_21-36-32.png

    Here's my HLSL Code up to 5:59 in the video where they get the shadow attenuation data:

    Code (CSharp):
    1. #ifndef LIGHTING_CEL_SHADED_INCLUDED
    2. #define LIGHTING_CEL_SHADED_INCLUDED
    3.  
    4. struct SurfaceVariables {
    5.     float3 normal;
    6.     float view;
    7.     float smoothness;
    8.     float shininess;
    9.     float rimThreshold;
    10. };
    11.  
    12. float3 CalculateCelShading(Light l, SurfaceVariables s)
    13. {
    14.     float diffuse = saturate(dot(s.normal, l.direction));
    15.     float attenuation = l.shadowAttenuation;
    16.  
    17.     float3 h = SafeNormalize(l.direction + s.view);
    18.     float specular = saturate(dot(s.normal, h));
    19.     specular = pow(specular, s.shininess);
    20.     specular *= diffuse * s.smoothness;
    21.  
    22.     float rim = 1 - dot(s.view, s.normal);
    23.     rim *= pow(diffuse, s.rimThreshold);
    24.  
    25.     return attenuation; // this part is intentional since I'm trying to see just the shadow attenuation all on its own like the video also does to compare my results
    26.     return l.color * (diffuse + max(specular, rim));
    27. }
    28.  
    29. void LightCelShaded_float(float smoothness, float rimThreshold, float3 Position, float3 normal, float3 view, out float3 Color)
    30. {
    31.     Color = float3(0.5f, 0.5f, 0.5f);
    32.     SurfaceVariables surfaceData;
    33.  
    34. #if defined(SHADERGRAPH_PREVIEW)
    35.  
    36. #else
    37.     surfaceData.normal = normal;
    38.     surfaceData.view = SafeNormalize(view);
    39.     surfaceData.smoothness = smoothness;
    40.     surfaceData.shininess = exp2(10 * smoothness + 1);
    41.     surfaceData.rimThreshold = rimThreshold;
    42.  
    43.     float4 shadowCoord = float4(0.5f, 0.5f, 0.5f, 0.5f);
    44.  
    45.     #if SHADOWS_SCREEN
    46.         float4 clipPos = TransformWorldToHClip(Position);
    47.         shadowCoord = ComputeScreenPos(clipPos);
    48.     #else
    49.         shadowCoord = TransformWorldToShadowCoord(Position);
    50.     #endif
    51.  
    52.     Light light = GetMainLight(shadowCoord);
    53.     Color = CalculateCelShading(light, surfaceData);
    54. #endif
    55.     }
    56. #endif
    I've tried this on two separate computers and get the exact same result and I swear I'm missing some setup that I should be doing prior to this that the video doesn't mention.

    I'm using Unity 2021.3.2f1 and URP 12.1.6