ARCoreImageTrackingSubsystem.cs
5.88 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
using System;
using System.IO;
using System.Runtime.InteropServices;
using Unity.Collections;
using UnityEngine.Scripting;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARCore
{
/// <summary>
/// The ARCore implementation of the <c>XRImageTrackingSubsystem</c>. Do not create this directly. Use the <c>SubsystemManager</c> instead.
/// </summary>
[Preserve]
public sealed class ARCoreImageTrackingSubsystem : XRImageTrackingSubsystem
{
internal static readonly string k_StreamingAssetsPath =
#if UNITY_EDITOR
Path.Combine(Application.streamingAssetsPath, "HiddenARCore");
#else
"jar:file://" + Application.dataPath + "!/assets/HiddenARCore";
#endif
internal static string GetPathForLibrary(XRReferenceImageLibrary library)
{
if (library == null)
throw new ArgumentNullException("library");
return Path.Combine(k_StreamingAssetsPath, library.guid.ToString() + ".imgdb");
}
class ARCoreProvider : Provider
{
#if UNITY_2020_2_OR_NEWER
public override void Start() { }
public override void Stop() { }
#endif
public override RuntimeReferenceImageLibrary imageLibrary
{
set
{
if (value == null)
{
UnityARCore_imageTracking_setDatabase(IntPtr.Zero);
}
else if (value is ARCoreImageDatabase database)
{
UnityARCore_imageTracking_setDatabase((IntPtr)database);
}
else
{
throw new ArgumentException($"The {value.GetType().Name} is not a valid ARCore image library.");
}
}
}
public unsafe override RuntimeReferenceImageLibrary CreateRuntimeLibrary(
XRReferenceImageLibrary serializedLibrary)
{
return new ARCoreImageDatabase(serializedLibrary);
}
public unsafe override TrackableChanges<XRTrackedImage> GetChanges(
XRTrackedImage defaultTrackedImage,
Allocator allocator)
{
void* addedPtr, updatedPtr, removedPtr;
int addedLength, updatedLength, removedLength, stride;
var context = UnityARCore_imageTracking_acquireChanges(
out addedPtr, out addedLength,
out updatedPtr, out updatedLength,
out removedPtr, out removedLength,
out stride);
try
{
return new TrackableChanges<XRTrackedImage>(
addedPtr, addedLength,
updatedPtr, updatedLength,
removedPtr, removedLength,
defaultTrackedImage, stride,
allocator);
}
finally
{
UnityARCore_imageTracking_releaseChanges(context);
}
}
public override void Destroy() => UnityARCore_imageTracking_destroy();
/// <summary>
/// This must be implemented if supportsMovingImages is true.
/// ARCore doesn't let you set the max number -- it just tracks everything
/// </summary>
public override int requestedMaxNumberOfMovingImages
{
get => m_RequestedMaxNumberOfMovingImages;
set => m_RequestedMaxNumberOfMovingImages = value;
}
int m_RequestedMaxNumberOfMovingImages;
public override int currentMaxNumberOfMovingImages => Mathf.Max(m_RequestedMaxNumberOfMovingImages, GetNumberOfTrackedImages());
[DllImport("UnityARCore")]
static unsafe extern void UnityARCore_imageTracking_setDatabase(IntPtr imageDatabase);
[DllImport("UnityARCore")]
static extern void UnityARCore_imageTracking_destroy();
[DllImport("UnityARCore")]
static extern unsafe void* UnityARCore_imageTracking_acquireChanges(
out void* addedPtr, out int addedLength,
out void* updatedPtr, out int updatedLength,
out void* removedPtr, out int removedLength,
out int stride);
[DllImport("UnityARCore")]
static extern unsafe void UnityARCore_imageTracking_releaseChanges(void* changes);
[DllImport("UnityARCore", EntryPoint="UnityARCore_imageTracking_getNumberOfTrackedImages")]
static extern int GetNumberOfTrackedImages();
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void RegisterDescriptor()
{
if (!Api.platformAndroid || !Api.loaderPresent)
return;
XRImageTrackingSubsystemDescriptor.Create(new XRImageTrackingSubsystemDescriptor.Cinfo
{
id = "ARCore-ImageTracking",
#if UNITY_2020_2_OR_NEWER
providerType = typeof(ARCoreImageTrackingSubsystem.ARCoreProvider),
subsystemTypeOverride = typeof(ARCoreImageTrackingSubsystem),
#else
subsystemImplementationType = typeof(ARCoreImageTrackingSubsystem),
#endif
supportsMovingImages = true,
supportsMutableLibrary = true,
supportsImageValidation = true,
});
}
#if !UNITY_2020_2_OR_NEWER
/// <summary>
/// Creates the ARCore-specific implementation which will service the `XRImageTrackingSubsystem`.
/// </summary>
/// <returns>A new instance of the `Provider` specific to ARCore.</returns>
protected override Provider CreateProvider() => new ARCoreProvider();
#endif
}
}