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

Get Brightness of an object

Discussion in 'Shaders' started by sampa93, Sep 17, 2020.

  1. sampa93

    sampa93

    Joined:
    Jul 18, 2020
    Posts:
    10
    Hello community,

    what is the best way in Unity3D (not AR) to get the light intensity ("brightness" / "luminance"?) of an object?

    Is it possible to solve it with shaders?

    Multiple objects should be able to do the calculation - but every object with its own luminance.

    Thanks in advance for your responses to this question.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Shaders let you calculate the brightness for each visible pixel of an object's surface on the GPU. If you need an average brightness of an entire object ... you're far better off doing it on the CPU using multiple ray casts from within the bounds of your object to nearby lights, or maybe sampling from the lightmap of the object you're standing on (which is how games from the late 90's ~ early 00's did it), or decoding data from light probe spherical harmonics ... or some combination of all 3.
     
  3. sampa93

    sampa93

    Joined:
    Jul 18, 2020
    Posts:
    10
    Wouldnt the distance to the actual light object give me only information about how far the object is, but not how strong the light is shining (and if its hitting the object)?
     
  4. sampa93

    sampa93

    Joined:
    Jul 18, 2020
    Posts:
    10
    Light probes would not work if I want to react on a flashlight, correct?
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Light probes are for static lights. Any dynamic lights you’ll have to ray cast against.

    As for brightness, if you have the distance to a dynamic light, you can calculate the brightness. Or at least a decent approximation of it. You can get basically a 100% perfect calculation for the falloff of a light based on the distance and brightness, the falloff calculation for Unity’s lights is known, but you’ll have to estimate factors like how much light is hitting the total surface of the object. And maybe take into account the albedo of the object too depending on how fancy you want to be.
     
  6. sampa93

    sampa93

    Joined:
    Jul 18, 2020
    Posts:
    10
    Calculating the fall off souds good. But are there any examples? I cant find something
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    The default Unity distance attenuation function for spot and point lights.
    Code (csharp):
    1. Light light = // the current light you’re calculating the falloff for
    2. Vector3 curPos = // the current position you’re testing against
    3.  
    4. // distance from light
    5. float dist = Vector3.Distance(curPos, light.transform.position);
    6.  
    7. // normalized linear distance, 0.0 at light, 1.0 at range
    8. float normDist = dist / light.range;
    9.  
    10. // Unity’s default attention function
    11. float atten = Mathf.Clamp01(1.0f / (1.0f + 25.0f * normDist*normDist) * ((1.0f - normDist) * 5.0f));
    Further, for a spotlight you’ll want to calculate the cone falloff. For Unity’s spot lights, this is implemented using a default light cookie that is projected using the light’s frustum. Unless you’re using especially interesting custom cookies, I wouldn’t bother spending too much time trying to get this to line up too much. Just check the angle between the position and the light forward.
    Code (CSharp):
    1. float angle = Vector3.Angle(curPos - light.transform.position, light.transform.forward);
    2.  
    3. // basic approximation of default spotlight cookie
    4. float spotFalloff = Mathf.Clamp01((1.0f - (angle / light.spotAngle)) * 5f);
     
    StellarVeil likes this.