Пример #1
0
        public async Task <bool> Register(StereoApplication app, SpatialCoordinateSystem coordinateSystem, System.Numerics.Vector3 extents, int trianglesPerCubicMeter = 1000, bool onlyAdd = false, bool convertToLeftHanded = true)
        {
            this.currentHoloApp          = app;
            this.trianglesPerCubicMeter  = trianglesPerCubicMeter;
            this.currentCoordinateSystem = coordinateSystem;
            ConvertToLeftHanded          = convertToLeftHanded;
            OnlyAdd = onlyAdd;

            var result = await SpatialSurfaceObserver.RequestAccessAsync();

            if (result != SpatialPerceptionAccessStatus.Allowed)
            {
                return(false);
            }

            observer = new SpatialSurfaceObserver();
            observer.SetBoundingVolume(SpatialBoundingVolume.FromBox(coordinateSystem, new SpatialBoundingBox {
                Extents = extents
            }));

            foreach (var item in observer.GetObservedSurfaces())
            {
                lock (UpdateCache)
                {
                    UpdateCache[item.Key] = item.Value.UpdateTime.ToUniversalTime();
                }
                ProcessSurface(item.Value);
            }
            observer.ObservedSurfacesChanged += Observer_ObservedSurfacesChanged;

            return(true);
        }
        void BuildSpatialSurfaceObserver()
        {
            _spatialSurfaceObserver = new SpatialSurfaceObserver();

            var positionFormat = DirectXPixelFormat.R32G32B32A32Float;
            var normalFormat   = DirectXPixelFormat.R32G32B32A32Float;

            _spatialSurfaceMeshOptions = new SpatialSurfaceMeshOptions
            {
                IncludeVertexNormals = true,
                VertexPositionFormat = positionFormat,
                VertexNormalFormat   = normalFormat,
                TriangleIndexFormat  = DirectXPixelFormat.R16UInt
            };

            var boundingBox = new SpatialBoundingBox
            {
                Center  = new Vector3(0f, 0f, 0f),
                Extents = new Vector3(10f, 10f, 10f)
            };
            var bounds = SpatialBoundingVolume.FromBox(_spatialCoordinateSystem, boundingBox);

            _spatialSurfaceObserver.SetBoundingVolume(bounds);

            _spatialSurfaceObserver.ObservedSurfacesChanged += SpatialSurfaceObserverOnObservedSurfacesChanged;
        }
Пример #3
0
        internal static void SurfacesChanged(SpatialSurfaceObserver sender, object args)
        {
            List <Guid> toRemove          = new List <Guid>();
            var         surfaceCollection = sender.GetObservedSurfaces();

            foreach (var pair in surfaces)
            {
                if (!surfaceCollection.ContainsKey(pair.Key))
                {
                    toRemove.Add(pair.Key);
                }
            }
            foreach (var id in toRemove)
            {
                surfaceRemoved(id);
                surfaces.Remove(id);
            }
            foreach (var pair in surfaceCollection)
            {
                if (surfaces.ContainsKey(pair.Key))
                {
                    surfaceModified(pair.Key, pair.Value);
                    surfaces[pair.Key] = pair.Value;
                }
                else
                {
                    surfaceAdded(pair.Key, pair.Value);
                    surfaces.Add(pair.Key, pair.Value);
                }
            }
        }
Пример #4
0
        /// <inheritdoc/>
        public override async void StartObserving()
        {
            if (IsRunning)
            {
                return;
            }

            if (spatialSurfaceObserver == null)
            {
                Debug.LogError($"Failed to start {Name}! {nameof(spatialSurfaceObserver)} is null!");
                return;
            }

            if (currentAccessStatus != SpatialPerceptionAccessStatus.Allowed)
            {
                currentAccessStatus = await SpatialSurfaceObserver.RequestAccessAsync();
            }

            if (currentAccessStatus != SpatialPerceptionAccessStatus.Allowed)
            {
                Debug.LogWarning($"Failed to start {Name}! Access is {currentAccessStatus}.");
                return;
            }

            base.StartObserving();

            // We want the first update immediately.
            lastUpdated = 0;
        }
