Search Unity

Question How to get the brightness of an object/texture?

Discussion in 'High Definition Render Pipeline' started by capocchione, Nov 13, 2020.

  1. capocchione

    capocchione

    Joined:
    Dec 9, 2019
    Posts:
    47
    I am still struggling trying to read/extract/compute the amount of light hitting an object.

    In the standard render pipeline, I've somehow managed the question doing something like this:


    Code (CSharp):
    1.  
    2.  
    3. public class LightCheckScript : MonoBehaviour
    4. {
    5.     public RenderTexture sourceTexture;
    6.     float LightLevel;
    7.     public int Light;
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         RenderTexture tmp = RenderTexture.GetTemporary(
    13.                     sourceTexture.width,
    14.                     sourceTexture.height,
    15.                     0,
    16.                     RenderTextureFormat.Default,
    17.                     RenderTextureReadWrite.Linear);
    18.  
    19.         Graphics.Blit(sourceTexture, tmp);
    20.         RenderTexture previous = RenderTexture.active;
    21.         RenderTexture.active = tmp;
    22.  
    23.         Texture2D myTexture2D = new Texture2D(sourceTexture.width, sourceTexture.height);
    24.  
    25.         myTexture2D.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
    26.         myTexture2D.Apply();
    27.  
    28.         RenderTexture.active = previous;
    29.         RenderTexture.ReleaseTemporary(tmp);
    30.  
    31.         Color32[] colors = myTexture2D.GetPixels32();
    32.         Destroy(myTexture2D); // Check
    33.         LightLevel = 0;
    34.         for (int i = 0; i < colors.Length; i++)
    35.         {
    36.             LightLevel += (0.2126f * colors[i].r) + (0.7152f * colors[i].g) + (0.0722f * colors[i].b);
    37.         }
    38.         LightLevel -= 259330;
    39.         //LightLevel = LightLevel / colors.Length;
    40.         //Light = Mathf.RoundToInt(LightLevel);
    41.         Debug.Log("Light: " + LightLevel);
    42.     }
    43. }
    44.  
    It works but I would like a more flexible/simplified solution based on physics. And here comes the HRDP.
    I have read about HDRP and I understood that the light is calculated physically in real time, meaning that I have the amount of light (calculated by physics) with Physical Light Units (PLU) such as candela, lumen, lux and so on...
    (https://docs.unity3d.com/Packages/c...inition@10.1/manual/Physical-Light-Units.html)

    Now, there is a way I could read these light unit values of a gameobject? For example, setting all the scene with all lights and stuff, putting a simple cube in the scene, how could I read the candedla/lux/lumen values hitting the cube?
    Thank everyone, sorry for the stupid questions
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,074
    As i know it is not easy task.
    Once I have done something similar. It was calculating amount of light in a point in world space.

    To do this first I had to calculate light from light probes in this point.
    Than find all the lights that point was in range with.
    And than ray cast from point to each light to see if it is not occluded.

    I ditched the idea in the end, it was not worth the effort.
     
    capocchione likes this.
  3. capocchione

    capocchione

    Joined:
    Dec 9, 2019
    Posts:
    47
    So there is no way to achieve this? I don't care about performance, it looks like an easy task but so complicated to do. I know how to calculate the brightness from RGB values (see equation in the code, line 36) of a texture, but how could I "access" to the texture in HDRP so I can read the RGB values (that I assume are changing in real time according to light changes)?