Skip to content

Commit d481010

Browse files
committed
Removed the need for event listeners for data transfer between JS and C#
1 parent 6324344 commit d481010

File tree

6 files changed

+115
-123
lines changed

6 files changed

+115
-123
lines changed

Build/Build/Build.data.unityweb

-48 Bytes
Binary file not shown.

Build/Build/Build.wasm

3.84 KB
Binary file not shown.

Build/Build/Build.wasm.framework.unityweb

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

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

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -45,105 +45,4 @@ mergeInto(LibraryManager.library, {
4545
ControllerPulse: function(controller, intensity, duration) {
4646
document.dispatchEvent(new CustomEvent('callHapticPulse', {detail: {'controller' : controller, 'intensity' : intensity, 'duration': duration}}));
4747
},
48-
49-
ListenWebXRData: function() {
50-
// Listen for headset updates from webxr.jspre and load data into shared Array which we pick up in Unity.
51-
document.addEventListener('XRData', function(evt) {
52-
var data = evt.detail;
53-
var index = 0;
54-
if (Module.XRSharedArray.byteLength == 0) {
55-
Module.XRSharedArray = new Float32Array(buffer, Module.XRSharedArrayOffset, Module.XRSharedArrayLength);
56-
}
57-
Object.keys(data).forEach(function (key, i) {
58-
var dataLength = data[key].length;
59-
if (dataLength) {
60-
for (var x = 0; x < dataLength; x++) {
61-
Module.XRSharedArray[index++] = data[key][x];
62-
}
63-
}
64-
});
65-
});
66-
document.addEventListener('XRControllersData', function(evt) {
67-
var data = evt.detail;
68-
var index = 0;
69-
if (Module.ControllersArray.byteLength == 0) {
70-
Module.ControllersArray = new Float32Array(buffer, Module.ControllersArrayOffset, Module.ControllersArrayLength);
71-
}
72-
Object.keys(data).forEach(function (key, i) {
73-
Module.ControllersArray[index++] = data[key].frame;
74-
Module.ControllersArray[index++] = data[key].enabled;
75-
Module.ControllersArray[index++] = data[key].hand;
76-
Module.ControllersArray[index++] = data[key].positionX;
77-
Module.ControllersArray[index++] = data[key].positionY;
78-
Module.ControllersArray[index++] = data[key].positionZ;
79-
Module.ControllersArray[index++] = data[key].rotationX;
80-
Module.ControllersArray[index++] = data[key].rotationY;
81-
Module.ControllersArray[index++] = data[key].rotationZ;
82-
Module.ControllersArray[index++] = data[key].rotationW;
83-
Module.ControllersArray[index++] = data[key].trigger;
84-
Module.ControllersArray[index++] = data[key].squeeze;
85-
Module.ControllersArray[index++] = data[key].thumbstick;
86-
Module.ControllersArray[index++] = data[key].thumbstickX;
87-
Module.ControllersArray[index++] = data[key].thumbstickY;
88-
Module.ControllersArray[index++] = data[key].touchpad;
89-
Module.ControllersArray[index++] = data[key].touchpadX;
90-
Module.ControllersArray[index++] = data[key].touchpadY;
91-
Module.ControllersArray[index++] = data[key].buttonA;
92-
Module.ControllersArray[index++] = data[key].buttonB;
93-
Module.ControllersArray[index++] = data[key].updatedGrip;
94-
if (data[key].updatedGrip == 1) {
95-
Module.ControllersArray[index++] = data[key].gripPositionX;
96-
Module.ControllersArray[index++] = data[key].gripPositionY;
97-
Module.ControllersArray[index++] = data[key].gripPositionZ;
98-
Module.ControllersArray[index++] = data[key].gripRotationX;
99-
Module.ControllersArray[index++] = data[key].gripRotationY;
100-
Module.ControllersArray[index++] = data[key].gripRotationZ;
101-
Module.ControllersArray[index++] = data[key].gripRotationW;
102-
} else {
103-
index += 7;
104-
}
105-
});
106-
});
107-
document.addEventListener('XRHandsData', function(evt) {
108-
var data = evt.detail;
109-
var index = 0;
110-
if (Module.HandsArray.byteLength == 0) {
111-
Module.HandsArray = new Float32Array(buffer, Module.HandsArrayOffset, Module.HandsArrayLength);
112-
}
113-
Object.keys(data).forEach(function (key, i) {
114-
Module.HandsArray[index++] = data[key].frame;
115-
Module.HandsArray[index++] = data[key].enabled;
116-
Module.HandsArray[index++] = data[key].hand;
117-
Module.HandsArray[index++] = data[key].trigger;
118-
Module.HandsArray[index++] = data[key].squeeze;
119-
for (var j = 0; j < 25; j++) {
120-
Module.HandsArray[index++] = data[key].joints[j].enabled;
121-
Module.HandsArray[index++] = data[key].joints[j].position[0];
122-
Module.HandsArray[index++] = data[key].joints[j].position[1];
123-
Module.HandsArray[index++] = data[key].joints[j].position[2];
124-
Module.HandsArray[index++] = data[key].joints[j].rotation[0];
125-
Module.HandsArray[index++] = data[key].joints[j].rotation[1];
126-
Module.HandsArray[index++] = data[key].joints[j].rotation[2];
127-
Module.HandsArray[index++] = data[key].joints[j].rotation[3];
128-
Module.HandsArray[index++] = data[key].joints[j].radius;
129-
}
130-
});
131-
});
132-
document.addEventListener('XRViewerHitTestPose', function(evt) {
133-
var data = evt.detail;
134-
var index = 0;
135-
if (Module.ViewerHitTestPoseArray.byteLength == 0) {
136-
Module.ViewerHitTestPoseArray = new Float32Array(buffer, Module.ViewerHitTestPoseArrayOffset, Module.ViewerHitTestPoseArrayLength);
137-
}
138-
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.frame;
139-
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.available;
140-
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.position[0];
141-
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.position[1];
142-
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.position[2];
143-
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.rotation[0];
144-
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.rotation[1];
145-
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.rotation[2];
146-
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.rotation[3];
147-
});
148-
}
14948
});

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

