ARLightEstimationData.cs
11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using UnityEngine.Rendering;
using StringBuilder = System.Text.StringBuilder;
namespace UnityEngine.XR.ARFoundation
{
/// <summary>
/// A structure for light estimation information provided by the AR device.
/// </summary>
public struct ARLightEstimationData : IEquatable<ARLightEstimationData>
{
/// <summary>
/// An estimate for the average brightness in the scene.
/// Use <c>averageBrightness.HasValue</c> to determine if this information is available.
/// </summary>
/// <remarks>
/// <see cref="averageBrightness"/> may be null when light estimation is not enabled in the <see cref="ARSession"/>,
/// or if a platform-specific error has occurred.
/// </remarks>
public float? averageBrightness
{
get
{
if (m_AverageBrightness.HasValue)
return m_AverageBrightness;
else if (m_AverageIntensityInLumens.HasValue)
return ConvertLumensToBrightness(m_AverageIntensityInLumens.Value);
return null;
}
set => m_AverageBrightness = value;
}
/// <summary>
/// An estimate for the average color temperature of the scene.
/// Use <c>averageColorTemperature.HasValue</c> to determine if this information is available.
/// </summary>
/// <remarks>
/// <see cref="averageColorTemperature"/> may be null when light estimation is not enabled in the <see cref="ARSession"/>,
/// if the platform does not support it, or if a platform-specific error has occurred.
/// </remarks>
public float? averageColorTemperature { get; set; }
/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// <see cref="colorCorrection"/> may be null when light estimation is not enabled in the <see cref="ARSession"/>,
/// if the platform does not support it, or if a platform-specific error has occurred.
/// </remarks>
public Color? colorCorrection { get; set; }
/// <summary>
/// An estimate for the average intensity, in lumens, in the scene.
/// Use <c>averageIntensityInLumens.HasValue</c> to determine if this information is available.
/// </summary>
/// <remarks>
/// <see cref="averageIntensityInLumens"/> may be null when light estimation is not enabled in the <see cref="ARSession"/>,
/// or if a platform-specific error has occurred.
/// </remarks>
public float? averageIntensityInLumens
{
get
{
if (m_AverageIntensityInLumens.HasValue)
return m_AverageIntensityInLumens;
else if (m_AverageBrightness.HasValue)
return ConvertBrightnessToLumens(m_AverageBrightness.Value);
return null;
}
set => m_AverageIntensityInLumens = value;
}
/// <summary>
/// An estimate of the intensity of the main light in lumens.
/// </summary>
/// <remarks>
/// <see cref="mainLightIntensityLumens"/> may be null when light estimation is not enabled in the <see cref="ARSession"/>
/// or a platform-specific error has occurred.
/// </remarks>
public float? mainLightIntensityLumens { get; set; }
/// <summary>
/// An estimate for the average brightness of the main light estimate in the scene.
/// Use <c>averageMainLightBrightness.HasValue</c> to determine if this information is available.
/// </summary>
/// <remarks>
/// <see cref="averageMainLightBrightness"/> may be null when light estimation is not enabled in the <see cref="ARSession"/>,
/// or if a platform-specific error has occurred.
/// </remarks>
public float? averageMainLightBrightness
{
get
{
if (m_MainLightBrightness.HasValue)
return m_MainLightBrightness;
else if (mainLightIntensityLumens.HasValue)
return ConvertLumensToBrightness(mainLightIntensityLumens.Value);
return null;
}
set => m_MainLightBrightness = value;
}
/// <summary>
/// The estimated color of the main light.
/// </summary>
/// <remarks>
/// <see cref="mainLightColor"/> may be null when light estimation is not enabled in the <see cref="ARSession"/>
/// or a platform-specific error has occurred.
/// </remarks>
public Color? mainLightColor { get; set; }
/// <summary>
/// An estimate of where the main light of the scene is coming from.
/// </summary>
/// <remarks>
/// <see cref="mainLightDirection"/> may be null when light estimation is not enabled in the <see cref="ARSession"/>
/// or a platform-specific error has occurred.
/// </remarks>
public Vector3? mainLightDirection { get; set; }
/// <summary>
/// An estimation of the ambient scene lighting using spherical harmonics at the Level 2.
/// </summary>
/// <remarks>
/// <see cref="ambientSphericalHarmonics"/> may be null when light estimation is not enabled in the <see cref="ARSession"/>
/// or a platform-specific error has occurred.
/// </remarks>
public SphericalHarmonicsL2? ambientSphericalHarmonics { get; set; }
/// <summary>
/// Generates a hash code suitable for use in <c>HashSet</c> and <c>Dictionary</c>.
/// </summary>
/// <returns>A hash of the <see cref="ARLightEstimationData"/>.</returns>
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;
}
}
/// <summary>
/// Compares for equality.
/// </summary>
/// <param name="obj">An <c>object</c> to compare against.</param>
/// <returns><c>true</c> if <paramref name="obj"/> is an <see cref="ARLightEstimationData"/> and
/// <see cref="Equals(ARLightEstimationData)"/> is also <c>true</c>. Otherwise, <c>false</c>.</returns>
public override bool Equals(object obj)
{
if (!(obj is ARLightEstimationData))
return false;
return Equals((ARLightEstimationData)obj);
}
/// <summary>
/// Generates a string representation of this <see cref="ARLightEstimationData"/>.
/// </summary>
/// <returns>A string representation of this <see cref="ARLightEstimationData"/>.</returns>
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}";
}
/// <summary>
/// Compares for equality.
/// </summary>
/// <param name="other">The other <see cref="ARLightEstimationData"/> to compare against.</param>
/// <returns><c>true</c> if the <see cref="ARLightEstimationData"/> represents the same object.</returns>
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);
}
/// <summary>
/// Compares <paramref name="lhs"/> and <paramref name="rhs"/> for equality using <see cref="Equals(ARLightEstimationData)"/>.
/// </summary>
/// <param name="lhs">The left-hand-side <see cref="ARLightEstimationData"/> of the comparison.</param>
/// <param name="rhs">The right-hand-side <see cref="ARLightEstimationData"/> of the comparison.</param>
/// <returns><c>true</c> if <paramref name="lhs"/> compares equal to <paramref name="rhs"/>, <c>false</c> otherwise.</returns>
public static bool operator ==(ARLightEstimationData lhs, ARLightEstimationData rhs) => lhs.Equals(rhs);
/// <summary>
/// Compares <paramref name="lhs"/> and <paramref name="rhs"/> for inequality using <see cref="Equals(ARLightEstimationData)"/>.
/// </summary>
/// <param name="lhs">The left-hand-side <see cref="ARLightEstimationData"/> of the comparison.</param>
/// <param name="rhs">The right-hand-side <see cref="ARLightEstimationData"/> of the comparison.</param>
/// <returns><c>false</c> if <paramref name="lhs"/> compares equal to <paramref name="rhs"/>, <c>true</c> otherwise.</returns>
public static bool operator !=(ARLightEstimationData lhs, ARLightEstimationData rhs) => !lhs.Equals(rhs);
float ConvertBrightnessToLumens(float brightness)
{
return Mathf.Clamp(brightness*k_MaxLuminosity, 0f, k_MaxLuminosity);
}
float ConvertLumensToBrightness(float lumens)
{
return Mathf.Clamp(lumens/k_MaxLuminosity, 0f, 1f);
}
private float? m_AverageBrightness;
private float? m_AverageIntensityInLumens;
private float? m_MainLightBrightness;
const float k_MaxLuminosity = 2000.0f;
}
}