Face tracking
Face tracking is an important part of AR with many practical use cases, including face effects, filters, and "try-ons" which allow the user to simulate makeup, eyeglasses, or different hairstyles.
MARS applications have face-tracking ability when you deploy your app to an Android or iOS device. In the Unity Editor MARS applications can use face tracking in Recorded mode or Live mode simulations. The MARS package content includes default Session Recordings that are videos with pre-recorded face tracking data. You can play these videos in Recorded mode to test against recorded face data. MARS also supports face tracking against a pure video in simulation. The video feed can come from either a camera in Live mode or custom video in Recorded mode. To use this kind of face tracking you need to download and install ULSee, which is a third-party plug-in. You can still create facemasks in the Editor without ULSee.
Using a facemask template
From Unity's main menu, go to Window > MARS > Choose Template, and select the Facemask option. This opens a template Scene with all the elements you need to incorporate face tracking in your app, which you then need to save separately (menu: File > Save As).
To test out face tracking capabilities while designing your AR app, make sure that the Simulation view is open, and set its mode to either Live (to get a stream from the first webcam MARS detects), or Recorded (if you have a pre-recorded video to work with). Face tracking works with both these options.
Loading custom recorded videos
To load your own video clip for testing inside MARS, you must create a Session Recording asset that references the video clip. In the Project window, right-click the video clip then select Create > MARS > Session Recording from Video Clip.
After you've done this, you must refresh Recorded mode environments. From Unity's main menu, go to Window > MARS > Developer > Refresh Session Recordings.
You should now see your video in the Environment drop-down list of the Device view.
Placing digital content on a face
The Facemask template places a head model in the middle of your Scene.
To create facemasks, decorate this model as if it were a mannequin. When you drag a Prefab from your Project onto the face, different key ‘landmarks’ (such as the eyebrow or nose) light up as the cursor hovers that area. Release the mouse button to anchor the GameObject to that particular feature. MARS places the GameObject as a child GameObject of the face landmark in the Transform hierarchy.
To reposition a Prefab or move it to a different landmark, select the GameObject, hold down the Shift key, and then drag the GameObject to its new position.
To control how dragged GameObjects should snap and align to the face, use the Placement Options section of the main Unity toolbar. The options available are Snap to Pivot and Orient to Surface. If you enable Orient to Surface, you can also select which direction Unity considers to be "forward".
Note: If you use a Unity version older than 2019.3, these options don't exist in the main toolbar; instead, they are in the MARS Panel's Placement Options section.
Testing face tracking on MARS
Test your facemask in the Device view. You can set the Mode to Live or Recorded, as described above.
The first time you select Live mode, you will be prompted to allow permission to use the camera. Click Allow to allow access and turn on the camera. If you click Deny, you will see a console warning instructing you on how to allow access every time MARS tries to use the camera.
On Mac OS X version 10.14 (Mojave) and above, the operating system already handles camera access permission, and you can allow or deny access under System Preferences > Security & Privacy > Camera. The setting for Unity Hub controls whether or not all versions of the Unity Editor have access to the camera. If you installed the Editor outside of the Hub, you might see individual Editor versions appear in this view.
On Windows and earlier version of Mac OS X, MARS will ask for permission using a custom prompt, and track the setting under Preferences > MARS. Toggle the Camera permission setting in this view to allow or deny access after the initial prompt.
Face tracking considerations
To preview your face tracking app in Play mode, you must enable the Simulate in Play Mode option in the Project Settings. From Unity's main menu, go to Edit > Project Settings > MARS > Scene Module.