Lines changed: 114 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Module['WebXR'] = Module['WebXR'] || {};
2+
13
setTimeout(function () {
24
if (GL && GL.createContext)
35
{
@@ -228,7 +230,7 @@ setTimeout(function () {
228230
var thisXRMananger = this;
229231
navigator.xr.isSessionSupported('immersive-vr').then(function (supported) {
230232
thisXRMananger.isVRSupported = supported;
231-
if (document.body.dataset.unityLoaded)
233+
if (Module.WebXR.unityLoaded)
232234
{
233235
document.dispatchEvent(new CustomEvent('onVRSupportedCheck', { detail:{supported:thisXRMananger.isVRSupported} }));
234236
thisXRMananger.UpdateXRCapabilities();
@@ -237,7 +239,7 @@ setTimeout(function () {
237239

238240
navigator.xr.isSessionSupported('immersive-ar').then(function (supported) {
239241
thisXRMananger.isARSupported = supported;
240-
if (document.body.dataset.unityLoaded)
242+
if (Module.WebXR.unityLoaded)
241243
{
242244
document.dispatchEvent(new CustomEvent('onARSupportedCheck', { detail:{supported:thisXRMananger.isARSupported} }));
243245
thisXRMananger.UpdateXRCapabilities();
@@ -350,15 +352,15 @@ setTimeout(function () {
350352
this.xrData.handLeft.frame = -1;
351353
this.xrData.handRight.frame = -1;
352354

353-
document.dispatchEvent(new CustomEvent('XRControllersData', { detail: {
355+
this.updateUnityXRControllersData({
354356
controllerA: this.xrData.controllerA,
355357
controllerB: this.xrData.controllerB
356-
}}));
358+
});
357359

358-
document.dispatchEvent(new CustomEvent('XRHandsData', { detail: {
360+
this.updateUnityXRHandsData({
359361
handLeft: this.xrData.handLeft,
360362
handRight: this.xrData.handRight
361-
}}));
363+
});
362364

363365
this.gameModule.WebXR.OnEndXR();
364366
this.didNotifyUnity = false;
@@ -561,7 +563,7 @@ setTimeout(function () {
561563
}
562564

563565
XRManager.prototype.unityLoaded = function (event) {
564-
document.body.dataset.unityLoaded = 'true';
566+
Module.WebXR.unityLoaded = 'true';
565567

566568
this.setGameModule(event.detail.module);
567569

@@ -896,9 +898,9 @@ setTimeout(function () {
896898
} else {
897899
xrData.viewerHitTestPose.available = 0;
898900
}
899-
document.dispatchEvent(new CustomEvent('XRViewerHitTestPose', { detail: {
901+
this.updateUnityXRViewerHitTestPose({
900902
viewerHitTestPose: xrData.viewerHitTestPose
901-
}}));
903+
});
902904
}
903905

904906
if (xrData.controllerA.updatedProfiles == 1 || xrData.controllerB.updatedProfiles == 1)
@@ -918,24 +920,24 @@ setTimeout(function () {
918920
}
919921

920922
// Dispatch event with headset data to be handled in webxr.jslib
921-
document.dispatchEvent(new CustomEvent('XRData', { detail: {
923+
this.updateUnityXRData({
922924
leftProjectionMatrix: xrData.leftProjectionMatrix,
923925
rightProjectionMatrix: xrData.rightProjectionMatrix,
924926
leftViewRotation: xrData.leftViewRotation,
925927
rightViewRotation: xrData.rightViewRotation,
926928
leftViewPosition: xrData.leftViewPosition,
927929
rightViewPosition: xrData.rightViewPosition
928-
}}));
930+
});
929931

930-
document.dispatchEvent(new CustomEvent('XRControllersData', { detail: {
932+
this.updateUnityXRControllersData({
931933
controllerA: xrData.controllerA,
932934
controllerB: xrData.controllerB
933-
}}));
935+
});
934936

935-
document.dispatchEvent(new CustomEvent('XRHandsData', { detail: {
937+
this.updateUnityXRHandsData({
936938
handLeft: xrData.handLeft,
937939
handRight: xrData.handRight
938-
}}));
940+
});
939941

940942
if (!this.didNotifyUnity)
941943
{
@@ -981,6 +983,103 @@ setTimeout(function () {
981983
this.didNotifyUnity = true;
982984
}
983985
}
986+
987+
XRManager.prototype.updateUnityXRData = function (data) {
988+
var index = 0;
989+
if (Module.XRSharedArray.byteLength == 0) {
990+
Module.XRSharedArray = new Float32Array(buffer, Module.XRSharedArrayOffset, Module.XRSharedArrayLength);
991+
}
992+
Object.keys(data).forEach(function (key, i) {
993+
var dataLength = data[key].length;
994+
if (dataLength) {
995+
for (var x = 0; x < dataLength; x++) {
996+
Module.XRSharedArray[index++] = data[key][x];
997+
}
998+
}
999+
});
1000+
}
1001+
1002+
XRManager.prototype.updateUnityXRControllersData = function (data) {
1003+
var index = 0;
1004+
if (Module.ControllersArray.byteLength == 0) {
1005+
Module.ControllersArray = new Float32Array(buffer, Module.ControllersArrayOffset, Module.ControllersArrayLength);
1006+
}
1007+
Object.keys(data).forEach(function (key, i) {
1008+
Module.ControllersArray[index++] = data[key].frame;
1009+
Module.ControllersArray[index++] = data[key].enabled;
1010+
Module.ControllersArray[index++] = data[key].hand;
1011+
Module.ControllersArray[index++] = data[key].positionX;
1012+
Module.ControllersArray[index++] = data[key].positionY;
1013+
Module.ControllersArray[index++] = data[key].positionZ;
1014+
Module.ControllersArray[index++] = data[key].rotationX;
1015+
Module.ControllersArray[index++] = data[key].rotationY;
1016+
Module.ControllersArray[index++] = data[key].rotationZ;
1017+
Module.ControllersArray[index++] = data[key].rotationW;
1018+
Module.ControllersArray[index++] = data[key].trigger;
1019+
Module.ControllersArray[index++] = data[key].squeeze;
1020+
Module.ControllersArray[index++] = data[key].thumbstick;
1021+
Module.ControllersArray[index++] = data[key].thumbstickX;
1022+
Module.ControllersArray[index++] = data[key].thumbstickY;
1023+
Module.ControllersArray[index++] = data[key].touchpad;
1024+
Module.ControllersArray[index++] = data[key].touchpadX;
1025+
Module.ControllersArray[index++] = data[key].touchpadY;
1026+
Module.ControllersArray[index++] = data[key].buttonA;
1027+
Module.ControllersArray[index++] = data[key].buttonB;
1028+
Module.ControllersArray[index++] = data[key].updatedGrip;
1029+
if (data[key].updatedGrip == 1) {
1030+
Module.ControllersArray[index++] = data[key].gripPositionX;
1031+
Module.ControllersArray[index++] = data[key].gripPositionY;
1032+
Module.ControllersArray[index++] = data[key].gripPositionZ;
1033+
Module.ControllersArray[index++] = data[key].gripRotationX;
1034+
Module.ControllersArray[index++] = data[key].gripRotationY;
1035+
Module.ControllersArray[index++] = data[key].gripRotationZ;
1036+
Module.ControllersArray[index++] = data[key].gripRotationW;
1037+
} else {
1038+
index += 7;
1039+
}
1040+
});
1041+
}
1042+
1043+
XRManager.prototype.updateUnityXRHandsData = function (data) {
1044+
var index = 0;
1045+
if (Module.HandsArray.byteLength == 0) {
1046+
Module.HandsArray = new Float32Array(buffer, Module.HandsArrayOffset, Module.HandsArrayLength);
1047+
}
1048+
Object.keys(data).forEach(function (key, i) {
1049+
Module.HandsArray[index++] = data[key].frame;
1050+
Module.HandsArray[index++] = data[key].enabled;
1051+
Module.HandsArray[index++] = data[key].hand;
1052+
Module.HandsArray[index++] = data[key].trigger;
1053+
Module.HandsArray[index++] = data[key].squeeze;
1054+
for (var j = 0; j < 25; j++) {
1055+
Module.HandsArray[index++] = data[key].joints[j].enabled;
1056+
Module.HandsArray[index++] = data[key].joints[j].position[0];
1057+
Module.HandsArray[index++] = data[key].joints[j].position[1];
1058+
Module.HandsArray[index++] = data[key].joints[j].position[2];
1059+
Module.HandsArray[index++] = data[key].joints[j].rotation[0];
1060+
Module.HandsArray[index++] = data[key].joints[j].rotation[1];
1061+
Module.HandsArray[index++] = data[key].joints[j].rotation[2];
1062+
Module.HandsArray[index++] = data[key].joints[j].rotation[3];
1063+
Module.HandsArray[index++] = data[key].joints[j].radius;
1064+
}
1065+
});
1066+
}
1067+
1068+
XRManager.prototype.updateUnityXRViewerHitTestPose = function (data) {
1069+
var index = 0;
1070+
if (Module.ViewerHitTestPoseArray.byteLength == 0) {
1071+
Module.ViewerHitTestPoseArray = new Float32Array(buffer, Module.ViewerHitTestPoseArrayOffset, Module.ViewerHitTestPoseArrayLength);
1072+
}
1073+
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.frame;
1074+
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.available;
1075+
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.position[0];
1076+
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.position[1];
1077+
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.position[2];
1078+
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.rotation[0];
1079+
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.rotation[1];
1080+
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.rotation[2];
1081+
Module.ViewerHitTestPoseArray[index++] = data.viewerHitTestPose.rotation[3];
1082+
}
9841083

9851084
function initWebXRManager () {
9861085
var xrManager = window.xrManager = new XRManager();
@@ -1021,8 +1120,6 @@ setTimeout(function () {
10211120

10221121
}, 0);
10231122

1024-
Module['WebXR'] = Module['WebXR'] || {};
1025-
10261123
Module['WebXR'].GetBrowserObject = function () {
10271124
return Browser;
10281125
}

Packages/webxr/Runtime/XRPlugin/WebXRSubsystem.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ private void InternalStart()
163163
Native.InitHandsArray(handsArray, handsArray.Length);
164164
Native.InitViewerHitTestPoseArray(viewerHitTestPoseArray, viewerHitTestPoseArray.Length);
165165
Native.InitXRSharedArray(sharedArray, sharedArray.Length);
166-
Native.ListenWebXRData();
167166
}
168167

169168
private static class Native
@@ -192,9 +191,6 @@ private static class Native
192191
[DllImport("__Internal")]
193192
public static extern void ControllerPulse(int controller, float intensity, float duration);
194193

195-
[DllImport("__Internal")]
196-
public static extern void ListenWebXRData();
197-
198194
[DllImport("__Internal")]
199195
public static extern void set_webxr_events(Action<int, float, float, float, float, float, float, float, float> on_start_ar,
200196
Action<int, float, float, float, float, float, float, float, float> on_start_vr,

0 commit comments

Comments
 (0)