Skip to main content
Version: 12 Dec 2024

Meshing Visualizer Example

This section includes an example on how to change the color or material of the mesh generated by the UnityEngine.XR.MagicLeap.MeshingSubsystemComponent. Developers may choose to use this feature to do things like visualize the mesh objects when initializing their application and then changing the material to only occlude other virtual objects.

Not all materials are supported on all mesh types. For example a point cloud shader might be preferred when visualizing the generated mesh in MeshingSubsystemComponent.MeshType.PointCloud mode.

caution

Before proceeding make sure you have an object with a properly configured MeshingSubsystemComponent attached. In addition to having the SPATIAL_MAPPING permission to be enabled in your project's Manifest Settings. (Edit > Project Settings > Magic Leap > Manifest Settings)

Example

This example sets the Mesh Render Material on the meshes generated by the Mesh Subsystem component.

using UnityEngine;
using UnityEngine.XR.MagicLeap;

public class MeshVisualizerExample : MonoBehaviour
{

#if UNITY_EDITOR || UNITY_MAGICLEAP || UNITY_ANDROID
[SerializeField, Tooltip("The MeshingSubsystemComponent from which to get update on mesh types.")]
private MeshingSubsystemComponent _meshingSubsystemComponent;

[SerializeField, Tooltip("The type of mesh to render.")]
private MeshingSubsystemComponent.MeshType _meshType;

[SerializeField, Tooltip("The material to apply to the mesh.")]
private Material _coloredMaterial = null;

/// <summary>
/// Register for new and updated fragments.
/// </summary>
void Start()
{
_meshingSubsystemComponent.meshAdded += HandleOnMeshReady;
_meshingSubsystemComponent.meshUpdated += HandleOnMeshReady;
SetVisualizationMode(_meshType);
}

/// <summary>
/// Unregister callbacks.
/// </summary>
void OnDestroy()
{
_meshingSubsystemComponent.meshAdded -= HandleOnMeshReady;
_meshingSubsystemComponent.meshUpdated -= HandleOnMeshReady;
}

/// <summary>
/// Developers should clear mesh data when changing the mesh type at runtime.
/// </summary>
public void SetVisualizationMode(MeshingSubsystemComponent.MeshType type)
{
_meshingSubsystemComponent.requestedMeshType = type;
_meshingSubsystemComponent.DestroyAllMeshes();
_meshingSubsystemComponent.RefreshAllMeshes();
}

/// <summary>
/// Handles the MeshReady event, which tracks and assigns the correct mesh renderer materials.
/// </summary>
/// <param name="meshId">Id of the mesh that got added / upated.</param>
private void HandleOnMeshReady(UnityEngine.XR.MeshId meshId)
{
if (_meshingSubsystemComponent.meshIdToGameObjectMap.ContainsKey(meshId))
{
MeshRenderer meshRenderer =
_meshingSubsystemComponent.meshIdToGameObjectMap[meshId].GetComponent<MeshRenderer>();
meshRenderer.enabled = true;
meshRenderer.material = _coloredMaterial;
}
}
#endif
}