Skip to content

Commit 94c7a9b

Browse files
committed
Support for targetRaySpace for controllers poses
1 parent 6858535 commit 94c7a9b

File tree

8 files changed

+101
-13
lines changed

8 files changed

+101
-13
lines changed

Packages/webxr-interactions/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ 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+
### Added
9+
- Check for alwaysUseGrip in ControllerInteraction.
810

911
## [0.6.0] - 2020-01-23
1012
### Added

Packages/webxr-interactions/Runtime/Scripts/ControllerInteraction.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ private void OnEnable()
8080
controller.OnControllerActive += SetControllerVisible;
8181
controller.OnHandActive += SetHandJointsVisible;
8282
controller.OnHandUpdate += OnHandUpdate;
83+
controller.OnAlwaysUseGripChanged += SetInputProfileModelPose;
8384
}
8485

8586
private void OnDisable()
8687
{
8788
controller.OnControllerActive -= SetControllerVisible;
8889
controller.OnHandActive -= SetHandJointsVisible;
8990
controller.OnHandUpdate -= OnHandUpdate;
91+
controller.OnAlwaysUseGripChanged -= SetInputProfileModelPose;
9092
}
9193

9294
private void Update()
@@ -165,6 +167,7 @@ private void SetControllerVisible(bool visible)
165167
// We want to use WebXR Input Profiles
166168
if (visible && useInputProfile)
167169
{
170+
SetInputProfileModelPose(controller.GetAlwaysUseGrip());
168171
if (inputProfileModel != null && loadedModel)
169172
{
170173
// There's a loaded Input Profile Model
@@ -193,6 +196,12 @@ private void SetControllerVisible(bool visible)
193196
}
194197
}
195198

