private async void OnParticipantConnected(SpatialCoordinateSystemParticipant participant)
        {
            DebugLog($"Waiting for the set of supported localizers from connected participant {participant.SocketEndpoint.Address}");

            // When a remote participant connects, get the set of ISpatialLocalizers that peer
            // supports. This is asynchronous, as it comes across the network.
            ISet <Guid> peerSupportedLocalizers = await participant.GetPeerSupportedLocalizersAsync();

            // If there are any supported localizers, find the first configured localizer in the
            // list that supports that type. If and when one is found, use it to perform localization.
            if (peerSupportedLocalizers != null)
            {
                DebugLog($"Received a set of {peerSupportedLocalizers.Count} supported localizers");

                if (SpatialLocalizationInitializationSettings.IsInitialized &&
                    TryRunLocalization(SpatialLocalizationInitializationSettings.Instance.PrioritizedInitializers, peerSupportedLocalizers, participant))
                {
                    // Succeeded at using a custom localizer specified by the app.
                    return;
                }

                if (TryRunLocalization(defaultSpatialLocalizationInitializers, peerSupportedLocalizers, participant))
                {
                    // Succeeded at using one of the default localizers from the prefab.
                    return;
                }

                Debug.LogWarning($"None of the configured LocalizationInitializers were supported by the connected participant, localization will not be started");
            }
            else
            {
                Debug.LogWarning($"No supported localizers were received from the participant, localization will not be started");
            }
        }
        /// <summary>
        /// Runs a localization session on the specific <see cref="SpatialCoordinateSystemParticipant"/>'s connected peer, followed
        /// by creating a persisted WorldAnchor-based <see cref="Microsoft.MixedReality.SpatialAlignment.ISpatialCoordinate"/> based on the located coordinate.
        /// </summary>
        /// <param name="participant">The participant to use to initiate the remote localization sessions.</param>
        /// <param name="spatialLocalizerId">The ID of the <see cref="Microsoft.MixedReality.SpectatorView.ISpatialLocalizer"/> to use
        /// for discovering a spatial coordinate.</param>
        /// <param name="settings">The settings to pass to the remote localizer.</param>
        public async void RunRemoteLocalizationWithWorldAnchorPersistence(SpatialCoordinateSystemParticipant participant, Guid spatialLocalizerId, ISpatialLocalizationSettings settings)
        {
            // If the initial request to restore a coordinate from a WorldAnchor hasn't completed, wait for that to complete first.
            if (participantLocalizationTasks.TryGetValue(participant, out Task <bool> currentTask))
            {
                await currentTask;
            }

            // Request localization using the specific localizer and settings, and wait for that localization to complete.
            participantLocalizationTasks[participant] = currentTask = SpatialCoordinateSystemManager.Instance.RunRemoteLocalizationAsync(participant.NetworkConnection, spatialLocalizerId, settings);
            bool localizationSucceeded = await currentTask;

            if (localizationSucceeded)
            {
                var supportedLocalizers = await participant.GetPeerSupportedLocalizersAsync();

                if (supportedLocalizers.Contains(WorldAnchorSpatialLocalizer.Id))
                {
                    // Once the specific localizer has found a shared coordinate, ask the WorldAnchorSpatialLocalizer
                    // to create a WorldAnchor-based coordinate at the same location, and persist that coordinate across sessions.
                    participantLocalizationTasks[participant] = currentTask = SpatialCoordinateSystemManager.Instance.RunRemoteLocalizationAsync(participant.NetworkConnection, WorldAnchorSpatialLocalizer.Id, new WorldAnchorSpatialLocalizationSettings
                    {
                        Mode           = WorldAnchorLocalizationMode.CreateAnchorAtWorldTransform,
                        AnchorId       = CompositorWorldAnchorId,
                        AnchorPosition = participant.PeerSpatialCoordinateWorldPosition,
                        AnchorRotation = participant.PeerSpatialCoordinateWorldRotation
                    });
                    await currentTask;
                }
            }
            else
            {
                Debug.LogError($"Remote localization failed on device {participant.NetworkConnection} for spatial localizer {spatialLocalizerId}");
            }
        }
        private async Task <bool> ConditionallyRunWorldAnchorLocalizationAsync(SpatialCoordinateSystemParticipant participant)
        {
            var supportedLocalizers = await participant.GetPeerSupportedLocalizersAsync();

            if (!supportedLocalizers.Contains(WorldAnchorSpatialLocalizer.Id))
            {
                return(false);
            }

            return(await SpatialCoordinateSystemManager.Instance.RunRemoteLocalizationAsync(participant.NetworkConnection, WorldAnchorSpatialLocalizer.Id, new WorldAnchorSpatialLocalizationSettings
            {
                Mode = WorldAnchorLocalizationMode.LocateExistingAnchor,
                AnchorId = CompositorWorldAnchorId
            }));
        }
