Skip to content
This repository was archived by the owner on Sep 17, 2022. It is now read-only.

Commit bb7240c

Browse files
committed
Light Estimation feature added
Due addition of the aboce feature ARcore and ARfoundation packages needed to be updated to 4.1.5..If any problems happen make sure to upgrade the packages.
1 parent 6d8d409 commit bb7240c

File tree

6 files changed

+180
-103
lines changed

6 files changed

+180
-103
lines changed

Assets/Scenes/ARStage.unity

Lines changed: 27 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RenderSettings:
3838
m_ReflectionIntensity: 1
3939
m_CustomReflection: {fileID: 0}
4040
m_Sun: {fileID: 0}
41-
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
41+
m_IndirectSpecularColor: {r: 0.37311953, g: 0.38074014, b: 0.3587274, a: 1}
4242
m_UseRadianceAmbientProbe: 0
4343
--- !u!157 &3
4444
LightmapSettings:
@@ -161,98 +161,6 @@ RectTransform:
161161
m_AnchoredPosition: {x: 0, y: 0.80029297}
162162
m_SizeDelta: {x: 0, y: 1.6231384}
163163
m_Pivot: {x: 0.5, y: 0.5}
164-
--- !u!1 &118669237
165-
GameObject:
166-
m_ObjectHideFlags: 0
167-
m_CorrespondingSourceObject: {fileID: 0}
168-
m_PrefabInstance: {fileID: 0}
169-
m_PrefabAsset: {fileID: 0}
170-
serializedVersion: 6
171-
m_Component:
172-
- component: {fileID: 118669239}
173-
- component: {fileID: 118669238}
174-
m_Layer: 0
175-
m_Name: Directional Light
176-
m_TagString: Untagged
177-
m_Icon: {fileID: 0}
178-
m_NavMeshLayer: 0
179-
m_StaticEditorFlags: 0
180-
m_IsActive: 1
181-
--- !u!108 &118669238
182-
Light:
183-
m_ObjectHideFlags: 0
184-
m_CorrespondingSourceObject: {fileID: 0}
185-
m_PrefabInstance: {fileID: 0}
186-
m_PrefabAsset: {fileID: 0}
187-
m_GameObject: {fileID: 118669237}
188-
m_Enabled: 1
189-
serializedVersion: 10
190-
m_Type: 1
191-
m_Shape: 0
192-
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
193-
m_Intensity: 1
194-
m_Range: 10
195-
m_SpotAngle: 30
196-
m_InnerSpotAngle: 21.80208
197-
m_CookieSize: 10
198-
m_Shadows:
199-
m_Type: 2
200-
m_Resolution: -1
201-
m_CustomResolution: -1
202-
m_Strength: 1
203-
m_Bias: 0.05
204-
m_NormalBias: 0.4
205-
m_NearPlane: 0.2
206-
m_CullingMatrixOverride:
207-
e00: 1
208-
e01: 0
209-
e02: 0
210-
e03: 0
211-
e10: 0
212-
e11: 1
213-
e12: 0
214-
e13: 0
215-
e20: 0
216-
e21: 0
217-
e22: 1
218-
e23: 0
219-
e30: 0
220-
e31: 0
221-
e32: 0
222-
e33: 1
223-
m_UseCullingMatrixOverride: 0
224-
m_Cookie: {fileID: 0}
225-
m_DrawHalo: 0
226-
m_Flare: {fileID: 0}
227-
m_RenderMode: 0
228-
m_CullingMask:
229-
serializedVersion: 2
230-
m_Bits: 4294967295
231-
m_RenderingLayerMask: 1
232-
m_Lightmapping: 4
233-
m_LightShadowCasterMode: 0
234-
m_AreaSize: {x: 1, y: 1}
235-
m_BounceIntensity: 1
236-
m_ColorTemperature: 6570
237-
m_UseColorTemperature: 0
238-
m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0}
239-
m_UseBoundingSphereOverride: 0
240-
m_ShadowRadius: 0
241-
m_ShadowAngle: 0
242-
--- !u!4 &118669239
243-
Transform:
244-
m_ObjectHideFlags: 0
245-
m_CorrespondingSourceObject: {fileID: 0}
246-
m_PrefabInstance: {fileID: 0}
247-
m_PrefabAsset: {fileID: 0}
248-
m_GameObject: {fileID: 118669237}
249-
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
250-
m_LocalPosition: {x: 0, y: 3, z: 0}
251-
m_LocalScale: {x: 1, y: 1, z: 1}
252-
m_Children: []
253-
m_Father: {fileID: 0}
254-
m_RootOrder: 0
255-
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
256164
--- !u!1 &145743102
257165
GameObject:
258166
m_ObjectHideFlags: 0
@@ -728,6 +636,7 @@ MonoBehaviour:
728636
m_EditorClassIdentifier:
729637
m_AttemptUpdate: 1
730638
m_MatchFrameRate: 1
639+
m_TrackingMode: 2
731640
--- !u!1 &669288912
732641
GameObject:
733642
m_ObjectHideFlags: 0
@@ -866,7 +775,7 @@ Transform:
866775
m_LocalScale: {x: 1, y: 1, z: 1}
867776
m_Children: []
868777
m_Father: {fileID: 0}
869-
m_RootOrder: 2
778+
m_RootOrder: 1
870779
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
871780
--- !u!1 &703624896
872781
GameObject:
@@ -1166,7 +1075,7 @@ Transform:
11661075
- {fileID: 543700800}
11671076
- {fileID: 1696152894}
11681077
m_Father: {fileID: 0}
1169-
m_RootOrder: 1
1078+
m_RootOrder: 0
11701079
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
11711080
--- !u!1 &781962095
11721081
GameObject:
@@ -1216,8 +1125,6 @@ MonoBehaviour:
12161125
m_EditorClassIdentifier:
12171126
m_UseCustomMaterial: 0
12181127
m_CustomMaterial: {fileID: 0}
1219-
m_UseCustomRendererAsset: 0
1220-
m_CustomRendererAsset: {fileID: 0}
12211128
--- !u!114 &781962098
12221129
MonoBehaviour:
12231130
m_ObjectHideFlags: 0
@@ -1230,8 +1137,11 @@ MonoBehaviour:
12301137
m_Script: {fileID: 11500000, guid: 4966719baa26e4b0e8231a24d9bd491a, type: 3}
12311138
m_Name:
12321139
m_EditorClassIdentifier:
1233-
m_FocusMode: 1
1234-
m_LightEstimationMode: 0
1140+
m_FocusMode: -1
1141+
m_LightEstimationMode: -1
1142+
m_AutoFocus: 1
1143+
m_LightEstimation: -1
1144+
m_FacingDirection: 1
12351145
--- !u!114 &781962099
12361146
MonoBehaviour:
12371147
m_ObjectHideFlags: 0
@@ -1275,7 +1185,7 @@ Camera:
12751185
height: 1
12761186
near clip plane: 0.1
12771187
far clip plane: 20
1278-
field of view: 60
1188+
field of view: 26.991467
12791189
orthographic: 0
12801190
orthographic size: 5
12811191
m_Depth: 0
@@ -1612,6 +1522,7 @@ MonoBehaviour:
16121522
m_Script: {fileID: 11500000, guid: fa17d122634046b4a8e23048891fafc5, type: 3}
16131523
m_Name:
16141524
m_EditorClassIdentifier:
1525+
m_RaycastPrefab: {fileID: 0}
16151526
--- !u!114 &1084428878
16161527
MonoBehaviour:
16171528
m_ObjectHideFlags: 0
@@ -2036,6 +1947,7 @@ GameObject:
20361947
m_Component:
20371948
- component: {fileID: 1396019539}
20381949
- component: {fileID: 1396019540}
1950+
- component: {fileID: 1396019541}
20391951
m_Layer: 0
20401952
m_Name: Directional Light
20411953
m_TagString: Untagged
@@ -2050,13 +1962,13 @@ Transform:
20501962
m_PrefabInstance: {fileID: 0}
20511963
m_PrefabAsset: {fileID: 0}
20521964
m_GameObject: {fileID: 1396019538}
2053-
m_LocalRotation: {x: 0.41860905, y: -0, z: -0, w: 0.9081665}
1965+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
20541966
m_LocalPosition: {x: 0, y: 0, z: 0}
20551967
m_LocalScale: {x: 1, y: 1, z: 1}
20561968
m_Children: []
20571969
m_Father: {fileID: 763329528}
20581970
m_RootOrder: 0
2059-
m_LocalEulerAnglesHint: {x: 49.494003, y: 0, z: 0}
1971+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
20601972
--- !u!108 &1396019540
20611973
Light:
20621974
m_ObjectHideFlags: 0
@@ -2118,6 +2030,19 @@ Light:
21182030
m_UseBoundingSphereOverride: 0
21192031
m_ShadowRadius: 0
21202032
m_ShadowAngle: 0
2033+
--- !u!114 &1396019541
2034+
MonoBehaviour:
2035+
m_ObjectHideFlags: 0
2036+
m_CorrespondingSourceObject: {fileID: 0}
2037+
m_PrefabInstance: {fileID: 0}
2038+
m_PrefabAsset: {fileID: 0}
2039+
m_GameObject: {fileID: 1396019538}
2040+
m_Enabled: 1
2041+
m_EditorHideFlags: 0
2042+
m_Script: {fileID: 11500000, guid: 4117ea859710c4c42b38c4cb4802a615, type: 3}
2043+
m_Name:
2044+
m_EditorClassIdentifier:
2045+
arCameraManager: {fileID: 781962098}
21212046
--- !u!1 &1487940038
21222047
GameObject:
21232048
m_ObjectHideFlags: 0

