private void OnWorldAnchorTrackingChanged(UnityEngine.XR.WSA.WorldAnchor wa, bool tracked)
 {
     if (mHoloLensTrackingCallback != null)
     {
         // translate from world anchor to trackable behaviour
         foreach (KeyValuePair <TrackableIdPair, UnityEngine.XR.WSA.WorldAnchor> worldAnchor in mWorldAnchors)
         {
             if (worldAnchor.Value == wa)
             {
                 mHoloLensTrackingCallback(worldAnchor.Key, tracked);
             }
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Exports and uploads the anchor to the sharing service.
        /// </summary>
        /// <param name="anchor">The anchor to export.</param>
        /// <returns>Success</returns>
        protected override void ExportAnchor(UnityEngine.XR.WSA.WorldAnchor anchor)
        {
            if (SharingStage.Instance == null ||
                SharingStage.Instance.Manager == null ||
                SharingStage.Instance.CurrentRoom == null)
            {
                Debug.LogErrorFormat("[SharingWorldAnchorManager] Failed to export anchor \"{0}\"!  The sharing service was not ready.", anchor.name);
                if (AnchorDebugText != null)
                {
                    AnchorDebugText.text += string.Format("\nFailed to export anchor \"{0}\"!  The sharing service was not ready.", anchor.name);
                }

                return;
            }

            if (!shouldExportAnchors)
            {
                if (ShowDetailedLogs)
                {
                    Debug.LogWarningFormat("[SharingWorldAnchorManager] Attempting to export anchor \"{0}\".", anchor.name);
                }

                if (AnchorDebugText != null)
                {
                    AnchorDebugText.text += string.Format("\nAttempting to export anchor \"{0}\".", anchor.name);
                }

                if (currentAnchorTransferBatch == null)
                {
                    currentAnchorTransferBatch = new UnityEngine.XR.WSA.Sharing.WorldAnchorTransferBatch();
                    if (AnchorDebugText != null)
                    {
                        AnchorDebugText.text += "\nCreating a new World Anchor Transfer Batch...";
                    }
                }
                else
                {
                    Debug.LogWarning("[SharingWorldAnchorManager] We didn't properly cleanup our WorldAnchorTransferBatch!");
                    if (AnchorDebugText != null)
                    {
                        AnchorDebugText.text += "\nWe didn't properly cleanup our WorldAnchorTransferBatch!";
                    }
                }

                currentAnchorTransferBatch.AddWorldAnchor(anchor.name, anchor);
                shouldExportAnchors = true;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// When an anchor isn't located immediately we subscribe to this event so
        /// we can save the anchor when it is finally located.
        /// </summary>
        /// <param name="self">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 self, bool located)
        {
            if (located)
            {
                Debug.Log(gameObject.name + " : World anchor located successfully.");

                SaveAnchor(self);

                // Once the anchor is located we can unsubscribe from this event.
                self.OnTrackingChanged -= Anchor_OnTrackingChanged;
            }
            else
            {
                Debug.LogError(gameObject.name + " : World anchor failed to locate.");
            }
        }
Ejemplo n.º 4
0
        public void MakeNewAnchor()
        {
            if (PlayerPrefs.HasKey(SavedAnchorKey))
            {
                PlayerPrefs.DeleteKey(SavedAnchorKey);
            }

            UnityEngine.XR.WSA.WorldAnchor currentAnchor = objectToAnchor.GetComponent <UnityEngine.XR.WSA.WorldAnchor>();
            if (currentAnchor != null)
            {
                DestroyImmediate(currentAnchor);
            }

            AnchorName = "";
            CreateAnchor();
        }
        /// <summary>
        /// Creates an anchor, attaches it to the gameObjectToAnchor, and saves the anchor to the anchor store.
        /// </summary>
        /// <param name="gameObjectToAnchor">The GameObject to attach the anchor to.</param>
        /// <param name="anchorName">The name to give to the anchor.</param>
        private void CreateAnchor(GameObject gameObjectToAnchor, string anchorName)
        {
            UnityEngine.XR.WSA.WorldAnchor anchor = gameObjectToAnchor.AddComponent <UnityEngine.XR.WSA.WorldAnchor>();
            anchor.name = anchorName;

            // Sometimes the anchor is located immediately. In that case it can be saved immediately.
            if (anchor.isLocated)
            {
                SaveAnchor(anchor);
            }
            else
            {
                // Othertimes we must wait for the
                anchor.OnTrackingChanged += Anchor_OnTrackingChanged;
            }
        }
        /// <summary>
        /// Removes the anchor from the game object and deletes the anchor
        /// from the anchor store.
        /// </summary>
        /// <param name="gameObjectToUnanchor">gameObject to remove the anchor from.</param>
        public void RemoveAnchor(GameObject gameObjectToUnanchor)
        {
            // This case is unexpected, but just in case.
            if (AnchorStore == null)
            {
                Debug.LogError("remove anchor called before anchor store is ready.");
            }

            UnityEngine.XR.WSA.WorldAnchor anchor = gameObjectToUnanchor.GetComponent <UnityEngine.XR.WSA.WorldAnchor>();

            if (anchor != null)
            {
                AnchorStore.Delete(anchor.name);
                DestroyImmediate(anchor);
            }
        }
Ejemplo n.º 7
0
        // Use this for initialization
        IEnumerator Start()
        {
            //mCol = GetComponent<Collider>();

            if (deferredParent == null)
            {
                deferredParent = transform;
            }

            while (!WorldAnchorManager.IsInitialized)
            {
                yield return(null);
            }
            WorldErrors.Print("World Anchor Manager Initialized");

            if (store == null)                  //store should be loaded once
            {
                WorldErrors.Print("Getting Store");
                if (!GettingStore)
                {
                    GettingStore = true;
                    UnityEngine.XR.WSA.Persistence.WorldAnchorStore.GetAsync(GotStore);
                }
                while (store == null)
                {
                    yield return(null);
                }

                WorldErrors.Print("Got Store");
            }

            UnityEngine.XR.WSA.WorldAnchor wa = store.Load(anchorName, gameObject);
            if (wa == null)                 //no anchor found
            {
                WorldErrors.Print("No Anchor, creating one");
                NoAnchor();
                WorldAnchorManager.Instance.AttachAnchor(deferredParent.gameObject, anchorName);
                //StartCoroutine(AnchorTest());
            }
            else
            {
                WorldErrors.Print("Loaded Anchor");
                LoadedAnchor();
            }

            yield return(null);
        }
Ejemplo n.º 8
0
        private void AttachingAnchor_OnTrackingChanged(UnityEngine.XR.WSA.WorldAnchor self, bool located)
        {
            if (located)
            {
                string meshName = self.gameObject.GetComponent <MeshFilter>().mesh.name;
                if (!anchorStore.Save(meshName, self))
                {
                    Debug.Log("" + meshName + ": Anchor save failed...");
                }
                else
                {
                    Debug.Log("" + meshName + ": Anchor SAVED...");
                }

                self.OnTrackingChanged -= AttachingAnchor_OnTrackingChanged;
            }
        }
        /// <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)
        {
            if (SharingStage.Instance.ShowDetailedLogs)
            {
                Debug.Log("anchor " + located.ToString());
            }

            if (located)
            {
                AnchorLoadComplete();
            }
            else
            {
                Debug.LogWarning("Failed to find local anchor from cache.");
                MakeAnchorDataRequest();
            }

            self.OnTrackingChanged -= ImportExportAnchorManager_OnTrackingChanged_Attaching;
        }
        /// <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)
            {
                if (SharingStage.Instance.ShowDetailedLogs)
                {
                    Debug.Log("Found anchor, ready to export");
                }

                currentState = ImportExportState.ReadyToExportInitialAnchor;
            }
            else
            {
                Debug.LogError("Failed to locate local anchor!");
                currentState = ImportExportState.Failed;
            }

            self.OnTrackingChanged -= Anchor_OnTrackingChanged_InitialAnchor;
        }
        public void CreateWorldAnchor(Vector3 position)
        {
            GameObject sourceObject = GalaxyExplorerManager.Instance.ViewLoaderScript.gameObject;

            sourceObject.transform.position = position;

            // rotate to face camera
            var lookDirection = Camera.main.transform.position - position;

            lookDirection.y = 0;
            var rotation = Quaternion.LookRotation(-lookDirection.normalized);

            sourceObject.transform.rotation = rotation;

            anchor = sourceObject.AddComponent <UnityEngine.XR.WSA.WorldAnchor>();
            if (anchor)
            {
                anchor.OnTrackingChanged += GalaxyWorldAnchor_OnTrackingChanged;
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// If we are supposed to create the anchor for export, this is the function to call.
        /// </summary>
        public void CreateAnchor()
        {
#if UNITY_EDITOR
            Debug.LogError("Anchors cannot be created from the Unity editor.");
#endif

            exportingAnchorBytes.Clear();
            GenericNetworkTransmitter.Instance.SetData(null);
            objectToAnchor = UNetAnchorManager.Instance.gameObject;

            UnityEngine.XR.WSA.WorldAnchor worldAnchor = objectToAnchor.GetComponent <UnityEngine.XR.WSA.WorldAnchor>();
            if (worldAnchor == null)
            {
                worldAnchor = objectToAnchor.AddComponent <UnityEngine.XR.WSA.WorldAnchor>();
            }

            Debug.Log("Checking for saved anchor: " + AnchorName);
            if (PlayerPrefs.HasKey(SavedAnchorKey) && AttachToCachedAnchor(PlayerPrefs.GetString(SavedAnchorKey)))
            {
                exportingAnchorName = PlayerPrefs.GetString(SavedAnchorKey);
                Debug.Log("found " + AnchorName + " again");
            }
            else if (PlayerPrefs.HasKey(AnchorName) && AttachToCachedAnchor(AnchorName))
            {
                exportingAnchorName = AnchorName;
                Debug.Log("_found " + AnchorName + " again");
            }
            else
            {
                exportingAnchorName = Guid.NewGuid().ToString();
            }

            UnityEngine.XR.WSA.Sharing.WorldAnchorTransferBatch watb = new UnityEngine.XR.WSA.Sharing.WorldAnchorTransferBatch();

            Debug.Log("exporting " + exportingAnchorName);
            watb.AddWorldAnchor(exportingAnchorName, worldAnchor);
            UnityEngine.XR.WSA.Sharing.WorldAnchorTransferBatch.ExportAsync(watb, WriteBuffer, ExportComplete);

            SaveAnchor(exportingAnchorName);
        }
 private void InternalDeleteWA(UnityEngine.XR.WSA.WorldAnchor wa)
 {
     // unregister for callbacks first
     wa.OnTrackingChanged -= OnWorldAnchorTrackingChanged;
     GameObject.DestroyImmediate(wa);
 }
 private void GalaxyWorldAnchor_OnTrackingChanged(UnityEngine.XR.WSA.WorldAnchor self, bool located)
 {
     // Debug.Log($"WorldAnchorHandler: tracking changed to {(located ? "located":"lost")}");
     GalaxyExplorerManager.Instance.TransitionManager.CurrentActiveScene?.SetActive(located);
 }
Ejemplo n.º 15
0
        IEnumerator DoCalibration(bool recalibrate = false)
        {
#if !UNITY_EDITOR
            while (!WorldAnchorManager.IsInitialized)
            {
                yield return(null);
            }

            //give it some time
            yield return(new WaitForSeconds(2f));

            //get anchor store
            if (store == null)
            {
                WorldAnchorStore.GetAsync(GotStore);
                while (store == null)
                {
                    //Debug.Log("Bla");
                    yield return(null);
                }
            }

            //check if we have stored anchorData for this App & WIFI
            anchorXZ       = store.Load(strXZ, goXZ.gameObject);
            anchorMinXMinZ = store.Load(strMinXMinZ, goMinXMinZ.gameObject);
            anchorCenter   = store.Load(strCenter, optitrackCenter.gameObject);
            if (anchorCenter != null)
            {
                //load follow-up directly...
                //if (followupScene != null ) {
                //	SceneManager.LoadScene(followupScene);
                //}
                //yield break;

                //for now always recalibrate until it works...

                /*
                 * if (!recalibrate) {
                 *      //calibration already exists, only continue if we've forced re-calibration
                 *      //store current data (the actual positions will change every time you boot)
                 *      StartCoroutine(StoreCalibrationData());
                 *      yield break;
                 * } */
            }
#endif

            if (anchorCenter != null)
            {
                WorldAnchorManager.Instance.RemoveAnchor(optitrackCenter.gameObject);
                yield return(new WaitForSeconds(.25f));

                optitrackCenter.transform.position = Vector3.zero;
                optitrackCenter.transform.rotation = Quaternion.identity;
            }

            //display message "please place exactly 2 rigidbodies at XZ, -X-Z (two corners of the room)"
            feedbackCanvas.enabled = true;
            feedbackText.text      = "Place rigidbodies called \"xz\" and \"minxminz\" at positive and negative corners of the mocap space (found: 0).";
            feedbackButton.enabled = true;

            List <RigidbodyDefinition> defList = null;

            done = false;
            bool canContinue = false;
            while (!canContinue)
            {
                //check if there are three rigidbodies, and they are in the desired quadrants
                if (OptiTrackOSCClient.GetAllRigidbodies(out defList))
                {
                    int activeCount = 0;
                    for (int i = 0; i < defList.Count; ++i)
                    {
                        if (defList[i].isActive)
                        {
                            activeCount++;
                        }
                    }

                    feedbackText.text = "Place rigidbodies called \"xz\" and \"minxminz\" at positive and negative corners of the mocap space (found: " + activeCount + ").";

                    if (activeCount >= 2)
                    {
                        bool XZ = false, minXMinZ = false;
                        foreach (RigidbodyDefinition def in defList)
                        {
                            if (def.position.x > 0 && def.position.z > 0 && def.name == "xz")
                            {
                                XZ     = true;
                                vXZOsc = def.position;
                            }
                            if (def.position.x < 0 && def.position.z < 0 && def.name == "minxminz")
                            {
                                minXMinZ     = true;
                                vMinXMinZOsc = def.position;
                            }
                        }
                        if (XZ && minXMinZ && done)
                        {
                            canContinue = true;
                            continue;
                        }
                    }
                }
                yield return(null);
            }

            //display message: "align these three anchors with their correct rigidbody as best you can (height & rotation don't need to be exact)"
            //activate button
            feedbackCanvas.enabled = true;
            feedbackText.text      = "Align the two virtual anchors with the real objects you just placed. Say \"Next\" when you are done.";
            feedbackButton.enabled = true;

            //activate anchors that user must place
            goXZ.gameObject.SetActive(true);
            goMinXMinZ.gameObject.SetActive(true);

            //re-create/activate their world anchors
            ReAnchor(ref anchorXZ, ref goXZ);
            ReAnchor(ref anchorMinXMinZ, ref goMinXMinZ);

            //make sure they are movable
            goXZ.movable       = true;
            goMinXMinZ.movable = true;

            done = false;
            while (!done)
            {
                readyToCalibrate = true;
                yield return(null);
            }

            readyToCalibrate = false;

            //make sure we can't move the anchors anymore...
            goXZ.movable       = false;
            goMinXMinZ.movable = false;

            StartCoroutine(StoreCalibrationData());

            feedbackCanvas.enabled = false;

            yield return(null);
        }
Ejemplo n.º 16
0
        private void GalaxyWorldAnchor_OnTrackingChanged(UnityEngine.XR.WSA.WorldAnchor self, bool located)
        {
            viewLoaderAnchorActivelyTracking = located;

            SetViewLoaderActive(located);
        }