# Spatial Meshing Sample

WARNING

The Spatial Meshing feature is marked as experimental because optimizations on the package and Snapdragon Spaces Service side are breaking backwards compatibility from release to release at the moment. Mesh normals are also not implemented yet.

This sample demonstrates how to generate and visualize the spatial mesh approximating the environment in the real world. For basic information about spatial meshing and what AR Foundation's AR Mesh Manager component does, please refer to the Unity documentation (opens new window). In order to use this feature it has to be enabled in the OpenXR plugin settings located under Project Settings > XR Plug-in Management > OpenXR (> Android Tab).

# How the sample works

ARMeshManager is required to be a child of the ARSessionOrigin. This component has a reference to the MeshFilter that will be generated when a mesh is available. Normals, Tangents, Texture Coordinates, and Colors are turned off since the mesh itself does not contain this data.

WARNING

Attaching the ARMeshManager to the camera object will result in changes to the object's Scale, from the original (1, 1, 1) to (10, 10, 10). This will cause rendering issues of the application on headworn devices until the original camera scale is restored.

AR Mesh Manager Component

By default, when the sample is opened, it generates a mesh with its polygons adapted to the detected environment. The mesh is visualized with the custom MeshVisualization.shader that generates normals for visualization purposes only.

By subscribing to the ARMeshManager's meshesChanged callback, data can be retrieved about when a mesh is added, updated or removed.

# Sample Code

    private ARMeshManager _meshManager;

        public void Awake() {
            _meshManager = FindObjectOfType<ARMeshManager>();
        }

        public override void OnEnable() {
            ...
            _meshManager.meshesChanged += OnMeshesChanged;
        }

        public override void OnDisable() {
            ...
            _meshManager.meshesChanged -= OnMeshesChanged;
        }

        void OnMeshesChanged(ARMeshesChangedEventArgs args) {
            foreach (MeshFilter meshFilter in args.added) {
                ...
            }

            foreach (MeshFilter meshFilter in args.updated) {
                ...
            }

            foreach (MeshFilter meshFilter in args.removed) {
                ...
            }
        }