Plane detection allows us to detect horizontal and vertical surfaces in your environment. These surfaces usually called planes. Planes allow us to augment an object on the surface area.
ARFoundation provides support for ARKit and ARCore for iOS and android platforms. Let’s try to make a plane detection project for ARCore.
AR PlaneManager detects the plane in the environment around us. It creates a plane object when the surface is detected, updated, added, or removed.
Planes usually represent the pose, dimensions, and convex boundary points. Planes updated their boundary on every update it detects the plane changed event by the plane manager.
In this project, we are going to create a basic plane detection and visualization to which an augmented object is placed.
Create an AR Plane Detection Demo
- Create a new Unity 3D project.
- Goto the Windows-> Package Manager and installed the following packages,
- ARFoundation (preview.7 – 4.1.0)
- ARCore XR Plugin (preview.7 – 4.1.0)
- Universal RP (7.4.3)
- XR Interaction Toolkit (Preview – 0.9.4)
- Delete the MainCamera from the Hierarchy.
- Right-click in the Hierarchy Goto XR-> AR Session, this will create an AR Session object in the hierarchy.
- Again Goto XR-> AR Session Origin, this will create an AR Session origin object in the hierarchy.
- AR Session origin has an AR Camera as a child, tag it as a MainCamera.
- Select the AR session origin object and add an AR Plane Manager script to it.
- AR PlaneManager needs an AR Plane Prefab to which it creates planes in the real-world surfaces.
Now, we need to create an empty game object and named it AR Plane, and attached the following components to it.
- AR Plane Script
- AR Plane Mesh Visualizer Script
- Mesh Filter
- Mesh Renderer
Material is required to see mesh renderer in the real world, so let’s create a material called FeatheredPlaneMaterial. After that, the material needs a shader and texture, you can found this in the project link below.
Make a Prefab by dragging this AR Plane object to the Project tab, similarly, we make other prefabs and assign this prefab to the AR PlaneManager Script.
In addition, Change the build platform to android and then do the following configurations,
- Goto Edit->Project Settings->XR Plugin Management->Select Android Platform->Plugin Provider->Select ARCore
- Goto PlayerSettings->Other Settings->Rendering->Remove Vulkan from the graphics API.
- Unselect the Multithreaded rendering.
- Change the Minimum API Level to 26.
- In addition, Add your scene to builds settings and build.
- Open this demo in android devices and move around this device to capture the surface of the real world.
After that, the planes are shown on the devices as soon as the surface is detected. A similar effect can be achieved in the iOS devices too.
Find out the whole project on the GitHub.