private void Observer_OnSurfaceChanged(UnityEngine.XR.WSA.SurfaceId surfaceId, UnityEngine.XR.WSA.SurfaceChange changeType, Bounds bounds, System.DateTime updateTime) { GameObject surface; switch (changeType) { case UnityEngine.XR.WSA.SurfaceChange.Updated: case UnityEngine.XR.WSA.SurfaceChange.Added: if (!surfaces.TryGetValue(surfaceId.handle, out surface)) { // If we are adding a new surface, construct a GameObject // to represent its state and attach some Mesh-related // components to it. surface = new GameObject(string.Format("Surface-{0}", surfaceId)); surface.AddComponent <MeshFilter>(); surface.AddComponent <MeshRenderer>().sharedMaterial = DrawMaterial; surface.AddComponent <MeshCollider>(); surface.AddComponent <UnityEngine.XR.WSA.WorldAnchor>(); // Set the layer that this SpatialMapping surface is a part of surface.layer = PhysicsLayer; // Add the surface to our dictionary of known surfaces so // we can interact with it later. surfaces[surfaceId.handle] = surface; } UnityEngine.XR.WSA.SurfaceData smsd = new UnityEngine.XR.WSA.SurfaceData( surfaceId, surface.GetComponent <MeshFilter>(), surface.GetComponent <UnityEngine.XR.WSA.WorldAnchor>(), surface.GetComponent <MeshCollider>(), TrianglesPerCubicMeter, true); surfaceDataQueue.Enqueue(smsd); break; case UnityEngine.XR.WSA.SurfaceChange.Removed: if (surfaces.TryGetValue(surfaceId.handle, out surface)) { surfaces.Remove(surfaceId.handle); Destroy(surface); } break; } }
/// <summary> /// Handles the SurfaceObserver's OnSurfaceChanged event. /// </summary> /// <param name="id">The identifier assigned to the surface which has changed.</param> /// <param name="changeType">The type of change that occurred on the surface.</param> /// <param name="bounds">The bounds of the surface.</param> /// <param name="updateTime">The date and time at which the change occurred.</param> private void SurfaceObserver_OnSurfaceChanged(UnityEngine.XR.WSA.SurfaceId id, UnityEngine.XR.WSA.SurfaceChange changeType, Bounds bounds, System.DateTime updateTime) { // Verify that the client of the Surface Observer is expecting updates. if (ObserverState != ObserverStates.Running) { return; } GameObject surface; switch (changeType) { // Adding and updating are nearly identical. The only difference is if a new gameobject to contain // the surface needs to be created. case UnityEngine.XR.WSA.SurfaceChange.Added: case UnityEngine.XR.WSA.SurfaceChange.Updated: // Check to see if the surface is known to the observer. // If so, we want to add it for cleanup after we get new meshes // We do this because Unity doesn't properly cleanup baked collision data if (surfaces.TryGetValue(id.handle, out surface)) { pendingCleanup.Add(id.handle, surface); surfaces.Remove(id.handle); } // Get an available surface object ready to be used surface = GetSurfaceObject(id.handle, transform); surface.layer = LayerMask.NameToLayer("Wall"); surface.tag = "Wall"; // Add the surface to our dictionary of known surfaces so // we can interact with it later. surfaces.Add(id.handle, surface); // Add the request to create the mesh for this surface to our work queue. QueueSurfaceDataRequest(id, surface); break; case UnityEngine.XR.WSA.SurfaceChange.Removed: // Always process surface removal events. // This code can be made more thread safe if (surfaces.TryGetValue(id.handle, out surface)) { surfaces.Remove(id.handle); CleanupSurface(surface); RemoveSurfaceObject(surface, false); } break; } // Event if (SurfaceChanged != null) { SurfaceChanged(id, changeType, bounds, updateTime); } }
/// <summary> /// Handles the SurfaceObserver's OnSurfaceChanged event. /// </summary> /// <param name="id">The identifier assigned to the surface which has changed.</param> /// <param name="changeType">The type of change that occurred on the surface.</param> /// <param name="bounds">The bounds of the surface.</param> /// <param name="updateTime">The date and time at which the change occurred.</param> private void SurfaceObserver_OnSurfaceChanged(UnityEngine.XR.WSA.SurfaceId id, UnityEngine.XR.WSA.SurfaceChange changeType, Bounds bounds, DateTime updateTime) { // Verify that the client of the Surface Observer is expecting updates. if (ObserverState != ObserverStates.Running) { return; } switch (changeType) { case UnityEngine.XR.WSA.SurfaceChange.Added: case UnityEngine.XR.WSA.SurfaceChange.Updated: surfaceWorkQueue.Enqueue(id); break; case UnityEngine.XR.WSA.SurfaceChange.Removed: SurfaceObject?removedSurface = RemoveSurfaceIfFound(id.handle, destroyGameObject: false); if (removedSurface != null) { ReclaimSurface(removedSurface.Value); } break; default: Debug.LogErrorFormat("Unexpected {0} value: {1}.", changeType.GetType(), changeType); break; } }
/// <summary> /// Called when a surface is going to be added, removed, or updated. /// We only care about removal so we can remove our internal copy of the surface mesh. /// </summary> /// <param name="surfaceId">The surface ID that is being added/removed/updated</param> /// <param name="changeType">Added | Removed | Updated</param> /// <param name="bounds">The world volume the mesh is in.</param> /// <param name="updateTime">When the mesh was updated.</param> private void MappingObserver_SurfaceChanged(UnityEngine.XR.WSA.SurfaceId surfaceId, UnityEngine.XR.WSA.SurfaceChange changeType, Bounds bounds, DateTime updateTime) { // We only need to worry about removing meshes from our list. Adding and updating is // done when the mesh data is actually ready. if (changeType == UnityEngine.XR.WSA.SurfaceChange.Removed) { int meshIndex = FindMeshIndexInInputMeshList(surfaceId.handle); if (meshIndex >= 0) { inputMeshList.RemoveAt(meshIndex); } } }