首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Oculus浏览器中通过GamePad API访问Oculus Go控制器?

在Oculus浏览器中通过GamePad API访问Oculus Go控制器?
EN

Stack Overflow用户
提问于 2018-11-08 05:49:09
回答 1查看 444关注 0票数 3

我正在我的新Oculus Go上测试我的几个three.js应用程序。我想知道是否有可能只使用目前主要浏览器提供的GamePad应用程序接口来访问控制器。

看一下Oculus文档,似乎可以通过Unity附带的OVRManager或通过UnReal蓝图来完成。但我正在努力避免另一条学习曲线,如果我能避免的话,我也会增加我的应用程序。

作为VR的新手,似乎最好的方法是使用Gamepad API,做这样的事情(它的核心还不是有效的代码,但我正在尝试用这种方法来解决问题,到目前为止,除了在进入VR模式时破坏应用程序之外,没有任何结果):

代码语言:javascript
运行
复制
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
        }
      }
    });

}

在我关于这个主题的another, narrower question中,有人建议我在我的应用程序中创建一个Unity构建来获得我想要的结果,这似乎既是额外的学习曲线,又增加了我的开销。如果有必要的话,我会做的,但如果我不做,我宁愿不做。

最终,我希望能够使用这样的逻辑来支持大多数“主要”控制器:

代码语言:javascript
运行
复制
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...
            }
        }
    }

}

但出于测试的目的,我很感激现在能让Oculus Go控制器运行起来。

所以..。是否可以通过Gamepad API访问Oculus Go控制器,以及如何访问设备属性,如按钮、轴和方向以及相关的gamepad事件?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-07-29 04:41:47

当然,您可以直接使用Gamepad API,而无需求助于Unity。几个月前,我写了自己的Oculus Go controller,它需要的代码比你想象的要少得多。因为我的代码已经在GitHub上免费提供了,所以我不会把它复制到这个站点上。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53198367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档