|
10 | 10 | this.gamepads = [];
|
11 | 11 | this.controllerA = new XRControllerData();
|
12 | 12 | this.controllerB = new XRControllerData();
|
| 13 | + this.handLeft = new XRHandData(); |
| 14 | + this.handRight = new XRHandData(); |
| 15 | + this.frameNumber = 0; |
13 | 16 | this.xrData = null;
|
14 | 17 | }
|
15 | 18 |
|
16 | 19 | function XRControllerData() {
|
| 20 | + this.frame = 0; |
17 | 21 | // TODO: set enabled 0 if controller was enable and then disable
|
18 | 22 | this.enabled = 0;
|
19 | 23 | this.hand = 0;
|
|
36 | 40 | this.buttonB = 0;
|
37 | 41 | }
|
38 | 42 |
|
| 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 | + |
39 | 63 | function XRManager() {
|
40 | 64 | this.arSession = null;
|
41 | 65 | this.vrSession = null;
|
|
111 | 135 | window.requestAnimationFrame( tempRender );
|
112 | 136 | navigator.xr.requestSession('immersive-ar', {
|
113 | 137 | requiredFeatures: ['local-floor'], // TODO: Get this value from Unity
|
114 |
| - optionalFeatures: ['dom-overlay'], |
| 138 | + optionalFeatures: ['dom-overlay', 'hand-tracking'], |
115 | 139 | domOverlay: {root: this.canvas.parentElement}
|
116 | 140 | }).then(async (session) => {
|
117 | 141 | this.waitingHandheldARHack = false;
|
|
128 | 152 | XRManager.prototype.onRequestVRSession = function () {
|
129 | 153 | if (!this.isVRSupported) return;
|
130 | 154 | 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'] |
132 | 157 | }).then(async (session) => {
|
133 | 158 | session.isImmersive = true;
|
134 | 159 | session.isInSession = true;
|
|
212 | 237 |
|
213 | 238 | if (hand == 0 || hand == 2) {
|
214 | 239 | xrData.controllerA = controller;
|
| 240 | + xrData.handRight.trigger = controller.trigger; |
| 241 | + xrData.handRight.squeeze = controller.squeeze; |
215 | 242 | } else {
|
216 | 243 | xrData.controllerB = controller;
|
| 244 | + xrData.handLeft.trigger = controller.trigger; |
| 245 | + xrData.handLeft.squeeze = controller.squeeze; |
217 | 246 | }
|
218 | 247 | }
|
219 | 248 | }
|
|
336 | 365 | }
|
337 | 366 |
|
338 | 367 | 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; |
339 | 376 | if (!inputSources || !inputSources.length) {
|
340 | 377 | return;
|
341 | 378 | }
|
342 | 379 | for (var i = 0; i < inputSources.length; i++) {
|
343 | 380 | let inputSource = inputSources[i];
|
344 | 381 | // 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) { |
346 | 411 | let inputPose = frame.getPose(inputSource.gripSpace, refSpace);
|
347 | 412 | if (inputPose) {
|
348 | 413 | var position = inputPose.transform.position;
|
|
521 | 586 | }
|
522 | 587 |
|
523 | 588 | var xrData = this.xrData;
|
| 589 | + xrData.frameNumber++; |
524 | 590 |
|
525 | 591 | for (let view of pose.views) {
|
526 | 592 | if (view.eye === 'left') {
|
|
544 | 610 | controllerA: xrData.controllerA,
|
545 | 611 | controllerB: xrData.controllerB
|
546 | 612 | }}));
|
| 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 | + }})); |
547 | 623 |
|
548 | 624 | if (!this.didNotifyUnity)
|
549 | 625 | {
|
|
0 commit comments