/// <summary> /// Set the display option of the spatial mesh. /// </summary> /// <param name="meshDisplayOption">visible, occlusion, none</param> public void SetMeshDisplayOption(SpatialAwarenessMeshDisplayOptions meshDisplayOption) { // Cast the spatial awareness system to IMixedRealityDataProviderAccess to get an observer var access = CoreServices.SpatialAwarenessSystem as IMixedRealityDataProviderAccess; // Get the mesh observer specified by its name var observer = access.GetDataProvider <IMixedRealitySpatialAwarenessMeshObserver>(observerName); observer.DisplayOption = meshDisplayOption; }
/// <summary> /// Applies the mesh display option to existing meshes when modified at runtime. /// </summary> /// <param name="option">The <see cref="SpatialAwarenessMeshDisplayOptions"/> to apply to the meshes.</param> private void ApplyUpdatedMeshDisplayOption(SpatialAwarenessMeshDisplayOptions option) { bool enable = (option != SpatialAwarenessMeshDisplayOptions.None); foreach (SpatialAwarenessMeshObject meshObject in Meshes.Values) { if ((meshObject == null) || (meshObject.Renderer == null)) { continue; } if (enable) { meshObject.Renderer.sharedMaterial = (option == SpatialAwarenessMeshDisplayOptions.Visible) ? VisibleMaterial : OcclusionMaterial; } meshObject.Renderer.enabled = enable; } }
/// <summary> /// Applies the mesh display option to existing meshes when modified at runtime. /// </summary> /// <param name="option">The <see cref="SpatialAwarenessMeshDisplayOptions"/> value to be used to determine the appropriate material.</param> protected virtual void ApplyUpdatedMeshDisplayOption(SpatialAwarenessMeshDisplayOptions option) { using (ApplyUpdatedMeshDisplayOptionPerfMarker.Auto()) { bool enable = (option != SpatialAwarenessMeshDisplayOptions.None); foreach (SpatialAwarenessMeshObject meshObject in Meshes.Values) { if (meshObject?.Renderer == null) { continue; } if (enable) { meshObject.Renderer.sharedMaterial = (option == SpatialAwarenessMeshDisplayOptions.Visible) ? VisibleMaterial : OcclusionMaterial; } meshObject.Renderer.enabled = enable; } } }
private void MeshGenerationAction(MeshGenerationResult meshGenerationResult) { if (!IsRunning) { return; } using (MeshGenerationActionPerfMarker.Auto()) { if (outstandingMeshObject == null) { Debug.LogWarning($"MeshGenerationAction called for mesh id {meshGenerationResult.MeshId} while no request was outstanding."); return; } switch (meshGenerationResult.Status) { case MeshGenerationStatus.InvalidMeshId: case MeshGenerationStatus.Canceled: case MeshGenerationStatus.UnknownError: outstandingMeshObject = null; break; case MeshGenerationStatus.Success: // Since there is only one outstanding mesh object, update the id to match // the one received after baking. SpatialAwarenessMeshObject meshObject = outstandingMeshObject; meshObject.Id = meshGenerationResult.MeshId.GetHashCode(); outstandingMeshObject = null; // Apply the appropriate material to the mesh. SpatialAwarenessMeshDisplayOptions displayOption = DisplayOption; if (displayOption != SpatialAwarenessMeshDisplayOptions.None) { meshObject.Renderer.enabled = true; meshObject.Renderer.sharedMaterial = (displayOption == SpatialAwarenessMeshDisplayOptions.Visible) ? VisibleMaterial : OcclusionMaterial; meshObject.Collider.material = PhysicsMaterial; } else { meshObject.Renderer.enabled = false; } // Recalculate the mesh normals if requested. if (RecalculateNormals) { meshObject.Filter.sharedMesh.RecalculateNormals(); } // Add / update the mesh to our collection bool sendUpdatedEvent = false; if (meshes.ContainsKey(meshObject.Id)) { // Reclaim the old mesh object for future use. ReclaimMeshObject(meshes[meshObject.Id]); meshes.Remove(meshObject.Id); sendUpdatedEvent = true; } meshes.Add(meshObject.Id, meshObject); meshObject.GameObject.transform.parent = (ObservedObjectParent.transform != null) ? ObservedObjectParent.transform : null; meshEventData.Initialize(this, meshObject.Id, meshObject); if (sendUpdatedEvent) { SpatialAwarenessSystem?.HandleEvent(meshEventData, OnMeshUpdated); } else { SpatialAwarenessSystem?.HandleEvent(meshEventData, OnMeshAdded); } break; } } }
/// <summary> /// Handles the SurfaceObserver's OnDataReady event. /// </summary> /// <param name="cookedData">Struct containing output data.</param> /// <param name="outputWritten">Set to true if output has been written.</param> /// <param name="elapsedCookTimeSeconds">Seconds between mesh cook request and propagation of this event.</param> private void SurfaceObserver_OnDataReady(SurfaceData cookedData, bool outputWritten, float elapsedCookTimeSeconds) { if (!IsRunning) { return; } if (outstandingMeshObject == null) { return; } if (!outputWritten) { ReclaimMeshObject(outstandingMeshObject); outstandingMeshObject = null; return; } // Since there is only one outstanding mesh object, update the id to match // the one received after baking. SpatialAwarenessMeshObject meshObject = outstandingMeshObject; meshObject.Id = cookedData.id.handle; outstandingMeshObject = null; // Apply the appropriate material to the mesh. SpatialAwarenessMeshDisplayOptions displayOption = DisplayOption; if (displayOption != SpatialAwarenessMeshDisplayOptions.None) { meshObject.Renderer.enabled = true; meshObject.Renderer.sharedMaterial = (displayOption == SpatialAwarenessMeshDisplayOptions.Visible) ? VisibleMaterial : OcclusionMaterial; } else { meshObject.Renderer.enabled = false; } // Recalculate the mesh normals if requested. if (RecalculateNormals) { meshObject.Filter.sharedMesh.RecalculateNormals(); } // Add / update the mesh to our collection bool sendUpdatedEvent = false; if (meshes.ContainsKey(cookedData.id.handle)) { // Reclaim the old mesh object for future use. ReclaimMeshObject(meshes[cookedData.id.handle]); meshes.Remove(cookedData.id.handle); sendUpdatedEvent = true; } meshes.Add(cookedData.id.handle, meshObject); meshObject.GameObject.transform.parent = (ObservedObjectParent.transform != null) ? ObservedObjectParent.transform : null; meshEventData.Initialize(this, cookedData.id.handle, meshObject); if (sendUpdatedEvent) { SpatialAwarenessSystem?.HandleEvent(meshEventData, OnMeshUpdated); } else { SpatialAwarenessSystem?.HandleEvent(meshEventData, OnMeshAdded); } }