Create a simple Animoji with AR
Tags: beginners, ar, swift, animoji
Published: 2021-03-08 03:22:44

In this tutorial, we'll learn how to create an Animoji using Reality Composer and ARKit within Swift. Let's start with the definition of Animoji: It is an interactive filter that is featured in modern iPhones and uses face recognition to track face patterns. 
We will focus on creating a character that can wink and blink at the same time that we are doing the same action.

The final result of the App would look like this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j383yeljxd5a2qqklv3c.PNG)

Let's begin!

1. First Create a new project in Xcode, chose Augmented reality Application type.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9crdw1d7r56xf8y1k5r.png)
Then set the project settings, make sure to select RealityKit as "Content Technology"
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p8904o1p6jv3rseuomrz.png)

 
2. On your Project folder select the .rcproject file and chose the option on the right top corner that says "Open in Reality Composer".
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssrzg0cenbh9hmyzupxn.png) This will open the application where you'll be able to create your own shapes and/or use the built-in figures.

Then, head over to the properties panel and make sure to select the Face Anchor type.

3. On the top, you'll find a series of objects and shapes that you can integrate into the face model. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4yz3jv0bdxalveiqldz.png) Start designing your Animoji, eyes, mouth, ears, etc. You can set colours and object materials on the properties panel. 

4. Before going to the code we must set identifiers to both of the eyes objects that the character has, like this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44upf4byjdsg4pg19i8s.png) _(make sure to use proper names that you can easily identify)_.

Next, add an identifier name to our scene opening the top left scene tab.

> *A Scene is a collection of entities that contain Anchors, Objects, Behaviours and the Physics world*.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zajlec98gzvfd5zvrn2t.png)

5. Open the ViewController.swift file and import at the top the ARKit that will allow us to use our front camera and track our facial movements.
`import ARKit`

6. Add the `ARSessionDelegate` to the ViewController Class to control and follow the changes on face anchors.

7. Create a variable that will save the scene created on reality composer. Then create 2 more variables for the eyes, set them as Entity type. 
> *Entity is every object created on the reality composer scene*

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1smlre71otfedufsx57j.png)

8. create a `viewWillAppear(- animated: bool)` function, it will load the view by itself. Inside it, create a Face Tracking configuration constant to detect faces in device's front camera feed and run the arView session setting a delegate.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/adeqqhv7ht112xwltsp7.png)

9. Load the Animoji scene with `Anchor variable name = rcproject file name.loadAnimoji()`. If desired use try/catch to handle load scene errors and then, append it to the arView scene.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ss3evr35xvg77yk36atu.png)

10. Target the eye objects on reality composer and save them on the global variables previously created.
```eyeRight = schnauzerAnchor.findEntity(named: "eyeRight")!
eyeLeft = schnauzerAnchor.findEntity(named: "eyeLeft")!```

**Make sure that the names match with the identifiers set on reality composer objects**

11. Handle the eye blink: Create a session function `func session (_ session:ARSession, didUpdate frame: ARFrame)`

- set a variable to store the ARFaceAnchor object to find face anchors.
- Make a for into loop thought the anchors found using `frame.anchors` to make a face anchor checking.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4wzb5e5k138fewxilau8.png)
_Here's an explanation_
[ARFaceAnchor](https://developer.apple.com/documentation/arkit/arfaceanchor)

12. Get Value of eyes movement: We'll use the blendShapes dictionary that represents the facial features recognized by ARKit. the value of each key on the dictionary is a  floating-point number indicating the current position of the feature and it ranges from 0.0 (neutral) to 1.0 (maximum movement).

Finally, we will match the eye-opening and closing with the character's eye objects, scaling them on the z-axis.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9dfy3dl3sl2kwvogkrh.png)

And we are officially done! Now you can run your application and share your Animoji :D

##### Demo Video
[Youtube] (https://youtu.be/VDMJYfkbqZE)

###### useful resources
* [set elements in scene] (https://developer.apple.com/documentation/realitykit/creating_3d_content_with_reality_composer/configuring_elements_in_a_scene)
* [Manipulate objects from code] (https://developer.apple.com/documentation/realitykit/creating_3d_content_with_reality_composer/manipulating_reality_composer_scenes_from_code)
* [eye blendingShapes] (https://developer.apple.com/documentation/arkit/arfaceanchor/blendshapelocation/2928261-eyeblinkleft)