Augmented Faces with Unity and ARCore
Tags: beginners, tutorial, unity3d, android
Published: 2022-01-25 13:20:32

Last time we created an Augmented Reality app that recognized vertical planes (like walls) and added a virtual painting there. You can check the tutorial from [here](https://dev.to/whatminjacodes/plane-detection-with-arcore-and-unity-3245) if you haven't seen it.

This time we will create face filters! The face filter takes inspiration from one of my favorite videogames Horizon Zero Dawn. The finished project is in my [Github](https://github.com/whatminjacodes/tech-tuesday-tutorials/tree/main/intro-to-ar/04-face-filter) if you just want to test this out without coding it yourself.

This tutorial assumes you have at least basic understanding of how Unity works (what are GameObjects and Prefabs etc) so if you are new to Unity I recommend you to first go through their beginner tutorial [Roll-a-Ball](https://learn.unity.com/project/roll-a-ball) that explains the basics well!

Start by downloading a base project from my [Github](https://github.com/whatminjacodes/tech-tuesday-tutorials/tree/main/intro-to-ar/01-arcore-and-unity-setup) or follow the [first tutorial](https://dev.to/whatminjacodes/unity-setup-and-arcore-installation-19md) from this series before going forward.

## Adding face detection
Open the Unity project and click on AR Session Origin from Hierarchy and click on AR Camera. Switch the camera's facing direction to User.

![Facing direction of the camera](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpujzfhiew42rm1in1az.jpg)

Click on AR Session Origin and Add Component. Search for AR Face Manager script and add it. 

![Create a default face](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xf1atp9yewvckwjhsttb.jpg)

Then right click on Hierarchy and choose XR -> AR Default Face. Create a folder called Prefabs on Assets and drag and drop AR Default Face there and then delete it from Hierarchy. 

![Adding AR Default Face prefab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5k42m5bh5b3u40fcdpfu.jpg)

Drag the created prefab from Assets to AR Face Manager's Face Prefab (in AR Session Origin).

![Built project with Default Face](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4uopqzmeicthxr67dov.jpg)

If you Build and Run the app now, you will see a bit weird looking green face augmented on your detected face.

## Adding hologram
Download a hologram 3D model from [here](https://github.com/whatminjacodes/tech-tuesday-tutorials/tree/main/intro-to-ar/04-face-filter/face-filter/Assets/_Hologram). It takes inspiration from one of my favorite video games Horizon Zero Dawn, where the main character Aloy uses a device which created a similar kind of hologram.

Download everything from the hologram folder (link above) and create a new folder in your project's Assets and copy them there. There should be a HologramPrefab which already has all the needed settings and components. It's Position should be X = -0.09, Y = 0.05 and Z = 0.02, Rotation X = 0, Y = -115 and Z = 0 and Scale X, Y and Z 1.2.

It also has a ControllingGlitch script and Hologram Material. I followed a tutorial made by [sharpcoderblog.com](https://sharpcoderblog.com/blog/create-a-hologram-effect-in-unity-3d) so check their tutorial out if you want to understand better how the Hologram shader and material works.

![Open AR Default Face prefab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipcc11e70994xnqvt7fg.jpg)

Open the AR Default Face Prefab that we created earlier by clicking it once and then clicking a button Open Prefab.

![Add HologramPrefab as a child of AR Default Face](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7xur26ba1ti6i1li7d1.jpg)

Add HologramPrefab as a child of AR Default Face. Make sure it's position, rotation and scale stays the same.

We still need to change the material of the parent GameObject (AR Default Face). Currently it is still using the green default material. Right click on Assets folder and click Create -> Shader -> Unlit Shader. This creates a new shader file. Name it FaceOccluder, open it and copy paste the code below:

```glsl
// Tutorial for shader from https://tutorialsforar.com/how-to-hide-objects-using-ar-foundation-in-unity/
Shader "Custom/FaceOccluder"
{
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        Tags { "Queue" = "Geometry-1" }
        ZWrite On
        ZTest LEqual
        ColorMask 0

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                return fixed4(0.0, 0.0, 0.0, 0.0);
            }
            ENDCG
        }
    }
}
```

I'm not going into details how shaders work, because it's a big subject, but basically it calculates each pixel and doesn't draw the ones that are located around our face. This way the hologram will disappear behind our face when rotating our head enough. It makes the hologram feel more real. You can read more in the tutorial where I found the occlusion shader code: [tutorialsforar.com](https://tutorialsforar.com/how-to-hide-objects-using-ar-foundation-in-unity/).

![Creating new material](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22ur1x49tim4ayvox34l.jpg)

Next right click on Assets folder and choose Create -> Material and name it FaceOccluder. Drag and drop the shader file we created earlier on the material (or click on the Material and choose the Shader manually like in the picture above). 

![Adding material to AR Default Face](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vmz945vj3btckwqrff7.jpg)

Then drag and drop the material on AR Default Face.

## Finished
Now everything's done! You can build the project and you should see a hologram following your head movement!

![Gif animation of the finished project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/in0nog4ygq6euzx1abov.gif)

Give me a follow if you want to see more tutorials! You can also follow my Instagram [whatminjaplays](https://www.instagram.com/whatminjaplays/) if you are interested to see more about my days as a software developer and a gaming enthusiast!