Skip to content

Instantly share code, notes, and snippets.

@canxerian
Created August 14, 2022 08:37
Show Gist options
  • Save canxerian/47f17e553620424c49e47b418acab3d5 to your computer and use it in GitHub Desktop.
Save canxerian/47f17e553620424c49e47b418acab3d5 to your computer and use it in GitHub Desktop.
A Unity component for using ARFoundation's Light Estimation feature to modify the main light in our scene
using UnityEngine;
using UnityEngine.XR.ARFoundation;
/// <summary>
/// A Unity component for using ARFoundation's Light Estimation feature to modify
/// the main light in our scene
/// </summary>
public class LightEstimation : MonoBehaviour
{
[SerializeField]
[Tooltip("Reference to the ARCameraManager")]
private ARCameraManager aRCameraManager;
[SerializeField]
[Tooltip("The light that we want to modify based on light estimation data")]
private Light mainLight;
private void OnEnable()
{
aRCameraManager.frameReceived += OnFrameReceived;
}
private void OnDisable()
{
aRCameraManager.frameReceived -= OnFrameReceived;
}
private void OnFrameReceived(ARCameraFrameEventArgs args)
{
var lightData = args.lightEstimation;
if (lightData.averageMainLightBrightness.HasValue)
{
mainLight.intensity = lightData.averageMainLightBrightness.Value * 1.3f;
}
if (lightData.mainLightColor.HasValue)
{
mainLight.color = lightData.mainLightColor.Value;
}
if (lightData.mainLightDirection.HasValue)
{
mainLight.transform.rotation = Quaternion.LookRotation(lightData.mainLightDirection.Value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment