Exemplo n.º 1
0
    public void LocalizeAnchor()
    {
        DispatcherQueue.Enqueue(() =>
        {
            if (_cloudSpatialAnchorSession == null)
            {
                Log("CloudSpatialAnchorSession was null. Weird.", Color.red);
                return;
            }
            else
            {
                // Initialize session fresh & clean
                CleanupObjects();
                _cloudSpatialAnchorSession.Stop();
                _cloudSpatialAnchorSession.Dispose();
                _cloudSpatialAnchorSession = null;
                InitializeSession();

                // Create a Watcher with anchor ID to locate the anchor that was created before
                AnchorLocateCriteria criteria = new AnchorLocateCriteria
                {
                    Identifiers = new string[] { _currentCloudAnchorId }
                };
                _cloudSpatialAnchorSession.CreateWatcher(criteria);

                Log($"Localizing anchor with {_cloudSpatialAnchorSession.GetActiveWatchers().Count} watchers.\r\nLook around to gather spatial data...");
            }
        });
    }
        public CloudSpatialAnchorWatcher StartLocating(AnchorLocateCriteria criteria)
        {
            // Only 1 active watcher at a time is permitted.
            StopLocating();

            return(spatialAnchorsSession.CreateWatcher(criteria));
        }
Exemplo n.º 3
0
 public CloudSpatialAnchorWatcher CreateWatcher()
 {
     if (SessionValid())
     {
         return(cloudSpatialAnchorSession.CreateWatcher(anchorLocateCriteria));
     }
     else
     {
         return(null);
     }
 }
Exemplo n.º 4
0
    public void LoadAzureSpatialAnchors()
    {
        // CloudSpatialAnchorセッションを初期化するための処理を実行
        ASAInitializeSession();
        Debug.Log("ASA Info: 保存したアンカーを検索します。");
        // 保存したAnchorを検索するWatcherを生成する
        AnchorLocateCriteria criteria = new AnchorLocateCriteria();

        criteria.Identifiers = new string[] { identifierEntity.Identifier };
        cloudSpatialAnchorSession.CreateWatcher(criteria);
        Debug.Log("ASA Info: Watcherを生成しました。 ActiveなWatcherの数: " + cloudSpatialAnchorSession.GetActiveWatchers().Count);
    }
        private async void LoadAnchorAsync()
        {
            AnchorLocateCriteria criteria = new AnchorLocateCriteria();
            string anchorId = await storageService.GetAnchorId();

            // Local anchor is the same as the last saved one. No need to reload it.
            if (currentlyLoadingAnchorId == anchorId || loadedAnchor != null && loadedAnchor.Identifier == anchorId)
            {
                return;
            }

            Debug.Log(DEBUG_FILTER + "we are going to load anchor with id: " + anchorId);
            currentlyLoadingAnchorId = anchorId;

            // Stop all other watchers.
            foreach (var w in cloudSession.GetActiveWatchers())
            {
                w.Stop();
            }

            criteria.Identifiers = new string[] { anchorId };
            cloudSession.CreateWatcher(criteria);

            cloudSession.AnchorLocated += (object sender, AnchorLocatedEventArgs args) =>
            {
                switch (args.Status)
                {
                case LocateAnchorStatus.Located:
                    loadedAnchor = args.Anchor;
                    // Run on UI thread.
                    QueueOnUpdate(() => SpawnOrMoveCurrentAnchoredObject(loadedAnchor));
                    break;

                case LocateAnchorStatus.AlreadyTracked:
                    Debug.Log(DEBUG_FILTER + "Anchor already tracked. Identifier: " + args.Identifier);
                    loadedAnchor = args.Anchor;
                    break;

                case LocateAnchorStatus.NotLocatedAnchorDoesNotExist:
                    Debug.Log(DEBUG_FILTER + "Anchor not located. Identifier: " + args.Identifier);
                    break;

                case LocateAnchorStatus.NotLocated:
                    Debug.LogError("ASA Error: Anchor not located does not exist. Identifier: " + args.Identifier);
                    break;
                }
            };
        }
Exemplo n.º 6
0
    /// <summary>
    /// Called by GestureRecognizer when a tap is detected.
    /// </summary>
    /// <param name="tapEvent">The tap.</param>
    public void HandleTap(TappedEventArgs tapEvent)
    {
        if (tapExecuted)
        {
            return;
        }
        tapExecuted = true;

        // We have saved an anchor, so we will now look for it.
        if (!String.IsNullOrEmpty(cloudSpatialAnchorId))
        {
            Debug.Log("ASA Info: We will look for a placed anchor.");
            tapExecuted = true;

            ResetSession(() =>
            {
                InitializeSession();

                // Create a Watcher to look for the anchor we created.
                AnchorLocateCriteria criteria = new AnchorLocateCriteria();
                criteria.Identifiers          = new string[] { cloudSpatialAnchorId };
                cloudSpatialAnchorSession.CreateWatcher(criteria);

                Debug.Log("ASA Info: Watcher created. Number of active watchers: " + cloudSpatialAnchorSession.GetActiveWatchers().Count);
            });
            return;
        }

        Debug.Log("ASA Info: We will create a new anchor.");

        // Clean up any anchors that have been placed.
        CleanupObjects();

        // Construct a Ray using forward direction of the HoloLens.
        Ray GazeRay = new Ray(tapEvent.headPose.position, tapEvent.headPose.forward);

        // Raycast to get the hit point in the real world.
        RaycastHit hitInfo;

        Physics.Raycast(GazeRay, out hitInfo, float.MaxValue);

        CreateAndSaveSphere(hitInfo.point);
    }
Exemplo n.º 7
0
    private void GoToNextLocation()
    {
        userInitiatesNextStep = true;

        AnchorIdsToLocate = new List <string>
        {
            chapterAnchors[step]
        };
        step += 1;

        anchorLocateCriteria = new AnchorLocateCriteria
        {
            BypassCache = true,
            Identifiers = AnchorIdsToLocate.ToArray(),
            Strategy    = LocateStrategy.AnyStrategy
        };
        cloudSpatialAnchorSession.CreateWatcher(anchorLocateCriteria);
        feedback.text = "Watching";
    }
Exemplo n.º 8
0
    /// <summary>
    /// Begins the anchor location procedure using the ASA Anchor Watchers
    /// </summary>
    /// <param name="anchorId">The ID of the anchor to locate.</param>
    public void LocateAnchor(string anchorId)
    {
        // Ignore if it is a fake anchor
        if (anchorId == FakeCloudSpatialAnchorId)
        {
            return;
        }

        if (AnchorWatcher != null)
        {
            Debug.LogError("ASA Info: A watcher is already running.");
            return;
        }

        AnchorLocateCriteria criteria = new AnchorLocateCriteria();

        criteria.Identifiers = new string[] { anchorId };
        AnchorWatcher        = CloudSpatialAnchorSession.CreateWatcher(criteria);
    }
    public void LocateAnchor()
    {
        // We have saved an anchor, so we will now look for it.
        if (!String.IsNullOrEmpty(cloudSpatialAnchorId))
        {
            // Create a Watcher to look for the anchor we created.
            AnchorLocateCriteria criteria = new AnchorLocateCriteria();
            criteria.Identifiers = new string[] { cloudSpatialAnchorId };
            cloudSpatialAnchorSession.CreateWatcher(criteria);

            Debug.Log("ASA Info: Watcher created. Number of active watchers: " + cloudSpatialAnchorSession.GetActiveWatchers().Count);

            cloudSpatialAnchorSession.AnchorLocated          += CloudSpatialAnchorSession_AnchorLocated;
            cloudSpatialAnchorSession.LocateAnchorsCompleted += CloudSpatialAnchorSession_LocateAnchorsCompleted;
        }

        //Debug.Log("ASA Info: We will create a new anchor.");

        //CreateAndSaveSphere();
    }
