Unity MR Part 6: Planes
Tags: unity3d, mixedreality, tutorial, beginners
Published: 2024-01-11 08:50:58

👀 Stumbled here on accident? Start with the [introduction](https://dev.to/taikonauten/part-0-introduction-56fl)!

📚 In this article, we'll focus on rendering a GameObject for every detected horizontal plane obtained from the Space Setup. Additionally, we will create a Material specifically for each of these GameObjects. This process is integral to visualizing spatial data and enhancing the interactive elements of our project.

---

ℹ️ If you find yourself facing any difficulties, remember that you can always refer to or download the code from our accompanying [GitHub repository](https://github.com/taikonauten/unity-mr-article-series/tree/main/06_Planes)

---

ℹ️ Before beginning, it's necessary to execute the Space Setup on your Meta Quest, found under **Physical Space → Space Setup**. This process enables mapping of various objects like doors, walls, tables, couches, and more. The Meta Quest memorizes these details, allowing for an enhanced virtual reality experience that fully utilizes your area's boundaries.

---

## Adding the AR Plane Manager

Add the `AR Plane Manager` to our existing `XR Origin (XR Rig)` GameObject by using `Add Component`.


![The XR Origin (XR Rig) GameObject with the added AR Plane Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zd89rut5i0oks84dscs2.png)
<figcaption>The XR Origin (XR Rig) GameObject with the added AR Plane Manager</figcaption>
---

## Creating a Prefab for our detected planes

As illustrated in the screenshot, it's necessary to define a Plane Prefab, which we will create in the following steps. To do this, go to your project's hierarchy and create an AR Default Plane by right-clicking and navigating to **XR → AR Default Plane**.

ℹ️ The `AR Default Plane` in Unity is a preset GameObject used to visually represent detected planes in an AR environment. It's often used as a default visual marker for floors, walls, or other flat surfaces that the AR system identifies in the real world, providing a reference point for placing virtual objects in relation to these physical surfaces.


![Hierarchy with the added AR Default Plane](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3qrjqqmxnonn6x2vrzn.png)
<figcaption>Hierarchy with the added AR Default Plane</figcaption>
---

Create a new folder named `Prefabs` in your `Assets` folder.

Then, create a Prefab by dragging the `AR Default Plane` GameObject from your hierarchy into your **Assets/Prefabs** folder. For those unfamiliar with the concept of Prefabs, it's advisable to refer to Unity's official documentation on Prefabs [Unity - Manual: Prefabs](https://docs.unity3d.com/2023.2/Documentation/Manual/Prefabs.html) for a thorough understanding.

By dragging and dropping the GameObject into the Prefabs folder, you create a reusable Prefab. After you have created this Prefab, remove the `AR Default Plane` GameObject from your hierarchy.


![Creating the AR Default Plane Prefab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88kugr1ze00n00ofjc0f.png)
<figcaption>Creating the AR Default Plane Prefab</figcaption>
---

Next, select your `XR Origin (XR Rig)` in the hierarchy and assign the `AR Default Plane` Prefab to the `AR Plane Manager`. The AR Plane Manager will instantiate an `AR Default Plane` GameObject at each plane's position based on spatial data.


![Selecting the AR Default Plane for the AR Plane Manager
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6anl5npj4kidh1a26vex.png)
<figcaption>Selecting the AR Default Plane for the AR Plane Manager
</figcaption>
---

For the purposes of this article series, we only require horizontal planes. As shown in the upcoming screenshot, select `Horizontal` as the Detection Mode, preparing the system to specifically detect and interact with horizontal surfaces in the environment.


![Setting the Detection Mode for the AR Plane Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttvw7e821cjcjnjwv07g.png)
<figcaption>Setting the Detection Mode for the AR Plane Manager</figcaption>
---

## Setup the default plane Prefab

Finally, we need to modify the `AR Default Plane` Prefab. Open it in the Prefab Editor by double-clicking on it in your Project window. The primary adjustment involves assigning an appropriate material to the mesh.

ℹ️ For those unfamiliar with creating materials in Unity, more information can be found in the [Unity documentation](https://docs.unity3d.com/Manual/materials-introduction.html), which provides comprehensive guidance on material creation and customization in Unity environments.

Create a new Material in your **Assets/Materials** folder and name it `PlaneMaterial`. You’ll find the Assets folder under `Project`. Create an empty folder Materials if not already done. As this material is intended for debugging purposes, we'll construct a basic one. This straightforward approach streamlines the process, ensuring that the material effectively serves its debugging function without the need for complex configurations or properties. You can choose the values as in the next screenshot or choose your own Material.


![PlaneMaterial Surface Options/Inputs (Example)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5zw5cc0yd9e1w0dc8vgn.png)
<figcaption>PlaneMaterial Surface Options/Inputs (Example)</figcaption>
---

Also update the `Base Map` color under `Surface Inputs`. Choose `FFFFFFF` and for Alpha `0`.


![Base Map color for PlaneMaterial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahmlb3lqerloun66ovdx.png)
<figcaption>Base Map color for PlaneMaterial</figcaption>
---

Now that we've created the material, it's time to assign it to the `Mesh Renderer` of your newly created `AR Default Plane` prefab. Edit the Prefab by double-clicking the `Ar Default Plane` under **Assets/Prefabs/AR Default Plane**.


![Assigning the PlaneMaterial to the Mesh Renderer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3hfgr9miybxmuhjt7d3d.png)
<figcaption>Assigning the PlaneMaterial to the Mesh Renderer</figcaption>
---

If there's an existing entry in the Mesh Renderer, remove it first. Then, add `PlaneMaterial` as `Element 0`. This step ensures that our custom material is properly applied, allowing for the intended visual representation and debugging functionality within the AR environment.

The second and final modification for our `AR Default Plane` involves turning off the `Cast Shadows` option. We don't want planes casting shadows on each other in our space, as this would appear unrealistic. The real environment already casts shadows, so disabling this feature ensures a more natural and coherent visual experience in our AR setup.


![Disable Cast Shadows on our AR Default Plane](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnhee7izv8goh1cryhy0.png)
<figcaption>Disable Cast Shadows on our AR Default Plane</figcaption>
---

## Testing the app

That's all for now. We're ready to test our application. Simply hit `Build And Run` in the Unity Editor as explained in the last articles.

As shown in the upcoming screenshot, you'll see planes like the floor the couch are now rendered. Additionally, notice that raycasting is operational. In the next article about `Anchors`, we'll focus on enhancing the color of the raycast when a hit test is successful, further refining our application.


![The app after adding planes to the scene](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ok84gatbp1ve4ii70rnv.png)
<figcaption>The app after adding planes to the scene</figcaption>
---

## Next article

In our upcoming article, we will explore the critical role of Raycasts in MR environments. Raycasts are fundamental in MR for detecting and interacting with objects in a virtual space. We will cover how Raycasts work, their applications in our scenario, and how to effectively implement them in Unity.