Skip to content

Hand tracking ​

Face-tracking and image-tracking projects can read the player's hands: which are in view, named poses like a fist or a thumbs up, pinches, swipes and waves, and where each hand is, in the picture and in the world. this.hands has all of it, with the same shape as the face's gestures: state to check and events to hear.

this.hands is null in a project without hand tracking, in other kinds of project, and in the editor viewport. Always allow for that.

Turning it on ​

A hand model costs a download the first time someone plays and frame rate while it runs, so a project only has it when it asks. The Environment panel's Hand tracking section decides:

RunWhat happens
Auto (default)On when any script under assets/ mentions .hands, onHandGesture or getHandTracker. A mention in a comment doesn't count
OnAlways on, for a script that reaches hands some other way
OffNever. this.hands is null, and the build says which scripts that leaves without hands

A project with it off ships exactly as it did before hand tracking existed: neither the hand detector nor the code that reads it is in its build. Follow sets 1 or 2 hands, 1 by default: with room for two and one in view, the detector keeps searching the whole frame for the second.

The hand detector loads after the face or image tracker has started, so it never holds that up. Until it's ready, status() is "loading" and no hands are read; if it can't start, status() is "failed", error() says why, and the rest of the experience carries on.

Reading hands ​

ts
import { Behaviour } from "@relu-interactives/spatial-ecs";

export class ThumbsUpToSpin extends Behaviour {
  onUpdate() {
    if (this.hands?.isPose("thumbsUp")) {
      this.transform.rotation.y += 2 * this.deltaTime;
    }
  }
}
MethodWhat it gives
list()Every hand in view
get(side)That hand, or null
first()The hand that's been in view longest, or null
isPose(pose, side?)Whether a hand holds a pose. With no side, either hand
didGesture(name, side?)That gesture fired on this frame
status(), error()Whether the detector is up
stats()How often the detector runs and what each run costs

Each hand:

Property or methodWhat it is
sideThe person's own hand: "left" is their left hand
pictureSideWhich side of the picture it's on
poseThe named pose it holds, or null
pinchHow close thumb and first finger are, 0 apart and 1 touching
landmarksAll 21 points, x and y 0 to 1 across the picture from its top left
metresThe same 21 points in metres, around the hand
getPicturePosition(point?)Where a place on the hand is in the picture, 0 to 1
getScreenPosition(point?)Where it is on screen, in CSS pixels like a pointer event's clientX, clientY
getWorldPosition(point?, target?)Where it is in the world
getWorldRotation(target?)Which way the hand is turned: +Y along the fingers, +Z out of the palm
getWorldSize()The palm's length in world units, for sizing something to the hand
getScreenSize()The palm's length on screen, in CSS pixels
isTouching(point, position, radius?)Whether a world position lands on that place as the viewer sees it. Depth doesn't count. radius is in palm lengths, 0.7 by default

The places are palm (the middle of the palm, the default) and the 21 landmarks: wrist, thumbBase, thumbKnuckle, thumbJoint, thumbTip, then for index, middle, ring and pinky, Knuckle, Joint, Upper and Tip (indexTip, pinkyKnuckle).

Poses ​

PoseThe hand
openPalmAll four fingers out
fistAll four folded, the thumb tucked or out to the side
pointThe first finger out, the rest folded
peaceFirst and middle fingers out, the others folded
pinchThumb and first finger touching, the others not all out
okThumb and first finger touching, the other three out
thumbsUp, thumbsDownA fist with the thumb out, pointing up or down the picture

Anything else, a hand between poses or half closed, is null, which is most of what a camera sees. A pose has to hold for a moment before it counts, so a hand passing through one doesn't fire it. Poses are worked out from the hand's own shape, so they read the same near or far and turned any way, except thumbs up and down, which are about which way is up in the picture.

Gestures ​

ts
import { Behaviour, type HandGestureEvent } from "@relu-interactives/spatial-ecs";

export class PoseSwitch extends Behaviour {
  data = { state: "off" };

  onHandGesture(gesture: HandGestureEvent) {
    if (gesture.name !== "poseStart") return;
    if (gesture.pose === "openPalm") this.data.state = "on";
    if (gesture.pose === "fist") this.data.state = "off";
  }
}
GestureWhen
handFound, handLostA hand comes into view, or leaves
poseStart, poseEndA hand takes a pose, or leaves it. pose says which
pinchStart, pinchEndThumb and first finger touch, or part. Any pose
swipeLeft, swipeRight, swipeUp, swipeDownA quick move across the picture
waveA hand going from side to side, more than once

The event carries name, side, pose and hand (the hand, null on handLost). this.hands.on(name, listener) hears the same, and returns what takes the listener off; "*" hears everything. When a hand leaves, what it was doing ends: a poseStart always gets its poseEnd.

Left and right ​

Two sides mean two different things, and each is named for what it is. hand.side is the person's own hand, on any camera. hand.pictureSide is the side of the picture it's on. Swipes are the picture's: swipeLeft goes left on screen, on every camera, the same way the face's left and right are the picture's. On a selfie, the picture is a mirror, so the player's right hand shows on the right and a swipe to their own left is a swipeLeft.

Where a hand is in the world ​

A camera can't see depth, so a hand's distance is worked out:

  • Face tracking: the world is in centimetres, and a hand's distance comes from how big it looks against how big a hand is. A hand held near the face lands near the face.
  • Image tracking: the world has no real size, so a hand is put at the depth of the tracked image. Something on the image and a hand over it then overlap on screen when they overlap in the world. While no image is tracked, a hand stays at the last image's depth, or close to the camera if none has been found yet.

On screen the position is always where the hand is: isTouching and getScreenPosition don't depend on the depth at all.

Attaching something to a hand ​

attach keeps an entity on a hand: its Transform is written every frame, in its parent's space.

ts
import { Behaviour } from "@relu-interactives/spatial-ecs";

export class ShieldOnPalm extends Behaviour {
  init() {
    this.hands?.attach(this.entityId, { side: "any", point: "palm" });
  }

  dispose() {
    this.hands?.detach(this.entityId);
  }
}
OptionDefaultWhat it does
side"any""left", "right", or the first hand in view
point"palm"Where on the hand
scaleWithHandtrue1 unit is a palm's length
rotateWithHandtrueTurns with the hand
hideWhenLosttrueHidden while the hand is out of view

What it costs ​

The detector reads the same frames the face or image tracker reads, on its own schedule. With no hand in view it looks for one about 6 times a second, which is the expensive part; once it has a hand it follows it about 20 times a second. Whatever the device, it never spends more than a quarter of the time detecting: on a slow phone it reads hands less often rather than taking the frame rate with it, and it never waits more than a second between looks. Between reads, positions ease toward the latest, so something on a hand moves smoothly. stats() reports the real rate and cost.

Measured on a desktop Mac, not a phone: a detection took about 7 ms with no hand in view, 9 ms following one hand, and 15 ms following one hand with room for two. The face tracker's own step was 9 to 12 ms beside it, the same as without. The first time someone plays, a project downloads the hand model, 7.8 MB, and in image tracking also MediaPipe's runtime, 11 MB, which a face project already has. Nobody has run hand tracking on a phone yet: the schedule is what keeps a slow one playable, by reading hands less often.