Пример #5
0
        public async Task <bool> Register(HoloApplication app, SpatialCoordinateSystem coordinateSystem, System.Numerics.Vector3 extents, int trianglesPerCubicMeter = 1000)
        {
            this.currentHoloApp          = app;
            this.trianglesPerCubicMeter  = trianglesPerCubicMeter;
            this.currentCoordinateSystem = coordinateSystem;

            var result = await SpatialSurfaceObserver.RequestAccessAsync();

            if (result != SpatialPerceptionAccessStatus.Allowed)
            {
                return(false);
            }

            observer = new SpatialSurfaceObserver();
            observer.SetBoundingVolume(SpatialBoundingVolume.FromBox(coordinateSystem, new SpatialBoundingBox {
                Extents = extents
            }));
            foreach (var surface in observer.GetObservedSurfaces())
            {
                updateCache[surface.Key] = surface.Value.UpdateTime.UtcDateTime;
                ProcessSurface(surface.Value);
            }
            observer.ObservedSurfacesChanged += Observer_ObservedSurfacesChanged;

            return(true);
        }
Пример #6
0
 public void Stop()
 {
     if (observer != null)
     {
         observer.ObservedSurfacesChanged -= Observer_ObservedSurfacesChanged;
     }
     observer = null;
 }
        async void CheckAccess()
        {
            var res = await SpatialSurfaceObserver.RequestAccessAsync();

            if (res != SpatialPerceptionAccessStatus.Allowed)
            {
                throw new Exception("No access to spatial data.");
            }
        }
Пример #8
0
        /// <inheritdoc />
        public WindowsMixedRealitySpatialMeshObserver(string name, uint priority, WindowsMixedRealitySpatialMeshObserverProfile profile, IMixedRealitySpatialAwarenessSystem parentService)
            : base(name, priority, profile, parentService)
        {
#if WINDOWS_UWP
            if (profile.MeshLevelOfDetail == SpatialAwarenessMeshLevelOfDetail.Custom)
            {
                trianglesPerCubicMeter = profile.TrianglesPerCubicMeter;
            }

            if (SpatialSurfaceObserver.IsSupported())
            {
                spatialSurfaceObserver    = new SpatialSurfaceObserver();
                spatialSurfaceMeshOptions = new SpatialSurfaceMeshOptions {
                    IncludeVertexNormals = true
                };

                // TODO Determine which formats are the correct ones to use.
                var supportedVertexPositionFormats = SpatialSurfaceMeshOptions.SupportedVertexPositionFormats;
                var supportedVertexNormalFormats   = SpatialSurfaceMeshOptions.SupportedVertexNormalFormats;

                for (int i = 0; i < supportedVertexPositionFormats.Count; i++)
                {
                    if (supportedVertexPositionFormats[i] == Windows.Graphics.DirectX.DirectXPixelFormat.R16G16B16A16IntNormalized)
                    {
                        spatialSurfaceMeshOptions.VertexPositionFormat = Windows.Graphics.DirectX.DirectXPixelFormat.R16G16B16A16IntNormalized;
                        break;
                    }
                }

                for (int i = 0; i < supportedVertexNormalFormats.Count; i++)
                {
                    if (supportedVertexNormalFormats[i] == Windows.Graphics.DirectX.DirectXPixelFormat.R8G8B8A8IntNormalized)
                    {
                        spatialSurfaceMeshOptions.VertexNormalFormat = Windows.Graphics.DirectX.DirectXPixelFormat.R8G8B8A8IntNormalized;
                        break;
                    }
                }

                // If a very high detail setting with spatial mapping is used, it can be beneficial
                // to use a 32-bit unsigned integer format for indices instead of the default 16-bit.
                if (MeshLevelOfDetail == SpatialAwarenessMeshLevelOfDetail.High)
                {
                    var supportedTriangleIndexFormats = SpatialSurfaceMeshOptions.SupportedTriangleIndexFormats;

                    for (int i = 0; i < supportedTriangleIndexFormats.Count; i++)
                    {
                        if (supportedTriangleIndexFormats[i] == Windows.Graphics.DirectX.DirectXPixelFormat.R8G8B8A8IntNormalized)
                        {
                            spatialSurfaceMeshOptions.TriangleIndexFormat = Windows.Graphics.DirectX.DirectXPixelFormat.R32UInt;
                        }
                    }
                }
            }
        }
        async void SpatialSurfaceObserverOnObservedSurfacesChanged(SpatialSurfaceObserver sender, object args)
        {
            var observedSurfaces = _spatialSurfaceObserver.GetObservedSurfaces();

            foreach (var surfacePair in observedSurfaces)
            {
                var spatialSurfaceInfo = surfacePair.Value;
                await _surfaceMeshList.AddOrUpdateAsync(spatialSurfaceInfo, _spatialSurfaceMeshOptions);
            }

            var allIds = (from item in observedSurfaces
                          select item.Key).ToList();

            _surfaceMeshList.Prune(allIds);
            _loadingComplete = true;
        }
