Building A Simple ARCore Demo With Augmented Faces

Introduction

 
In this article, I will explain the Google ARCore basics with augmented face examples.
 

ARCore

 
ARCore integrates the Android device camera and motion features to produce augmented reality experiences in your app or game. Augmented reality (AR) describes user experiences that add 2D or 3D elements to the live view from a device's camera, in a way that makes those elements appear to inhabit the real world. ARCore combines device motion tracking, camera scene capture, advanced scene processing, and display conveniences to simplify the task of building an AR experience. You can use these technologies to create many kinds of AR experiences using the back camera or front camera of an Android device.
 
Prerequisites
 
This article recommends that you should have a basic understanding of Android development, and the basics of the ARCore framework.
 
To test out your ARCore app, you will need one of Samsung's ARCore compatible devices, which are devices that have Samsung Gallaxy A8 processors & (Api Level = 26 ) or higher. Let's dive right into the following points below:
  • Creating a New Project for ARCore apps
  • Setting Up Google ARCore Scene View
  • Connecting Google ARCore
  • Configuring of AugmentedFace controller
  • Allowing Camera Usage
  • Adding 3D Facemask view
  • Player Setting ARCore Scene

Creating a New Project

 
In the Xcode menu, select File > New > Project ... Choose Single View App and press next. Xcode has the ARCore template but actually, you can just use the Single View App template to build an AR app.
 
Building A Simple ARCore Demo With Augmented Faces
 
You can name your project whatever you want. I named my project world tracking demo, and pressed next to create a new project.
 

Setting Up Google ARCore Scene View

 
Now open up main.Assets. Drag the ARCore Scene view onto your view controller.
Building A Simple ARCore Demo With Augmented Faces
 

Connecting Google ARCore

 
On the Main.Google ARCore file, go up to the toolbar and open up the Assistant Editor. Add an import statement at the top of the HomeViewController.AugmentedFaces files to import ARCore.
 
Then, hold control and drag from the Google ARCore Scene View to the HomeViewContoller.AugmentedFaces file. When prompted, name the Google ARCore sceneView.
 
Building A Simple ARCore Demo With Augmented Faces
 

Configuration of AugmentedFaces Controller

 
Now it's time to configure the Google ARCore Scene View. Insert the following code into the AugmentedFaces Controller.
  1. using System.Collections.Generic;  
  2. using GoogleARCore;  
  3. using UnityEngine;  
  4. using UnityEngine.UI;  
  5. public class AugmentedFacesExampleController: MonoBehaviour {  
  6.     public GameObject FaceAttachment;  
  7.     private bool m_IsQuitting = false;  
  8.     private List < AugmentedFace > m_TempAugmentedFaces = new List < AugmentedFace > ();  
  9.     public void Awake() {  
  10.         Application.targetFrameRate = 60;  
  11.     }  
  12.     public void Update() {  
  13.         _UpdateApplicationLifecycle();  
  14.         Session.GetTrackables < AugmentedFace > (m_TempAugmentedFaces, TrackableQueryFilter.All);  
  15.         if (m_TempAugmentedFaces.Count == 0) {  
  16.             Screen.sleepTimeout = SleepTimeout.SystemSetting;  
  17.             FaceAttachment.SetActive(false);  
  18.         } else {  
  19.             Screen.sleepTimeout = SleepTimeout.NeverSleep;  
  20.             FaceAttachment.SetActive(true);  
  21.         }  
  22.     }  
  23.     private void _UpdateApplicationLifecycle() {  
  24.         if (Input.GetKey(KeyCode.Escape)) {  
  25.             Application.Quit();  
  26.         }  
  27.         if (m_IsQuitting) {  
  28.             return;  
  29.         }  
  30.         if (Session.Status == SessionStatus.ErrorPermissionNotGranted) {  
  31.             _ShowAndroidToastMessage("Camera permission is needed to run this application.");  
  32.             m_IsQuitting = true;  
  33.             Invoke("_DoQuit", 0.5 f);  
  34.         } else if (Session.Status.IsError()) {  
  35.             _ShowAndroidToastMessage("ARCore encountered a problem connecting.  Please start the app again.");  
  36.             m_IsQuitting = true;  
  37.             Invoke("_DoQuit", 0.5 f);  
  38.         }  
  39.     }  
  40.     private void _DoQuit() {  
  41.         Application.Quit();  
  42.     }  
  43.     private void _ShowAndroidToastMessage(string message) {  
  44.         AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");  
  45.         AndroidJavaObject unityActivity = unityPlayer.GetStatic < AndroidJavaObject > ("currentActivity");  
  46.         if (unityActivity != null) {  
  47.             AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast");  
  48.             unityActivity.Call("runOnUiThread"new AndroidJavaRunnable(() => {  
  49.                 AndroidJavaObject toastObject = toastClass.CallStatic < AndroidJavaObject > ("makeText", unityActivity, message, 0);  
  50.                 toastObject.Call("show");  
  51.             }));  
  52.         }  
  53.     }  
  54.   }  
  55. }   
In the above viewDidLoad() method, we initialized an AR configuration called ARWorldTrackingConfiguration.This is a configuration for running world tracking. 
 

Allowing camera permission

 
Before we run our app, we need to get user permission that we will utilize their device's camera for augmented reality. So open up Info.plist. Select the ARCore Device. Drag the ARCamera for Camera Config filter. Set the value permission message.
 
Building A Simple ARCore Demo With Augmented Faces
 
Now you should be able to see your camera's view.
 
We have configured our sceneView's session to run the world tracking configuration.
 

Adding 3D AugmentedFacesView

 
We are going to add a 3D AugmentedFaces on the scene View, the code is shown below
 
Building A Simple ARCore Demo With Augmented Faces
 
Now add AugmentedFaces that is called from the viewdidLoad() AugmentedFace.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. using UnityEngine.SceneManagement;  
  5.   
  6. public class menustart : MonoBehaviour  
  7. {  
  8.     public void ChangeSceneStratfacemask1(string sceneName)  
  9.   
  10.     {  
  11.         Application.LoadLevel ("facemask1");  
  12.     }  
  13.   
  14. }  

Player Setting Google ARCore Scene

 
In the Xcode menu, select File > Build Settings > Player Setting ... Switch the platform and open XR Setting and select ARCore Supported.
 
Building A Simple ARCore Demo With Augmented Faces
 
Build and Run the app, and you will able to see a floating 3D augmented face.
 
Building A Simple ARCore Demo With Augmented Faces