Skip to content

Commit 2441201

Browse files
committed
Ooops, forgot the FullView template
1 parent 106a1ed commit 2441201

File tree

1 file changed

+79
-3
lines changed
  • Assets/WebGLTemplates/WebXRFullView

1 file changed

+79
-3
lines changed

Assets/WebGLTemplates/WebXRFullView/webxr.js

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
this.gamepads = [];
1111
this.controllerA = new XRControllerData();
1212
this.controllerB = new XRControllerData();
13+
this.handLeft = new XRHandData();
14+
this.handRight = new XRHandData();
15+
this.frameNumber = 0;
1316
this.xrData = null;
1417
}
1518

1619
function XRControllerData() {
20+
this.frame = 0;
1721
// TODO: set enabled 0 if controller was enable and then disable
1822
this.enabled = 0;
1923
this.hand = 0;
@@ -36,6 +40,26 @@
3640
this.buttonB = 0;
3741
}
3842

43+
function XRHandData() {
44+
this.frame = 0;
45+
// TODO: set enabled 0 if hand was enable and then disable
46+
this.enabled = 0;
47+
this.hand = 0;
48+
this.trigger = 0;
49+
this.squeeze = 0;
50+
this.joints = [];
51+
for (let i = 0; i < 25; i++) {
52+
this.joints.push(new XRJointData());
53+
}
54+
}
55+
56+
function XRJointData() {
57+
this.enabled = 0;
58+
this.position = [0, 0, 0];
59+
this.rotation = [0, 0, 0, 1];
60+
this.radius = 0;
61+
}
62+
3963
function XRManager() {
4064
this.arSession = null;
4165
this.vrSession = null;
@@ -111,7 +135,7 @@
111135
window.requestAnimationFrame( tempRender );
112136
navigator.xr.requestSession('immersive-ar', {
113137
requiredFeatures: ['local-floor'], // TODO: Get this value from Unity
114-
optionalFeatures: ['dom-overlay'],
138+
optionalFeatures: ['dom-overlay', 'hand-tracking'],
115139
domOverlay: {root: this.canvas.parentElement}
116140
}).then(async (session) => {
117141
this.waitingHandheldARHack = false;
@@ -128,7 +152,8 @@
128152
XRManager.prototype.onRequestVRSession = function () {
129153
if (!this.isVRSupported) return;
130154
navigator.xr.requestSession('immersive-vr', {
131-
requiredFeatures: ['local-floor'] // TODO: Get this value from Unity
155+
requiredFeatures: ['local-floor'], // TODO: Get this value from Unity
156+
optionalFeatures: ['hand-tracking']
132157
}).then(async (session) => {
133158
session.isImmersive = true;
134159
session.isInSession = true;
@@ -212,8 +237,12 @@
212237

213238
if (hand == 0 || hand == 2) {
214239
xrData.controllerA = controller;
240+
xrData.handRight.trigger = controller.trigger;
241+
xrData.handRight.squeeze = controller.squeeze;
215242
} else {
216243
xrData.controllerB = controller;
244+
xrData.handLeft.trigger = controller.trigger;
245+
xrData.handLeft.squeeze = controller.squeeze;
217246
}
218247
}
219248
}
@@ -336,13 +365,49 @@
336365
}
337366

338367
XRManager.prototype.getXRControllersData = function(frame, inputSources, refSpace, xrData) {
368+
xrData.handLeft.enabled = 0;
369+
xrData.handRight.enabled = 0;
370+
xrData.controllerA.enabled = 0;
371+
xrData.controllerB.enabled = 0;
372+
xrData.handLeft.frame = xrData.frameNumber;
373+
xrData.handRight.frame = xrData.frameNumber;
374+
xrData.controllerA.frame = xrData.frameNumber;
375+
xrData.controllerB.frame = xrData.frameNumber;
339376
if (!inputSources || !inputSources.length) {
340377
return;
341378
}
342379
for (var i = 0; i < inputSources.length; i++) {
343380
let inputSource = inputSources[i];
344381
// Show the input source if it has a grip space
345-
if (inputSource.gripSpace) {
382+
if (inputSource.hand) {
383+
var hand = 1;
384+
var xrHand = xrData.handLeft;
385+
if (inputSource.handedness == 'right') {
386+
hand = 2;
387+
xrHand = xrData.handRight;
388+
}
389+
xrHand.enabled = 1;
390+
xrHand.hand = hand;
391+
for (let j = 0; j < 25; j++) {
392+
let joint = null;
393+
if (inputSource.hand[j] !== null) {
394+
joint = frame.getJointPose(inputSource.hand[j], refSpace);
395+
}
396+
if (joint !== null) {
397+
xrHand.joints[j].enabled = 1;
398+
xrHand.joints[j].position[0] = joint.transform.position.x;
399+
xrHand.joints[j].position[1] = joint.transform.position.y;
400+
xrHand.joints[j].position[2] = -joint.transform.position.z;
401+
xrHand.joints[j].rotation[0] = -joint.transform.orientation.x;
402+
xrHand.joints[j].rotation[1] = -joint.transform.orientation.y;
403+
xrHand.joints[j].rotation[2] = joint.transform.orientation.z;
404+
xrHand.joints[j].rotation[3] = joint.transform.orientation.w;
405+
if (joint.radius !== null) {
406+
xrHand.joints[j].radius = joint.radius;
407+
}
408+
}
409+
}
410+
} else if (inputSource.gripSpace) {
346411
let inputPose = frame.getPose(inputSource.gripSpace, refSpace);
347412
if (inputPose) {
348413
var position = inputPose.transform.position;
@@ -521,6 +586,7 @@
521586
}
522587

523588
var xrData = this.xrData;
589+
xrData.frameNumber++;
524590

525591
for (let view of pose.views) {
526592
if (view.eye === 'left') {
@@ -544,6 +610,16 @@
544610
controllerA: xrData.controllerA,
545611
controllerB: xrData.controllerB
546612
}}));
613+
614+
document.dispatchEvent(new CustomEvent('XRControllersData', { detail: {
615+
controllerA: xrData.controllerA,
616+
controllerB: xrData.controllerB
617+
}}));
618+
619+
document.dispatchEvent(new CustomEvent('XRHandsData', { detail: {
620+
handLeft: xrData.handLeft,
621+
handRight: xrData.handRight
622+
}}));
547623

548624
if (!this.didNotifyUnity)
549625
{

0 commit comments

Comments
 (0)