3

I'm testing out a couple of my three.js apps in my new Oculus Go. I'm wondering if it's possible to access the controller just using the GamePad API that seems to be available today with the major browsers.

Looking at the Oculus documentation, it seems like it can be done through the OVRManager that comes with Unity, or through UnReal Blueprints. But I'm trying to avoid another learning curve, as well as bulking up my apps if I can avoid it.

As a newbie to VR, it seems the most favorable way to proceed would simply be with using the Gamepad API by doing something like this (the guts of this isn't working code just yet, but I'm trying to get at the problem with this sort of approach, and no results so far other than breaking the app when I go into VR mode):

var gamePadState = {
  lastButtons: {},
  lastAxes: {}
};

function onGamePad(){

    Array.prototype.forEach.call( navigator.getGamepads(), function (activePad, padIndex){
      if ( activePad.connected ) {
        if (activePad.id.includes("Oculus Go")) {
          // Process buttons and axes for the Gear VR touch panel
          activePad.buttons.forEach( function ( gamepadButton, buttonIndex ){
            if ( buttonIndex === 0 && gamepadButton.pressed && !lastButtons[buttonIndex]){
              // Handle tap
              dollyCam.translateZ( -0.01 );
            }
            gamePadState.lastButtons[buttonIndex] = gamepadButton.pressed;
          });

          activePad.axes.forEach( function (axisValue, axisIndex ) {
            if (axisIndex === 0 && axisValue < 0 && lastAxes[axisIndex] >= 0 ){
              // Handle swipe right
            } else if (axisIndex === 0 && axisValue > 0 && lastAxes[axisIndex] <= 0) {
              // Handle swipe left
            } else if (axisIndex === 1 && axisValue < 0 && lastAxes[axisIndex] >= 0) {
              // Handle swipe up
            } else if (axisIndex === 1 && axisValue > 0 && lastAxes[axisIndex] <= 0) {
              // Handle swipe down
            }
            gamePadState.lastAxes[axisIndex] = axisValue;
          });
        } else {
          // This is a connected Bluetooth gamepad which you may want to support in your VR experience
        }
      }
    });

}

In another, narrower question I posed on this topic, I was advised to create a Unity build in my app to get the results I wanted, which seems like both an additional learning curve, plus bulking up my overhead. I'll do it if I have to, but I'd rather not, if I don't.

Ultimately I'd like to be able to support most of the "major" controllers using logic like this:

onGamePad( event ){

    var gamePads = navigator.getGamepads();

    if ( gamePads && gamePads.length > 0 && event.isSomeGamePadEventOfSomeSort ){
        for ( var p = 0; p < gamePads.length; p ++ ){
            if ( gamePads[ p ].id.includes( "Oculus Go" ) ){
                // process buttons and axes for the Oculus Go Controller here
                if ( event[ some property... gamePad?? ].id.includes( "Oculus Go" ) && event[ some property... gamePad?? ].button.pressed === someIndex ){
                    doSomething();              
                }
            }
            else if ( gamePads[ p ].id.includes( "Oculus Gear VR" ){
                // Process buttons and axes for the Oculus Gear VR Controller here...
            }
        }
    }

}

But for testing purposes I'd be grateful just to get the Oculus Go controller going for now.

So... Is it possible to access the Oculus Go controller via the Gamepad API, and how can I access the device properties such as buttons, axes and orientation, and related gamepad events?

Thanks.

Emma Scott Lavin
  • 185
  • 1
  • 14
  • Could you explain a little more how using `navigator.getGamepads()` from the gamepad API doesn't meet your needs? I am not an expert on web vr so apologies if this is something that should be obvious to me. – Ruzihm Nov 07 '18 at 22:20
  • Its actually a bit difficult to explain since I'm not yet able to inspect my code from inside the Oculus Go. Sorry, wish I could be a bit more helpful there, but part of the problem is that I can't see what errors I might be getting. Meanwhile trying to access the controller from my laptop, where I CAN inspect code might be significantly more roundabout in the long run. So... maybe I need to pose yet another question about how to be able to inspect the code I'm running from inside the Oculus browser in the headset. 90 minute posting limitations here, on StackExchange, I'll see what I can do. – Emma Scott Lavin Nov 07 '18 at 22:45
  • Try the steps here if you haven't already: https://developer.oculus.com/documentation/vrweb/latest/concepts/carmel-remote-debugging/ – Ruzihm Nov 07 '18 at 22:49
  • @Ruzihm link is dead – kungfooman Feb 07 '23 at 14:21

1 Answers1

1

Of course you can use the Gamepad API directly without recourse to Unity. Some months ago I wrote my own Oculus Go controller and it takes far less code than you'd think. Since my code is already freely available on GitHub, I'm not copying it to this site.

Paul Masson
  • 241
  • 1
  • 7
  • You mean https://github.com/paulmasson/paulmasson.github.io/blob/master/webxr-worlds/js/VRController.js now? – kungfooman Feb 07 '23 at 14:22