【问题标题】:Access Oculus Go Controller via GamePad API in Oculus Browser?通过 Oculus 浏览器中的 GamePad API 访问 Oculus Go 控制器?
【发布时间】:2019-04-11 09:48:00
【问题描述】:

我正在我的新 Oculus Go 中测试我的几个 three.js 应用程序。我想知道是否可以仅使用目前主流浏览器上似乎可用的 GamePad API 来访问控制器。

查看 Oculus 文档,似乎可以通过 Unity 附带的 OVRManager 或通过 UnReal Blueprints 完成。但是我试图避免另一个学习曲线,如果可以避免的话,我也会扩大我的应用程序。

作为 VR 的新手,似乎最有利的方式就是使用 Gamepad API 执行类似的操作(这部分代码还不能正常工作,但我正在尝试在这种方法的问题上,除了在我进入 VR 模式时破坏应用程序之外没有任何结果):

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 构建以获得我想要的结果,这似乎既是额外的学习曲线,又会增加我的开销。如果必须,我会这样做,但如果我不这样做,我宁愿不这样做。

最终我希望能够使用如下逻辑支持大多数“主要”控制器:

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 控制器,以及如何访问设备属性,例如按钮、轴和方向以及相关的游戏板事件?

谢谢。

【问题讨论】:

  • 您能否再解释一下如何使用游戏手柄 API 中的 navigator.getGamepads() 无法满足您的需求?我不是 web vr 方面的专家,所以如果这对我来说应该是显而易见的,我们深表歉意。
  • 这实际上有点难以解释,因为我还不能从 Oculus Go 内部检查我的代码。抱歉,希望我能在那里提供更多帮助,但部分问题是我看不到我可能会遇到什么错误。同时尝试从我的笔记本电脑访问控制器,从长远来看,我可以检查代码可能会更加迂回。所以......也许我需要提出另一个问题,关于如何能够检查我在耳机的 Oculus 浏览器中运行的代码。 90 分钟的发帖限制,在 StackExchange 上,我会看看我能做些什么。
  • 如果您还没有尝试这里的步骤:developer.oculus.com/documentation/vrweb/latest/concepts/…

标签: javascript three.js virtual-reality oculus oculusgo


【解决方案1】:

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

【讨论】:

    最近更新 更多