Assets/Scripts/LightEstimation.cs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using UnityEngine;
2+
using UnityEngine.Rendering;
3+
using UnityEngine.XR.ARFoundation;
4+
5+
[RequireComponent(typeof(Light))]
6+
public class LightEstimation : MonoBehaviour
7+
{
8+
[SerializeField]
9+
private ARCameraManager arCameraManager;
10+
11+
Light mainLight;
12+
13+
/// <summary>
14+
/// The estimated brightness of the physical environment, if available.
15+
/// </summary>
16+
public float? brightness { get; private set; }
17+
18+
/// <summary>
19+
/// The estimated color temperature of the physical environment, if available.
20+
/// </summary>
21+
public float? colorTemperature { get; private set; }
22+
23+
/// <summary>
24+
/// The estimated color correction value of the physical environment, if available.
25+
/// </summary>
26+
public Color? colorCorrection { get; private set; }
27+
28+
/// <summary>
29+
/// The estimated direction of the main light of the physical environment, if available.
30+
/// </summary>
31+
public Vector3? mainLightDirection { get; private set; }
32+
33+
/// <summary>
34+
/// The estimated color of the main light of the physical environment, if available.
35+
/// </summary>
36+
public Color? mainLightColor { get; private set; }
37+
38+
/// <summary>
39+
/// The estimated intensity in lumens of main light of the physical environment, if available.
40+
/// </summary>
41+
public float? mainLightIntensityLumens { get; private set; }
42+
43+
/// <summary>
44+
/// The estimated spherical harmonics coefficients of the physical environment, if available.
45+
/// </summary>
46+
public SphericalHarmonicsL2? sphericalHarmonics { get; private set; }
47+
48+
void Awake()
49+
{
50+
mainLight = GetComponent<Light>();
51+
}
52+
void OnEnable()
53+
{
54+
if (arCameraManager != null)
55+
arCameraManager.frameReceived += FrameChanged;
56+
}
57+
void OnDisable()
58+
{
59+
if (arCameraManager != null)
60+
arCameraManager.frameReceived -= FrameChanged;
61+
}
62+
63+
void FrameChanged(ARCameraFrameEventArgs args)
64+
{
65+
if (args.lightEstimation.averageBrightness.HasValue)
66+
{
67+
brightness = args.lightEstimation.averageBrightness.Value;
68+
mainLight.intensity = brightness.Value;
69+
}
70+
if (args.lightEstimation.averageColorTemperature.HasValue)
71+
{
72+
colorTemperature = args.lightEstimation.averageColorTemperature.Value;
73+
mainLight.colorTemperature = colorTemperature.Value;
74+
}
75+
76+
if (args.lightEstimation.colorCorrection.HasValue)
77+
{
78+
colorCorrection = args.lightEstimation.colorCorrection.Value;
79+
mainLight.color = colorCorrection.Value;
80+
}
81+
if (args.lightEstimation.mainLightDirection.HasValue)
82+
{
83+
mainLightDirection = args.lightEstimation.mainLightDirection;
84+
mainLight.transform.rotation = Quaternion.LookRotation(mainLightDirection.Value);
85+
}
86+
if (args.lightEstimation.mainLightColor.HasValue)
87+
{
88+
mainLightColor = args.lightEstimation.mainLightColor;
89+
90+
#if PLATFORM_ANDROID
91+
// ARCore needs to apply energy conservation term (1 / PI) and be placed in gamma
92+
mainLight.color = mainLightColor.Value / Mathf.PI;
93+
mainLight.color = mainLight.color.gamma;
94+
95+
// ARCore returns color in HDR format (can be represented as FP16 and have values above 1.0)
96+
var camera = arCameraManager.GetComponentInParent<Camera>();
97+
if (camera == null || !camera.allowHDR)
98+
{
99+
Debug.LogWarning($"HDR Rendering is not allowed. Color values returned could be above the maximum representable value.");
100+
}
101+
#endif
102+
}
103+
if (args.lightEstimation.mainLightIntensityLumens.HasValue)
104+
{
105+
mainLightIntensityLumens = args.lightEstimation.mainLightIntensityLumens;
106+
mainLight.intensity = args.lightEstimation.averageMainLightBrightness.Value;
107+
}
108+
if (args.lightEstimation.ambientSphericalHarmonics.HasValue)
109+
{
110+
sphericalHarmonics = args.lightEstimation.ambientSphericalHarmonics;
111+
RenderSettings.ambientMode = AmbientMode.Skybox;
112+
RenderSettings.ambientProbe = sphericalHarmonics.Value;
113+
}
114+
}
115+
}

Assets/Scripts/LightEstimation.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: 9dae4501572e1418791be3e3bf1f7faa, type: 3}
13+
m_Name: AR Core Settings
14+
m_EditorClassIdentifier:
15+
m_Requirement: 0
16+
m_Depth: 0
17+
m_IgnoreGradleVersion: 0

Assets/XR/Settings/AR Core Settings.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/XR/XRGeneralSettings.asset

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ MonoBehaviour:
4444
m_RequiresSettingsUpdate: 0
4545
m_AutomaticLoading: 0
4646
m_AutomaticRunning: 0
47-
m_Loaders: []
47+
m_Loaders:
48+
- {fileID: 11400000, guid: 0450ab9a2603c59499d4e0c0abc60789, type: 2}

0 commit comments

Comments
 (0)