ARTextureInfo.cs
10.3 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
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using UnityEngine;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.Rendering;
using Object = UnityEngine.Object;
namespace UnityEngine.XR.ARFoundation
{
/// <summary>
/// Container that pairs a <see cref="Unity.XR.ARSubsystems.XRTextureDescriptor"/> that wraps a native texture
/// object and a <c>Texture</c> that is created for the native texture object.
/// </summary>
internal struct ARTextureInfo : IEquatable<ARTextureInfo>, IDisposable
{
/// <summary>
/// Constant for whether the texture is in a linear color space.
/// </summary>
/// <value>
/// Constant for whether the texture is in a linear color space.
/// </value>
const bool k_TextureHasLinearColorSpace = false;
/// <summary>
/// The texture descriptor describing the metadata for the native texture object.
/// </summary>
/// <value>
/// The texture descriptor describing the metadata for the native texture object.
/// </value>
public XRTextureDescriptor descriptor
{
get { return m_Descriptor; }
}
XRTextureDescriptor m_Descriptor;
/// <summary>
/// The Unity <c>Texture</c> object for the native texture.
/// </summary>
/// <value>
/// The Unity <c>Texture</c> object for the native texture.
/// </value>
public Texture texture
{
get { return m_Texture; }
}
Texture m_Texture;
/// <summary>
/// Constructs the texture info with the given descriptor and material.
/// </summary>
/// <param name="descriptor">The texture descriptor wrapping a native texture object.</param>
public ARTextureInfo(XRTextureDescriptor descriptor)
{
m_Descriptor = descriptor;
m_Texture = CreateTexture(m_Descriptor);
}
/// <summary>
/// Resets the texture info back to the default state destroying the texture game object, if one exists.
/// </summary>
public void Reset()
{
m_Descriptor.Reset();
DestroyTexture();
}
/// <summary>
/// Destroys the texture, and sets the property to <c>null</c>.
/// </summary>
void DestroyTexture()
{
if (m_Texture != null)
{
UnityEngine.Object.Destroy(m_Texture);
m_Texture = null;
}
}
/// <summary>
/// Sets the current descriptor, and creates/updates the associated texture as appropriate.
/// </summary>
/// <param name="textureInfo">The texture info to update.</param>
/// <param name="descriptor">The texture descriptor wrapping a native texture object.</param>
/// <returns>
/// The updated texture information.
/// </returns>
public static ARTextureInfo GetUpdatedTextureInfo(ARTextureInfo textureInfo, XRTextureDescriptor descriptor)
{
// If the current and given descriptors are equal, exit early from this method.
if (textureInfo.m_Descriptor.Equals(descriptor))
{
return textureInfo;
}
// If the given descriptor is invalid, destroy any existing texture, and return the default texture
// info.
if (!descriptor.valid)
{
textureInfo.DestroyTexture();
return default(ARTextureInfo);
}
DebugWarn.WhenFalse(textureInfo.m_Descriptor.dimension == TextureDimension.None || textureInfo.m_Descriptor.dimension == descriptor.dimension)?.
WithMessage($"Texture descriptor dimension should not change from {textureInfo.m_Descriptor.dimension} to {descriptor.dimension}.");
// If there is a texture already and if the descriptors have identical texture metadata, we only need
// to update the existing texture with the given native texture object.
if ((textureInfo.m_Texture != null) && textureInfo.m_Descriptor.hasIdenticalTextureMetadata(descriptor))
{
// Update the current descriptor with the given descriptor.
textureInfo.m_Descriptor = descriptor;
// Update the current texture with the native texture object.
switch(descriptor.dimension)
{
#if UNITY_2020_2_OR_NEWER
case TextureDimension.Tex3D:
((Texture3D)textureInfo.m_Texture).UpdateExternalTexture(textureInfo.m_Descriptor.nativeTexture);
break;
#endif
case TextureDimension.Tex2D:
((Texture2D)textureInfo.m_Texture).UpdateExternalTexture(textureInfo.m_Descriptor.nativeTexture);
break;
case TextureDimension.Cube:
((Cubemap)textureInfo.m_Texture).UpdateExternalTexture(textureInfo.m_Descriptor.nativeTexture);
break;
default:
throw new NotSupportedException($"'{descriptor.dimension.ToString()}' is not a supported texture type.");
}
}
// Else, we need to destroy the existing texture object and create a new texture object.
else
{
// Update the current descriptor with the given descriptor.
textureInfo.m_Descriptor = descriptor;
// Replace the current texture with a newly created texture, and update the material.
textureInfo.DestroyTexture();
textureInfo.m_Texture = CreateTexture(textureInfo.m_Descriptor);
}
return textureInfo;
}
/// <summary>
/// Create the texture object for the native texture wrapped by the valid descriptor.
/// </summary>
/// <param name="descriptor">The texture descriptor wrapping a native texture object.</param>
/// <returns>
/// If the descriptor is valid, the <c>Texture</c> object created from the texture descriptor. Otherwise,
/// <c>null</c>.
/// </returns>
static Texture CreateTexture(XRTextureDescriptor descriptor)
{
if (!descriptor.valid)
{
return null;
}
switch(descriptor.dimension)
{
#if UNITY_2020_2_OR_NEWER
case TextureDimension.Tex3D:
return Texture3D.CreateExternalTexture(descriptor.width, descriptor.height,
descriptor.depth, descriptor.format,
(descriptor.mipmapCount != 0), descriptor.nativeTexture);
#endif
case TextureDimension.Tex2D:
var texture = Texture2D.CreateExternalTexture(descriptor.width, descriptor.height,
descriptor.format, (descriptor.mipmapCount != 0),
k_TextureHasLinearColorSpace,
descriptor.nativeTexture);
// NB: SetWrapMode needs to be the first call here, and the value passed
// needs to be kTexWrapClamp - this is due to limitations of what
// wrap modes are allowed for external textures in OpenGL (which are
// used for ARCore), as Texture::ApplySettings will eventually hit
// an assert about an invalid enum (see calls to glTexParameteri
// towards the top of ApiGLES::TextureSampler)
// reference: "3.7.14 External Textures" section of
// https://www.khronos.org/registry/OpenGL/extensions/OES/OES_EGL_image_external.txt
// (it shouldn't ever matter what the wrap mode is set to normally, since
// this is for a pass-through video texture, so we shouldn't ever need to
// worry about the wrap mode as textures should never "wrap")
texture.wrapMode = TextureWrapMode.Clamp;
texture.filterMode = FilterMode.Bilinear;
texture.hideFlags = HideFlags.HideAndDontSave;
return texture;
case TextureDimension.Cube:
return Cubemap.CreateExternalTexture(descriptor.width,
descriptor.format,
(descriptor.mipmapCount != 0),
descriptor.nativeTexture);
default:
return null;
}
}
public static bool IsSupported(XRTextureDescriptor descriptor)
{
if(descriptor.dimension == TextureDimension.Tex3D)
{
#if UNITY_2020_2_OR_NEWER
return true;
#else
return false;
#endif
}
else if(descriptor.dimension == TextureDimension.Tex2D)
{
return true;
}
else if(descriptor.dimension == TextureDimension.Cube)
{
return true;
}
else
{
return false;
}
}
public void Dispose()
{
DestroyTexture();
}
public override int GetHashCode()
{
int hash = 486187739;
unchecked
{
hash = hash * 486187739 + m_Descriptor.GetHashCode();
hash = hash * 486187739 + ((m_Texture == null) ? 0 : m_Texture.GetHashCode());
}
return hash;
}
public bool Equals(ARTextureInfo other)
{
return m_Descriptor.Equals(other) && (m_Texture == other.m_Texture);
}
public override bool Equals(System.Object obj)
{
return ((obj is ARTextureInfo) && Equals((ARTextureInfo)obj));
}
public static bool operator ==(ARTextureInfo lhs, ARTextureInfo rhs)
{
return lhs.Equals(rhs);
}
public static bool operator !=(ARTextureInfo lhs, ARTextureInfo rhs)
{
return !lhs.Equals(rhs);
}
}
}