Search Unity

Question Removed smooth attenuation but there is still fall-off?

Discussion in 'Universal Render Pipeline' started by SinSinx2, Jan 18, 2022.

  1. SinSinx2

    SinSinx2

    Joined:
    Mar 13, 2019
    Posts:
    12
    So basically I went into the Universal RP/ShaderLibrary/Lighting.hlsl file and changed the GetAdditionalPerObjectLight function to this

    Code (CSharp):
    1. // Fills a light struct given a perObjectLightIndex
    2. Light GetAdditionalPerObjectLight(int perObjectLightIndex, float3 positionWS)
    3. {
    4.     // Abstraction over Light input constants
    5. #if USE_STRUCTURED_BUFFER_FOR_LIGHT_DATA
    6.     float4 lightPositionWS = _AdditionalLightsBuffer[perObjectLightIndex].position;
    7.     half3 color = _AdditionalLightsBuffer[perObjectLightIndex].color.rgb;
    8.     half4 distanceAndSpotAttenuation = _AdditionalLightsBuffer[perObjectLightIndex].attenuation;
    9.     half4 spotDirection = _AdditionalLightsBuffer[perObjectLightIndex].spotDirection;
    10. #else
    11.     float4 lightPositionWS = _AdditionalLightsPosition[perObjectLightIndex];
    12.     half3 color = _AdditionalLightsColor[perObjectLightIndex].rgb;
    13.     half4 distanceAndSpotAttenuation = _AdditionalLightsAttenuation[perObjectLightIndex];
    14.     half4 spotDirection = _AdditionalLightsSpotDir[perObjectLightIndex];
    15. #endif
    16.  
    17.     // Directional lights store direction in lightPosition.xyz and have .w set to 0.0.
    18.     // This way the following code will work for both directional and punctual lights.
    19.     float3 lightVector = lightPositionWS.xyz - positionWS * lightPositionWS.w;
    20.     float distanceSqr = max(dot(lightVector, lightVector), HALF_MIN);
    21.  
    22.     half3 lightDirection = half3(lightVector * rsqrt(distanceSqr));
    23.     float range = rsqrt(distanceAndSpotAttenuation.x);
    24.     int distance = (distanceSqr / range);
    25.     half attenuation = clamp(1.0f - distance, 0.0, 1.0);
    26.  
    27.     Light light;
    28.     light.direction = lightDirection;
    29.     light.distanceAttenuation = attenuation;
    30.     light.shadowAttenuation = 1.0; // This value can later be overridden in GetAdditionalLight(uint i, float3 positionWS, half4 shadowMask)
    31.     light.color = color;
    32.  
    33.     return light;
    34. }
    Since the distance is an integer the attenuation will immediately change from 1 to 0 so there should be no smooth fall-off. At first it looked like it worked but upon closer inspection there is still more light at the center than the edges

    upload_2022-1-18_11-54-43.png

    I have turned off specular highlights and environment reflections on the table material but the highlight is still there

    upload_2022-1-18_11-56-44.png
     
    Last edited: Jan 18, 2022