Exemplo n.º 10
0
    /// <summary>
    /// Searches for an Azure Spatial Anchor if one exists.
    /// </summary>
    public async void FindAzureSpatialAnchorAsync()
    {
#if UNITY_EDITOR
        if (IsSlateActive)
        {
            Instructions.text = "Azure Spatial Anchor sessions can't be started when running in the Unity Editor. Build and deploy this sample to a HoloLens device to be able to create and find Azure Spatial Anchors.\n\nLook at the slate and it will detect your gaze and update itself.";
        }
        else
        {
            Instructions.text = "Azure Spatial Anchor sessions can't be started when running in the Unity Editor. Build and deploy this sample to a HoloLens device to be able to create and find Azure Spatial Anchors.\n\nUse the ray coming from either hand to position the Litecoin currency symbol. Air tap to place the symbol. This will be the location where your mining stats will be displayed.\n\nWhen you're ready, turn either hand so that your palm is facing you to access the hand menu. Tap the \"Create Spatial Anchor\" button to save the Azure Spatial Anchor.";
        }

        // This asynchronous task just prevents a warning due to this method being asynchronous and otherwise lacking an await operator.
        await Task.Delay(300);

        return;
#else
        // An Azure Spatial Anchor exists.
        if (!string.IsNullOrEmpty(cloudSpatialAnchorId))
        {
            if (IsSlateActive)
            {
                Destroy(GameObject.FindGameObjectWithTag("Slate"));
                IsSlateActive = false;
            }
            else
            {
                Destroy(GameObject.FindGameObjectWithTag("Litecoin"));
            }

            Instructions.text = "An Azure Spatial Anchor has been previously created and saved. Look around to find the the anchor.";
            await Task.Delay(3000);

            StopSession(() =>
            {
                StartSession();

                // Create a Watcher to look for the Azure Spatial Anchor.
                AnchorLocateCriteria criteria = new AnchorLocateCriteria
                {
                    Identifiers = new string[] { cloudSpatialAnchorId }
                };

                cloudSpatialAnchorSession.CreateWatcher(criteria);
            });
        }
        // An Azure Spatial Anchor does not exisit.
        else
        {
            // It shouldn't be possible for the slate to be active on HoloLens without an Azure Spatial Anchor having been created.
            if (IsSlateActive)
            {
                Destroy(GameObject.FindGameObjectWithTag("Slate"));
                IsSlateActive = false;
                DisplayLitecoinSymbol();
            }
            else
            {
                Instructions.text = "Before you can search for an Azure Spatial Anchor, you must first create one.";
                await Task.Delay(3000);

                // The Litecoin symbol should already be in the scene, so we won't instantiate a new one.
                Instructions.text = "Use the ray coming from either hand to position the Litecoin currency symbol. Air tap to place the symbol. This will be the location where your mining stats will be displayed.\n\nWhen you're ready, turn either hand so that your palm is facing you to access the hand menu. Tap the \"Create Spatial Anchor\" button to save the Azure Spatial Anchor.";
            }
        }
#endif
    }
Exemplo n.º 11
0
        private async Task OnDiscoverCoordinatesImplAsync(CancellationToken cancellationToken, string[] idsToLocate = null)
        {
            await EnsureInitializedAsync().Unless(cancellationToken);

            RequestSessionStart();

            try
            {
                AnchorLocateCriteria anchorLocateCriteria = new AnchorLocateCriteria();

                HashSet <string> ids = new HashSet <string>();
                if (idsToLocate?.Length > 0)
                {
                    anchorLocateCriteria.Identifiers = idsToLocate;
                    for (int i = 0; i < idsToLocate.Length; i++)
                    {
                        if (!knownCoordinates.ContainsKey(idsToLocate[i]))
                        {
                            ids.Add(idsToLocate[i]);
                        }
                    }

                    if (ids.Count == 0)
                    {
                        // We know already all of the coordintes
                        return;
                    }
                }

                using (CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
                {
                    // Local handler
                    async void AnchorLocatedHandler(object sender, AnchorLocatedEventArgs args)
                    {
                        if (args.Status == LocateAnchorStatus.Located)
                        {
                            // Switch to UI thread for the rest here
                            await gameThreadSynchronizationContext;

                            GameObject gameObject = CreateGameObjectFrom(args);

                            SpatialAnchorsCoordinate coordinate = new SpatialAnchorsCoordinate(args.Anchor, gameObject);
                            OnNewCoordinate(coordinate.Id, coordinate);

                            lock (ids)
                            {
                                // If we succefully removed one and we are at 0, then stop.
                                // If we never had to locate any, we would always be at 0 but never remove any.
                                if (ids.Remove(args.Identifier) && ids.Count == 0)
                                {
                                    // We found all requested, stop
                                    cts.Cancel();
                                }
                            }
                        }
                    }

                    session.AnchorLocated += AnchorLocatedHandler;
                    CloudSpatialAnchorWatcher watcher = session.CreateWatcher(anchorLocateCriteria);
                    try
                    {
                        await cts.Token.AsTask().IgnoreCancellation();
                    }
                    finally
                    {
                        session.AnchorLocated -= AnchorLocatedHandler;
                        watcher.Stop();
                    }
                }
            }
            finally
            {
                ReleaseSessionStartRequest();
            }
        }