Example #4
0
        private async Task <bool> TryRunLocalizationForParticipantAsync(SpatialCoordinateSystemParticipant participant)
        {
            DebugLog($"Waiting for the set of supported localizers from connected participant {participant.SocketEndpoint.Address}");

            if (!peerSupportedLocalizers.ContainsKey(participant))
            {
                // When a remote participant connects, get the set of ISpatialLocalizers that peer
                // supports. This is asynchronous, as it comes across the network.
                peerSupportedLocalizers[participant] = await participant.GetPeerSupportedLocalizersAsync();
            }

            // If there are any supported localizers, find the first configured localizer in the
            // list that supports that type. If and when one is found, use it to perform localization.
            if (peerSupportedLocalizers.TryGetValue(participant, out var supportedLocalizers) &&
                supportedLocalizers != null)
            {
                DebugLog($"Received a set of {peerSupportedLocalizers.Count} supported localizers");

                var initializers = new List <SpatialLocalizationInitializer>();
                if (SpatialLocalizationInitializationSettings.IsInitialized &&
                    SpatialLocalizationInitializationSettings.Instance.PrioritizedInitializers != null)
                {
                    DebugLog($"Found prioritized spatial localization initializers list in spectator view settings");
                    foreach (var initializer in SpatialLocalizationInitializationSettings.Instance.PrioritizedInitializers)
                    {
                        initializers.Add(initializer);
                    }
                }

                if (defaultSpatialLocalizationInitializers != null)
                {
                    DebugLog($"Found default spatial localization initializers list for spectator view settings");
                    foreach (var initializer in defaultSpatialLocalizationInitializers)
                    {
                        initializers.Add(initializer);
                    }
                }

                return(await TryRunLocalizationWithInitializerAsync(initializers, supportedLocalizers, participant));
            }
            else
            {
                Debug.LogWarning($"No supported localizers were received from the participant, localization will not be started");
            }

            return(false);
        }
        private async Task <bool> TryRunLocalizationForParticipantAsync(SpatialCoordinateSystemParticipant participant)
        {
            DebugLog($"Waiting for the set of supported localizers from connected participant {participant.SocketEndpoint.Address}");

            if (!peerSupportedLocalizers.ContainsKey(participant))
            {
                // When a remote participant connects, get the set of ISpatialLocalizers that peer
                // supports. This is asynchronous, as it comes across the network.
                peerSupportedLocalizers[participant] = await participant.GetPeerSupportedLocalizersAsync();
            }

            // If there are any supported localizers, find the first configured localizer in the
            // list that supports that type. If and when one is found, use it to perform localization.
            if (peerSupportedLocalizers.TryGetValue(participant, out var supportedLocalizers) &&
                supportedLocalizers != null)
            {
                DebugLog($"Received a set of {peerSupportedLocalizers.Count} supported localizers");
                DebugLog($"Using prioritized initializers list from spectator view settings: {SpatialLocalizationInitializationSettings.IsInitialized && SpatialLocalizationInitializationSettings.Instance.PrioritizedInitializers != null}");

                if (SpatialLocalizationInitializationSettings.IsInitialized &&
                    await TryRunLocalizationWithInitializerAsync(SpatialLocalizationInitializationSettings.Instance.PrioritizedInitializers, supportedLocalizers, participant))
                {
                    // Succeeded at using a custom localizer specified by the app.
                    return(true);
                }

                if (await TryRunLocalizationWithInitializerAsync(defaultSpatialLocalizationInitializers, supportedLocalizers, participant))
                {
                    // Succeeded at using one of the default localizers from the prefab.
                    return(true);
                }

                Debug.LogWarning($"None of the configured LocalizationInitializers were supported by the connected participant, localization will not be started");
            }
            else
            {
                Debug.LogWarning($"No supported localizers were received from the participant, localization will not be started");
            }

            return(false);
        }