Пример #10
0
        /// <summary>
        /// Initialize all resources used by this instance.
        /// </summary>
        public async void Initialize()
        {
            // The surface mapping API reads information about the user's environment. The user must
            // grant permission to the app to use this capability of the Windows Holographic device.
            var status = await SpatialSurfaceObserver.RequestAccessAsync();

            if (status != SpatialPerceptionAccessStatus.Allowed)
            {
                return;
            }

            // If status is allowed, we can create the surface observer.
            this.surfaceObserver = new SpatialSurfaceObserver();
            this.surfaceObserver.ObservedSurfacesChanged += this.ObservedSurfacesChanged;
            this.RefreshBoundingVolume();
        }
Пример #11
0
        internal async void setUpSpatialMapping(SpatialCoordinateSystem coordinateSystem)
        {
            if (!Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 4) || SpatialSurfaceObserver.IsSupported())
            {
                SpatialPerceptionAccessStatus status = await SpatialSurfaceObserver.RequestAccessAsync();

                if (status == SpatialPerceptionAccessStatus.Allowed)
                {
                    SpatialSurfaceObserver observer    = new SpatialSurfaceObserver();
                    SpatialBoundingBox     boundingBox = new SpatialBoundingBox()
                    {
                        Center  = new System.Numerics.Vector3(0, 0, 0),
                        Extents = new System.Numerics.Vector3(40, 40, 5)
                    };
                    SpatialBoundingVolume bounds = SpatialBoundingVolume.FromBox(coordinateSystem, boundingBox);
                    observer.SetBoundingVolume(bounds);
                    observer.ObservedSurfacesChanged += new TypedEventHandler <SpatialSurfaceObserver, object>(ClockworkSocket.SurfacesChanged);
                }
            }
        }
Пример #12
0
        public async void Initialize(SpatialCoordinateSystem coordinateSystem)
        {
            CoordinateSystem = coordinateSystem;
            var requestStatus = await SpatialSurfaceObserver.RequestAccessAsync();

            if (requestStatus == SpatialPerceptionAccessStatus.Allowed)
            {
                SurfaceObserver = new SpatialSurfaceObserver();
                var boundingBox = new SpatialBoundingBox()
                {
                    Center  = Vector3.Zero,
                    Extents = new Vector3(10.0f, 10.0f, 2.5f)
                };
                SurfaceObserver.SetBoundingVolume(SpatialBoundingVolume.FromBox(coordinateSystem, boundingBox));
                await CreateDeviceDenpendantResources();

                SurfaceObserver.ObservedSurfacesChanged += OnObservedSurfacesChanged;
                Active = true;
            }
        }
Пример #13
0
        private bool InitializeUWP()
        {
            // Ask or check for Spatial permissions
            SpatialSurfaceObserver.RequestAccessAsync().Completed = (i, s) => {
                if (s == AsyncStatus.Completed && i.GetResults() == SpatialPerceptionAccessStatus.Allowed)
                {
                    observer = new SpatialSurfaceObserver();
                    observer.ObservedSurfacesChanged += OnSurfaceUpdate;
                    UpdateBounds(center, radius);
                }
            };

            // Establish the coordinate frame of reference
            var locator = SpatialLocator.GetDefault();

            if (locator != null)
            {
                locator.LocatabilityChanged += OnLocated;
                OnLocated(locator, null);
            }
            return(true);
        }
Пример #14
0
        private async Task InitializeSurfaceObservation()
        {
            var requestStatus = await SpatialSurfaceObserver.RequestAccessAsync();

            if (requestStatus == SpatialPerceptionAccessStatus.Allowed)
            {
                SurfaceObserver = new SpatialSurfaceObserver();
                var boundingBox = new SpatialBoundingBox
                {
                    Center  = Vector3.Zero,
                    Extents = new Vector3(10.0f, 10.0f, 10.0f)
                };
                SurfaceObserver.SetBoundingVolume(SpatialBoundingVolume.FromBox(CoordinateSystem, boundingBox));
                SurfaceObserver.ObservedSurfacesChanged += (sender, _) =>
                {
                    if (!GeometryPaused)
                    {
                        Meshes.ProcessSurfaces(sender.GetObservedSurfaces());
                    }
                };
            }
            MeshTexturer.InitializeTextures();
        }
