AR - Virtual Face Paintings (Premium, Difficulty: 2)
CreatiCode last edited by info-creaticode
When you run on the AR face camera, you have the option of displaying some or all of these 4 areas: face, eyes, mouth and lips.
In this tutorial, you will build a program that allows the user to try different face paintings.
Create a new project with these 3 starting blocks:
Note that these input options are changed for the “switch to AR face camera” block:
- The markers are not shown, since we will focus on the face meshes;
- Emulation mode is used, which allows us to easily look at the result from different angles;
- All 4 meshes are turned on.
You should get the default face like this:
By default, the button will be added at the center of the stage. When you use the “Widget Position” tool to drag it to the left top, and the block will be updated automatically:
Whenever the button is clicked, we would like to update the face color. To do that, we need the “when widget clicked” block, and then add the “update color” block.
Since “All” areas are selected, the entire face will be updated to the color you choose:
Instead of fixed color, you can randomly select the color whenever the button is clicked. The key is to use the “color” reporter block with a “pick random” block:
Now we can repeatedly click the “Colors” button to change colors:
You can use the “area” input to control which part of the face to change color. The “Main” area refers to the face, the “Second” area refers to the eyes, the “Third” area refers to inside the mouth, and the “Forth” area refers to the lips.
For example, we can use 4 “update color” blocks, and select a different area for each block:
For the second face painting effect, let’s convert the face to a tiger. Please add a new button for “Tiger”, and drag it next to the first button:
When the new button is clicked, we should apply a new texture to the face. You can search for an image online with the face of a tiger, or some other animal. For example, this is a good tiger face image to use:
You need to download that image file, then upload it into your project as a costume.
Once that’s done, you can use the “update texture using costume” block to apply this texture to the entire face (all areas). Note that some images will show up upside-down, so it needs to be rotated 180 degrees.
Now you get a tiger face like this:
The tiger face still has one major issue: only the center area of the tiger face is used. To make sure more of the tiger face is used, we can apply a simple trick.
In the costume editor, convert the tiger costume to a “vector”, then shrink it smaller. After that, draw a transparent rectangle (no fill) around the tiger’s face. This will shrink the tiger’s face, so it will fit into the face mesh.
Now we are mostly done with the coding, we can start to test with the real camera, and make adjustments. For that, we just need to switch the “emulation mode” to “No”.
Here is the final output:
There are unlimited ways for you to paint the face mesh differently. Here are some ideas:
- Transparent Colors: you can try to use transparent colors for the face mesh, so the real face is still visible.
- Text or Object: you can try to write some text or paint a small object (like a tree) on the face.
- Other Animal Faces: you can try to replace the tiger costume with another animal face.