Skip to main content
Version: 12 Dec 2024

Plane Classification

note

This example uses the OpenXR Unity API. For a previous version using the Unity MLSDK, see this page.

Plane Classification for the Magic Leap 2 is managed using the Unity XR AR Subsystems. For in-depth API references, please refer to the Unity.XR.AR.Subsystems.PlaneClassification docs.

caution

Make sure your project has the SPATIAL_MAPPING permission enabled in Project Settings.

Example Script

The following script changes the color of detected planes based on their AR Subsystem classification.


using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

public class PlanePrefabExample : MonoBehaviour
{
private const int GRAY_PLANE_QUEUE = 3001;
private const int DEFAULT_PLANE_QUEUE = 3000;

void Start()
{
ColorClassify();
}

private void ColorClassify()
{
var plane = GetComponent<ARPlane>();
var color = plane.classification switch
{
PlaneClassification.Floor => Color.green,
PlaneClassification.Ceiling => Color.blue,
PlaneClassification.Wall => Color.red,
PlaneClassification.Table => Color.yellow,
_ => Color.gray
};

var mat = GetComponent<MeshRenderer>().material;
mat.color = color;
mat.renderQueue = color == Color.gray ? GRAY_PLANE_QUEUE : DEFAULT_PLANE_QUEUE;
}
}

The above snippet doesn’t request permissions, so make sure to include a script that does so in your final scene. This script needs to be attached to the plane prefab used in the AR Plane Manager Script attached to the ML Rig. You can view the complete prefab in the Unity Examples project available for download in ML Hub.

Resources