Пример #15
0
        void Observer_ObservedSurfacesChanged(SpatialSurfaceObserver sender, object args)
        {
            var surfaces = sender.GetObservedSurfaces();

            Application.InvokeOnMain(() => currentHoloApp.HandleActiveSurfacesChanged(new HashSet <string>(surfaces.Keys.Select(k => k.ToString()))));
            foreach (var surface in surfaces.Values)
            {
                lock (updateCache)
                {
                    DateTime updateTime;
                    if (updateCache.TryGetValue(surface.Id, out updateTime) && updateTime >= surface.UpdateTime.UtcDateTime)
                    {
                        return;
                    }
                    updateCache[surface.Id] = surface.UpdateTime.UtcDateTime;
                }
                if (observer == null)
                {
                    return;
                }
                ProcessSurface(surface);
            }
        }
Пример #16
0
        async void Observer_ObservedSurfacesChanged(SpatialSurfaceObserver sender, object args)
        {
            foreach (var item in sender.GetObservedSurfaces())
            {
                lock (UpdateCache)
                {
                    DateTimeOffset updateTime;
                    var            time = item.Value.UpdateTime.ToUniversalTime();
                    if (UpdateCache.TryGetValue(item.Key, out updateTime) && (updateTime >= time || OnlyAdd))
                    {
                        continue;
                    }

                    UpdateCache[item.Key] = time;
                }

                if (observer == null)
                {
                    return;
                }

                await ProcessSurface(item.Value).ConfigureAwait(false);
            }
        }
Пример #17
0
        private void OnSurfaceUpdate(SpatialSurfaceObserver observer, object args)
        {
            var surfaceDict = observer.GetObservedSurfaces();

            foreach (var surface in surfaceDict)
            {
                // Find or add a surface mesh for this surface
                SurfaceMesh mesh = meshes.Find(m => m.id == surface.Key);
                if (mesh == null)
                {
                    mesh = new SurfaceMesh {
                        mesh       = new Mesh(),
                        id         = surface.Key,
                        lastUpdate = new DateTimeOffset()
                    };
                    meshes.Add(mesh);
                }

                // Ask for an update to the mesh data, if it's old
                if (surface.Value.UpdateTime > mesh.lastUpdate)
                {
                    mesh.lastUpdate = surface.Value.UpdateTime;
                    SpatialSurfaceMeshOptions opts = new SpatialSurfaceMeshOptions();
                    opts.IncludeVertexNormals = false;
                    opts.TriangleIndexFormat  = Windows.Graphics.DirectX.DirectXPixelFormat.R32UInt;
                    opts.VertexPositionFormat = Windows.Graphics.DirectX.DirectXPixelFormat.R32G32B32A32Float;
                    surface.Value.TryComputeLatestMeshAsync(trisPerMeter, opts).Completed = (info, state) => {
                        // Send update to the main thread for upload to GPU
                        if (state == Windows.Foundation.AsyncStatus.Completed)
                        {
                            queuedUpdates.Add(info.GetResults());
                        }
                    };
                }
            }
        }
Пример #18
0
        async private void GalleryGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            VideoItem selectedVideo = (VideoItem)e.ClickedItem;

            if (selectedVideo != null)
            {
                if ((HolographicSpace.IsSupported && HolographicSpace.IsAvailable) ||
                    SpatialSurfaceObserver.IsSupported())
                {
                    var appViewSource = new AppViewSource(selectedVideo.SourceUri);
                    var appView       = CoreApplication.CreateNewView(appViewSource);
                    await appView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                    {
                        int appviewId  = ApplicationView.GetForCurrentView().Id;
                        bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(appviewId, ViewSizePreference.Default, mainviewId, ViewSizePreference.Default);
                        System.Diagnostics.Debug.Assert(viewShown);
                    });
                }
                else
                {
                    Frame.Navigate(typeof(PlaybackPage), selectedVideo.SourceUri);
                }
            }
        }
Пример #19
0
 private void ObservedSurfacesChanged(SpatialSurfaceObserver sender, object args)
 {
     this.pendingChanges = true;
 }
Пример #20
0
 /// <summary>
 /// Dispose all unmanaged resources
 /// </summary>
 public void Dispose()
 {
     this.surfaceObserver.ObservedSurfacesChanged -= this.ObservedSurfacesChanged;
     this.surfaceObserver = null;
 }
Пример #21
0
        private void OnObservedSurfacesChanged(SpatialSurfaceObserver sender, object args)
        {
            var observedSurfaces = sender.GetObservedSurfaces();

            Meshes.ProcessSurfaces(observedSurfaces);
        }