-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathXRObjectTrackingFeature.cs
More file actions
202 lines (181 loc) · 7.5 KB
/
Copy pathXRObjectTrackingFeature.cs
File metadata and controls
202 lines (181 loc) · 7.5 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
// <copyright file="XRObjectTrackingFeature.cs" company="Google LLC">
//
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
//-----------------------------------------------------------------------
namespace Google.XR.Extensions
{
using System.Collections.Generic;
using Google.XR.Extensions.Internal;
using UnityEngine;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.XR.Management;
using UnityEngine.XR.OpenXR;
using UnityEngine.XR.OpenXR.Features;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.XR.OpenXR.Features;
#endif
/// <summary>
/// This <see cref="OpenXRInteractionFeature"/> configures Android XR extensions
/// <c>XR_ANDROID_trackables</c> and <c>XR_ANDROID_trackables_object</c> at runtime and provides
/// <see cref="XRObjectTrackingSubsystem"/> implementation that works on Android XR platform.
///
/// Note: due to the dependency on <see cref="XRSessionFeature"/> and the shared native provider
/// of <see cref="XRTrackableFeature"/>, its priority must be lower than both features so the
/// feature registration happens after XrInstanceManager and XrTrackableProvider creation.
/// </summary>
#if UNITY_EDITOR
[OpenXRFeature(UiName = UiName,
BuildTargetGroups = new[] { BuildTargetGroup.Android },
Company = "Google",
OpenxrExtensionStrings = ExtensionStrings,
Desc = "Enable Android XR Object Tracking at runtime. " +
"And provide the implementation of XRObjectTrackingSubsystem",
Version = "1.0.0",
Category = FeatureCategory.Feature,
FeatureId = FeatureId,
Priority = 97)]
#endif
public class XRObjectTrackingFeature : OpenXRFeature
{
/// <summary>
/// The UI name shows on the XR Plug-in Management panel, help users to understand
/// validation errors and expected fixes.
/// </summary>
public const string UiName = "Android XR (Extensions): Object Tracking";
/// <summary>
/// The feature ID string.
/// </summary>
public const string FeatureId = "com.google.xr.extensions.object_tracking";
/// <summary>
/// The OpenXR Extension string. Used to check if this extensions is
/// available or enabled.
/// To enable runtime confiugration, always enabling persistence extension
/// when it's available.
/// </summary>
public const string ExtensionStrings =
"XR_ANDROID_trackables " +
"XR_ANDROID_trackables_object";
/// <summary>
/// Runtime permission required to enable scene understanding.
/// </summary>
public static readonly AndroidXRPermission RequiredPermission =
AndroidXRPermission.SceneUnderstandingCoarse;
internal static bool? _extensionEnabled = null;
private XRObjectTrackingSubsystem _trackingSubsystem = null;
/// <summary>
/// Gets if the required OpenXR extension is enabled.
/// When OpenXR runtime is waiting, it returns <c>null</c>. Otherwise, it indicates
/// whether the XR_ANDROID_trackables and XR_ANDROID_trackables_object extensions are
/// available on current device.
/// </summary>
public static bool? IsExtensionEnabled => _extensionEnabled;
/// <inheritdoc/>
protected override bool OnInstanceCreate(ulong xrInstance)
{
if (!base.OnInstanceCreate(xrInstance))
{
return false;
}
string[] extensions = ExtensionStrings.Split(" ");
_extensionEnabled = false;
foreach (string extension in extensions)
{
if (!OpenXRRuntime.IsExtensionEnabled(extension))
{
return false;
}
}
_extensionEnabled = true;
// Basic tracking funtion uses XrTrackableProvider.
return XRInstanceManagerApi.Register(ApiXrFeature.Trackable);
}
/// <inheritdoc/>
protected override void OnInstanceDestroy(ulong xrInstance)
{
// Due the sharing of XrTrackableProvider among all trackables,
// DO NOT unregister ApiXrFeature.Trackable from a signle feature.
// Leave to XrInstanceManager to destroy XrTrackableProvider.
}
/// <inheritdoc/>
protected override void OnSubsystemCreate()
{
var descriptors = new List<XRObjectTrackingSubsystemDescriptor>();
SubsystemManager.GetSubsystemDescriptors(descriptors);
if (descriptors.Count < 1)
{
Debug.LogWarning("Failed to find XRObjectTrackingSubsystemDescriptor.");
return;
}
// Create custom XRObjectTrackingSubsystem.
CreateSubsystem<XRObjectTrackingSubsystemDescriptor, XRObjectTrackingSubsystem>(
descriptors, AndroidXRObjectTrackingSubsystem._id);
XRLoader xrLoader = XRSessionFeature.GetXRLoader();
if (xrLoader != null)
{
_trackingSubsystem = xrLoader.GetLoadedSubsystem<XRObjectTrackingSubsystem>();
}
else
{
Debug.LogWarning("Failed to find any active loader.");
}
if (_trackingSubsystem == null)
{
Debug.LogErrorFormat(
"Failed to find descriptor '{0}' - Object Tracking will not do anything!",
AndroidXRObjectTrackingSubsystem._id);
return;
}
else
{
Debug.LogFormat("Created {0}.", _trackingSubsystem.GetType());
}
}
/// <inheritdoc/>
protected override void OnSubsystemStart()
{
Debug.Log($"{ApiConstants.LogTag}:: Start AndroidXRObjectTrackingSubsystem.");
if (_trackingSubsystem != null)
{
// TODO(b/322517976): track long term solution and update accordingly.
// Temp solution to workaround subsystem crashes around null library.
// It still results in spamming errors from ARTrackedObjectManager.
Debug.Log("Setting an empty library before start.");
_trackingSubsystem.library = new XRReferenceObjectLibrary();
_trackingSubsystem.Start();
}
}
/// <inheritdoc/>
protected override void OnSubsystemStop()
{
Debug.Log($"{ApiConstants.LogTag}:: Stop AndroidXRObjectTrackingSubsystem.");
if (_trackingSubsystem != null)
{
_trackingSubsystem.Stop();
}
}
/// <inheritdoc/>
protected override void OnSubsystemDestroy()
{
Debug.Log($"{ApiConstants.LogTag}:: Destroy AndroidXRObjectTrackingSubsystem.");
if (_trackingSubsystem != null)
{
_trackingSubsystem.Destroy();
}
}
}
}