Skip to content

Commit 0f38328

Browse files
committed
Changed the way hands data is sent from JS to C#
1 parent 88cbd7a commit 0f38328

File tree

3 files changed

+38
-80
lines changed

3 files changed

+38
-80
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ mergeInto(LibraryManager.library, {
1818
Module.ControllersArray = new Float32Array(buffer, byteOffset, length);
1919
},
2020

21-
InitHandsArray: function(byteOffset, length) {
22-
Module.HandsArrayOffset = byteOffset;
23-
Module.HandsArrayLength= length;
24-
Module.HandsArray = new Float32Array(buffer, byteOffset, length);
21+
InitHandsArray: function(byteOffset) {
22+
Module.HandsArrayOffset = byteOffset / 4;
2523
},
2624

2725
InitViewerHitTestPoseArray: function(byteOffset, length) {

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

Lines changed: 34 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,16 @@ setTimeout(function () {
108108
}
109109

110110
function XRHandData() {
111-
this.frame = 0;
112-
// TODO: set enabled 0 if hand was enable and then disable
113-
this.enabled = 0;
114-
this.hand = 0;
115-
this.trigger = 0;
116-
this.squeeze = 0;
117-
this.joints = [];
118-
for (var i = 0; i < 25; i++) {
119-
this.joints.push(new XRJointData());
120-
}
111+
this.bufferIndex = 0;
121112
this.poses = new Float32Array(16 * 25);
122113
this.radii = new Float32Array(25);
123114
this.jointQuaternion = new Float32Array(4);
124115
this.jointIndex = 0;
116+
this.unityJointIndex = 0;
125117
this.handValuesType = 0;
126118
this.hasRadii = false;
127119
}
128120

129-
function XRJointData() {
130-
this.position = [0, 0, 0];
131-
this.rotation = [0, 0, 0, 1];
132-
this.radius = 0;
133-
}
134-
135121
function XRHitPoseData() {
136122
this.frame = 0;
137123
this.available = 0;
@@ -344,23 +330,21 @@ setTimeout(function () {
344330

345331
this.xrData.controllerA.enabled = 0;
346332
this.xrData.controllerB.enabled = 0;
347-
this.xrData.handLeft.enabled = 0;
348-
this.xrData.handRight.enabled = 0;
349333

350334
this.xrData.controllerA.frame = -1;
351335
this.xrData.controllerB.frame = -1;
352-
this.xrData.handLeft.frame = -1;
353-
this.xrData.handRight.frame = -1;
336+
337+
this.xrData.handLeft.bufferIndex = Module.HandsArrayOffset;
338+
this.xrData.handRight.bufferIndex = Module.HandsArrayOffset + 205;
339+
Module.HEAPF32[this.xrData.handLeft.bufferIndex] = -1; // XRHandData.frame
340+
Module.HEAPF32[this.xrData.handRight.bufferIndex] = -1; // XRHandData.frame
341+
Module.HEAPF32[this.xrData.handLeft.bufferIndex + 1] = 0; // XRHandData.enabled
342+
Module.HEAPF32[this.xrData.handRight.bufferIndex + 1] = 0; // XRHandData.enabled
354343

355344
this.updateUnityXRControllersData({
356345
controllerA: this.xrData.controllerA,
357346
controllerB: this.xrData.controllerB
358347
});
359-
360-
this.updateUnityXRHandsData({
361-
handLeft: this.xrData.handLeft,
362-
handRight: this.xrData.handRight
363-
});
364348

365349
this.gameModule.WebXR.OnEndXR();
366350
this.didNotifyUnity = false;
@@ -426,12 +410,14 @@ setTimeout(function () {
426410

427411
if (hand == 0 || hand == 2) {
428412
xrData.controllerA = controller;
429-
xrData.handRight.trigger = controller.trigger;
430-
xrData.handRight.squeeze = controller.squeeze;
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
431416
} else {
432417
xrData.controllerB = controller;
433-
xrData.handLeft.trigger = controller.trigger;
434-
xrData.handLeft.squeeze = controller.squeeze;
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
435421
}
436422
} else {
437423
var xPercentage = 0.5;
@@ -602,12 +588,14 @@ setTimeout(function () {
602588
}
603589

604590
XRManager.prototype.getXRControllersData = function(frame, inputSources, refSpace, xrData) {
605-
xrData.handLeft.enabled = 0;
606-
xrData.handRight.enabled = 0;
591+
xrData.handLeft.bufferIndex = Module.HandsArrayOffset;
592+
xrData.handRight.bufferIndex = Module.HandsArrayOffset + 205;
593+
Module.HEAPF32[xrData.handLeft.bufferIndex] = xrData.frameNumber; // XRHandData.frame
594+
Module.HEAPF32[xrData.handRight.bufferIndex] = xrData.frameNumber; // XRHandData.frame
595+
Module.HEAPF32[xrData.handLeft.bufferIndex + 1] = 0; // XRHandData.enabled
596+
Module.HEAPF32[xrData.handRight.bufferIndex + 1] = 0; // XRHandData.enabled
607597
xrData.controllerA.enabled = 0;
608598
xrData.controllerB.enabled = 0;
609-
xrData.handLeft.frame = xrData.frameNumber;
610-
xrData.handRight.frame = xrData.frameNumber;
611599
xrData.controllerA.frame = xrData.frameNumber;
612600
xrData.controllerB.frame = xrData.frameNumber;
613601
if (!inputSources || !inputSources.length || inputSources.length == 0) {
@@ -620,12 +608,12 @@ setTimeout(function () {
620608
// Show the input source if it has a grip space
621609
if (inputSource.hand) {
622610
var xrHand = xrData.handLeft;
623-
xrHand.hand = 1;
611+
Module.HEAPF32[xrHand.bufferIndex + 2] = 1; // XRHandData.hand
624612
if (inputSource.handedness == 'right') {
625613
xrHand = xrData.handRight;
626-
xrHand.hand = 2;
614+
Module.HEAPF32[xrHand.bufferIndex + 2] = 2; // XRHandData.hand
627615
}
628-
xrHand.enabled = 1;
616+
Module.HEAPF32[xrHand.bufferIndex + 1] = 1; // XRHandData.enabled
629617

630618
if (xrHand.handValuesType == 0) {
631619
if (inputSource.hand.values) {
@@ -638,7 +626,7 @@ setTimeout(function () {
638626
xrHand.handValuesType == 1 ? inputSource.hand.values() : inputSource.hand,
639627
refSpace,
640628
xrHand.poses)) {
641-
xrHand.enabled = 0;
629+
Module.HEAPF32[xrHand.bufferIndex + 1] = 0; // XRHandData.enabled
642630
continue;
643631
}
644632
if (!xrHand.hasRadii)
@@ -649,17 +637,18 @@ setTimeout(function () {
649637
}
650638
for (var j = 0; j < 25; j++) {
651639
xrHand.jointIndex = j*16;
640+
xrHand.unityJointIndex = xrHand.bufferIndex + 5 + (j*8);
652641
if (!isNaN(xrHand.poses[xrHand.jointIndex])) {
653-
xrHand.joints[j].position[0] = xrHand.poses[xrHand.jointIndex+12];
654-
xrHand.joints[j].position[1] = xrHand.poses[xrHand.jointIndex+13];
655-
xrHand.joints[j].position[2] = -xrHand.poses[xrHand.jointIndex+14];
642+
Module.HEAPF32[xrHand.unityJointIndex] = xrHand.poses[xrHand.jointIndex+12]; // XRJointData.position.x
643+
Module.HEAPF32[xrHand.unityJointIndex + 1] = xrHand.poses[xrHand.jointIndex+13]; // XRJointData.position.y
644+
Module.HEAPF32[xrHand.unityJointIndex + 2] = -xrHand.poses[xrHand.jointIndex+14]; // XRJointData.position.z
656645
this.quaternionFromMatrix(xrHand.jointIndex, xrHand.poses, xrHand.jointQuaternion);
657-
xrHand.joints[j].rotation[0] = -xrHand.jointQuaternion[0];
658-
xrHand.joints[j].rotation[1] = -xrHand.jointQuaternion[1];
659-
xrHand.joints[j].rotation[2] = xrHand.jointQuaternion[2];
660-
xrHand.joints[j].rotation[3] = xrHand.jointQuaternion[3];
646+
Module.HEAPF32[xrHand.unityJointIndex + 3] = -xrHand.jointQuaternion[0]; // XRJointData.rotation.x
647+
Module.HEAPF32[xrHand.unityJointIndex + 4] = -xrHand.jointQuaternion[1]; // XRJointData.rotation.y
648+
Module.HEAPF32[xrHand.unityJointIndex + 5] = xrHand.jointQuaternion[2]; // XRJointData.rotation.z
649+
Module.HEAPF32[xrHand.unityJointIndex + 6] = xrHand.jointQuaternion[3]; // XRJointData.rotation.w
661650
if (!isNaN(xrHand.radii[j])) {
662-
xrHand.joints[j].radius = xrHand.radii[j];
651+
Module.HEAPF32[xrHand.unityJointIndex + 7] = xrHand.radii[j]; // XRJointData.radius
663652
}
664653
}
665654
}
@@ -960,11 +949,6 @@ setTimeout(function () {
960949
controllerA: xrData.controllerA,
961950
controllerB: xrData.controllerB
962951
});
963-
964-
this.updateUnityXRHandsData({
965-
handLeft: xrData.handLeft,
966-
handRight: xrData.handRight
967-
});
968952

969953
if (!this.didNotifyUnity)
970954
{
@@ -1069,30 +1053,6 @@ setTimeout(function () {
10691053
});
10701054
}
10711055

1072-
XRManager.prototype.updateUnityXRHandsData = function (data) {
1073-
var index = 0;
1074-
if (Module.HandsArray.byteLength == 0) {
1075-
Module.HandsArray = new Float32Array(buffer, Module.HandsArrayOffset, Module.HandsArrayLength);
1076-
}
1077-
Object.keys(data).forEach(function (key, i) {
1078-
Module.HandsArray[index++] = data[key].frame;
1079-
Module.HandsArray[index++] = data[key].enabled;
1080-
Module.HandsArray[index++] = data[key].hand;
1081-
Module.HandsArray[index++] = data[key].trigger;
1082-
Module.HandsArray[index++] = data[key].squeeze;
1083-
for (var j = 0; j < 25; j++) {
1084-
Module.HandsArray[index++] = data[key].joints[j].position[0];
1085-
Module.HandsArray[index++] = data[key].joints[j].position[1];
1086-
Module.HandsArray[index++] = data[key].joints[j].position[2];
1087-
Module.HandsArray[index++] = data[key].joints[j].rotation[0];
1088-
Module.HandsArray[index++] = data[key].joints[j].rotation[1];
1089-
Module.HandsArray[index++] = data[key].joints[j].rotation[2];
1090-
Module.HandsArray[index++] = data[key].joints[j].rotation[3];
1091-
Module.HandsArray[index++] = data[key].joints[j].radius;
1092-
}
1093-
});
1094-
}
1095-
10961056
XRManager.prototype.updateUnityXRViewerHitTestPose = function (data) {
10971057
var index = 0;
10981058
if (Module.ViewerHitTestPoseArray.byteLength == 0) {

Packages/webxr/Runtime/XRPlugin/WebXRSubsystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private void InternalStart()
171171
#if UNITY_WEBGL
172172
Native.set_webxr_events(OnStartAR, OnStartVR, UpdateVisibilityState, OnEndXR, OnXRCapabilities, OnInputProfiles);
173173
Native.InitControllersArray(controllersArray, controllersArray.Length);
174-
Native.InitHandsArray(handsArray, handsArray.Length);
174+
Native.InitHandsArray(handsArray);
175175
Native.InitViewerHitTestPoseArray(viewerHitTestPoseArray, viewerHitTestPoseArray.Length);
176176
Native.InitXRSharedArray(sharedArray, sharedArray.Length);
177177
#endif
@@ -187,7 +187,7 @@ private static class Native
187187
public static extern void InitControllersArray(float[] array, int length);
188188

189189
[DllImport("__Internal")]
190-
public static extern void InitHandsArray(float[] array, int length);
190+
public static extern void InitHandsArray(float[] array);
191191

192192
[DllImport("__Internal")]
193193
public static extern void InitViewerHitTestPoseArray(float[] array, int length);

0 commit comments

Comments
 (0)