-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathARLightEstimate.cs
142 lines (110 loc) · 4.16 KB
/
ARLightEstimate.cs
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
using System;
using System.Runtime.InteropServices;
namespace UnityEngine.XR.iOS
{
public struct ARLightEstimate
{
public float ambientIntensity;
}
[Serializable]
public struct UnityARLightEstimate
{
public float ambientIntensity;
public float ambientColorTemperature;
public UnityARLightEstimate(float intensity, float temperature)
{
ambientIntensity = intensity;
ambientColorTemperature = temperature;
}
};
public struct MarshalDirectionalLightEstimate
{
public Vector4 primaryDirAndIntensity;
public IntPtr sphericalHarmonicCoefficientsPtr;
public float [] SphericalHarmonicCoefficients { get { return MarshalCoefficients(sphericalHarmonicCoefficientsPtr); } }
float [] MarshalCoefficients(IntPtr ptrFloats)
{
int numFloats = 27;
float [] workCoefficients = new float[numFloats];
Marshal.Copy (ptrFloats, workCoefficients, 0, (int)(numFloats));
RotateForUnity (ref workCoefficients, 0);
RotateForUnity (ref workCoefficients, 9);
RotateForUnity (ref workCoefficients, 18);
return workCoefficients;
}
void RotateForUnity(ref float[] shc, int startIndex)
{
//from observation, it looks like top, bottom and left,right are flipped (x,y)
// we got this info:
// Lets assume Unity uses the same convention for spherical harmonics (yzx) wrt its own coordinate system as scenekit.
// (This could be another source of error).
// To tackle a constant transform between the unity and scenekit world coordinate system conventions:
// Axis flips work as follows for the 9 components of the sh vector:
// 0-component: no change
// 1-component: negate if y is supposed to be flipped
// 2-component: negate if z is supposed to be flipped
// 3-component: negate if x is supposed to be flipped
// 4-component: negate if x or y are supposed to be flipped, but not both!
// 5-component: negate if y or z are supposed to be flipped, but not both!
// 6-component: no change
// 7-component: negate if x or z are supposed to be flipped, but not both!
// 8-component: no change
shc [startIndex+1] = -shc [startIndex+1];
shc [startIndex+3] = -shc [startIndex+3];
shc [startIndex + 5] = -shc [startIndex + 5];
shc [startIndex + 7] = -shc [startIndex + 7];
}
}
public class UnityARDirectionalLightEstimate
{
public Vector3 primaryLightDirection;
public float primaryLightIntensity;
public float [] sphericalHarmonicsCoefficients;
public UnityARDirectionalLightEstimate (float [] SHC, Vector3 direction, float intensity)
{
sphericalHarmonicsCoefficients = SHC;
primaryLightDirection = direction;
primaryLightIntensity = intensity;
}
};
public enum LightDataType
{
LightEstimate,
DirectionalLightEstimate
}
public struct UnityMarshalLightData
{
public LightDataType arLightingType;
public UnityARLightEstimate arLightEstimate;
public MarshalDirectionalLightEstimate arDirectonalLightEstimate;
public UnityMarshalLightData(LightDataType ldt, UnityARLightEstimate ule, MarshalDirectionalLightEstimate mdle)
{
arLightingType = ldt;
arLightEstimate = ule;
arDirectonalLightEstimate = mdle;
}
public static implicit operator UnityARLightData(UnityMarshalLightData rValue)
{
UnityARDirectionalLightEstimate udle = null;
if (rValue.arLightingType == LightDataType.DirectionalLightEstimate) {
Vector4 lightDirAndIntensity = rValue.arDirectonalLightEstimate.primaryDirAndIntensity;
Vector3 lightDir = new Vector3 (lightDirAndIntensity.x, lightDirAndIntensity.y, lightDirAndIntensity.z);
float[] shc = rValue.arDirectonalLightEstimate.SphericalHarmonicCoefficients;
udle = new UnityARDirectionalLightEstimate (shc, lightDir, lightDirAndIntensity.w);
}
return new UnityARLightData(rValue.arLightingType, rValue.arLightEstimate, udle);
}
}
public struct UnityARLightData
{
public LightDataType arLightingType;
public UnityARLightEstimate arLightEstimate;
public UnityARDirectionalLightEstimate arDirectonalLightEstimate;
public UnityARLightData(LightDataType ldt, UnityARLightEstimate ule, UnityARDirectionalLightEstimate udle)
{
arLightingType = ldt;
arLightEstimate = ule;
arDirectonalLightEstimate = udle;
}
}
}