- Home /
Question by
mikkelsen1996 · Oct 30, 2021 at 03:59 PM ·
c#cameralight
Cannot read lightEstimation values with ARFoundation,How to read all light estimation values in ARFoundation
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! :)
using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
namespace UnityEngine.XR.ARFoundation.Samples
{
/// A component that can be used to access the most recently received basic light estimation information
/// for the physical environment as observed by an AR device.
[RequireComponent(typeof(Light))]
public class LightEstimation : MonoBehaviour
{
[SerializeField]
[Tooltip("The ARCameraManager which will produce frame events containing light estimation information.")]
ARCameraManager m_CameraManager;
[SerializeField]
private Text brightnessValue;
[SerializeField]
private Text tempValue;
[SerializeField]
private Text ColorCorrectionValue;
[SerializeField]
private Text mainLightDirectionValue;
[SerializeField]
private Text mainLightColorValue;
[SerializeField]
private Text mainLightIntensityLumensValue;
[SerializeField]
private Text sphericalHarmonicsValue;
/// Get or set the <c>ARCameraManager.
public ARCameraManager cameraManager
{
get { return m_CameraManager; }
set
{
if (m_CameraManager == value)
return;
if (m_CameraManager != null)
m_CameraManager.frameReceived -= FrameChanged;
m_CameraManager = value;
if (m_CameraManager != null & enabled)
m_CameraManager.frameReceived += FrameChanged;
}
}
/// The estimated brightness of the physical environment, if available.
public float? brightness { get; private set; }
/// The estimated color temperature of the physical environment, if available.
public float? colorTemperature { get; private set; }
/// The estimated color correction value of the physical environment, if available.
public Color? colorCorrection { get; private set; }
/// The estimated direction of the main light of the physical environment, if available.
public Vector3? mainLightDirection { get; private set; }
/// The estimated color of the main light of the physical environment, if available.
public Color? mainLightColor { get; private set; }
/// The estimated intensity in lumens of main light of the physical environment, if available.
public float? mainLightIntensityLumens { get; private set; }
/// The estimated spherical harmonics coefficients of the physical environment, if available.
public SphericalHarmonicsL2? sphericalHarmonics { get; private set; }
void Awake()
{
///we fetch directional light reference.
m_Light = GetComponent<Light>();
}
void OnEnable()
{
if (m_CameraManager != null)
m_CameraManager.frameReceived += FrameChanged;
}
void OnDisable()
{
if (m_CameraManager != null)
{
m_CameraManager.frameReceived -= FrameChanged;
}
}
void FrameChanged(ARCameraFrameEventArgs args)
{
if (args.lightEstimation.averageBrightness.HasValue)
{
brightness = args.lightEstimation.averageBrightness.Value;
m_Light.intensity = brightness.Value;
brightnessValue.text = "Brightness: " + m_Light.intensity;
}
else
{
brightness = null;
brightnessValue.text = "No value";
}
if (args.lightEstimation.averageColorTemperature.HasValue)
{
colorTemperature = args.lightEstimation.averageColorTemperature.Value;
m_Light.colorTemperature = colorTemperature.Value;
tempValue.text = "temp: " + m_Light.colorTemperature;
}
else
{
colorTemperature = null;
tempValue.text = "No value";
}
if (args.lightEstimation.colorCorrection.HasValue)
{
colorCorrection = args.lightEstimation.colorCorrection.Value;
m_Light.color = colorCorrection.Value;
ColorCorrectionValue.text = "correction: " + m_Light.color;
}
else
{
colorCorrection = null;
ColorCorrectionValue.text = "No value";
}
if (args.lightEstimation.mainLightDirection.HasValue)
{
mainLightDirection = args.lightEstimation.mainLightDirection;
m_Light.transform.rotation = Quaternion.LookRotation(mainLightDirection.Value);
mainLightDirectionValue.text = "Direction: " + m_Light.transform.rotation;
}
else
{
mainLightDirection = null;
mainLightDirectionValue.text = "No value";
}
if (args.lightEstimation.mainLightColor.HasValue)
{
mainLightColor = args.lightEstimation.mainLightColor;
mainLightColorValue.text = "mainLightColor: " + mainLightColor;
}
else
{
mainLightColor = null;
mainLightColorValue.text = "No value";
Console.WriteLine("mainLightColor: " + args.lightEstimation.mainLightColor);
}
if (args.lightEstimation.mainLightIntensityLumens.HasValue)
{
mainLightIntensityLumens = args.lightEstimation.mainLightIntensityLumens;
m_Light.intensity = args.lightEstimation.averageMainLightBrightness.Value;
mainLightIntensityLumensValue.text = "IntensityLumens: " + m_Light.intensity;
}
else
{
mainLightIntensityLumens = null;
mainLightIntensityLumensValue.text = "No value";
}
if (args.lightEstimation.ambientSphericalHarmonics.HasValue)
{
sphericalHarmonics = args.lightEstimation.ambientSphericalHarmonics;
RenderSettings.ambientMode = AmbientMode.Skybox;
RenderSettings.ambientProbe = sphericalHarmonics.Value;
sphericalHarmonicsValue.text = "Harmonics: " + sphericalHarmonics;
}
else
{
sphericalHarmonics = null;
sphericalHarmonicsValue.text = "No value";
}
}
Light m_Light;
}
}
skærmbillede-2021-10-30-kl-133615.png
(29.3 kB)
skærmbillede-2021-10-30-kl-133439.png
(17.8 kB)
Comment