forked from microsoft/MixedRealityCompanionKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnchorManager.cs
More file actions
435 lines (379 loc) · 14.1 KB
/
Copy pathAnchorManager.cs
File metadata and controls
435 lines (379 loc) · 14.1 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
using UnityEngine.XR.WSA;
using UnityEngine.XR.WSA.Sharing;
using UnityEngine.XR.WSA.Persistence;
#if WINDOWS_UWP
using Windows.Networking;
using Windows.Networking.Connectivity;
#endif
namespace SimpleSharing
{
public class AnchorManager : SimpleSharing.Singleton<AnchorManager>
{
/// <summary>
/// Keeps track of the name of the world anchor to use.
/// </summary>
string AnchorName = "";
const string SavedAnchorKey = "SavedAnchorName";
WorldAnchorStore anchorStore = null;
/// <summary>
/// Sometimes we'll see a really small anchor blob get generated.
/// These tend to not work, so we have a minimum trustable size.
/// </summary>
private const uint minTrustworthySerializedAnchorDataSize = 500000;
List<string> localIPs;
string localIPv4 = string.Empty;
#pragma warning disable 0414
bool? anchorOwner = null;
#pragma warning restore 0414
/// <summary>
/// List of bytes that represent the anchor data to export.
/// </summary>
private List<byte> exportingAnchorBytes = new List<byte>();
/// <summary>
/// The UNetNetworkTransmitter in the scene which can send an anchor to another device.
/// </summary>
private AnchorNetworkTransmitter networkTransmitter;
/// <summary>
/// The object to attach the anchor to when created or imported.
/// </summary>
public GameObject objectToAnchor;
/// <summary>
/// The anchorData to import.
/// </summary>
private byte[] anchorData = null;
/// <summary>
/// Tracks if we have updated data to import.
/// </summary>
#pragma warning disable 0414
private bool gotOne = false;
#pragma warning restore 0414
/// <summary>
/// Tracks if we have a shared anchor established
/// </summary>
public bool AnchorEstablished { get; set; }
/// <summary>
/// Tracks if an import is in flight.
/// </summary>
public bool ImportInProgress { get; private set; }
/// <summary>
/// Tracks if a download is in flight.
/// </summary>
public bool DownloadingAnchor { get; private set; }
private void Start()
{
#if WINDOWS_UWP
localIPs = new List<string>();
// Get the ip of the Hololens to see if this device should create and share the anchor.
IReadOnlyList<HostName> hosts = NetworkInformation.GetHostNames();
foreach (HostName aName in hosts)
{
if (aName.Type == HostNameType.Ipv4)
{
localIPs.Add(aName.ToString().Trim());
if (aName.ToString().Split('.').Length == 4)
{
localIPv4 = aName.ToString().Trim();
Debug.Log("Setting local IP to " + localIPv4);
}
}
}
WorldAnchorStore.GetAsync(AnchorStoreReady);
#endif
networkTransmitter = AnchorNetworkTransmitter.Instance;
networkTransmitter.dataReadyEvent += NetworkTransmitter_dataReadyEvent;
}
void AnchorStoreReady(WorldAnchorStore store)
{
anchorStore = store;
}
private void Update()
{
if (anchorStore == null)
{
return;
}
#if WINDOWS_UWP
// Check if we should create an anchor.
if (!anchorOwner.HasValue &&
AnchorNetworkTransmitter.Instance != null &&
(AnchorNetworkTransmitter.Instance.AnchorOwnerIP != string.Empty
|| AnchorNetworkTransmitter.Instance.ForceCreateAnchor)
)
{
if (localIPs.Contains(AnchorNetworkTransmitter.Instance.AnchorOwnerIP)
|| AnchorNetworkTransmitter.Instance.ForceCreateAnchor)
{
anchorOwner = true;
CreateAnchor();
}
else
{
anchorOwner = false;
WaitForAnchor();
}
}
if (gotOne)
{
Debug.Log("importing");
gotOne = false;
ImportInProgress = true;
WorldAnchorTransferBatch.ImportAsync(anchorData, ImportComplete);
}
#endif
}
public void ResetAnchor()
{
#if WINDOWS_UWP
if (anchorStore != null)
{
anchorStore.Clear();
}
if (anchorOwner.HasValue && anchorOwner.Value == true)
{
MakeNewAnchor();
}
#endif
}
/// <summary>
/// If we are supposed to create the anchor for export, this is the function to call.
/// </summary>
public void CreateAnchor()
{
if (PlayerPrefs.HasKey(SavedAnchorKey) && AttachToCachedAnchor(PlayerPrefs.GetString(SavedAnchorKey)))
{
AnchorName = PlayerPrefs.GetString(SavedAnchorKey);
Debug.Log("found " + AnchorName + " again");
}
else
{
Debug.Log("Could not find cached anchor.");
AnchorName = Guid.NewGuid().ToString();
}
exportingAnchorBytes.Clear();
AnchorNetworkTransmitter.Instance.SetData(null);
WorldAnchor worldAnchor = objectToAnchor.GetComponent<WorldAnchor>();
if (worldAnchor == null)
{
Debug.Log("Adding a new anchor.");
worldAnchor = objectToAnchor.AddComponent<WorldAnchor>();
}
if (!AnchorEstablished)
{
if (worldAnchor.isLocated)
{
Debug.Log("World anchor already located.");
Debug.Log("Saving Anchor.");
AnchorEstablished = true;
SaveAnchor(worldAnchor);
}
else
{
worldAnchor.OnTrackingChanged += Anchor_OnTrackingChanged;
Anchor_OnTrackingChanged(worldAnchor, worldAnchor.isLocated);
}
}
Debug.Log("exporting " + AnchorName);
WorldAnchorTransferBatch watb = new WorldAnchorTransferBatch();
watb.AddWorldAnchor(AnchorName, worldAnchor);
WorldAnchorTransferBatch.ExportAsync(watb, WriteBuffer, ExportComplete);
}
/// <summary>
/// If we don't have the anchor already, call this to download the anchor.
/// </summary>
public void WaitForAnchor()
{
DownloadingAnchor = networkTransmitter.RequestAndGetData();
if (!DownloadingAnchor)
{
Invoke("WaitForAnchor", 0.5f);
}
}
/// <summary>
/// Attempts to attach to an anchor by anchorName in the local store..
/// </summary>
/// <returns>True if it attached, false if it could not attach</returns>
private bool AttachToCachedAnchor(string CachedAnchorName)
{
if (string.IsNullOrEmpty(CachedAnchorName))
{
Debug.Log("Ignoring empty name");
return false;
}
#if UNITY_WSA
if (anchorStore == null)
{
Debug.LogWarning("Could not find anchor store.");
return false;
}
Debug.Log("Looking for " + CachedAnchorName);
string[] ids = anchorStore.GetAllIds();
for (int index = 0; index < ids.Length; index++)
{
if (ids[index] == CachedAnchorName)
{
Debug.Log("Using what we have");
WorldAnchor anchor = anchorStore.Load(ids[index], objectToAnchor);
if (anchor == null)
{
// We did not find a valid anchor.
return false;
}
AnchorEstablished = true;
return true;
}
else
{
Debug.Log(ids[index]);
}
}
#endif
// Didn't find the anchor.
return false;
}
/// <summary>
/// Called when anchor data is ready.
/// </summary>
/// <param name="data">The data blob to import.</param>
private void NetworkTransmitter_dataReadyEvent(byte[] data)
{
Debug.Log("Anchor data arrived.");
anchorData = data;
Debug.Log(data.Length);
DownloadingAnchor = false;
gotOne = true;
}
/// <summary>
/// Called when a remote anchor has been deserialized
/// </summary>
/// <param name="status">Tracks if the import worked</param>
/// <param name="wat">The WorldAnchorTransferBatch that has the anchor information.</param>
private void ImportComplete(SerializationCompletionReason status, WorldAnchorTransferBatch wat)
{
if (status == SerializationCompletionReason.Succeeded && wat.GetAllIds().Length > 0)
{
Debug.Log("Import complete");
string first = wat.GetAllIds()[0];
Debug.Log("Anchor name: " + first);
if (AttachToCachedAnchor(first))
{
Debug.Log("Imported and attached to an anchor that we already cached.");
return;
}
WorldAnchor existingAnchor = objectToAnchor.GetComponent<WorldAnchor>();
if (existingAnchor != null)
{
DestroyImmediate(existingAnchor);
}
WorldAnchor anchor = wat.LockObject(first, objectToAnchor);
anchor.OnTrackingChanged += Anchor_OnTrackingChanged;
Anchor_OnTrackingChanged(anchor, anchor.isLocated);
ImportInProgress = false;
}
else
{
// if we failed, we can simply try again.
gotOne = true;
Debug.Log("Import fail");
}
}
private void Anchor_OnTrackingChanged(WorldAnchor self, bool located)
{
#if UNITY_WSA
if (located)
{
Debug.Log("Saving Anchor.");
AnchorEstablished = true;
SaveAnchor(self);
self.OnTrackingChanged -= Anchor_OnTrackingChanged;
}
#endif
}
private void SaveAnchor(WorldAnchor wa)
{
anchorStore.Save(AnchorName, wa);
PlayerPrefs.SetString(SavedAnchorKey, AnchorName);
PlayerPrefs.Save();
}
/// <summary>
/// Called as anchor data becomes available to export
/// </summary>
/// <param name="data">The next chunk of data.</param>
private void WriteBuffer(byte[] data)
{
exportingAnchorBytes.AddRange(data);
}
/// <summary>
/// Called when serializing an anchor is complete.
/// </summary>
/// <param name="status">If the serialization succeeded.</param>
private void ExportComplete(SerializationCompletionReason status)
{
if (status == SerializationCompletionReason.Succeeded && exportingAnchorBytes.Count > minTrustworthySerializedAnchorDataSize)
{
anchorData = exportingAnchorBytes.ToArray();
AnchorNetworkTransmitter.Instance.SetData(anchorData);
Debug.Log("Anchor ready " + exportingAnchorBytes.Count);
AnchorNetworkTransmitter.Instance.ConfigureAsServer();
// Let server know our anchor is ready.
SharingManager.Instance.SendAnchorName(AnchorName, localIPv4);
InvokeRepeating("SendAnchorName", 5, 5);
AnchorEstablished = true;
}
else
{
Debug.Log("Create anchor failed " + status + " " + exportingAnchorBytes.Count);
exportingAnchorBytes.Clear();
DestroyImmediate(objectToAnchor.GetComponent<WorldAnchor>());
CreateAnchor();
}
}
public void AnchorFoundRemotely()
{
if (UnityEngine.XR.WSA.HolographicSettings.IsDisplayOpaque)
{
return;
}
#if UNITY_WSA
Debug.Log("Setting saved anchor to " + AnchorName);
SaveAnchor(objectToAnchor.GetComponent<WorldAnchor>());
#endif
}
public void MakeNewAnchor()
{
Debug.Log("Making a new anchor.");
if (PlayerPrefs.HasKey(SavedAnchorKey))
{
Debug.Log("Deleting existing anchor key.");
PlayerPrefs.DeleteKey(SavedAnchorKey);
}
WorldAnchor currentAnchor = objectToAnchor.GetComponent<WorldAnchor>();
if (currentAnchor != null)
{
DestroyImmediate(currentAnchor);
}
AnchorName = "";
CreateAnchor();
}
// Periodically tell the server our anchor name.
// Only sending the name once may cause our server to miss the name if the server was started after the anchor was created.
//NOTE: You may not need to do this if you are using UNET's HLAPI syncvars in your networking stack.
private void SendAnchorName()
{
if (AnchorEstablished)
{
SharingManager.Instance.SendAnchorName(AnchorName, localIPv4);
}
}
private void OnDestroy()
{
CancelInvoke("SendAnchorName");
}
}
}