AREnvironmentProbe.cs
8.51 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
using System;
using UnityEngine.Rendering;
using UnityEngine.XR.ARSubsystems;
using Object = UnityEngine.Object;
namespace UnityEngine.XR.ARFoundation
{
/// <summary>
/// A game object component to manage the reflection probe settings as the environment probe changes are applied.
/// </summary>
[RequireComponent(typeof(ReflectionProbe))]
[DisallowMultipleComponent]
[DefaultExecutionOrder(ARUpdateOrder.k_EnvironmentProbe)]
[HelpURL(HelpUrls.ApiWithNamespace + nameof(AREnvironmentProbe) + ".html")]
public class AREnvironmentProbe : ARTrackable<XREnvironmentProbe, AREnvironmentProbe>
{
/// <summary>
/// The reflection probe component attached to the game object.
/// </summary>
/// <value>
/// The reflection probe component attached to the game object.
/// </value>
ReflectionProbe m_ReflectionProbe;
/// <summary>
/// The current environment texture data wrapping the reflection probe environment texture.
/// </summary>
/// <value>
/// The current environment texture data wrapping the reflection probe environment texture.
/// </value>
XRTextureDescriptor m_CurrentTextureDescriptor;
/// <summary>
/// The texture info of the custom baked texture from the reflection probe.
/// </summary>
/// <value>
/// The texture info of the custom baked texture from the reflection probe.
/// </value>
ARTextureInfo m_CustomBakedTextureInfo;
/// <summary>
/// Specifies the texture filter mode to be used with the environment texture.
/// </summary>
/// <value>
/// The texture filter mode to be used with the environment texture.
/// </value>
public FilterMode environmentTextureFilterMode
{
get => m_EnvironmentTextureFilterMode;
set
{
m_EnvironmentTextureFilterMode = value;
if ((m_ReflectionProbe != null) && (m_ReflectionProbe.customBakedTexture != null))
{
m_ReflectionProbe.customBakedTexture.filterMode = m_EnvironmentTextureFilterMode;
}
}
}
FilterMode m_EnvironmentTextureFilterMode = FilterMode.Trilinear;
/// <summary>
/// The placement type (for example, manual or automatic). If manual, this probe was created by <see cref="AREnvironmentProbeManager.AddEnvironmentProbe(Pose, Vector3, Vector3)"/>.
/// </summary>
public AREnvironmentProbePlacementType placementType { get; internal set; }
/// <summary>
/// The size (dimensions) of the environment probe.
/// </summary>
public Vector3 size => sessionRelativeData.size;
/// <summary>
/// The extents of the environment probe. This is always half the <see cref="size"/>.
/// </summary>
public Vector3 extents => size * .5f;
/// <summary>
/// A native pointer associated with this environment probe.
/// The data pointed to by this pointer is implementation defined.
/// While the lifetime is also implementation defined, it should be valid at
/// least until the next frame.
/// </summary>
public IntPtr nativePtr => sessionRelativeData.nativePtr;
/// <summary>
/// The <c>XRTextureDescriptor</c> associated with this environment probe. This is used to generate the cubemap texture on the reflection probe component.
/// </summary>
public XRTextureDescriptor textureDescriptor => m_CurrentTextureDescriptor;
/// <summary>
/// Initializes the game object transform and reflection probe component from the scene.
/// </summary>
void Awake()
{
m_ReflectionProbe = GetComponent<ReflectionProbe>();
// Set the reflection probe mode to use a custom baked texture.
m_ReflectionProbe.mode = ReflectionProbeMode.Custom;
}
Transform GetTrackablesParent()
{
if (AREnvironmentProbeManager.instance is AREnvironmentProbeManager manager)
{
return manager.GetComponent<ARSessionOrigin>().trackablesParent;
}
return null;
}
internal protected override void OnAfterSetSessionRelativeData()
{
var trackablesParent = GetTrackablesParent();
if (trackablesParent && transform.parent != trackablesParent)
{
var pose = sessionRelativeData.pose;
// Compute the desired world-space transform matrix for the environment probe
var desiredLocalToWorld = trackablesParent.localToWorldMatrix * Matrix4x4.TRS(pose.position, pose.rotation, sessionRelativeData.scale);
if (transform.parent)
{
// If the probe has a parent (i.e., it is not at the root), then we need to compute
// its localScale such that its final world-space scale will match what it would be
// if it were under the ARSessionOrigin.
var localToParent = transform.parent.worldToLocalMatrix * desiredLocalToWorld;
transform.localScale = localToParent.lossyScale;
}
else
{
// Otherwise, it is at the scene root, so just set its localScale is its world-space scale.
transform.localScale = desiredLocalToWorld.lossyScale;
}
}
else
{
transform.localScale = sessionRelativeData.scale;
}
// Update the environment texture if the environment texture is valid.
if (sessionRelativeData.textureDescriptor.valid)
{
UpdateEnvironmentTexture(sessionRelativeData.textureDescriptor);
}
// Update the reflection probe box.
m_ReflectionProbe.center = Vector3.zero;
m_ReflectionProbe.size = sessionRelativeData.size;
m_ReflectionProbe.boxProjection = !float.IsInfinity(m_ReflectionProbe.size.x);
// Manual placement is set by the manager. Unknown means it must have been added automatically.
if (placementType == AREnvironmentProbePlacementType.Unknown)
{
placementType = AREnvironmentProbePlacementType.Automatic;
}
}
/// <summary>
/// Applies the texture data in the <c>XRTextureDescriptor</c> to the reflection probe settings.
/// </summary>
/// <param name="textureDescriptor">The environment texture data to apply to the reflection probe baked
/// texture.</param>
void UpdateEnvironmentTexture(XRTextureDescriptor textureDescriptor)
{
if(m_ReflectionProbe.customBakedTexture == null)
{
m_CustomBakedTextureInfo = new ARTextureInfo(textureDescriptor);
}
else
{
m_CustomBakedTextureInfo = ARTextureInfo.GetUpdatedTextureInfo(m_CustomBakedTextureInfo, textureDescriptor);
}
m_CustomBakedTextureInfo.texture.filterMode = m_EnvironmentTextureFilterMode;
m_ReflectionProbe.customBakedTexture = m_CustomBakedTextureInfo.texture as Cubemap;
// Update the current environment texture metadata.
m_CurrentTextureDescriptor = textureDescriptor;
}
/// <summary>
/// Generates a string representation of this <see cref="AREnvironmentProbe"/>.
/// </summary>
/// <returns>A string representation of this <see cref="AREnvironmentProbe"/>.</returns>
public override string ToString() => $"{name} [trackableId:{trackableId.ToString()}]";
void OnEnable()
{
if (AREnvironmentProbeManager.instance is AREnvironmentProbeManager manager)
{
manager.TryAddEnvironmentProbe(this);
}
else
{
pending = true;
}
}
void Update()
{
if (trackableId.Equals(TrackableId.invalidId) && AREnvironmentProbeManager.instance is AREnvironmentProbeManager manager)
{
manager.TryAddEnvironmentProbe(this);
}
}
void OnDisable()
{
if (AREnvironmentProbeManager.instance is AREnvironmentProbeManager manager)
{
manager.TryRemoveEnvironmentProbe(this);
}
}
}
}