DEV Community

Cover image for Unity MR Part 6: Planes
tststs for Taikonauten

Posted on • Updated on • Originally published at Medium

Unity MR Part 6: Planes

๐Ÿ‘€ Stumbled here on accident? Start with the introduction!

๐Ÿ“š 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


โ„น๏ธ 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

The XR Origin (XR Rig) GameObject with the added AR Plane Manager

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

Hierarchy with the added AR Default Plane

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 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

Creating the AR Default Plane Prefab

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<br>

Selecting the AR Default Plane for the AR Plane Manager

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

Setting the Detection Mode for the AR Plane Manager

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, 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)

PlaneMaterial Surface Options/Inputs (Example)

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

Base Map color for PlaneMaterial

Base Map color for PlaneMaterial

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

Assigning the PlaneMaterial to the Mesh Renderer

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

Disable Cast Shadows on our AR Default Plane

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

The app after adding planes to the scene

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.

Top comments (0)