我正在我的新 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
}
}
});
}
在我就这个主题提出的另一个更狭窄的问题中,有人建议我在我的应用程序中创建一个 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 控制器,以及如何访问设备属性,例如按钮、轴和方向以及相关的游戏手柄事件?
谢谢。