Skip to content

Commit

Permalink
Merge pull request #226 from De-Panther/buttons_touched_support
Browse files Browse the repository at this point in the history
Added support for buttons touched values
  • Loading branch information
De-Panther committed Jun 4, 2022
2 parents da255a8 + b0b9404 commit 892d1e8
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Packages/webxr/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added
- GetButtonTouched to WebXRController, to support buttons touched values.

### Fixed
- Another ugly hack to fix WebXR Viewer viewports on iOS.
- Issue of a delay on controller buttons press.
Expand Down
14 changes: 13 additions & 1 deletion Packages/webxr/Runtime/Plugins/WebGL/webxr.jspre
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,21 @@ setTimeout(function () {
this.rotationZIndex = index++;
this.rotationWIndex = index++;
this.triggerIndex = index++;
this.triggerTouchedIndex = index++;
this.squeezeIndex = index++;
this.squeezeTouchedIndex = index++;
this.thumbstickIndex = index++;
this.thumbstickTouchedIndex = index++;
this.thumbstickXIndex = index++;
this.thumbstickYIndex = index++;
this.touchpadIndex = index++;
this.touchpadTouchedIndex = index++;
this.touchpadXIndex = index++;
this.touchpadYIndex = index++;
this.buttonAIndex = index++;
this.buttonATouchedIndex = index++;
this.buttonBIndex = index++;
this.buttonBTouchedIndex = index++;
this.updatedGripIndex = index++;
this.gripPositionXIndex = index++;
this.gripPositionYIndex = index++;
Expand Down Expand Up @@ -763,21 +769,27 @@ setTimeout(function () {
switch (j) {
case 0:
Module.HEAPF32[controller.triggerIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.trigger
Module.HEAPF32[controller.triggerTouchedIndex] = inputSource.gamepad.buttons[j].touched; // XRControllerData.triggerTouched
break;
case 1:
Module.HEAPF32[controller.squeezeIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.squeeze
Module.HEAPF32[controller.squeezeTouchedIndex] = inputSource.gamepad.buttons[j].touched; // XRControllerData.squeezeTouched
break;
case 2:
Module.HEAPF32[controller.touchpadIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.touchpad
Module.HEAPF32[controller.touchpadTouchedIndex] = inputSource.gamepad.buttons[j].touched; // XRControllerData.touchpadTouched
break;
case 3:
Module.HEAPF32[controller.thumbstickIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.thumbstick
Module.HEAPF32[controller.thumbstickTouchedIndex] = inputSource.gamepad.buttons[j].touched; // XRControllerData.thumbstickTouched
break;
case 4:
Module.HEAPF32[controller.buttonAIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.buttonA
Module.HEAPF32[controller.buttonATouchedIndex] = inputSource.gamepad.buttons[j].touched; // XRControllerData.buttonATouched
break;
case 5:
Module.HEAPF32[controller.buttonBIndex] = inputSource.gamepad.buttons[j].value; // XRControllerData.buttonB
Module.HEAPF32[controller.buttonBTouchedIndex] = inputSource.gamepad.buttons[j].touched; // XRControllerData.buttonBTouched
break;
}
}
Expand Down Expand Up @@ -857,7 +869,7 @@ setTimeout(function () {
session.addEventListener('visibilitychange', this.onSessionVisibilityEvent);

this.xrData.controllerA.setIndices(Module.ControllersArrayOffset);
this.xrData.controllerB.setIndices(Module.ControllersArrayOffset + 28);
this.xrData.controllerB.setIndices(Module.ControllersArrayOffset + 34);
this.xrData.handLeft.setIndices(Module.HandsArrayOffset);
this.xrData.handRight.setIndices(Module.HandsArrayOffset + 205);
this.xrData.viewerHitTestPose.setIndices(Module.ViewerHitTestPoseArrayOffset);
Expand Down
69 changes: 55 additions & 14 deletions Packages/webxr/Runtime/Scripts/WebXRController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,21 @@ public enum Axis2DTypes
public WebXRControllerHand hand = WebXRControllerHand.NONE;

private float trigger;
private bool triggerTouched;
private float squeeze;
private bool squeezeTouched;
private float thumbstick;
private bool thumbstickTouched;
private float thumbstickX;
private float thumbstickY;
private float touchpad;
private bool touchpadTouched;
private float touchpadX;
private float touchpadY;
private float buttonA;
private bool buttonATouched;
private float buttonB;
private bool buttonBTouched;

private WebXRControllerButton[] buttons;

Expand Down Expand Up @@ -102,42 +108,48 @@ private void Awake()
private void InitButtons()
{
buttons = new WebXRControllerButton[6];
buttons[(int)ButtonTypes.Trigger] = new WebXRControllerButton(trigger == 1, trigger);
buttons[(int)ButtonTypes.Grip] = new WebXRControllerButton(squeeze == 1, squeeze);
buttons[(int)ButtonTypes.Thumbstick] = new WebXRControllerButton(thumbstick == 1, thumbstick);
buttons[(int)ButtonTypes.Touchpad] = new WebXRControllerButton(touchpad == 1, touchpad);
buttons[(int)ButtonTypes.ButtonA] = new WebXRControllerButton(buttonA == 1, buttonA);
buttons[(int)ButtonTypes.ButtonB] = new WebXRControllerButton(buttonB == 1, buttonB);
buttons[(int)ButtonTypes.Trigger] = new WebXRControllerButton(trigger == 1, triggerTouched, trigger);
buttons[(int)ButtonTypes.Grip] = new WebXRControllerButton(squeeze == 1, squeezeTouched, squeeze);
buttons[(int)ButtonTypes.Thumbstick] = new WebXRControllerButton(thumbstick == 1, thumbstickTouched, thumbstick);
buttons[(int)ButtonTypes.Touchpad] = new WebXRControllerButton(touchpad == 1, touchpadTouched, touchpad);
buttons[(int)ButtonTypes.ButtonA] = new WebXRControllerButton(buttonA == 1, buttonATouched, buttonA);
buttons[(int)ButtonTypes.ButtonB] = new WebXRControllerButton(buttonB == 1, buttonBTouched, buttonB);
}

private void UpdateAllButtons()
{
buttons[(int)ButtonTypes.Trigger].UpdateState(trigger == 1, trigger);
buttons[(int)ButtonTypes.Grip].UpdateState(squeeze == 1, squeeze);
buttons[(int)ButtonTypes.Thumbstick].UpdateState(thumbstick == 1, thumbstick);
buttons[(int)ButtonTypes.Touchpad].UpdateState(touchpad == 1, touchpad);
buttons[(int)ButtonTypes.ButtonA].UpdateState(buttonA == 1, buttonA);
buttons[(int)ButtonTypes.ButtonB].UpdateState(buttonB == 1, buttonB);
buttons[(int)ButtonTypes.Trigger].UpdateState(trigger == 1, triggerTouched, trigger);
buttons[(int)ButtonTypes.Grip].UpdateState(squeeze == 1, squeezeTouched, squeeze);
buttons[(int)ButtonTypes.Thumbstick].UpdateState(thumbstick == 1, thumbstickTouched, thumbstick);
buttons[(int)ButtonTypes.Touchpad].UpdateState(touchpad == 1, touchpadTouched, touchpad);
buttons[(int)ButtonTypes.ButtonA].UpdateState(buttonA == 1, buttonATouched, buttonA);
buttons[(int)ButtonTypes.ButtonB].UpdateState(buttonB == 1, buttonBTouched, buttonB);
}

private void UpdateHandButtons()
{
buttons[(int)ButtonTypes.Trigger].UpdateState(trigger == 1, trigger);
buttons[(int)ButtonTypes.Grip].UpdateState(squeeze == 1, squeeze);
buttons[(int)ButtonTypes.Trigger].UpdateState(trigger == 1, trigger == 1, trigger);
buttons[(int)ButtonTypes.Grip].UpdateState(squeeze == 1, squeeze == 1, squeeze);
}

private void ResetAllButtons()
{
trigger = 0;
triggerTouched = false;
squeeze = 0;
squeezeTouched = false;
thumbstick = 0;
thumbstickTouched = false;
thumbstickX = 0;
thumbstickY = 0;
touchpad = 0;
touchpadTouched = false;
touchpadX = 0;
touchpadY = 0;
buttonA = 0;
buttonATouched = false;
buttonB = 0;
buttonBTouched = false;
if (buttons?.Length == 6)
{
UpdateAllButtons();
Expand Down Expand Up @@ -203,6 +215,23 @@ private void TryUpdateButtons()
buttonB = buttonPressed ? 1 : 0;
}

if (!inputDevice.Value.TryGetFeatureValue(CommonUsages.primaryTouch, out buttonATouched))
{
buttonATouched = buttonA > 0;
}
if (!inputDevice.Value.TryGetFeatureValue(CommonUsages.secondaryTouch, out buttonBTouched))
{
buttonBTouched = buttonB > 0;
}
if (!inputDevice.Value.TryGetFeatureValue(CommonUsages.primary2DAxisTouch, out thumbstickTouched))
{
thumbstickTouched = thumbstick > 0;
}
if (!inputDevice.Value.TryGetFeatureValue(CommonUsages.secondary2DAxisTouch, out touchpadTouched))
{
touchpadTouched = touchpad > 0;
}

if (buttons?.Length != 6)
{
InitButtons();
Expand Down Expand Up @@ -259,6 +288,12 @@ public bool GetButtonUp(ButtonTypes buttonType)
return buttons[(int)buttonType].up;
}

public bool GetButtonTouched(ButtonTypes buttonType)
{
TryUpdateButtons();
return buttons[(int)buttonType].touched;
}

public float GetButtonIndexValue(int index)
{
TryUpdateButtons();
Expand Down Expand Up @@ -352,15 +387,21 @@ private void OnControllerUpdate(WebXRControllerData controllerData)
}

trigger = controllerData.trigger;
triggerTouched = controllerData.triggerTouched;
squeeze = controllerData.squeeze;
squeezeTouched = controllerData.squeezeTouched;
thumbstick = controllerData.thumbstick;
thumbstickTouched = controllerData.thumbstickTouched;
thumbstickX = controllerData.thumbstickX;
thumbstickY = controllerData.thumbstickY;
touchpad = controllerData.touchpad;
touchpadTouched = controllerData.touchpadTouched;
touchpadX = controllerData.touchpadX;
touchpadY = controllerData.touchpadY;
buttonA = controllerData.buttonA;
buttonATouched = controllerData.buttonATouched;
buttonB = controllerData.buttonB;
buttonBTouched = controllerData.buttonBTouched;

if (buttons?.Length != 6)
{
Expand Down
13 changes: 11 additions & 2 deletions Packages/webxr/Runtime/Scripts/WebXRControllerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ public class WebXRControllerData
public Vector3 gripPosition;
public Quaternion gripRotation;
public float trigger;
public bool triggerTouched;
public float squeeze;
public bool squeezeTouched;
public float thumbstick;
public bool thumbstickTouched;
public float thumbstickX;
public float thumbstickY;
public float touchpad;
public bool touchpadTouched;
public float touchpadX;
public float touchpadY;
public float buttonA;
public bool buttonATouched;
public float buttonB;
public bool buttonBTouched;
public string[] profiles;
}

Expand Down Expand Up @@ -100,19 +106,21 @@ public enum WebXRControllerHand
public class WebXRControllerButton
{
public bool pressed;
public bool touched;
public bool down;
public bool up;
public float value;

public WebXRControllerButton(bool isPressed, float buttonValue)
public WebXRControllerButton(bool isPressed, bool isTouched, float buttonValue)
{
down = false;
up = false;
pressed = isPressed;
touched = isTouched;
value = buttonValue;
}

public void UpdateState(bool isPressed, float buttonValue)
public void UpdateState(bool isPressed, bool isTouched, float buttonValue)
{
if (isPressed && pressed) // nothing
{
Expand All @@ -135,6 +143,7 @@ public void UpdateState(bool isPressed, float buttonValue)
up = true;
}
pressed = isPressed;
touched = isTouched;
value = buttonValue;
}
}
Expand Down
10 changes: 8 additions & 2 deletions Packages/webxr/Runtime/XRPlugin/WebXRSubsystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ internal delegate void StartXREvent(int viewsCount,
float[] sharedArray = new float[(2 * 16) + (2 * 7)];

// Shared array for controllers data
float[] controllersArray = new float[2 * 28];
float[] controllersArray = new float[2 * 34];

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

bool GetGamepadFromControllersArray(int controllerIndex, ref WebXRControllerData newControllerData)
{
int arrayPosition = controllerIndex * 28;
int arrayPosition = controllerIndex * 34;
int frameNumber = (int)controllersArray[arrayPosition++];
if (newControllerData.frame == frameNumber)
{
Expand All @@ -479,15 +479,21 @@ bool GetGamepadFromControllersArray(int controllerIndex, ref WebXRControllerData
newControllerData.rotation = new Quaternion(controllersArray[arrayPosition++], controllersArray[arrayPosition++], controllersArray[arrayPosition++],
controllersArray[arrayPosition++]);
newControllerData.trigger = controllersArray[arrayPosition++];
newControllerData.triggerTouched = controllersArray[arrayPosition++] != 0;
newControllerData.squeeze = controllersArray[arrayPosition++];
newControllerData.squeezeTouched = controllersArray[arrayPosition++] != 0;
newControllerData.thumbstick = controllersArray[arrayPosition++];
newControllerData.thumbstickTouched = controllersArray[arrayPosition++] != 0;
newControllerData.thumbstickX = controllersArray[arrayPosition++];
newControllerData.thumbstickY = controllersArray[arrayPosition++];
newControllerData.touchpad = controllersArray[arrayPosition++];
newControllerData.touchpadTouched = controllersArray[arrayPosition++] != 0;
newControllerData.touchpadX = controllersArray[arrayPosition++];
newControllerData.touchpadY = controllersArray[arrayPosition++];
newControllerData.buttonA = controllersArray[arrayPosition++];
newControllerData.buttonATouched = controllersArray[arrayPosition++] != 0;
newControllerData.buttonB = controllersArray[arrayPosition++];
newControllerData.buttonBTouched = controllersArray[arrayPosition++] != 0;
if (controllersArray[arrayPosition] == 1)
{
controllersArray[arrayPosition++] = 2;
Expand Down

0 comments on commit 892d1e8

Please sign in to comment.