Skip to content

Commit 892d1e8

Browse files
authored
Merge pull request #226 from De-Panther/buttons_touched_support
Added support for buttons touched values
2 parents da255a8 + b0b9404 commit 892d1e8

File tree

5 files changed

+91
-19
lines changed

5 files changed

+91
-19
lines changed

Packages/webxr/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
9+
### Added
10+
- GetButtonTouched to WebXRController, to support buttons touched values.
11+
812
### Fixed
913
- Another ugly hack to fix WebXR Viewer viewports on iOS.
1014
- Issue of a delay on controller buttons press.

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,21 @@ setTimeout(function () {
110110
this.rotationZIndex = index++;
111111
this.rotationWIndex = index++;
112112
this.triggerIndex = index++;
113+
this.triggerTouchedIndex = index++;
113114
this.squeezeIndex = index++;
115+
this.squeezeTouchedIndex = index++;
114116
this.thumbstickIndex = index++;
117+
this.thumbstickTouchedIndex = index++;
115118
this.thumbstickXIndex = index++;
116119
this.thumbstickYIndex = index++;
117120
this.touchpadIndex = index++;
121+
this.touchpadTouchedIndex = index++;
118122
this.touchpadXIndex = index++;
119123
this.touchpadYIndex = index++;
120124
this.buttonAIndex = index++;
125+
this.buttonATouchedIndex = index++;
121126
this.buttonBIndex = index++;
127+
this.buttonBTouchedIndex = index++;
122128
this.updatedGripIndex = index++;
123129
this.gripPositionXIndex = index++;
124130
this.gripPositionYIndex = index++;
@@ -763,21 +769,27 @@ setTimeout(function () {
763769
switch (j) {
764770
case 0:
765771
Module.HEAPF32[controller.triggerIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.trigger
772+
Module.HEAPF32[controller.triggerTouchedIndex] = inputSource.gamepad.buttons[j].touched; // XRControllerData.triggerTouched
766773
break;
767774
case 1:
768775
Module.HEAPF32[controller.squeezeIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.squeeze
776+
Module.HEAPF32[controller.squeezeTouchedIndex] = inputSource.gamepad.buttons[j].touched; // XRControllerData.squeezeTouched
769777
break;
770778
case 2:
771779
Module.HEAPF32[controller.touchpadIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.touchpad
780+
Module.HEAPF32[controller.touchpadTouchedIndex] = inputSource.gamepad.buttons[j].touched; // XRControllerData.touchpadTouched
772781
break;
773782
case 3:
774783
Module.HEAPF32[controller.thumbstickIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.thumbstick
784+
Module.HEAPF32[controller.thumbstickTouchedIndex] = inputSource.gamepad.buttons[j].touched; // XRControllerData.thumbstickTouched
775785
break;
776786
case 4:
777787
Module.HEAPF32[controller.buttonAIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.buttonA
788+
Module.HEAPF32[controller.buttonATouchedIndex] = inputSource.gamepad.buttons[j].touched; // XRControllerData.buttonATouched
778789
break;
779790
case 5:
780791
Module.HEAPF32[controller.buttonBIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.buttonB
792+
Module.HEAPF32[controller.buttonBTouchedIndex] = inputSource.gamepad.buttons[j].touched; // XRControllerData.buttonBTouched
781793
break;
782794
}
783795
}
@@ -857,7 +869,7 @@ setTimeout(function () {
857869
session.addEventListener('visibilitychange', this.onSessionVisibilityEvent);
858870

859871
this.xrData.controllerA.setIndices(Module.ControllersArrayOffset);
860-
this.xrData.controllerB.setIndices(Module.ControllersArrayOffset + 28);
872+
this.xrData.controllerB.setIndices(Module.ControllersArrayOffset + 34);
861873
this.xrData.handLeft.setIndices(Module.HandsArrayOffset);
862874
this.xrData.handRight.setIndices(Module.HandsArrayOffset + 205);
863875
this.xrData.viewerHitTestPose.setIndices(Module.ViewerHitTestPoseArrayOffset);

Packages/webxr/Runtime/Scripts/WebXRController.cs

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,21 @@ public enum Axis2DTypes
4141
public WebXRControllerHand hand = WebXRControllerHand.NONE;
4242

4343
private float trigger;
44+
private bool triggerTouched;
4445
private float squeeze;
46+
private bool squeezeTouched;
4547
private float thumbstick;
48+
private bool thumbstickTouched;
4649
private float thumbstickX;
4750
private float thumbstickY;
4851
private float touchpad;
52+
private bool touchpadTouched;
4953
private float touchpadX;
5054
private float touchpadY;
5155
private float buttonA;
56+
private bool buttonATouched;
5257
private float buttonB;
58+
private bool buttonBTouched;
5359

5460
private WebXRControllerButton[] buttons;
5561

@@ -102,42 +108,48 @@ private void Awake()
102108
private void InitButtons()
103109
{
104110
buttons = new WebXRControllerButton[6];
105-
buttons[(int)ButtonTypes.Trigger] = new WebXRControllerButton(trigger == 1, trigger);
106-
buttons[(int)ButtonTypes.Grip] = new WebXRControllerButton(squeeze == 1, squeeze);
107-
buttons[(int)ButtonTypes.Thumbstick] = new WebXRControllerButton(thumbstick == 1, thumbstick);
108-
buttons[(int)ButtonTypes.Touchpad] = new WebXRControllerButton(touchpad == 1, touchpad);
109-
buttons[(int)ButtonTypes.ButtonA] = new WebXRControllerButton(buttonA == 1, buttonA);
110-
buttons[(int)ButtonTypes.ButtonB] = new WebXRControllerButton(buttonB == 1, buttonB);
111+
buttons[(int)ButtonTypes.Trigger] = new WebXRControllerButton(trigger == 1, triggerTouched, trigger);
112+
buttons[(int)ButtonTypes.Grip] = new WebXRControllerButton(squeeze == 1, squeezeTouched, squeeze);
113+
buttons[(int)ButtonTypes.Thumbstick] = new WebXRControllerButton(thumbstick == 1, thumbstickTouched, thumbstick);
114+
buttons[(int)ButtonTypes.Touchpad] = new WebXRControllerButton(touchpad == 1, touchpadTouched, touchpad);
115+
buttons[(int)ButtonTypes.ButtonA] = new WebXRControllerButton(buttonA == 1, buttonATouched, buttonA);
116+
buttons[(int)ButtonTypes.ButtonB] = new WebXRControllerButton(buttonB == 1, buttonBTouched, buttonB);
111117
}
112118

113119
private void UpdateAllButtons()
114120
{
115-
buttons[(int)ButtonTypes.Trigger].UpdateState(trigger == 1, trigger);
116-
buttons[(int)ButtonTypes.Grip].UpdateState(squeeze == 1, squeeze);
117-
buttons[(int)ButtonTypes.Thumbstick].UpdateState(thumbstick == 1, thumbstick);
118-
buttons[(int)ButtonTypes.Touchpad].UpdateState(touchpad == 1, touchpad);
119-
buttons[(int)ButtonTypes.ButtonA].UpdateState(buttonA == 1, buttonA);
120-
buttons[(int)ButtonTypes.ButtonB].UpdateState(buttonB == 1, buttonB);
121+
buttons[(int)ButtonTypes.Trigger].UpdateState(trigger == 1, triggerTouched, trigger);
122+
buttons[(int)ButtonTypes.Grip].UpdateState(squeeze == 1, squeezeTouched, squeeze);
123+
buttons[(int)ButtonTypes.Thumbstick].UpdateState(thumbstick == 1, thumbstickTouched, thumbstick);
124+
buttons[(int)ButtonTypes.Touchpad].UpdateState(touchpad == 1, touchpadTouched, touchpad);
125+
buttons[(int)ButtonTypes.ButtonA].UpdateState(buttonA == 1, buttonATouched, buttonA);
126+
buttons[(int)ButtonTypes.ButtonB].UpdateState(buttonB == 1, buttonBTouched, buttonB);
121127
}
122128

123129
private void UpdateHandButtons()
124130
{
125-
buttons[(int)ButtonTypes.Trigger].UpdateState(trigger == 1, trigger);
126-
buttons[(int)ButtonTypes.Grip].UpdateState(squeeze == 1, squeeze);
131+
buttons[(int)ButtonTypes.Trigger].UpdateState(trigger == 1, trigger == 1, trigger);
132+
buttons[(int)ButtonTypes.Grip].UpdateState(squeeze == 1, squeeze == 1, squeeze);
127133
}
128134

129135
private void ResetAllButtons()
130136
{
131137
trigger = 0;
138+
triggerTouched = false;
132139
squeeze = 0;
140+
squeezeTouched = false;
133141
thumbstick = 0;
142+
thumbstickTouched = false;
134143
thumbstickX = 0;
135144
thumbstickY = 0;
136145
touchpad = 0;
146+
touchpadTouched = false;
137147
touchpadX = 0;
138148
touchpadY = 0;
139149
buttonA = 0;
150+
buttonATouched = false;
140151
buttonB = 0;
152+
buttonBTouched = false;
141153
if (buttons?.Length == 6)
142154
{
143155
UpdateAllButtons();
@@ -203,6 +215,23 @@ private void TryUpdateButtons()
203215
buttonB = buttonPressed ? 1 : 0;
204216
}
205217

218+
if (!inputDevice.Value.TryGetFeatureValue(CommonUsages.primaryTouch, out buttonATouched))
219+
{
220+
buttonATouched = buttonA > 0;
221+
}
222+
if (!inputDevice.Value.TryGetFeatureValue(CommonUsages.secondaryTouch, out buttonBTouched))
223+
{
224+
buttonBTouched = buttonB > 0;
225+
}
226+
if (!inputDevice.Value.TryGetFeatureValue(CommonUsages.primary2DAxisTouch, out thumbstickTouched))
227+
{
228+
thumbstickTouched = thumbstick > 0;
229+
}
230+
if (!inputDevice.Value.TryGetFeatureValue(CommonUsages.secondary2DAxisTouch, out touchpadTouched))
231+
{
232+
touchpadTouched = touchpad > 0;
233+
}
234+
206235
if (buttons?.Length != 6)
207236
{
208237
InitButtons();
@@ -259,6 +288,12 @@ public bool GetButtonUp(ButtonTypes buttonType)
259288
return buttons[(int)buttonType].up;
260289
}
261290

291+
public bool GetButtonTouched(ButtonTypes buttonType)
292+
{
293+
TryUpdateButtons();
294+
return buttons[(int)buttonType].touched;
295+
}
296+
262297
public float GetButtonIndexValue(int index)
263298
{
264299
TryUpdateButtons();
@@ -352,15 +387,21 @@ private void OnControllerUpdate(WebXRControllerData controllerData)
352387
}
353388

354389
trigger = controllerData.trigger;
390+
triggerTouched = controllerData.triggerTouched;
355391
squeeze = controllerData.squeeze;
392+
squeezeTouched = controllerData.squeezeTouched;
356393
thumbstick = controllerData.thumbstick;
394+
thumbstickTouched = controllerData.thumbstickTouched;
357395
thumbstickX = controllerData.thumbstickX;
358396
thumbstickY = controllerData.thumbstickY;
359397
touchpad = controllerData.touchpad;
398+
touchpadTouched = controllerData.touchpadTouched;
360399
touchpadX = controllerData.touchpadX;
361400
touchpadY = controllerData.touchpadY;
362401
buttonA = controllerData.buttonA;
402+
buttonATouched = controllerData.buttonATouched;
363403
buttonB = controllerData.buttonB;
404+
buttonBTouched = controllerData.buttonBTouched;
364405

365406
if (buttons?.Length != 6)
366407
{

Packages/webxr/Runtime/Scripts/WebXRControllerData.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@ public class WebXRControllerData
1313
public Vector3 gripPosition;
1414
public Quaternion gripRotation;
1515
public float trigger;
16+
public bool triggerTouched;
1617
public float squeeze;
18+
public bool squeezeTouched;
1719
public float thumbstick;
20+
public bool thumbstickTouched;
1821
public float thumbstickX;
1922
public float thumbstickY;
2023
public float touchpad;
24+
public bool touchpadTouched;
2125
public float touchpadX;
2226
public float touchpadY;
2327
public float buttonA;
28+
public bool buttonATouched;
2429
public float buttonB;
30+
public bool buttonBTouched;
2531
public string[] profiles;
2632
}
2733

@@ -100,19 +106,21 @@ public enum WebXRControllerHand
100106
public class WebXRControllerButton
101107
{
102108
public bool pressed;
109+
public bool touched;
103110
public bool down;
104111
public bool up;
105112
public float value;
106113

107-
public WebXRControllerButton(bool isPressed, float buttonValue)
114+
public WebXRControllerButton(bool isPressed, bool isTouched, float buttonValue)
108115
{
109116
down = false;
110117
up = false;
111118
pressed = isPressed;
119+
touched = isTouched;
112120
value = buttonValue;
113121
}
114122

115-
public void UpdateState(bool isPressed, float buttonValue)
123+
public void UpdateState(bool isPressed, bool isTouched, float buttonValue)
116124
{
117125
if (isPressed && pressed) // nothing
118126
{
@@ -135,6 +143,7 @@ public void UpdateState(bool isPressed, float buttonValue)
135143
up = true;
136144
}
137145
pressed = isPressed;
146+
touched = isTouched;
138147
value = buttonValue;
139148
}
140149
}

Packages/webxr/Runtime/XRPlugin/WebXRSubsystem.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ internal delegate void StartXREvent(int viewsCount,
281281
float[] sharedArray = new float[(2 * 16) + (2 * 7)];
282282

283283
// Shared array for controllers data
284-
float[] controllersArray = new float[2 * 28];
284+
float[] controllersArray = new float[2 * 34];
285285

286286
// Shared array for hands data
287287
float[] handsArray = new float[2 * (25 * 8 + 5)];
@@ -460,7 +460,7 @@ void GetVector3FromSharedArray(int index, ref Vector3 vec3)
460460

461461
bool GetGamepadFromControllersArray(int controllerIndex, ref WebXRControllerData newControllerData)
462462
{
463-
int arrayPosition = controllerIndex * 28;
463+
int arrayPosition = controllerIndex * 34;
464464
int frameNumber = (int)controllersArray[arrayPosition++];
465465
if (newControllerData.frame == frameNumber)
466466
{
@@ -479,15 +479,21 @@ bool GetGamepadFromControllersArray(int controllerIndex, ref WebXRControllerData
479479
newControllerData.rotation = new Quaternion(controllersArray[arrayPosition++], controllersArray[arrayPosition++], controllersArray[arrayPosition++],
480480
controllersArray[arrayPosition++]);
481481
newControllerData.trigger = controllersArray[arrayPosition++];
482+
newControllerData.triggerTouched = controllersArray[arrayPosition++] != 0;
482483
newControllerData.squeeze = controllersArray[arrayPosition++];
484+
newControllerData.squeezeTouched = controllersArray[arrayPosition++] != 0;
483485
newControllerData.thumbstick = controllersArray[arrayPosition++];
486+
newControllerData.thumbstickTouched = controllersArray[arrayPosition++] != 0;
484487
newControllerData.thumbstickX = controllersArray[arrayPosition++];
485488
newControllerData.thumbstickY = controllersArray[arrayPosition++];
486489
newControllerData.touchpad = controllersArray[arrayPosition++];
490+
newControllerData.touchpadTouched = controllersArray[arrayPosition++] != 0;
487491
newControllerData.touchpadX = controllersArray[arrayPosition++];
488492
newControllerData.touchpadY = controllersArray[arrayPosition++];
489493
newControllerData.buttonA = controllersArray[arrayPosition++];
494+
newControllerData.buttonATouched = controllersArray[arrayPosition++] != 0;
490495
newControllerData.buttonB = controllersArray[arrayPosition++];
496+
newControllerData.buttonBTouched = controllersArray[arrayPosition++] != 0;
491497
if (controllersArray[arrayPosition] == 1)
492498
{
493499
controllersArray[arrayPosition++] = 2;

0 commit comments

Comments
 (0)