Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add body tracking sample #178

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions proposals/body-tracking.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=no'>
<meta name='mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-capable' content='yes'>

<meta http-equiv="origin-trial" content="Ahfj+MLeL6bh+LNmpnSdepftxoDHHwjUG2KWZ4jjCb1WoZxtBlzF3cDHuJNVqnhr3HXJwQ+kLaw57NO15S0mRwwAAABkeyJvcmlnaW4iOiJodHRwczovL2ltbWVyc2l2ZS13ZWIuZ2l0aHViLmlvOjQ0MyIsImZlYXR1cmUiOiJXZWJYUlBsYW5lRGV0ZWN0aW9uIiwiZXhwaXJ5IjoxNjI5ODQ5NTk5fQ==">

<title>AR Plane Detection</title>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update title


<link href='../css/common.css' rel='stylesheet'></link>

</head>
<body>
<header>
<details open>
<summary>Simple body tracking</summary>
This sample demonstrates using the WebXR body tracking API
to show your body's joints.
<p>

<a class="back" href="./index.html">Back</a>
</p>
</details>
</header>

<script type="module">
// Code adapted from three.js' WebXR hit test sample.
// three.js is covered by MIT license which can be found at:
// https://github.com/mrdoob/three.js/blob/master/LICENSE


import * as THREE from 'https://unpkg.com/three@0.161.0/build/three.module.js';
import {WebXRButton} from '../js/util/webxr-button.js';
import {hitTest, filterHitTestResults} from '../js/hit-test.js';

let xrButton = null;
let camera, scene, renderer;
let room, spheres;
const scalehand = new THREE.Matrix4().makeScale(3, 3, 3);

init();

function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x505050 );

camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 50 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is kind of all over the place in this sample. Seems like you've probably got your editor set to tabs and these files use spaces? It'd be appreciated if you could normalize it.


const light = new THREE.HemisphereLight(0xffffff, 0xbbbbff, 1);
light.position.set(0.5, 1, 0.25);
scene.add(light);

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( render );
renderer.xr.enabled = true;
renderer.autoClear = false;
document.body.appendChild( renderer.domElement );

xrButton = new WebXRButton({
onRequestSession: onRequestSession,
onEndSession: onEndSession,
textEnterXRTitle: "START AR",
textXRNotFoundTitle: "AR NOT FOUND",
textExitXRTitle: "EXIT AR",
});

document.querySelector('header').appendChild(xrButton.domElement);

if (navigator.xr) {
navigator.xr.isSessionSupported('immersive-ar')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this functionality only available in AR on Quest? If not, is there any way can we make this demo work in either AR or VR?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@toji I am very curious about this too.

@cabanier Are you using some sort of polyfill? Or the API is giving you that information?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@toji I am very curious about this too.

no, it can also be used in VR

@cabanier Are you using some sort of polyfill? Or the API is giving you that information?

What do you mean? a polyfill for???

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cabanier Sorry, let me rephrase. Is the WebXR implementation in the browser providing you all the information for the body tracking? Using the body-tracking feature? Or you are generating this information somewhere else and injecting in the browser? I am asking because body-tracking is not officially described/supported by the WebXR specification.

I am just very curious to understand where the bones positions/rotations comes from.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cabanier Sorry, let me rephrase. Is the WebXR implementation in the browser providing you all the information for the body tracking? Using the body-tracking feature? Or you are generating this information somewhere else and injecting in the browser?

It's a new extension to WebXR that I'm proposing: https://cabanier.github.io/webxr-body-tracking/
It uses Meta's body tracking OpenXR implementation under the hood.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cabanier Thanks for explaining. Nice work, I hope it's accepted! 🤞

.then((supported) => {
xrButton.enabled = supported;
});
}

window.addEventListener( 'resize', onWindowResize );

}

function onRequestSession() {
let sessionInit = {
requiredFeatures: ['body-tracking'],
optionalFeatures: ['local-floor', 'bounded-floor'],
};
navigator.xr.requestSession('immersive-ar', sessionInit).then((session) => {
session.mode = 'immersive-ar';
xrButton.setSession(session);
onSessionStarted(session);
});
}

function onSessionStarted(session) {
session.addEventListener('end', onSessionEnded);

renderer.xr.setSession(session);

renderer.setAnimationLoop(render);
}

function onEndSession(session) {
session.end();
}

function onSessionEnded(event) {
xrButton.setSession(null);

renderer.setAnimationLoop(null);
}

function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize(window.innerWidth, window.innerHeight);
}


function render(timestamp, frame) {
if (frame) {

if ( frame.body ) {

let body = frame.body;

if (spheres == undefined) {

const geometry = new THREE.IcosahedronGeometry( 0.01, 3 );
const material = new THREE.MeshLambertMaterial();

spheres = new THREE.InstancedMesh( geometry, material, body.size );
spheres.translateZ( -1 ).setRotationFromMatrix (new THREE.Matrix4().makeRotationY(Math.PI));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not clear on how this works, and it's probably worth a comment for developers sake.

My guess is that this is a transform applied to the entire collection of instances spheres as a sort of root transform? If so, how does this result in the behavior in the video you linked, which appears to mirror your movements rather than show them rotated 180? (Although to be fair you never really moved your hands one at a time, so it's hard to tell. Full body movement suggests a mirror instead of a rotation, though.)

In any case, more comments throughout would be welcome!

spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
scene.add( spheres );

const color = new THREE.Color();

for ( let i = 0; i < spheres.count; i ++ ) {

spheres.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );

}

spheres.instanceMatrix.needsUpdate = true;

}

let i = 0;
const matrix = new THREE.Matrix4();
let space = renderer.xr.getReferenceSpace();
body.forEach(part => {

const pose = frame.getPose(part, space);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to encourage people to use fillPoses() here? Or does that introduce too many complications for an otherwise simple sample.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah. I forgot about that call :-)
I will update the code

const position = pose.transform.position;

if (!part.jointName.includes("hand")) {
matrix.copy(scalehand);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do hands need to be scaled up 3x? In your video it looks like the opposite, and the hands are scaled down?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, wait, I see. This is checking to ensure that "hand" isn't part of the name. That's pretty confusing. Can you invert the logic here?

} else {
matrix.identity();
}

matrix.setPosition( -position.x, position.y, position.z );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and I see that the positions are being inverted in X here too? Is THAT what causes the mirroring effect?

spheres.setMatrixAt( i++, matrix );

});

spheres.instanceMatrix.needsUpdate = true;

}

renderer.render(scene, camera);
}
}

</script>
</body>
</html>
3 changes: 3 additions & 0 deletions proposals/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ <h2 class='tagline'>Proposals</h2>
description: 'Demonstrates use of the mesh detection API in an immersive-ar session. ' +
'Implements JavaScript-level hit-test on the meshes and leverages the Anchors API.' },

{ title: 'Body tracking', category: 'AR',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my question above, doesn't seem like this is an AR-only feature? So I'd recommend using a category other than "AR" here.

Go ahead and make up something that feels appropriate. You can see from the Test Pages tab that we don't exactly stick to a rigid system here. I just don't want to give the impression that it's limited to a given session type unless it actually is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, just like hands this will also work in VR. I'll code something up

path: 'body-tracking.html',
description: 'Demonstrates use of the body tracking in an immersive-ar session. ' },
];

let mainElement = document.getElementById("main");
Expand Down