199+
private void SetInputProfileModelPose(bool alwaysUseGrip)
200+
{
201+
inputProfileModelParent.transform.localPosition = alwaysUseGrip ? Vector3.zero : controller.gripPosition;
202+
inputProfileModelParent.transform.localRotation = alwaysUseGrip ? Quaternion.identity : controller.gripRotation;
203+
}
204+
196205
private void SetHandJointsVisible(bool visible)
197206
{
198207
handJointsVisible = visible;

Packages/webxr/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ 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+
### Added
9+
- Support for `targetRaySpace` for controllers poses.
10+
11+
### Changed
12+
- Use `targetRaySpace` as the default controller pose instead of `gripSpace`.
813

914
## [0.6.0] - 2020-01-23
1015
### Added

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ mergeInto(LibraryManager.library, {
8282
Module.ControllersArray[index++] = data[key].touchpadY;
8383
Module.ControllersArray[index++] = data[key].buttonA;
8484
Module.ControllersArray[index++] = data[key].buttonB;
85+
Module.ControllersArray[index++] = data[key].updateGrip;
86+
if (data[key].updateGrip == 1) {
87+
Module.ControllersArray[index++] = data[key].gripPositionX;
88+
Module.ControllersArray[index++] = data[key].gripPositionY;
89+
Module.ControllersArray[index++] = data[key].gripPositionZ;
90+
Module.ControllersArray[index++] = data[key].gripRotationX;
91+
Module.ControllersArray[index++] = data[key].gripRotationY;
92+
Module.ControllersArray[index++] = data[key].gripRotationZ;
93+
Module.ControllersArray[index++] = data[key].gripRotationW;
94+
} else {
95+
index += 7;
96+
}
8597
});
8698
});
8799
document.addEventListener('XRHandsData', function(evt) {

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

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ setTimeout(function () {
8282
this.rotationY = 0;
8383
this.rotationZ = 0;
8484
this.rotationW = 0;
85+
this.gripPositionX = 0;
86+
this.gripPositionY = 0;
87+
this.gripPositionZ = 0;
88+
this.gripRotationX = 0;
89+
this.gripRotationY = 0;
90+
this.gripRotationZ = 0;
91+
this.gripRotationW = 0;
8592
this.trigger = 0;
8693
this.squeeze = 0;
8794
this.thumbstick = 0;
@@ -95,6 +102,7 @@ setTimeout(function () {
95102
this.gamepad = null;
96103
this.profiles = [];
97104
this.updatedProfiles = 0;
105+
this.updatedGrip = 0;
98106
}
99107

100108
function XRHandData() {
@@ -637,10 +645,10 @@ setTimeout(function () {
637645
}
638646
}
639647
} else if (inputSource.gripSpace) {
640-
var inputPose = frame.getPose(inputSource.gripSpace, refSpace);
641-
if (inputPose) {
642-
var position = inputPose.transform.position;
643-
var orientation = inputPose.transform.orientation;
648+
var inputRayPose = frame.getPose(inputSource.targetRaySpace, refSpace);
649+
if (inputRayPose) {
650+
var position = inputRayPose.transform.position;
651+
var orientation = inputRayPose.transform.orientation;
644652
var hand = 0;
645653
var controller = xrData.controllerA;
646654
if (inputSource.handedness == 'left') {
@@ -652,9 +660,8 @@ setTimeout(function () {
652660

653661
controller.enabled = 1;
654662
controller.hand = hand;
655-
656-
if (controller.updatedProfiles == 0)
657-
{
663+
664+
if (controller.updatedProfiles == 0) {
658665
controller.profiles = inputSource.profiles;
659666
controller.updatedProfiles = 1;
660667
}
@@ -667,9 +674,27 @@ setTimeout(function () {
667674
controller.rotationY = -orientation.y;
668675
controller.rotationZ = orientation.z;
669676
controller.rotationW = orientation.w;
677+
678+
if (controller.updatedGrip == 0) {
679+
var inputPose = frame.getPose(inputSource.gripSpace, refSpace);
680+
if (inputPose) {
681+
var gripPosition = inputPose.transform.position;
682+
var gripOrientation = inputPose.transform.orientation;
683+
684+
controller.gripPositionX = gripPosition.x;
685+
controller.gripPositionY = gripPosition.y;
686+
controller.gripPositionZ = - gripPosition.z;
687+
688+
controller.gripRotationX = -gripOrientation.x;
689+
controller.gripRotationY = -gripOrientation.y;
690+
controller.gripRotationZ = gripOrientation.z;
691+
controller.gripRotationW = gripOrientation.w;
692+
693+
controller.updatedGrip = 1;
694+
}
695+
}
670696

671697
// if there's gamepad, use the xr-standard mapping
672-
// TODO: check for profiles
673698
if (inputSource.gamepad) {
674699
for (var j = 0; j < inputSource.gamepad.buttons.length; j++) {
675700
switch (j) {
@@ -900,6 +925,12 @@ setTimeout(function () {
900925
controllerA: xrData.controllerA,
901926
controllerB: xrData.controllerB
902927
}}));
928+
if (xrData.controllerA.updatedGrip == 1) {
929+
xrData.controllerA.updatedGrip = 2;
930+
}
931+
if (xrData.controllerB.updatedGrip == 1) {
932+
xrData.controllerB.updatedGrip = 2;
933+
}
903934

904935
document.dispatchEvent(new CustomEvent('XRHandsData', { detail: {
905936
handLeft: xrData.handLeft,

Packages/webxr/Runtime/Scripts/WebXRController.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public enum Axis2DTypes
3535
public Action<bool> OnControllerActive;
3636
public Action<bool> OnHandActive;
3737
public Action<WebXRHandData> OnHandUpdate;
38+
public Action<bool> OnAlwaysUseGripChanged;
3839

3940
[Tooltip("Controller hand to use.")]
4041
public WebXRControllerHand hand = WebXRControllerHand.NONE;
@@ -57,6 +58,10 @@ public enum Axis2DTypes
5758

5859
private string[] profiles = null;
5960

61+
[SerializeField] private bool alwaysUseGrip = false;
62+
public Vector3 gripPosition { get; private set;} = Vector3.zero;
63+
public Quaternion gripRotation { get; private set;} = Quaternion.identity;
64+
6065
public bool isControllerActive
6166
{
6267
get
@@ -270,6 +275,17 @@ public float GetAxisIndexValue(int index)
270275
return 0;
271276
}
272277

278+
public void SetAlwaysUseGrip(bool value)
279+
{
280+
alwaysUseGrip = value;
281+
OnAlwaysUseGripChanged?.Invoke(alwaysUseGrip);
282+
}
283+
284+
public bool GetAlwaysUseGrip()
285+
{
286+
return alwaysUseGrip;
287+
}
288+
273289
public string[] GetProfiles()
274290
{
275291
return profiles;
@@ -287,8 +303,10 @@ private void OnControllerUpdate(WebXRControllerData controllerData)
287303

288304
profiles = controllerData.profiles;
289305

290-
transform.localRotation = controllerData.rotation;
291-
transform.localPosition = controllerData.position;
306+
gripRotation = controllerData.gripRotation;
307+
gripPosition = controllerData.gripPosition;
308+
transform.localRotation = alwaysUseGrip ? gripRotation : controllerData.rotation;
309+
transform.localPosition = alwaysUseGrip ? gripPosition : controllerData.position;
292310

293311
trigger = controllerData.trigger;
294312
squeeze = controllerData.squeeze;

Packages/webxr/Runtime/Scripts/WebXRControllerData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class WebXRControllerData
1010
public int hand;
1111
public Vector3 position;
1212
public Quaternion rotation;
13+
public Vector3 gripPosition;
14+
public Quaternion gripRotation;
1315
public float trigger;
1416
public float squeeze;
1517
public float thumbstick;

Packages/webxr/Runtime/XRPlugin/WebXRSubsystem.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public delegate void HeadsetUpdate(
242242
float[] sharedArray = new float[(2 * 16) + (2 * 7)];
243243

244244
// Shared array for controllers data
245-
float[] controllersArray = new float[2 * 20];
245+
float[] controllersArray = new float[2 * 28];
246246

247247
// Shared array for hands data
248248
float[] handsArray = new float[2 * (25 * 9 + 5)];
@@ -376,7 +376,7 @@ void GetVector3FromSharedArray(int index, ref Vector3 vec3)
376376

377377
bool GetGamepadFromControllersArray(int controllerIndex, ref WebXRControllerData newControllerData)
378378
{
379-
int arrayPosition = controllerIndex * 20;
379+
int arrayPosition = controllerIndex * 28;
380380
int frameNumber = (int)controllersArray[arrayPosition++];
381381
if (newControllerData.frame == frameNumber)
382382
{
@@ -403,7 +403,16 @@ bool GetGamepadFromControllersArray(int controllerIndex, ref WebXRControllerData
403403
newControllerData.touchpadX = controllersArray[arrayPosition++];
404404
newControllerData.touchpadY = controllersArray[arrayPosition++];
405405
newControllerData.buttonA = controllersArray[arrayPosition++];
406-
newControllerData.buttonB = controllersArray[arrayPosition];
406+
newControllerData.buttonB = controllersArray[arrayPosition++];
407+
if (controllersArray[arrayPosition++] == 1)
408+
{
409+
newControllerData.gripPosition = new Vector3(controllersArray[arrayPosition++], controllersArray[arrayPosition++], controllersArray[arrayPosition++]);
410+
newControllerData.gripRotation = new Quaternion(controllersArray[arrayPosition++], controllersArray[arrayPosition++], controllersArray[arrayPosition++],
411+
controllersArray[arrayPosition++]);
412+
Quaternion rotationOffset = Quaternion.Inverse(newControllerData.rotation);
413+
newControllerData.gripRotation = rotationOffset * newControllerData.gripRotation;
414+
newControllerData.gripPosition = rotationOffset * (newControllerData.gripPosition - newControllerData.position);
415+
}
407416
return true;
408417
}
409418

0 commit comments

Comments
 (0)