Access Oculus Go Controller via GamePad API in Ocu

2019-07-26 11:06发布

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.

0条回答
登录 后发表回答