In this tutorial, we will learn how to implement face tracking with Unity AR foundation. Face tracking is widely used in camera applications and photo filter apps. Face tracking is in built into AR foundation. So, you can activate it by just adding a component and a prefab. No coding required.
You can watch the tutorial in video format below.
You will have to setup the basic AR foundation project before starting. If you are not sure how then check out our tutorial on getting started with AR foundation.
You scene should have an AR Session Origin and an AR Session gameobject. If not add them by going to your hierarchy window and clicking on the + sign >XR>AR session origin.
Make sure the default camera is not present in the scene.
AR Face Manager
Select the AR Session Origin and go to the inspector window.
Click on Add Component and select AR Face Manager.
AR Face Manager requires a Prefab. So, the next step will be to create a prefab.
Go to hierarchy window
Click on + sign >XR>AR Default Face
Drag and Drop the AR Default Face to your project window and delete it from the Hierarchy.
Select the AR Session Origin in the Hierarchy.
Now drag and drop the AR Default Face from your project window to the Face Prefab parameter.
Selecting the Selfie Camera
Go to the Hierarchy window and click on the small arrow near AR Session Origin. This will show the child object AR camera. Select AR Camera and go to the inspector window.
Find the parameter called facing direction. Use the drop down to select user as the facing direction.
Now you are ready to build and deploy.
Go to File>Build Settings.
Connect your phone to the PC and make sure USB debugging is switched on.
Click on Build and Run.
You App should automatically run on your phone and it should place a green mask on your face.
Changing the Face mask
By default, Unity places a green color mask on the prefab. If you want to change it then you can give the prefab a different Material.
Create a new material and change the Albedo property of the material to your requirement.
Select the AR Default Face prefab in your project window and scroll down to the mesh renderer.
Replace the DebugFace material with the new material you have created.