Skip to content

Commit 56f492f

Browse files
committed
Changed the way controllers data is sent from JS to C#
1 parent 0f38328 commit 56f492f

File tree

3 files changed

+73
-166
lines changed

3 files changed

+73
-166
lines changed

Packages/webxr/Runtime/Plugins/WebGL/webxr.jslib

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ mergeInto(LibraryManager.library, {
1212
Module.WebXR.onUnityLoaded({detail: {state: 'Ready', module: Module}});
1313
},
1414

15-
InitControllersArray: function(byteOffset, length) {
16-
Module.ControllersArrayOffset = byteOffset;
17-
Module.ControllersArrayLength= length;
18-
Module.ControllersArray = new Float32Array(buffer, byteOffset, length);
15+
InitControllersArray: function(byteOffset) {
16+
Module.ControllersArrayOffset = byteOffset / 4;
1917
},
2018

2119
InitHandsArray: function(byteOffset) {

Packages/webxr/Runtime/Plugins/WebGL/webxr.jspre

Lines changed: 67 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -73,38 +73,10 @@ setTimeout(function () {
7373
}
7474

7575
function XRControllerData() {
76-
this.frame = 0;
77-
// TODO: set enabled 0 if controller was enable and then disable
78-
this.enabled = 0;
79-
this.hand = 0;
80-
this.positionX = 0;
81-
this.positionY = 0;
82-
this.positionZ = 0;
83-
this.rotationX = 0;
84-
this.rotationY = 0;
85-
this.rotationZ = 0;
86-
this.rotationW = 0;
87-
this.gripPositionX = 0;
88-
this.gripPositionY = 0;
89-
this.gripPositionZ = 0;
90-
this.gripRotationX = 0;
91-
this.gripRotationY = 0;
92-
this.gripRotationZ = 0;
93-
this.gripRotationW = 0;
94-
this.trigger = 0;
95-
this.squeeze = 0;
96-
this.thumbstick = 0;
97-
this.thumbstickX = 0;
98-
this.thumbstickY = 0;
99-
this.touchpad = 0;
100-
this.touchpadX = 0;
101-
this.touchpadY = 0;
102-
this.buttonA = 0;
103-
this.buttonB = 0;
76+
this.bufferIndex = 0;
10477
this.gamepad = null;
10578
this.profiles = [];
10679
this.updatedProfiles = 0;
107-
this.updatedGrip = 0;
10880
}
10981

11082
function XRHandData() {
@@ -327,25 +299,17 @@ setTimeout(function () {
327299
}
328300

329301
this.removeRemainingTouches();
330-
331-
this.xrData.controllerA.enabled = 0;
332-
this.xrData.controllerB.enabled = 0;
333-
334-
this.xrData.controllerA.frame = -1;
335-
this.xrData.controllerB.frame = -1;
336302

337-
this.xrData.handLeft.bufferIndex = Module.HandsArrayOffset;
338-
this.xrData.handRight.bufferIndex = Module.HandsArrayOffset + 205;
303+
Module.HEAPF32[this.xrData.controllerA.bufferIndex] = -1; // XRControllerData.frame
304+
Module.HEAPF32[this.xrData.controllerB.bufferIndex] = -1; // XRControllerData.frame
305+
Module.HEAPF32[this.xrData.controllerA.bufferIndex + 1] = 0; // XRControllerData.enabled
306+
Module.HEAPF32[this.xrData.controllerB.bufferIndex + 1] = 0; // XRControllerData.enabled
307+
339308
Module.HEAPF32[this.xrData.handLeft.bufferIndex] = -1; // XRHandData.frame
340309
Module.HEAPF32[this.xrData.handRight.bufferIndex] = -1; // XRHandData.frame
341310
Module.HEAPF32[this.xrData.handLeft.bufferIndex + 1] = 0; // XRHandData.enabled
342311
Module.HEAPF32[this.xrData.handRight.bufferIndex + 1] = 0; // XRHandData.enabled
343-
344-
this.updateUnityXRControllersData({
345-
controllerA: this.xrData.controllerA,
346-
controllerB: this.xrData.controllerB
347-
});
348-
312+
349313
this.gameModule.WebXR.OnEndXR();
350314
this.didNotifyUnity = false;
351315
this.canvas.width = this.canvas.parentElement.clientWidth * this.gameModule.asmLibraryArg._JS_SystemInfo_GetPreferredDevicePixelRatio();
@@ -384,40 +348,36 @@ setTimeout(function () {
384348
hand = 2;
385349
}
386350

387-
controller.enabled = 1;
388-
controller.hand = hand;
351+
Module.HEAPF32[controller.bufferIndex + 1] = 1; // XRControllerData.enabled
352+
Module.HEAPF32[controller.bufferIndex + 2] = hand; // XRControllerData.hand
389353

390354
switch (xrInputSourceEvent.type) {
391355
case "select":
392-
controller.trigger = 1;
356+
Module.HEAPF32[controller.bufferIndex + 10] = 1; // XRControllerData.trigger
393357
break;
394358
case "selectstart":
395-
controller.trigger = 1;
359+
Module.HEAPF32[controller.bufferIndex + 10] = 1; // XRControllerData.trigger
396360
break;
397361
case "selectend":
398-
controller.trigger = 0;
362+
Module.HEAPF32[controller.bufferIndex + 10] = 0; // XRControllerData.trigger
399363
break;
400364
case "squeeze":
401-
controller.squeeze = 1;
365+
Module.HEAPF32[controller.bufferIndex + 11] = 1; // XRControllerData.squeeze
402366
break;
403367
case "squeezestart":
404-
controller.squeeze = 1;
368+
Module.HEAPF32[controller.bufferIndex + 11] = 1; // XRControllerData.squeeze
405369
break;
406370
case "squeezeend":
407-
controller.squeeze = 0;
371+
Module.HEAPF32[controller.bufferIndex + 11] = 0; // XRControllerData.squeeze
408372
break;
409373
}
410374

411375
if (hand == 0 || hand == 2) {
412-
xrData.controllerA = controller;
413-
xrData.handRight.bufferIndex = Module.HandsArrayOffset + 205;
414-
Module.HEAPF32[xrData.handRight.bufferIndex + 3] = controller.trigger; // XRHandData.trigger
415-
Module.HEAPF32[xrData.handRight.bufferIndex + 4] = controller.squeeze; // XRHandData.squeeze
376+
Module.HEAPF32[xrData.handRight.bufferIndex + 3] = Module.HEAPF32[controller.bufferIndex + 10]; // XRHandData.trigger
377+
Module.HEAPF32[xrData.handRight.bufferIndex + 4] = Module.HEAPF32[controller.bufferIndex + 11]; // XRHandData.squeeze
416378
} else {
417-
xrData.controllerB = controller;
418-
xrData.handLeft.bufferIndex = Module.HandsArrayOffset;
419-
Module.HEAPF32[xrData.handLeft.bufferIndex + 3] = controller.trigger; // XRHandData.trigger
420-
Module.HEAPF32[xrData.handLeft.bufferIndex + 4] = controller.squeeze; // XRHandData.squeeze
379+
Module.HEAPF32[xrData.handLeft.bufferIndex + 3] = Module.HEAPF32[controller.bufferIndex + 10]; // XRHandData.trigger
380+
Module.HEAPF32[xrData.handLeft.bufferIndex + 4] = Module.HEAPF32[controller.bufferIndex + 11]; // XRHandData.squeeze
421381
}
422382
} else {
423383
var xPercentage = 0.5;
@@ -588,16 +548,14 @@ setTimeout(function () {
588548
}
589549

590550
XRManager.prototype.getXRControllersData = function(frame, inputSources, refSpace, xrData) {
591-
xrData.handLeft.bufferIndex = Module.HandsArrayOffset;
592-
xrData.handRight.bufferIndex = Module.HandsArrayOffset + 205;
593551
Module.HEAPF32[xrData.handLeft.bufferIndex] = xrData.frameNumber; // XRHandData.frame
594552
Module.HEAPF32[xrData.handRight.bufferIndex] = xrData.frameNumber; // XRHandData.frame
595553
Module.HEAPF32[xrData.handLeft.bufferIndex + 1] = 0; // XRHandData.enabled
596554
Module.HEAPF32[xrData.handRight.bufferIndex + 1] = 0; // XRHandData.enabled
597-
xrData.controllerA.enabled = 0;
598-
xrData.controllerB.enabled = 0;
599-
xrData.controllerA.frame = xrData.frameNumber;
600-
xrData.controllerB.frame = xrData.frameNumber;
555+
Module.HEAPF32[xrData.controllerA.bufferIndex] = xrData.frameNumber; // XRControllerData.frame
556+
Module.HEAPF32[xrData.controllerB.bufferIndex] = xrData.frameNumber; // XRControllerData.frame
557+
Module.HEAPF32[xrData.controllerA.bufferIndex + 1] = 0; // XRControllerData.enabled
558+
Module.HEAPF32[xrData.controllerB.bufferIndex + 1] = 0; // XRControllerData.enabled
601559
if (!inputSources || !inputSources.length || inputSources.length == 0) {
602560
this.removeRemainingTouches();
603561
return;
@@ -666,105 +624,99 @@ setTimeout(function () {
666624
hand = 2;
667625
}
668626

669-
controller.enabled = 1;
670-
controller.hand = hand;
627+
Module.HEAPF32[controller.bufferIndex + 1] = 1; // XRControllerData.enabled
628+
Module.HEAPF32[controller.bufferIndex + 2] = hand; // XRControllerData.hand
671629

672630
if (controller.updatedProfiles == 0) {
673631
controller.profiles = inputSource.profiles;
674632
controller.updatedProfiles = 1;
675633
}
676634

677-
controller.positionX = position.x;
678-
controller.positionY = position.y;
679-
controller.positionZ = -position.z;
635+
Module.HEAPF32[controller.bufferIndex + 3] = position.x; // XRControllerData.positionX
636+
Module.HEAPF32[controller.bufferIndex + 4] = position.y; // XRControllerData.positionY
637+
Module.HEAPF32[controller.bufferIndex + 5] = -position.z; // XRControllerData.positionZ
680638

681-
controller.rotationX = -orientation.x;
682-
controller.rotationY = -orientation.y;
683-
controller.rotationZ = orientation.z;
684-
controller.rotationW = orientation.w;
639+
Module.HEAPF32[controller.bufferIndex + 6] = -orientation.x; // XRControllerData.rotationX
640+
Module.HEAPF32[controller.bufferIndex + 7] = -orientation.y; // XRControllerData.rotationY
641+
Module.HEAPF32[controller.bufferIndex + 8] = orientation.z; // XRControllerData.rotationZ
642+
Module.HEAPF32[controller.bufferIndex + 9] = orientation.w; // XRControllerData.rotationW
685643

686-
if (controller.updatedGrip == 0) {
644+
if (Module.HEAPF32[controller.bufferIndex + 20] == 0 && inputSource.gripSpace) { // XRControllerData.updatedGrip
687645
var inputPose = frame.getPose(inputSource.gripSpace, refSpace);
688646
if (inputPose) {
689647
var gripPosition = inputPose.transform.position;
690648
var gripOrientation = inputPose.transform.orientation;
691649

692-
controller.gripPositionX = gripPosition.x;
693-
controller.gripPositionY = gripPosition.y;
694-
controller.gripPositionZ = - gripPosition.z;
650+
Module.HEAPF32[controller.bufferIndex + 21] = gripPosition.x; // XRControllerData.gripPositionX
651+
Module.HEAPF32[controller.bufferIndex + 22] = gripPosition.y; // XRControllerData.gripPositionY
652+
Module.HEAPF32[controller.bufferIndex + 23] = -gripPosition.z; // XRControllerData.gripPositionZ
695653

696-
controller.gripRotationX = -gripOrientation.x;
697-
controller.gripRotationY = -gripOrientation.y;
698-
controller.gripRotationZ = gripOrientation.z;
699-
controller.gripRotationW = gripOrientation.w;
654+
Module.HEAPF32[controller.bufferIndex + 24] = -gripOrientation.x; // XRControllerData.gripRotationX
655+
Module.HEAPF32[controller.bufferIndex + 25] = -gripOrientation.y; // XRControllerData.gripRotationY
656+
Module.HEAPF32[controller.bufferIndex + 26] = gripOrientation.z; // XRControllerData.gripRotationZ
657+
Module.HEAPF32[controller.bufferIndex + 27] = gripOrientation.w; // XRControllerData.gripRotationW
700658

701-
controller.updatedGrip = 1;
659+
Module.HEAPF32[controller.bufferIndex + 20] = 1; // XRControllerData.updatedGrip
702660
}
703-
} else if (controller.updatedGrip == 1) {
704-
controller.updatedGrip = 2;
705661
}
706662

663+
var triggerIndex = controller.bufferIndex + 10;
664+
var squeezeIndex = controller.bufferIndex + 11;
707665
// if there's gamepad, use the xr-standard mapping
708666
if (inputSource.gamepad) {
709667
for (var j = 0; j < inputSource.gamepad.buttons.length; j++) {
710668
switch (j) {
711669
case 0:
712-
controller.trigger = inputSource.gamepad.buttons[j].value;
670+
Module.HEAPF32[triggerIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.trigger
713671
break;
714672
case 1:
715-
controller.squeeze = inputSource.gamepad.buttons[j].value;
673+
Module.HEAPF32[squeezeIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.squeeze
716674
break;
717675
case 2:
718-
controller.touchpad = inputSource.gamepad.buttons[j].value;
676+
Module.HEAPF32[controller.bufferIndex + 15] = inputSource.gamepad.buttons[j].value; // XRControllerData.touchpad
719677
break;
720678
case 3:
721-
controller.thumbstick = inputSource.gamepad.buttons[j].value;
679+
Module.HEAPF32[controller.bufferIndex + 12] = inputSource.gamepad.buttons[j].value; // XRControllerData.thumbstick
722680
break;
723681
case 4:
724-
controller.buttonA = inputSource.gamepad.buttons[j].value;
682+
Module.HEAPF32[controller.bufferIndex + 18] = inputSource.gamepad.buttons[j].value; // XRControllerData.buttonA
725683
break;
726684
case 5:
727-
controller.buttonB = inputSource.gamepad.buttons[j].value;
685+
Module.HEAPF32[controller.bufferIndex + 19] = inputSource.gamepad.buttons[j].value; // XRControllerData.buttonB
728686
break;
729687
}
730688
}
731689

732-
if (controller.trigger <= 0.02) {
733-
controller.trigger = 0;
734-
} else if (controller.trigger >= 0.98) {
735-
controller.trigger = 1;
690+
if (Module.HEAPF32[triggerIndex] <= 0.02) {
691+
Module.HEAPF32[triggerIndex] = 0;
692+
} else if (Module.HEAPF32[triggerIndex] >= 0.98) {
693+
Module.HEAPF32[triggerIndex] = 1;
736694
}
737695

738-
if (controller.squeeze <= 0.02) {
739-
controller.squeeze = 0;
740-
} else if (controller.squeeze >= 0.98) {
741-
controller.squeeze = 1;
696+
if (Module.HEAPF32[squeezeIndex] <= 0.02) {
697+
Module.HEAPF32[squeezeIndex] = 0;
698+
} else if (Module.HEAPF32[squeezeIndex] >= 0.98) {
699+
Module.HEAPF32[squeezeIndex] = 1;
742700
}
743701

744702
for (var j = 0; j < inputSource.gamepad.axes.length; j++) {
745703
switch (j) {
746704
case 0:
747-
controller.touchpadX = inputSource.gamepad.axes[j];
705+
Module.HEAPF32[controller.bufferIndex + 16] = inputSource.gamepad.axes[j]; // XRControllerData.touchpadX
748706
break;
749707
case 1:
750-
controller.touchpadY = -inputSource.gamepad.axes[j];
708+
Module.HEAPF32[controller.bufferIndex + 17] = -inputSource.gamepad.axes[j]; // XRControllerData.touchpadY
751709
break;
752710
case 2:
753-
controller.thumbstickX = inputSource.gamepad.axes[j];
711+
Module.HEAPF32[controller.bufferIndex + 13] = inputSource.gamepad.axes[j]; // XRControllerData.thumbstickX
754712
break;
755713
case 3:
756-
controller.thumbstickY = -inputSource.gamepad.axes[j];
714+
Module.HEAPF32[controller.bufferIndex + 14] = -inputSource.gamepad.axes[j]; // XRControllerData.thumbstickY
757715
break;
758716
}
759717
}
760718
}
761719
controller.gamepad = inputSource.gamepad;
762-
763-
if (hand == 0 || hand == 2) {
764-
xrData.controllerA = controller;
765-
} else {
766-
xrData.controllerB = controller;
767-
}
768720
}
769721
} else if (inputSource.xrTouchObject && !inputSource.xrTouchObject.ended && inputSource.gamepad && inputSource.gamepad.axes) {
770722
inputSource.xrTouchObject.UpdateTouch( this.canvas,
@@ -787,8 +739,6 @@ setTimeout(function () {
787739
var glLayer = new XRWebGLLayer(session, this.ctx);
788740
session.updateRenderState({ baseLayer: glLayer });
789741

790-
791-
792742
var refSpaceType = 'viewer';
793743
if (session.isImmersive) {
794744
refSpaceType = this.gameModule.WebXR.Settings.VRRequiredReferenceSpace[0];
@@ -810,12 +760,16 @@ setTimeout(function () {
810760
session.addEventListener('squeezeend', this.onInputEvent);
811761
session.addEventListener('visibilitychange', this.onSessionVisibilityEvent);
812762

763+
this.xrData.controllerA.bufferIndex = Module.ControllersArrayOffset;
764+
this.xrData.controllerB.bufferIndex = Module.ControllersArrayOffset + 28;
765+
this.xrData.handLeft.bufferIndex = Module.HandsArrayOffset;
766+
this.xrData.handRight.bufferIndex = Module.HandsArrayOffset + 205;
813767
this.xrData.controllerA.updatedProfiles = 0;
814768
this.xrData.controllerB.updatedProfiles = 0;
815769
this.xrData.controllerA.profiles = [];
816770
this.xrData.controllerB.profiles = [];
817-
this.xrData.controllerA.updatedGrip = 0;
818-
this.xrData.controllerB.updatedGrip = 0;
771+
Module.HEAPF32[this.xrData.controllerA.bufferIndex + 20] = 0; // XRControllerData.updatedGrip
772+
Module.HEAPF32[this.xrData.controllerB.bufferIndex + 20] = 0; // XRControllerData.updatedGrip
819773
}
820774
var thisXRMananger = this;
821775
session.requestReferenceSpace(refSpaceType).then(function (refSpace) {
@@ -944,11 +898,6 @@ setTimeout(function () {
944898
leftViewPosition: xrData.leftViewPosition,
945899
rightViewPosition: xrData.rightViewPosition
946900
});
947-
948-
this.updateUnityXRControllersData({
949-
controllerA: xrData.controllerA,
950-
controllerB: xrData.controllerB
951-
});
952901

953902
if (!this.didNotifyUnity)
954903
{
@@ -1012,47 +961,6 @@ setTimeout(function () {
1012961
});
1013962
}
1014963

1015-
XRManager.prototype.updateUnityXRControllersData = function (data) {
1016-
var index = 0;
1017-
if (Module.ControllersArray.byteLength == 0) {
1018-
Module.ControllersArray = new Float32Array(buffer, Module.ControllersArrayOffset, Module.ControllersArrayLength);
1019-
}
1020-
Object.keys(data).forEach(function (key, i) {
1021-
Module.ControllersArray[index++] = data[key].frame;
1022-
Module.ControllersArray[index++] = data[key].enabled;
1023-
Module.ControllersArray[index++] = data[key].hand;
1024-
Module.ControllersArray[index++] = data[key].positionX;
1025-
Module.ControllersArray[index++] = data[key].positionY;
1026-
Module.ControllersArray[index++] = data[key].positionZ;
1027-
Module.ControllersArray[index++] = data[key].rotationX;
1028-
Module.ControllersArray[index++] = data[key].rotationY;
1029-
Module.ControllersArray[index++] = data[key].rotationZ;
1030-
Module.ControllersArray[index++] = data[key].rotationW;
1031-
Module.ControllersArray[index++] = data[key].trigger;
1032-
Module.ControllersArray[index++] = data[key].squeeze;
1033-
Module.ControllersArray[index++] = data[key].thumbstick;
1034-
Module.ControllersArray[index++] = data[key].thumbstickX;
1035-
Module.ControllersArray[index++] = data[key].thumbstickY;
1036-
Module.ControllersArray[index++] = data[key].touchpad;
1037-
Module.ControllersArray[index++] = data[key].touchpadX;
1038-
Module.ControllersArray[index++] = data[key].touchpadY;
1039-
Module.ControllersArray[index++] = data[key].buttonA;
1040-
Module.ControllersArray[index++] = data[key].buttonB;
1041-
Module.ControllersArray[index++] = data[key].updatedGrip;
1042-
if (data[key].updatedGrip == 1) {
1043-
Module.ControllersArray[index++] = data[key].gripPositionX;
1044-
Module.ControllersArray[index++] = data[key].gripPositionY;
1045-
Module.ControllersArray[index++] = data[key].gripPositionZ;
1046-
Module.ControllersArray[index++] = data[key].gripRotationX;
1047-
Module.ControllersArray[index++] = data[key].gripRotationY;
1048-
Module.ControllersArray[index++] = data[key].gripRotationZ;
1049-
Module.ControllersArray[index++] = data[key].gripRotationW;
1050-
} else {
1051-
index += 7;
1052-
}
1053-
});
1054-
}
1055-
1056964
XRManager.prototype.updateUnityXRViewerHitTestPose = function (data) {
1057965
var index = 0;
1058966
if (Module.ViewerHitTestPoseArray.byteLength == 0) {

0 commit comments

Comments
 (0)