using System;
using UnityEngine.Rendering;
using UnityEngine.XR.ARFoundation.InternalUtils;
using StringBuilder = System.Text.StringBuilder;
namespace UnityEngine.XR.ARFoundation
{
///
/// A structure for light estimation information provided by the AR device.
///
///
/// While you can request any of these simultaneously, support for each varies among devices.
/// Some platforms might not be able to simultaneously provide all options,
/// or it might depend on other features (for example, camera **Facing Direction**).
///
public struct ARLightEstimationData : IEquatable
{
///
/// An estimate for the average brightness in the scene.
/// Use averageBrightness.HasValue to determine if this information is available.
///
///
/// can be null when light estimation is not enabled in the ,
/// or if a platform-specific error has occurred.
///
public float? averageBrightness
{
get
{
if (m_AverageBrightness.HasValue)
return m_AverageBrightness;
else if (m_AverageIntensityInLumens.HasValue)
return UnitConversionUtility.ConvertLumensToBrightness(m_AverageIntensityInLumens.Value);
return null;
}
set => m_AverageBrightness = value;
}
///
/// An estimate for the average color temperature of the scene.
/// Use averageColorTemperature.HasValue to determine if this information is available.
///
///
/// can be null when light estimation is not enabled in the ,
/// if the platform does not support it, or if a platform-specific error has occurred.
///
public float? averageColorTemperature { get; set; }
///
/// The scaling factors used for color correction.
/// The RGB scale factors are used to match the color of the light
/// in the scene. The alpha channel value is platform-specific.
///
///
/// can be null when light estimation is not enabled in the ,
/// if the platform does not support it, or if a platform-specific error has occurred.
///
public Color? colorCorrection { get; set; }
///
/// An estimate for the average intensity, in lumens, in the scene.
/// Use averageIntensityInLumens.HasValue to determine if this information is available.
///
///
/// can be null when light estimation is not enabled in the ,
/// or if a platform-specific error has occurred.
///
public float? averageIntensityInLumens
{
get
{
if (m_AverageIntensityInLumens.HasValue)
return m_AverageIntensityInLumens;
else if (m_AverageBrightness.HasValue)
return UnitConversionUtility.ConvertBrightnessToLumens(m_AverageBrightness.Value);
return null;
}
set => m_AverageIntensityInLumens = value;
}
///
/// An estimate of the intensity of the main light in lumens.
///
///
/// can be null when light estimation is not enabled in the
/// or a platform-specific error has occurred.
///
public float? mainLightIntensityLumens { get; set; }
///
/// An estimate for the average brightness of the main light estimate in the scene.
/// Use averageMainLightBrightness.HasValue to determine if this information is available.
///
///
/// can be null when light estimation is not enabled in the ,
/// or if a platform-specific error has occurred.
///
public float? averageMainLightBrightness
{
get
{
if (m_MainLightBrightness.HasValue)
return m_MainLightBrightness;
else if (mainLightIntensityLumens.HasValue)
return UnitConversionUtility.ConvertLumensToBrightness(mainLightIntensityLumens.Value);
return null;
}
set => m_MainLightBrightness = value;
}
///
/// The estimated color of the main light.
///
///
/// can be null when light estimation is not enabled in the
/// or a platform-specific error has occurred.
///
public Color? mainLightColor { get; set; }
///
/// An estimate of where the main light of the scene is coming from.
///
///
/// can be null when light estimation is not enabled in the
/// or a platform-specific error has occurred.
///
public Vector3? mainLightDirection { get; set; }
///
/// An estimation of the ambient scene lighting using spherical harmonics at the Level 2.
///
///
/// can be null when light estimation is not enabled in the
/// or a platform-specific error has occurred.
///
public SphericalHarmonicsL2? ambientSphericalHarmonics { get; set; }
///
/// Generates a hash code suitable for use in HashSet and Dictionary.
///
/// A hash of the .
public override int GetHashCode()
{
unchecked
{
return
((averageBrightness.GetHashCode() * 486187739 +
averageColorTemperature.GetHashCode()) * 486187739 +
colorCorrection.GetHashCode()) * 486187739 +
averageIntensityInLumens.GetHashCode() * 486187739 +
mainLightDirection.GetHashCode() * 486187739 +
mainLightColor.GetHashCode() * 486187739 +
mainLightIntensityLumens.GetHashCode() * 486187739 +
ambientSphericalHarmonics.GetHashCode() * 486187739;
}
}
///
/// Compares for equality.
///
/// An object to compare against.
/// true if is an and
/// is also true. Otherwise, false.
public override bool Equals(object obj)
{
if (!(obj is ARLightEstimationData))
return false;
return Equals((ARLightEstimationData)obj);
}
///
/// Generates a string representation of this .
///
/// A string representation of this .
public override string ToString()
{
var sphericalHarmonicsCoefficientsStr = new StringBuilder("");
if (ambientSphericalHarmonics.HasValue)
{
sphericalHarmonicsCoefficientsStr.Append("[");
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 9; ++j)
{
sphericalHarmonicsCoefficientsStr.Append(i * 3 + j != 26 ? $"{ambientSphericalHarmonics.Value[i, j]}, " : $"{ambientSphericalHarmonics.Value[i, j]}]");
}
}
}
return $"(Avg. Brightness: {averageBrightness}, Avg. Color Temperature: {averageColorTemperature}, "
+ $"Color Correction: {colorCorrection}, Avg. Intensity in Lumens: {averageIntensityInLumens}, "
+ $"Est. Main Light Direction: {mainLightDirection}, Est. Main Light Channel Intensity: {mainLightColor}, "
+ $"Est. Main Light Intensity in Lumens: {mainLightIntensityLumens}, Est. Ambient Spherical Harmonics:\n{sphericalHarmonicsCoefficientsStr}";
}
///
/// Compares for equality.
///
/// The other to compare against.
/// true if the represents the same object.
public bool Equals(ARLightEstimationData other)
{
return
(averageBrightness == other.averageBrightness) &&
(averageColorTemperature == other.averageColorTemperature) &&
(colorCorrection == other.colorCorrection) &&
(averageIntensityInLumens == other.averageIntensityInLumens) &&
(mainLightColor == other.mainLightColor) &&
(mainLightDirection == other.mainLightDirection) &&
(mainLightIntensityLumens == other.mainLightIntensityLumens) &&
(ambientSphericalHarmonics == other.ambientSphericalHarmonics);
}
///
/// Compares and for equality using .
///
/// The left-hand-side of the comparison.
/// The right-hand-side of the comparison.
/// true if compares equal to , false otherwise.
public static bool operator ==(ARLightEstimationData lhs, ARLightEstimationData rhs) => lhs.Equals(rhs);
///
/// Compares and for inequality using .
///
/// The left-hand-side of the comparison.
/// The right-hand-side of the comparison.
/// false if compares equal to , true otherwise.
public static bool operator !=(ARLightEstimationData lhs, ARLightEstimationData rhs) => !lhs.Equals(rhs);
float? m_AverageBrightness;
float? m_AverageIntensityInLumens;
float? m_MainLightBrightness;
}
}