Search Unity

Find light range?

Discussion in 'General Discussion' started by BigB, Sep 22, 2021.

  1. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    672
    Hi,
    I'm trying to know if a mesh is currently in the light or in the dark. Besides casting a ray to the light (to check if it's occluded by geometry), I need to know if the mesh is in the light radius.
    How do I check this?
    I have been using this formula :
    var Lightp = light.range * (light.intensity / 10.0f);

    But results are inconsistent.
    Any tips? :)
    Thanks!
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Being inside the light's radius is just checking distance against the range.

    The catch is that at "range" the brightness is 0, and the effective brightness changes based on a few different things.
    - How bright the light is
    - Light falloff curve (determined by render pipeline, I believe)
    - Material of the object being lit

    I would guess that the inconsistency of results stems from those things. The first two are perhaps the more likely culprits, and are solvable with math.
     
  3. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    672
    Thanks angrypenguin,

    You don't happen to know the light falloff curve on the Build In Render pipeline?
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    Well, this one is fun.

    In "legacy" renderer

    In internal renderer light brightness is determined by a ... texture lookup based on distance squared from light center.
    Code (csharp):
    1.  
    2. float getLightAttenuation(float3 worldPos){
    3. #if defined(POINT)
    4.    {
    5.        unityShadowCoord3 lightCoord = mul(_LightMatrix0, float4(worldPos, 1.0)).xyz;
    6.        float result = tex2D(_LightTexture0, dot(lightCoord, lightCoord).rr).UNITY_ATTEN_CHANNEL;
    7.        return result;
    8.    }  
    No mention anywhere what's written into that texture, but various sources claim that the attenuation is linear.

    Meaning 1.0/distance . where distance is distance to light center.

    However in URP(and likely HDRP) the falloff is 1.0/distanceSquared. I.e. 1.0/(distance*distance)
    This is mentioned here:
    https://docs.unity3d.com/Packages/c...l/universalrp-builtin-feature-comparison.html

    In both cases the falloff is technically infinite. In practice anything beyond light radius likely won't be lit.
     
    Shreddedcoconut likes this.
  5. ClaudiaTheDev

    ClaudiaTheDev

    Joined:
    Jan 28, 2018
    Posts:
    331