Skip to content

Commit

Permalink
Merge pull request #975 from dmarcos/oculusMobileButtonMappings
Browse files Browse the repository at this point in the history
Match Oculus Quest and Rift button mappings
  • Loading branch information
avaer committed Apr 25, 2019
2 parents 74857e0 + 44649f1 commit 0a070e6
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 35 deletions.
71 changes: 59 additions & 12 deletions deps/oculus-mobile/src/oculus-context.cpp
Expand Up @@ -267,11 +267,11 @@ NAN_METHOD(OculusMobileContext::WaitGetPoses) {
float *controllerMatrixLeft = float32ArrayData + index;
index += 16;
float *controllerStateLeft = float32ArrayData + index;
index += 5;
index += 12;
float *controllerMatrixRight = float32ArrayData + index;
index += 16;
float *controllerStateRight = float32ArrayData + index;
index += 5;
index += 12;

oculusMobileContext->PollEvents(false);

Expand Down Expand Up @@ -324,11 +324,34 @@ NAN_METHOD(OculusMobileContext::WaitGetPoses) {
ovrInputStateTrackedRemote remoteState;
remoteState.Header.ControllerType = ovrControllerType_TrackedRemote;
vrapi_GetCurrentInputState(oculusMobileContext->ovrState, capsHeader.DeviceID, &remoteState.Header);
controllerStateLeft[0] = remoteState.IndexTrigger;
controllerStateLeft[1] = remoteState.GripTrigger;
controllerStateLeft[2] = remoteState.TrackpadPosition.x;
controllerStateLeft[3] = remoteState.TrackpadPosition.y;
controllerStateLeft[4] = (remoteState.Buttons & ovrButton_Enter) ? 1 : (remoteState.TrackpadStatus ? 0.5 : 0);
if (remoteCaps.ControllerCapabilities & ovrControllerCaps_ModelOculusGo ||
remoteCaps.ControllerCapabilities & ovrControllerCaps_ModelGearVR) {
controllerStateLeft[0] = remoteState.IndexTrigger;
controllerStateLeft[1] = (remoteState.Buttons & ovrButton_Enter) ? 1 : 0;
controllerStateLeft[2] = (remoteState.Buttons & ovrButton_Back) ? 1 : 0;
controllerStateLeft[3] = remoteState.TrackpadPosition.x;
controllerStateLeft[4] = remoteState.TrackpadPosition.y;
} else {
// Buttons
controllerStateLeft[0] = (remoteState.Buttons & ovrButton_X) ? 1 : 0;
controllerStateLeft[1] = (remoteState.Buttons & ovrButton_Y) ? 1 : 0;
controllerStateLeft[2] = (remoteState.Buttons & ovrButton_LThumb) ? 1 : 0;
controllerStateLeft[3] = (remoteState.Buttons & ovrButton_Enter) ? 1 : 0;

// Triggers
controllerStateLeft[4] = remoteState.IndexTrigger;
controllerStateLeft[5] = remoteState.GripTrigger;

// Touches
controllerStateLeft[6] = (remoteState.Touches & ovrTouch_X) ? 1 : 0;
controllerStateLeft[7] = (remoteState.Touches & ovrTouch_Y) ? 1 : 0;
controllerStateLeft[8] = (remoteState.Touches & ovrTouch_Joystick) ? 1 : 0;
controllerStateLeft[9] = (remoteState.Touches & ovrTouch_IndexTrigger) ? 1 : 0;

// Thumbstick axis.
controllerStateLeft[10] = remoteState.Joystick.x;
controllerStateLeft[11] = remoteState.Joystick.y;
}
} else if (remoteCaps.ControllerCapabilities & ovrControllerCaps_RightHand) {
ovrTracking tracking;
vrapi_GetInputTrackingState(oculusMobileContext->ovrState, capsHeader.DeviceID, predictedDisplayTime, &tracking);
Expand All @@ -340,11 +363,35 @@ NAN_METHOD(OculusMobileContext::WaitGetPoses) {
ovrInputStateTrackedRemote remoteState;
remoteState.Header.ControllerType = ovrControllerType_TrackedRemote;
vrapi_GetCurrentInputState(oculusMobileContext->ovrState, capsHeader.DeviceID, &remoteState.Header);
controllerStateRight[0] = remoteState.IndexTrigger;
controllerStateRight[1] = remoteState.GripTrigger;
controllerStateRight[2] = remoteState.TrackpadPosition.x;
controllerStateRight[3] = remoteState.TrackpadPosition.y;
controllerStateRight[4] = (remoteState.Buttons & ovrButton_Enter) ? 1 : (remoteState.TrackpadStatus ? 0.5 : 0);

if (remoteCaps.ControllerCapabilities & ovrControllerCaps_ModelOculusGo ||
remoteCaps.ControllerCapabilities & ovrControllerCaps_ModelGearVR) {
controllerStateRight[0] = remoteState.IndexTrigger;
controllerStateRight[1] = (remoteState.Buttons & ovrButton_Enter) ? 1 : 0;
controllerStateRight[2] = (remoteState.Buttons & ovrButton_Back) ? 1 : 0;
controllerStateRight[3] = remoteState.TrackpadPosition.x;
controllerStateRight[4] = remoteState.TrackpadPosition.y;
} else {
// Buttons
controllerStateRight[0] = (remoteState.Buttons & ovrButton_A) ? 1 : 0;
controllerStateRight[1] = (remoteState.Buttons & ovrButton_B) ? 1 : 0;
controllerStateRight[2] = (remoteState.Buttons & ovrButton_RThumb) ? 1 : 0;
controllerStateRight[3] = (remoteState.Buttons & ovrButton_Enter) ? 1 : 0;

// Triggers
controllerStateRight[4] = remoteState.IndexTrigger;
controllerStateRight[5] = remoteState.GripTrigger;

// Touches
controllerStateRight[6] = (remoteState.Touches & ovrTouch_A) ? 1 : 0;
controllerStateRight[7] = (remoteState.Touches & ovrTouch_B) ? 1 : 0;
controllerStateRight[8] = (remoteState.Touches & ovrTouch_Joystick) ? 1 : 0;
controllerStateRight[9] = (remoteState.Touches & ovrTouch_IndexTrigger) ? 1 : 0;

// Thumbstick axis.
controllerStateRight[10] = remoteState.Joystick.x;
controllerStateRight[11] = remoteState.Joystick.y;
}
} else {
// not a hand
}
Expand Down
66 changes: 43 additions & 23 deletions src/index.js
Expand Up @@ -360,7 +360,7 @@ const rightControllerPositionArray3 = new Float32Array(3);
const rightControllerQuaternionArray4 = new Float32Array(4);

// oculus mobile
const oculusMobilePoseFloat32Array = new Float32Array(3 + 4 + 1 + 4 + (16*2) + (16*2) + (16+5) + (16+5));
const oculusMobilePoseFloat32Array = new Float32Array(3 + 4 + 1 + 4 + (16*2) + (16*2) + (16+12) + (16+12));

const handEntrySize = (1 + (5 * 5)) * (3 + 3);
const transformArray = new Float32Array(7 * 2);
Expand Down Expand Up @@ -539,7 +539,7 @@ if (nativeBindings.nativeOculusVR) {
const _attribute = (name, value) => {
if (name === 'width' || name === 'height') {
nativeBindings.nativeWindow.setCurrentWindowContext(windowHandle);

const [fbo, tex, depthTex, msFbo, msTex, msDepthTex] = system.CreateSwapChain(context, canvas.width, canvas.height);
context.setDefaultFramebuffer(msFbo);
vrPresentState.fbo = fbo;
Expand Down Expand Up @@ -1382,7 +1382,7 @@ const _startRenderLoop = () => {

vrPresentState.oculusSystem.Submit();
vrPresentState.hasPose = false;

const width = vrPresentState.glContext.canvas.width * (args.blit ? 0.5 : 1);
const height = vrPresentState.glContext.canvas.height;
nativeWindow.blitFrameBuffer(context, vrPresentState.msFbo, 0, width, height, width, height, true, false, false);
Expand Down Expand Up @@ -1774,8 +1774,8 @@ const _startRenderLoop = () => {
const leftGamepad = xrState.gamepads[0];
const gamepadFloat32Array = new Float32Array(oculusMobilePoseFloat32Array.buffer, index, 16);
index += 16*Float32Array.BYTES_PER_ELEMENT;
const buttonsFloat32Array = new Float32Array(oculusMobilePoseFloat32Array.buffer, index, 5);
index += 5*Float32Array.BYTES_PER_ELEMENT;
const buttonsFloat32Array = new Float32Array(oculusMobilePoseFloat32Array.buffer, index, 12);
index += 12*Float32Array.BYTES_PER_ELEMENT;
if (!isNaN(gamepadFloat32Array[0])) {
leftGamepad.connected[0] = true;

Expand All @@ -1784,17 +1784,27 @@ const _startRenderLoop = () => {
localVector.toArray(leftGamepad.position);
localQuaternion.toArray(leftGamepad.orientation);

leftGamepad.buttons[1].value[0] = buttonsFloat32Array[0]; // trigger
leftGamepad.buttons[1].pressed[0] = leftGamepad.buttons[1].touched[0] = buttonsFloat32Array[0] ? 1 : 0;
// pressed
leftGamepad.buttons[0].pressed[0] = buttonsFloat32Array[2]; // thumbstick
leftGamepad.buttons[1].pressed[0] = buttonsFloat32Array[4] >= 0.1; // trigger
leftGamepad.buttons[2].pressed[0] = buttonsFloat32Array[5] >= 0.1; // grip
leftGamepad.buttons[3].pressed[0] = buttonsFloat32Array[0] == 1; // xbutton
leftGamepad.buttons[4].pressed[0] = buttonsFloat32Array[1] == 1; // ybutton
leftGamepad.buttons[5].pressed[0] = buttonsFloat32Array[3] == 1; // menu

leftGamepad.buttons[2].value[0] = buttonsFloat32Array[1]; // grip
leftGamepad.buttons[2].pressed[0] = leftGamepad.buttons[2].touched[0] = buttonsFloat32Array[1] ? 1 : 0;
// touched
leftGamepad.buttons[0].touched[0] = buttonsFloat32Array[8]; // thumbstick
leftGamepad.buttons[1].touched[0] = buttonsFloat32Array[9]; // trigger
leftGamepad.buttons[3].touched[0] = buttonsFloat32Array[6]; // xbutton
leftGamepad.buttons[4].touched[0] = buttonsFloat32Array[7]; // ybutton

leftGamepad.axes[0] = buttonsFloat32Array[2];
leftGamepad.axes[1] = buttonsFloat32Array[3];
// thumbstick axis
leftGamepad.axes[0] = buttonsFloat32Array[10];
leftGamepad.axes[1] = buttonsFloat32Array[11];

leftGamepad.buttons[0].touched[0] = buttonsFloat32Array[4] >= 0.5 ? 1 : 0; // pad
leftGamepad.buttons[0].pressed[0] = buttonsFloat32Array[4] >= 1 ? 1 : 0;
// values
leftGamepad.buttons[1].value[0] = buttonsFloat32Array[4]; // trigger
leftGamepad.buttons[2].value[0] = buttonsFloat32Array[5]; // grip
} else {
leftGamepad.connected[0] = 0;
}
Expand All @@ -1803,8 +1813,8 @@ const _startRenderLoop = () => {
const rightGamepad = xrState.gamepads[1];
const gamepadFloat32Array = new Float32Array(oculusMobilePoseFloat32Array.buffer, index, 16);
index += 16*Float32Array.BYTES_PER_ELEMENT;
const buttonsFloat32Array = new Float32Array(oculusMobilePoseFloat32Array.buffer, index, 5);
index += 5*Float32Array.BYTES_PER_ELEMENT;
const buttonsFloat32Array = new Float32Array(oculusMobilePoseFloat32Array.buffer, index, 12);
index += 12*Float32Array.BYTES_PER_ELEMENT;
if (!isNaN(gamepadFloat32Array[0])) {
rightGamepad.connected[0] = true;

Expand All @@ -1813,17 +1823,27 @@ const _startRenderLoop = () => {
localVector.toArray(rightGamepad.position);
localQuaternion.toArray(rightGamepad.orientation);

rightGamepad.buttons[1].value[0] = buttonsFloat32Array[0]; // trigger
rightGamepad.buttons[1].pressed[0] = rightGamepad.buttons[1].touched[0] = buttonsFloat32Array[0] ? 1 : 0;
// pressed
rightGamepad.buttons[0].pressed[0] = buttonsFloat32Array[2]; // thumbstick
rightGamepad.buttons[1].pressed[0] = buttonsFloat32Array[4] >= 0.1; // trigger
rightGamepad.buttons[2].pressed[0] = buttonsFloat32Array[5] >= 0.1; // grip
rightGamepad.buttons[3].pressed[0] = buttonsFloat32Array[0] == 1; // xbutton
rightGamepad.buttons[4].pressed[0] = buttonsFloat32Array[1] == 1; // ybutton
rightGamepad.buttons[5].pressed[0] = buttonsFloat32Array[3] == 1; // menu

rightGamepad.buttons[2].value[0] = buttonsFloat32Array[1]; // grip
rightGamepad.buttons[2].pressed[0] = rightGamepad.buttons[2].touched[0] = buttonsFloat32Array[1] ? 1 : 0;
// touched
rightGamepad.buttons[0].touched[0] = buttonsFloat32Array[8]; // thumbstick
rightGamepad.buttons[1].touched[0] = buttonsFloat32Array[9]; // trigger
rightGamepad.buttons[3].touched[0] = buttonsFloat32Array[6]; // xbutton
rightGamepad.buttons[4].touched[0] = buttonsFloat32Array[7]; // ybutton

rightGamepad.axes[0] = buttonsFloat32Array[2];
rightGamepad.axes[1] = buttonsFloat32Array[3];
// thumbstick axis
rightGamepad.axes[0] = buttonsFloat32Array[10];
rightGamepad.axes[1] = buttonsFloat32Array[11];

rightGamepad.buttons[0].touched[0] = buttonsFloat32Array[4] >= 0.5 ? 1 : 0; // pad
rightGamepad.buttons[0].pressed[0] = buttonsFloat32Array[4] >= 1 ? 1 : 0;
// values
rightGamepad.buttons[1].value[0] = buttonsFloat32Array[4]; // trigger
rightGamepad.buttons[2].value[0] = buttonsFloat32Array[5]; // grip
} else {
rightGamepad.connected[0] = 0;
}
Expand Down

0 comments on commit 0a070e6

Please sign in to comment.