Skip to content

Commit 106a1ed

Browse files
committed
Support trigger and squeeze
1 parent f0fce1d commit 106a1ed

File tree

9 files changed

+45
-3
lines changed

9 files changed

+45
-3
lines changed

Assets/WebGLTemplates/WebXR/webxr.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
// TODO: set enabled 0 if hand was enable and then disable
4646
this.enabled = 0;
4747
this.hand = 0;
48+
this.trigger = 0;
49+
this.squeeze = 0;
4850
this.joints = [];
4951
for (let i = 0; i < 25; i++) {
5052
this.joints.push(new XRJointData());
@@ -235,8 +237,12 @@
235237

236238
if (hand == 0 || hand == 2) {
237239
xrData.controllerA = controller;
240+
xrData.handRight.trigger = controller.trigger;
241+
xrData.handRight.squeeze = controller.squeeze;
238242
} else {
239243
xrData.controllerB = controller;
244+
xrData.handLeft.trigger = controller.trigger;
245+
xrData.handLeft.squeeze = controller.squeeze;
240246
}
241247
}
242248
}

Assets/WebXR/Plugins/WebGL/webxr.jslib

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ mergeInto(LibraryManager.library, {
6060
HandsArray[index++] = data[key].frame;
6161
HandsArray[index++] = data[key].enabled;
6262
HandsArray[index++] = data[key].hand;
63+
HandsArray[index++] = data[key].trigger;
64+
HandsArray[index++] = data[key].squeeze;
6365
for (var j = 0; j < 25; j++) {
6466
HandsArray[index++] = data[key].joints[j].enabled;
6567
HandsArray[index++] = data[key].joints[j].position[0];

Assets/WebXR/Scripts/WebXRController.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class WebXRController : MonoBehaviour
4141
private Dictionary<string, WebXRControllerButton> buttonStates = new Dictionary<string, WebXRControllerButton>();
4242

4343
private Dictionary<int, Transform> handJoints = new Dictionary<int, Transform>();
44+
private bool handJointsVisible = false;
4445

4546
// Updates button states from Web gamepad API.
4647
private void UpdateButtons(WebXRControllerButton[] buttons)
@@ -176,9 +177,11 @@ private void OnHandUpdate(WebXRHandData handData)
176177
{
177178
if (!handData.enabled)
178179
{
180+
SetHandJointsVisible(false);
179181
return;
180182
}
181183
SetVisible(false);
184+
SetHandJointsVisible(true);
182185

183186
transform.localPosition = handData.joints[0].position;
184187
transform.localRotation = handData.joints[0].rotation;
@@ -212,6 +215,14 @@ private void OnHandUpdate(WebXRHandData handData)
212215
}
213216
}
214217
}
218+
219+
trigger = handData.trigger;
220+
squeeze = handData.squeeze;
221+
222+
WebXRControllerButton[] buttons = new WebXRControllerButton[2];
223+
buttons[0] = new WebXRControllerButton(trigger==1, trigger);
224+
buttons[1] = new WebXRControllerButton(squeeze==1, squeeze);
225+
UpdateButtons(buttons);
215226
}
216227
}
217228

@@ -241,6 +252,19 @@ private void SetVisible(bool visible)
241252
}
242253
}
243254

255+
private void SetHandJointsVisible(bool visible)
256+
{
257+
if (handJointsVisible == visible)
258+
{
259+
return;
260+
}
261+
handJointsVisible = visible;
262+
foreach (var handJoint in handJoints)
263+
{
264+
handJoint.Value.gameObject.SetActive(visible);
265+
}
266+
}
267+
244268
void OnEnable()
245269
{
246270
if (inputMap == null)

Assets/WebXR/Scripts/WebXRControllerData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class WebXRHandData
2828
public int frame;
2929
public bool enabled;
3030
public int hand;
31+
public float trigger;
32+
public float squeeze;
3133
public WebXRJointData[] joints = new WebXRJointData[25];
3234
public const int WRIST = 0;
3335
public const int THUMB_METACARPAL = 1;

Assets/WebXR/Scripts/WebXRManager.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private static extern void set_webxr_events(Action<int> on_start_ar,
6969
float[] controllersArray = new float[2 * 20];
7070

7171
// Shared array for hands data
72-
float[] handsArray = new float[2 * (25 * 9 + 3)];
72+
float[] handsArray = new float[2 * (25 * 9 + 5)];
7373

7474
private WebXRHandData leftHand = new WebXRHandData();
7575
private WebXRHandData rightHand = new WebXRHandData();
@@ -212,7 +212,7 @@ bool GetGamepadFromControllersArray(int controllerIndex, ref WebXRControllerData
212212

213213
bool GetHandFromHandsArray(int handIndex, ref WebXRHandData handObject)
214214
{
215-
int arrayPosition = handIndex * 228;
215+
int arrayPosition = handIndex * 230;
216216
int frameNumber = (int)handsArray[arrayPosition++];
217217
if (handObject.frame == frameNumber)
218218
{
@@ -221,6 +221,8 @@ bool GetHandFromHandsArray(int handIndex, ref WebXRHandData handObject)
221221
handObject.frame = frameNumber;
222222
handObject.enabled = handsArray[arrayPosition++] != 0;
223223
handObject.hand = (int)handsArray[arrayPosition++];
224+
handObject.trigger = handsArray[arrayPosition++];
225+
handObject.squeeze = handsArray[arrayPosition++];
224226
if (!handObject.enabled)
225227
{
226228
return true;

Build/Build/Build.data.unityweb

-124 Bytes
Binary file not shown.

Build/Build/Build.wasm

-736 Bytes
Binary file not shown.

Build/Build/Build.wasm.framework.unityweb

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

Build/webxr.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
// TODO: set enabled 0 if hand was enable and then disable
4646
this.enabled = 0;
4747
this.hand = 0;
48+
this.trigger = 0;
49+
this.squeeze = 0;
4850
this.joints = [];
4951
for (let i = 0; i < 25; i++) {
5052
this.joints.push(new XRJointData());
@@ -235,8 +237,12 @@
235237

236238
if (hand == 0 || hand == 2) {
237239
xrData.controllerA = controller;
240+
xrData.handRight.trigger = controller.trigger;
241+
xrData.handRight.squeeze = controller.squeeze;
238242
} else {
239243
xrData.controllerB = controller;
244+
xrData.handLeft.trigger = controller.trigger;
245+
xrData.handLeft.squeeze = controller.squeeze;
240246
}
241247
}
242248
}

0 commit comments

Comments
 (0)