Search Unity

Resolved Cannot read 'light estimation' values

Discussion in 'AR' started by mikkelsen1996, Oct 30, 2021.

  1. mikkelsen1996

    mikkelsen1996

    Joined:
    Sep 11, 2021
    Posts:
    5
    I'm trying to read light estimation values, but have only manage to read two out of the six values possible. I have made a scripts which should read all six values and display them in a canvas, however this only works for 'Brightness' and 'ColorTemperature', and the rest has no values in them. The device I'm using is a Iphone 13 pro. Unity version is 2020.3.18 and all package is up to date. I've included some images of the setup, since the problem could maybe have something to do with this being wrong. Hope someone can help! :)

    %%UPDATE%%
    I manage to read more values out by using the front facing camera instead which is very confusing to me ?? I am still missing the the most important value for me which is the 'mainLightColor'. If somebody now how to get this values, please help me.


    Code (csharp):
    1.  
    2.     using System;
    3.     using UnityEngine;
    4.     using UnityEngine.Rendering;
    5.     using UnityEngine.UI;
    6.     using UnityEngine.XR.ARFoundation;
    7.  
    8.     namespace UnityEngine.XR.ARFoundation.Samples
    9.     {
    10.  
    11.         /// A component that can be used to access the most recently received basic light estimation information
    12.         /// for the physical environment as observed by an AR device.
    13.         [RequireComponent(typeof(Light))]
    14.  
    15.  
    16.         public class LightEstimation : MonoBehaviour
    17.         {
    18.  
    19.             [SerializeField]
    20.             [Tooltip("The ARCameraManager which will produce frame events containing light estimation information.")]
    21.             ARCameraManager m_CameraManager;
    22.             [SerializeField]
    23.             private Text brightnessValue;
    24.             [SerializeField]
    25.             private Text tempValue;
    26.             [SerializeField]
    27.             private Text ColorCorrectionValue;
    28.             [SerializeField]
    29.             private Text mainLightDirectionValue;
    30.             [SerializeField]
    31.             private Text mainLightColorValue;
    32.             [SerializeField]
    33.             private Text mainLightIntensityLumensValue;
    34.             [SerializeField]
    35.             private Text sphericalHarmonicsValue;
    36.  
    37.             /// Get or set the <c>ARCameraManager.
    38.             public ARCameraManager cameraManager
    39.             {
    40.                 get { return m_CameraManager; }
    41.                 set
    42.                 {
    43.                     if (m_CameraManager == value)
    44.                         return;
    45.  
    46.                     if (m_CameraManager != null)
    47.                         m_CameraManager.frameReceived -= FrameChanged;
    48.  
    49.                     m_CameraManager = value;
    50.  
    51.                     if (m_CameraManager != null & enabled)
    52.                         m_CameraManager.frameReceived += FrameChanged;
    53.                 }
    54.             }
    55.  
    56.             /// The estimated brightness of the physical environment, if available.
    57.             public float? brightness { get; private set; }
    58.  
    59.             /// The estimated color temperature of the physical environment, if available.
    60.             public float? colorTemperature { get; private set; }
    61.  
    62.  
    63.             /// The estimated color correction value of the physical environment, if available.
    64.             public Color? colorCorrection { get; private set; }
    65.  
    66.             /// The estimated direction of the main light of the physical environment, if available.
    67.             public Vector3? mainLightDirection { get; private set; }
    68.  
    69.             /// The estimated color of the main light of the physical environment, if available.
    70.             public Color? mainLightColor { get; private set; }
    71.  
    72.             /// The estimated intensity in lumens of main light of the physical environment, if available.
    73.             public float? mainLightIntensityLumens { get; private set; }
    74.  
    75.             /// The estimated spherical harmonics coefficients of the physical environment, if available.
    76.             public SphericalHarmonicsL2? sphericalHarmonics { get; private set; }
    77.  
    78.             void Awake()
    79.             {
    80.                 ///we fetch directional light reference.
    81.                 m_Light = GetComponent<Light>();
    82.  
    83.             }
    84.  
    85.             void OnEnable()
    86.             {
    87.                 if (m_CameraManager != null)
    88.                     m_CameraManager.frameReceived += FrameChanged;
    89.  
    90.             }
    91.  
    92.             void OnDisable()
    93.             {
    94.                 if (m_CameraManager != null)
    95.                 {
    96.                     m_CameraManager.frameReceived -= FrameChanged;
    97.                 }
    98.             }
    99.  
    100.  
    101.             void FrameChanged(ARCameraFrameEventArgs args)
    102.             {
    103.                 if (args.lightEstimation.averageBrightness.HasValue)
    104.                 {
    105.                     brightness = args.lightEstimation.averageBrightness.Value;
    106.                     m_Light.intensity = brightness.Value;
    107.                     brightnessValue.text = "Brightness: " + m_Light.intensity;
    108.  
    109.                 }
    110.                 else
    111.                 {
    112.                     brightness = null;
    113.                     brightnessValue.text = "No value";
    114.                 }
    115.  
    116.                 if (args.lightEstimation.averageColorTemperature.HasValue)
    117.                 {
    118.                     colorTemperature = args.lightEstimation.averageColorTemperature.Value;
    119.                     m_Light.colorTemperature = colorTemperature.Value;
    120.                     tempValue.text = "temp: " + m_Light.colorTemperature;
    121.                 }
    122.                 else
    123.                 {
    124.                     colorTemperature = null;
    125.                     tempValue.text = "No value";
    126.                 }
    127.  
    128.                 if (args.lightEstimation.colorCorrection.HasValue)
    129.                 {
    130.                     colorCorrection = args.lightEstimation.colorCorrection.Value;
    131.                     m_Light.color = colorCorrection.Value;
    132.                     ColorCorrectionValue.text = "correction: " + m_Light.color;
    133.                 }
    134.                 else
    135.                 {
    136.                     colorCorrection = null;
    137.                     ColorCorrectionValue.text = "No value";
    138.                 }
    139.  
    140.                 if (args.lightEstimation.mainLightDirection.HasValue)
    141.                 {
    142.                     mainLightDirection = args.lightEstimation.mainLightDirection;
    143.                     m_Light.transform.rotation = Quaternion.LookRotation(mainLightDirection.Value);
    144.                     mainLightDirectionValue.text = "Direction: " + m_Light.transform.rotation;
    145.  
    146.  
    147.  
    148.                 }
    149.                 else
    150.                 {
    151.                     mainLightDirection = null;
    152.                     mainLightDirectionValue.text = "No value";
    153.                 }
    154.  
    155.                 if (args.lightEstimation.mainLightColor.HasValue)
    156.                 {
    157.                     mainLightColor = args.lightEstimation.mainLightColor;
    158.                     mainLightColorValue.text = "mainLightColor: " + mainLightColor;
    159.                 }
    160.                 else
    161.                 {
    162.                     mainLightColor = null;
    163.                     mainLightColorValue.text = "No value";
    164.                     Console.WriteLine("mainLightColor:  " + args.lightEstimation.mainLightColor);
    165.                 }
    166.  
    167.                 if (args.lightEstimation.mainLightIntensityLumens.HasValue)
    168.                 {
    169.                     mainLightIntensityLumens = args.lightEstimation.mainLightIntensityLumens;
    170.                     m_Light.intensity = args.lightEstimation.averageMainLightBrightness.Value;
    171.                     mainLightIntensityLumensValue.text = "IntensityLumens: " + m_Light.intensity;
    172.                 }
    173.                 else
    174.                 {
    175.                     mainLightIntensityLumens = null;
    176.                     mainLightIntensityLumensValue.text = "No value";
    177.                 }
    178.  
    179.  
    180.                 if (args.lightEstimation.ambientSphericalHarmonics.HasValue)
    181.                 {
    182.                     sphericalHarmonics = args.lightEstimation.ambientSphericalHarmonics;
    183.                     RenderSettings.ambientMode = AmbientMode.Skybox;
    184.                     RenderSettings.ambientProbe = sphericalHarmonics.Value;
    185.                     sphericalHarmonicsValue.text = "Harmonics: " + sphericalHarmonics;
    186.                 }
    187.                 else
    188.                 {
    189.                     sphericalHarmonics = null;
    190.                     sphericalHarmonicsValue.text = "No value";
    191.                 }
    192.             }
    193.  
    194.             Light m_Light;
    195.         }
    } Skærmbillede 2021-10-30 kl. 13.36.15.png Skærmbillede 2021-10-30 kl. 13.34.39.png Skærmbillede 2021-10-30 kl. 13.36.15.png Skærmbillede 2021-10-30 kl. 13.36.15.png Skærmbillede 2021-10-30 kl. 13.34.39.png
     
    Last edited: Nov 3, 2021
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    If you're going to post code, please use code-tags otherwise everyone is presented with a wall of plain-text as you can see.
     
  3. mikkelsen1996

    mikkelsen1996

    Joined:
    Sep 11, 2021
    Posts:
    5
    Thanks for the advice @MelvMay, it should be fixed now :)
     
    MelvMay likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Thanks!

    Sorry I can't actually help you with the content of your post, not my area of expertise unfortunately. Hopefully someone will be along who can help though.
     
    mikkelsen1996 likes this.
  5. Fangh

    Fangh

    Joined:
    Apr 19, 2013
    Posts:
    278
  6. mikkelsen1996

    mikkelsen1996

    Joined:
    Sep 11, 2021
    Posts:
    5
    @Fangh Thank you very much.. after looking into this I realized that the value I was looking for was already working fine in the project :D