Beginner’s guide to Light estimation in AR foundation Unity

AR technology has been growing rapidly in the last few years and the amount of information that you can gather from your camera is astonishing. Unity has been keeping up and updating its AR package to match the growing requirement. In the last tutorial we saw how to get started with AR in Unity and how to recognize planes using AR in the real world.

In this tutorial, we are going to see how to get the ambient light values using AR foundation. If you are totally new to AR then please go through our previous tutorial on getting started with AR foundation.

Enabling Light Estimation in ARcamera

In order to use and get the light values you need to enable Light estimation on your ARcamera.

Select your AR Camera and go to the inspector window.

Unity Black Friday Deals

Find AR camera Manager

Set the Light Estimation drop down to Everything.

Getting the Light value using script

Select the directional light in your scene and add a new script to it. Let’s call it “Estimate_light”.

Create a new Text UI to display the light value. For the sake of this tutorial, let’s display the color value.

Open the script in Visual studio.

Copy and paste the script below

using UnityEngine;
using UnityEngine.XR.ARFoundation;
using TMPro;

public class Estimate_light : MonoBehaviour
{
    public ARCameraManager arcamman;
    public TMP_Text brightness;
    Light our_light;
    void OnEnable()
    {
        arcamman.frameReceived+=getlight;
    }

    void OnDisable()
    {
        arcamman.frameReceived-=getlight;
    }
    // Start is called before the first frame update
    void Start()
    {
        our_light=GetComponent<Light>();
        
    }

    void getlight(ARCameraFrameEventArgs args)
    {
       if(args.lightEstimation.mainLightColor.HasValue)
       {
        //brightness.text=$"Color_value:{args.lightEstimation.mainLightColor.Value}";
        our_light.color=args.lightEstimation.mainLightColor.Value;
        float average_brightness=0.2126f * our_light.color.r + 0.7152f * our_light.color.g + 0.0722f * our_light.color.b;
        brightness.text=average_brightness.ToString();
       }
    }
   
}

Here is how the script works

Namespace “using UnityEngine.XR.ARFoundation” is required for this. This script uses Text mesh pro. If you want to use the default Unity text element then you need to replace “TMPro” with “UnityEngine.UI”.

Two public variables, one for the Text and the other for the AR camera manager. Let’s create a variable for our light so that we can assign the real-life light values to our directional light.

We are subscribing to the Event arcamman.frameReceived when the script is enabled and removing the subscription when the script is disabled.

In our Getlight function we are taking the variable input of type “ARCameraFrameEventArgs”.

This variable contains all the light values.

The available light values are dependent on your phone’s hardware. Not all values are available in all devices.

You can either use Debug.log and Android Logcat to check the data or display all values on the UI.

In the above example we are only displaying the main light’s color. This will give you an RGB value.

That’s it for this tutorial, if you have any other question, please leave it in the comment box below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.