TrackedImageSamples.cs
5.33 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 NUnit.Framework;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARFoundation
{
[TestFixture]
class TrackedImageSamples
{
// Disable "field never assigned to"
#pragma warning disable CS0649
class EnumerateTrackables
{
#region trackedimage_subscribe_to_events
[SerializeField]
ARTrackedImageManager m_TrackedImageManager;
void OnEnable() => m_TrackedImageManager.trackedImagesChanged += OnChanged;
void OnDisable() => m_TrackedImageManager.trackedImagesChanged -= OnChanged;
void OnChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
foreach (var newImage in eventArgs.added)
{
// Handle added event
}
foreach (var updatedImage in eventArgs.updated)
{
// Handle updated event
}
foreach (var removedImage in eventArgs.removed)
{
// Handle removed event
}
}
#endregion
#region trackedimage_enumerate_trackables
void ListAllImages()
{
Debug.Log(
$"There are {m_TrackedImageManager.trackables.count} images being tracked.");
foreach (var trackedImage in m_TrackedImageManager.trackables)
{
Debug.Log($"Image: {trackedImage.referenceImage.name} is at " +
$"{trackedImage.transform.position}");
}
}
#endregion
#region trackedimage_get_by_trackableId
ARTrackedImage GetImageAt(TrackableId trackableId)
{
return m_TrackedImageManager.trackables[trackableId];
}
#endregion
}
class ScheduleAddImageJob
{
#region trackedimage_ScheduleAddImageJob
[SerializeField]
ARTrackedImageManager m_TrackedImageManager;
void AddImage(Texture2D imageToAdd)
{
if (m_TrackedImageManager.referenceLibrary is MutableRuntimeReferenceImageLibrary mutableLibrary)
{
mutableLibrary.ScheduleAddImageWithValidationJob(
imageToAdd,
"my new image",
0.5f /* 50 cm */);
}
}
#endregion
#region trackedimage_DeallocateOnJobCompletion
struct DeallocateJob : IJob
{
[DeallocateOnJobCompletion]
public NativeArray<byte> data;
public void Execute() { }
}
void AddImage(NativeArray<byte> grayscaleImageBytes,
int widthInPixels, int heightInPixels,
float widthInMeters)
{
if (m_TrackedImageManager.referenceLibrary is MutableRuntimeReferenceImageLibrary mutableLibrary)
{
var aspectRatio = (float)widthInPixels / (float)heightInPixels;
var sizeInMeters = new Vector2(widthInMeters, widthInMeters * aspectRatio);
var referenceImage = new XRReferenceImage(
// Guid is assigned after image is added
SerializableGuid.empty,
// No texture associated with this reference image
SerializableGuid.empty,
sizeInMeters, "My Image", null);
var jobState = mutableLibrary.ScheduleAddImageWithValidationJob(
grayscaleImageBytes,
new Vector2Int(widthInPixels, heightInPixels),
TextureFormat.R8,
referenceImage);
// Schedule a job that deallocates the image bytes after the image
// is added to the reference image library.
new DeallocateJob { data = grayscaleImageBytes }.Schedule(jobState.jobHandle);
}
else
{
// Cannot add the image, so dispose its memory.
grayscaleImageBytes.Dispose();
}
}
#endregion
}
class CreateRuntimeLibrary
{
[SerializeField]
ARTrackedImageManager m_TrackedImageManager;
#region trackedimage_CreateRuntimeLibrary
void AddImage(Texture2D imageToAdd)
{
var library = m_TrackedImageManager.CreateRuntimeLibrary();
if (library is MutableRuntimeReferenceImageLibrary mutableLibrary)
{
mutableLibrary.ScheduleAddImageWithValidationJob(
imageToAdd,
"my new image",
0.5f /* 50 cm */);
}
}
#endregion
#region trackedimage_supportsMutableLibrary
bool DoesSupportMutableImageLibraries()
{
return m_TrackedImageManager.descriptor.supportsMutableLibrary;
}
#endregion
}
#pragma warning restore CS0649
}
}