Face tracking
A face-tracking project puts things on the person in front of the camera: a hat, glasses, a moustache. It has one head, an entity that stands for that person. In the editor it's a head you can see and place things on. At runtime it follows the face the camera finds.
Head space
Everything under the head is in head space, and it's the same in the editor and at runtime:
| Unit | One unit is the width of the face, 15.48 cm on the average head |
| Origin | On the head's centre line, about level with the ears |
| +X | To the viewer's right |
| +Y | Up |
| +Z | Out of the nose |
A hat built to fit a head one unit wide fits, and because the unit is the width of whoever is wearing it, the hat scales with them.
Left and right are as you look at the head, the way they fall on screen on any camera. earLeft is on the left of the picture.
Attaching to the head
FaceAttachmentComponent fixes an entity to a place on its parent head. The entity's ParentId stays the head, and its Transform becomes its offset from that place: position in head units, rotation relative to the head.
import { Behaviour } from "@relu-interactives/spatial-ecs";
import { FaceHeadComponent } from "@relu-interactives/spatial-ecs/components";
export class PutOnGlasses extends Behaviour {
onStart() {
const [head] = this.world.query(FaceHeadComponent);
const oms = this.world.getObjectManagementSystem();
oms.attachToFace(this.world, this.entityId, head, "noseBridge");
}
}The places are in FACE_ATTACH_POINTS:
| Name | Where | Follows expression |
|---|---|---|
noseBridge | Where glasses rest | No |
noseTip | The tip of the nose | No |
forehead | The middle of the forehead | No |
browCenter | Between the brows | No |
eyeLeft, eyeRight | The middle of each eye | No |
cheekLeft, cheekRight | The cheeks | No |
templeLeft, templeRight | Where a glasses arm passes | No |
earLeft, earRight | The ears | No |
headTop | The crown | No |
headBack | The back of the skull | No |
neckFront, neckBack | The neck | No |
mouth | The lips | Yes |
upperLip | Above the lip | Yes |
chin | The chin | Yes |
jawLeft, jawRight | The jaw | Yes |
Set followExpression to override that. A place that doesn't follow expression is steadier, because it comes from the shape of the person's head rather than the landmarks of this one frame. Glasses shouldn't twitch when someone smiles.
The occluder
FaceHeadComponent.occluder hides what's behind the person's head. It's a whole head (face, skull, ears and neck) that writes depth and no colour, so the back of a hat goes behind the wearer and a glasses arm disappears behind the ear.
It's fitted to the person. Over the first second or so it measures the width of their face at the jaw, cheeks, temples and forehead and the height of the face, then holds that, because a head doesn't change shape. The front of the occluder keeps following the expression.
Turn on showOccluder to draw it, in the editor and at runtime, when something looks wrong.
The tracker sees the face and not the hair, so the occluder's crown is an allowance: occluderHair, 2 cm above the skull by default, which matched real heads with short hair. Set it higher for big hair and to 0 for a bald head. It isn't used while something is attached to headTop, because a hat flattens hair and a raised crown would come through a tight one. Hair that stands well off the head isn't covered, and things behind it show through.
Face meshes
A face mesh is a surface on the skin that wears a picture: face paint, a mask, make-up, a glow. It isn't attached to a place. Its vertices are the tracked face's own, so it follows the face and its expression, and a painted cheek stays on the cheek through a smile.
FaceMeshComponent describes one, on an entity of kind faceMesh under the head:
| Field | What it does |
|---|---|
texture | The picture, a path or localasset:// URL. Empty paints tint alone |
opacity | 0 to 1 |
tint | Multiplies the picture, as #rrggbb |
blend | normal paints over, multiply darkens like make-up, additive adds light like a glow |
cutEyes, cutMouth | Leave them open, so the real eyes and mouth show through. On by default |
layer | Higher draws over lower |
textureOffset, textureScale | Move and scale a picture that's nearly lined up, as a share of its size |
The picture goes on through the face's UV layout: the canonical face mesh's own, the same in the editor and at runtime, square, with the forehead at the top. A picture painted on that layout wraps. A photo of a face doesn't line up. The editor's Face Mesh panel saves the layout as a 1024 x 1024 template to paint on.
How it's drawn
FaceMeshSystem draws every face mesh, and it needs a surface, so it isn't one of the systems World.setup adds itself. A face-tracking runtime passes the engine's live one, and an editor the canonical face:
import type { FaceCameraFrame, FaceSurface } from "@relu-interactives/spatial-ecs";
import { FaceMeshSystem } from "@relu-interactives/spatial-ecs/systems";
declare const engine: {
getFaceSurface(): FaceSurface;
getCameraFrame(): FaceCameraFrame | null;
};
export const faceMeshes = new FaceMeshSystem({
surface: engine.getFaceSurface(),
cameraFrame: () => engine.getCameraFrame(),
});Pass it to world.setup({ systems: [...] }) after the system that puts the head on the face.
It's unlit, so it looks the same whatever lights the scene has. It writes no depth, so layers on the one surface don't fight, and it relies on the head's occluder for the rest: the occluder hides the far cheek behind the nose on a turned head, and what's behind the person. Opaque wearables draw before it, so the brim of a hat covers the forehead's paint.
The AR canvas is transparent over the camera's video, so there's nothing in it to darken: three's own multiply would do nothing there. A multiply layer reads the camera instead, through cameraFrame, and only uploads it while a multiply layer exists. additive leaves the canvas's alpha alone, so the video behind it shows with the light added. With no cameraFrame (in an editor) a multiply darkens what's drawn under it.
In a script
import { Behaviour } from "@relu-interactives/spatial-ecs";
import { FaceMeshComponent } from "@relu-interactives/spatial-ecs/components";
export class GlowWhenOpen extends Behaviour {
private mesh: FaceMeshComponent | null = null;
onStart() {
this.mesh = this.getComponent(FaceMeshComponent);
}
onUpdate() {
if (!this.mesh) return;
const open = this.face?.getBlendshape("jawOpen") ?? 0;
this.mesh.opacity = Math.min(1, open * 2);
}
}Put it on the face mesh's entity. Changing texture loads the new picture, and the mesh hides until it's there.
Reading expressions in a script
this.face is the tracker in a face-tracking project and null anywhere else.
import { Behaviour } from "@relu-interactives/spatial-ecs";
export class OpenWide extends Behaviour {
onUpdate() {
const open = this.face?.getBlendshape("jawOpen") ?? 0;
this.transform.scale.y = 1 + open;
}
}getBlendshape takes ARKit-style names: jawOpen, mouthSmileLeft, eyeBlinkRight, browInnerUp and so on. getBlendshapes() returns all of them, isTracking() says whether a face is in view, and getHeadFit() says how this person's head compares with the average one.
For a game, read gestures instead: this.face.isMouthOpen(), isSmiling(), isWinking("left"), a nod or a blink through the onFaceGesture hook, all measured from this person's resting face, and where the mouth is with getWorldPosition("mouth"). See Face gestures. Hands work in face tracking too: see Hand tracking.
Found and lost
Scripts on the head and on attached objects get the same hooks as image targets:
import { Behaviour } from "@relu-interactives/spatial-ecs";
import type { WorldEntityView } from "@relu-interactives/spatial-ecs";
export class Hello extends Behaviour {
onTargetFound(_target: WorldEntityView) {
console.log("There you are");
}
onTargetLost(_target: WorldEntityView) {
console.log("Gone");
}
}onTargetFound runs each time a face comes into view, so something that plays once should keep track of that itself.

