/// <summary> /// Called when tracking changes for a 'cached' anchor. /// When an anchor isn't located immediately we subscribe to this event so /// we can save the anchor when it is finally located or downloaded. /// </summary> /// <param name="anchor">The anchor that is reporting a tracking changed event.</param> /// <param name="located">Indicates if the anchor is located or not located.</param> private void Anchor_OnTrackingChanged(UnityEngine.XR.WSA.WorldAnchor anchor, bool located) { if (located && SaveAnchor(anchor)) { if (ShowDetailedLogs) { Debug.LogFormat("[WorldAnchorManager] Successfully updated cached anchor \"{0}\".", anchor.name); } if (AnchorDebugText != null) { AnchorDebugText.text += string.Format("\nSuccessfully updated cached anchor \"{0}\".", anchor.name); } } else { if (ShowDetailedLogs) { Debug.LogFormat("[WorldAnchorManager] Failed to locate cached anchor \"{0}\", attempting to acquire anchor again.", anchor.name); } if (AnchorDebugText != null) { AnchorDebugText.text += string.Format("\nFailed to locate cached anchor \"{0}\", attempting to acquire anchor again.", anchor.name); } GameObject anchoredObject; AnchorGameObjectReferenceList.TryGetValue(anchor.name, out anchoredObject); AnchorGameObjectReferenceList.Remove(anchor.name); AttachAnchor(anchoredObject, anchor.name); } anchor.OnTrackingChanged -= Anchor_OnTrackingChanged; }
/// <summary> /// Saves the anchor to the anchor store. /// </summary> /// <param name="anchor">Anchor.</param> private bool SaveAnchor(UnityEngine.XR.WSA.WorldAnchor anchor) { // Save the anchor to persist holograms across sessions. if (AnchorStore.Save(anchor.name, anchor)) { if (ShowDetailedLogs) { Debug.LogFormat("[WorldAnchorManager] Successfully saved anchor \"{0}\".", anchor.name); } if (AnchorDebugText != null) { AnchorDebugText.text += string.Format("\nSuccessfully saved anchor \"{0}\".", anchor.name); } ExportAnchor(anchor); return(true); } Debug.LogErrorFormat("[WorldAnchorManager] Failed to save anchor \"{0}\"!", anchor.name); if (AnchorDebugText != null) { AnchorDebugText.text += string.Format("\nFailed to save anchor \"{0}\"!", anchor.name); } return(false); }
public bool SetMarker(int marker) { int idx = marker - 1; if (marker > Markers.Count) { return(false); } //RaycastHit hitInfo; /*Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hitInfo)*/ if (RosGazeManager.Instance.Focused) { if (anchorStore == null) { return(false); } // Delete WorldAnchor if selected marker currently has one UnityEngine.XR.WSA.WorldAnchor anchor = Markers[idx].GetComponent <UnityEngine.XR.WSA.WorldAnchor>(); if (anchor != null) { DestroyImmediate(anchor); } anchorStore.Delete(Markers[idx].name); // Move marker to new position Markers[idx].transform.position = RosGazeManager.Instance.position; // Set new WorldAnchor UnityEngine.XR.WSA.WorldAnchor attachingAnchor = Markers[idx].AddComponent <UnityEngine.XR.WSA.WorldAnchor>(); if (attachingAnchor.isLocated) { bool saved = anchorStore.Save(Markers[idx].name, attachingAnchor); Debug.Log("Saved new WorldAnchor position for " + Markers[idx].name + ": " + saved); } else { attachingAnchor.OnTrackingChanged += AttachingAnchor_OnTrackingChanged; } // return success return(true); } return(false); }
void PerformManipulationStop() { UnityEngine.XR.WSA.WorldAnchor attachingAnchor = gameObject.AddComponent <UnityEngine.XR.WSA.WorldAnchor>(); if (attachingAnchor.isLocated) { Debug.Log("Saving persisted position immediately"); bool saved = anchorStore.Save(ObjectAnchorStoreName, attachingAnchor); Debug.Log("saved: " + saved); DestroyImmediate(attachingAnchor); } else { attachingAnchor.OnTrackingChanged += AttachingAnchor_OnTrackingChanged; } }
/// <summary> /// If we are supposed to create the anchor for export, this is the function to call. /// </summary> public void CreateAnchor() { objectToAnchor = SharedCollection.Instance.gameObject; UnityEngine.XR.WSA.Sharing.WorldAnchorTransferBatch watb = new UnityEngine.XR.WSA.Sharing.WorldAnchorTransferBatch(); UnityEngine.XR.WSA.WorldAnchor worldAnchor = objectToAnchor.GetComponent <UnityEngine.XR.WSA.WorldAnchor>(); if (worldAnchor == null) { worldAnchor = objectToAnchor.AddComponent <UnityEngine.XR.WSA.WorldAnchor>(); } exportingAnchorName = Guid.NewGuid().ToString(); Debug.Log("exporting " + exportingAnchorName); watb.AddWorldAnchor(exportingAnchorName, worldAnchor); UnityEngine.XR.WSA.Sharing.WorldAnchorTransferBatch.ExportAsync(watb, WriteBuffer, ExportComplete); }
public void CreateAnchor() { Debug.Log("looking for " + ObjectAnchorStoreName); string[] ids = anchorStore.GetAllIds(); for (int index = 0; index < ids.Length; index++) { Debug.Log(ids[index]); if (ids[index] == ObjectAnchorStoreName) { UnityEngine.XR.WSA.WorldAnchor wa = anchorStore.Load(ids[index], gameObject); Placing = false; break; } } }
/// <summary> /// Callback to trigger when an anchor has been 'found'. /// </summary> private void Anchor_OnTrackingChanged_InitialAnchor(UnityEngine.XR.WSA.WorldAnchor self, bool located) { if (located) { Debug.Log("Found anchor, ready to export"); currentState = ImportExportState.ReadyToExportInitialAnchor; } else { Debug.Log("Failed to locate local anchor (super bad!)"); currentState = ImportExportState.Failed; } self.OnTrackingChanged -= Anchor_OnTrackingChanged_InitialAnchor; }
/// <summary> /// Called when tracking changes for a 'cached' anchor. /// </summary> /// <param name="self"></param> /// <param name="located"></param> private void ImportExportAnchorManager_OnTrackingChanged_Attaching(UnityEngine.XR.WSA.WorldAnchor self, bool located) { Debug.Log("anchor " + located); if (located) { currentState = ImportExportState.Ready; } else { Debug.Log("Failed to find local anchor from cache."); MakeAnchorDataRequest(); } self.OnTrackingChanged -= ImportExportAnchorManager_OnTrackingChanged_Attaching; }
private void ImportExportAnchorManager_OnTrackingChanged_Attaching(UnityEngine.XR.WSA.WorldAnchor self, bool located) { if (located) { // TODO: Inform Anchor is LOCKED IsAnchorLocated = true; } else { IsAnchorLocated = false; Debug.LogWarning("Anchor Manager: Failed to find local anchor from cache."); DebugDisplay(string.Format("\nFailed to find local anchor from cache.")); MakeAnchorDataRequest(); } self.OnTrackingChanged -= ImportExportAnchorManager_OnTrackingChanged_Attaching; }
private void ClearAnchors() { if (store != null) { store.Clear(); } Debug.Log("Cleared WorldAnchorStore"); m_PointOfReference = null; if (StatusText.Instance) { StatusText.Instance.SetText("Anchors Cleared"); } }
/// <summary> /// Starts establishing a new anchor. /// </summary> private void CreateAnchorLocally() { UnityEngine.XR.WSA.WorldAnchor anchor = GetComponent <UnityEngine.XR.WSA.WorldAnchor>(); if (anchor == null) { anchor = gameObject.AddComponent <UnityEngine.XR.WSA.WorldAnchor>(); } if (anchor.isLocated) { currentState = ImportExportState.ReadyToExportInitialAnchor; } else { anchor.OnTrackingChanged += Anchor_OnTrackingChanged_InitialAnchor; } }
void AnchorStoreReady(UnityEngine.XR.WSA.Persistence.WorldAnchorStore store) { anchorStore = store; Placing = true; Debug.Log("looking for " + ObjectAnchorStoreName); string[] ids = anchorStore.GetAllIds(); for (int index = 0; index < ids.Length; index++) { Debug.Log(ids[index]); if (ids[index] == ObjectAnchorStoreName) { UnityEngine.XR.WSA.WorldAnchor wa = anchorStore.Load(ids[index], gameObject); Placing = false; break; } } }
void OnSelect() { if (anchorStore == null) { return; } if (Placing) { UnityEngine.XR.WSA.WorldAnchor attachingAnchor = gameObject.AddComponent <UnityEngine.XR.WSA.WorldAnchor>(); if (attachingAnchor.isLocated) { Debug.Log("Saving persisted position immediately"); bool saved = anchorStore.Save(ObjectAnchorStoreName, attachingAnchor); Debug.Log("saved: " + saved); } else { attachingAnchor.OnTrackingChanged += AttachingAnchor_OnTrackingChanged; } } else { UnityEngine.XR.WSA.WorldAnchor anchor = gameObject.GetComponent <UnityEngine.XR.WSA.WorldAnchor>(); if (anchor != null) { DestroyImmediate(anchor); } string[] ids = anchorStore.GetAllIds(); for (int index = 0; index < ids.Length; index++) { Debug.Log(ids[index]); if (ids[index] == ObjectAnchorStoreName) { bool deleted = anchorStore.Delete(ids[index]); Debug.Log("deleted: " + deleted); break; } } } Placing = !Placing; }
public void StopPlacing() { if (anchorStore == null) { return; } UnityEngine.XR.WSA.WorldAnchor attachingAnchor = gameObject.AddComponent <UnityEngine.XR.WSA.WorldAnchor>(); if (attachingAnchor.isLocated) { Debug.Log("Saving persisted position immediately"); bool saved = anchorStore.Save(ObjectAnchorStoreName, attachingAnchor); Debug.Log("saved: " + saved); } else { attachingAnchor.OnTrackingChanged += AttachingAnchor_OnTrackingChanged; } }
private void Anchor_OnTrackingChanged_InitialAnchor(UnityEngine.XR.WSA.WorldAnchor self, bool located) { if (located) { Debug.Log("\nLocal Anchor Located - Ready for Export"); DebugDisplay(string.Format("\nLocal Anchor Located - Ready for Export")); CurrentState = AnchorManagementState.ReadyToExportLocalAnchor; IsAnchorLocated = true; } else { Debug.LogError("Anchor Manager: Failed to locate local anchor!"); DebugDisplay(string.Format("\nAnchor Manager: Failed to locate local anchor!")); IsAnchorLocated = false; } self.OnTrackingChanged -= Anchor_OnTrackingChanged_InitialAnchor; }
/// <summary> /// Called when a remote anchor has been deserialized /// </summary> /// <param name="status"></param> /// <param name="wat"></param> private void ImportComplete(UnityEngine.XR.WSA.Sharing.SerializationCompletionReason status, UnityEngine.XR.WSA.Sharing.WorldAnchorTransferBatch wat) { if (status == UnityEngine.XR.WSA.Sharing.SerializationCompletionReason.Succeeded && wat.GetAllIds().Length > 0) { Debug.Log("Import complete"); string first = wat.GetAllIds()[0]; Debug.Log("Anchor name: " + first); UnityEngine.XR.WSA.WorldAnchor anchor = wat.LockObject(first, gameObject); anchorStore.Save(first, anchor); currentState = ImportExportState.Ready; } else { Debug.Log("Import fail"); currentState = ImportExportState.DataReady; } }
private void OnImportComplete(UnityEngine.XR.WSA.Sharing.SerializationCompletionReason completionReason, UnityEngine.XR.WSA.Sharing.WorldAnchorTransferBatch deserializedTransferBatch) { if (completionReason != UnityEngine.XR.WSA.Sharing.SerializationCompletionReason.Succeeded) { LogAsync("Failed to import: " + completionReason.ToString()); return; } string[] ids = deserializedTransferBatch.GetAllIds(); if (ids.Length > 0) { if (m_PointOfReference) { DestroyImmediate(m_PointOfReference); } m_PointOfReferenceCube.transform.position = Vector3.zero; m_PointOfReferenceCube.transform.localScale = Vector3.one * 0.1f; m_PointOfReference = deserializedTransferBatch.LockObject(ids[0], m_PointOfReferenceCube); if (StatusText.Instance) { StatusText.Instance.SetText("Anchor Created"); } Debug.Log("Anchor Created"); if (store != null) { store.Delete(PointOfReferenceID); } StartCoroutine(SaveAnchor()); if (StatusText.Instance) { StatusText.Instance.SetText("Anchor Saved to Store"); } Debug.Log("Anchor Saved to Store"); } }
private void CreateLocalAnchor() { CurrentState = AnchorManagementState.CreatingLocalAnchor; // Use existing Anchor or Create as needed UnityEngine.XR.WSA.WorldAnchor anchor = gameObject.EnsureComponent <UnityEngine.XR.WSA.WorldAnchor>(); IsLocalAnchor = true; IsAnchorConfigured = false; exportingAnchorBytes.Clear(); if (anchor.isLocated) { CurrentState = AnchorManagementState.ReadyToExportLocalAnchor; } else { anchor.OnTrackingChanged += Anchor_OnTrackingChanged_InitialAnchor; } }
private static extern bool Internal_AddToWorkQueue(IntPtr observer, SurfaceDataReadyDelegate onDataReady, int surfaceId, MeshFilter filter, WorldAnchor wa, MeshCollider mc, float trisPerCubicMeter, bool createColliderData);
private extern static bool INTERNAL_CALL_IsLocated_Internal(WorldAnchor self);
private static void InvokeSurfaceDataReadyEvent(SurfaceDataReadyDelegate onDataReady, int surfaceId, MeshFilter outputMesh, WorldAnchor outputAnchor, MeshCollider outputCollider, float trisPerCubicMeter, bool bakeCollider, bool outputWritten, float elapsedBakeTimeSeconds) { if (onDataReady != null) { SurfaceData data; data.id.handle = surfaceId; data.outputMesh = outputMesh; data.outputAnchor = outputAnchor; data.outputCollider = outputCollider; data.trianglesPerCubicMeter = trisPerCubicMeter; data.bakeCollider = bakeCollider; onDataReady(data, outputWritten, elapsedBakeTimeSeconds); } }
/// <summary> /// Executes the anchor operations from the localAnchorOperations queue. /// </summary> /// <param name="anchorAttachmentInfo">Parameters for attaching the anchor.</param> protected void DoAnchorOperation(AnchorAttachmentInfo anchorAttachmentInfo) { string anchorId = anchorAttachmentInfo.AnchorName; GameObject anchoredGameObject = anchorAttachmentInfo.AnchoredGameObject; switch (anchorAttachmentInfo.Operation) { case AnchorOperation.Save: if (anchoredGameObject == null) { Debug.LogError("[WorldAnchorManager] The GameObject referenced must have been destroyed before we got a chance to anchor it."); if (AnchorDebugText != null) { AnchorDebugText.text += "\nThe GameObject referenced must have been destroyed before we got a chance to anchor it."; } break; } if (string.IsNullOrEmpty(anchorId)) { anchorId = anchoredGameObject.name; } // Try to load a previously saved world anchor. UnityEngine.XR.WSA.WorldAnchor savedAnchor = AnchorStore.Load(anchorId, anchoredGameObject); if (savedAnchor == null) { // Check if we need to import the anchor. if (ImportAnchor(anchorId, anchoredGameObject) == false) { if (ShowDetailedLogs) { Debug.LogFormat("[WorldAnchorManager] Anchor could not be loaded for \"{0}\". Creating a new anchor.", anchoredGameObject.name); } if (AnchorDebugText != null) { AnchorDebugText.text += string.Format("\nAnchor could not be loaded for \"{0}\". Creating a new anchor.", anchoredGameObject.name); } // Create anchor since one does not exist. CreateAnchor(anchoredGameObject, anchorId); } } else { savedAnchor.name = anchorId; if (ShowDetailedLogs) { Debug.LogFormat("[WorldAnchorManager] Anchor loaded from anchor store and updated for \"{0}\".", anchoredGameObject.name); } if (AnchorDebugText != null) { AnchorDebugText.text += string.Format("\nAnchor loaded from anchor store and updated for \"{0}\".", anchoredGameObject.name); } } AnchorGameObjectReferenceList.Add(anchorId, anchoredGameObject); break; case AnchorOperation.Delete: if (AnchorStore == null) { Debug.LogError("[WorldAnchorManager] Remove anchor called before anchor store is ready."); break; } // If we don't have a GameObject reference, let's try to get the GameObject reference from our dictionary. if (!string.IsNullOrEmpty(anchorId) && anchoredGameObject == null) { AnchorGameObjectReferenceList.TryGetValue(anchorId, out anchoredGameObject); } if (anchoredGameObject != null) { var anchor = anchoredGameObject.GetComponent <UnityEngine.XR.WSA.WorldAnchor>(); if (anchor != null) { anchorId = anchor.name; DestroyImmediate(anchor); } else { Debug.LogErrorFormat("[WorldAnchorManager] Unable remove WorldAnchor from {0}!", anchoredGameObject.name); if (AnchorDebugText != null) { AnchorDebugText.text += string.Format("\nUnable remove WorldAnchor from {0}!", anchoredGameObject.name); } } } else { Debug.LogError("[WorldAnchorManager] Unable find a GameObject to remove an anchor from!"); if (AnchorDebugText != null) { AnchorDebugText.text += "\nUnable find a GameObject to remove an anchor from!"; } } if (!string.IsNullOrEmpty(anchorId)) { AnchorGameObjectReferenceList.Remove(anchorId); DeleteAnchor(anchorId); } else { Debug.LogError("[WorldAnchorManager] Unable find an anchor to delete!"); if (AnchorDebugText != null) { AnchorDebugText.text += "\nUnable find an anchor to delete!"; } } break; default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// Called after creating a new anchor. /// </summary> /// <param name="anchor">The anchor to export.</param> /// <returns>Success.</returns> protected virtual void ExportAnchor(UnityEngine.XR.WSA.WorldAnchor anchor) { }