Access Oculus Go Controller via GamePad API in Oculus Browser?

霸气de小男生 提交于 2019-12-11 04:04:43

问题


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.


回答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.



来源:https://stackoverflow.com/questions/53198367/access-oculus-go-controller-via-gamepad-api-in-oculus-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!