网页是否可以仅使用一些通用的 javascript 代码(如 document.addEventListener
)来监听 Oculus Controller 的按钮按下情况?
默认情况下,在 VR 浏览器中,主要摇杆会上下滚动页面。这个想法是重新映射它以触发不同的 Action 。
根据我的研究,我似乎需要使用 A 型框架,但我正在寻找一种通用解决方案,该解决方案仅使用 Vanilla Javascript 即可在不同网站上运行,而不是在沉浸式环境中运行。
最佳答案
简而言之,没有。
在沉浸式 WebXR 上下文之外无法与 Oculus Controller 交互。
Inputs and Sources section of the WebXR API状态:
While the Gamepad record is defined by the Gamepad API specification, it's not actually managed by the Gamepad API
因此 Gamepad API 将无法工作,我们不得不使用 WebXR API。此外,我们被迫为我们的 webxr session 使用一种沉浸式模式。 WebXR Controller Sample状态(确认这一点,请参阅下面规则中的评论):
WebXR gamepads are not available during inline sessions.
话虽这么说,你仍然可以......
用最少的(普通的)Javascript 来做。
根据您对 Vanilla 的定义,这将起作用(Codesandbox):
const gl2 = document
.querySelector('canvas.c3d')
.getContext('webgl2');
const session = await navigator.xr.requestSession('immersive-vr')
session.updateRenderState({ baseLayer: new XRWebGLLayer(session, gl2) });
const hookGamepadControls = () => {
const gamepad = session?.inputSources?.[0]?.gamepad; //replace with code to choose appropriate input source/controller
if (gamepad) {
setInterval(scroll(gamepad), 30);
}
};
session.requestAnimationFrame(hookGamepadControls); //We can request a single frame and run a timer to poll controller, no need to request additional frames
显示 Oculus 或任何 VR/AR/XR Controller /游戏 handle 应遵循的最低规则:
- 主要规则:
- 我们不能在
inline
上下文中,只能在immersive-vr
或immersive-ar
中(否则source.gamepad
将为空) - 我们必须在框架上下文中(否则
source.gamepad
将为空)
- 我们不能在
- 项圈规则:
- 我们必须将渲染状态连接到 GL 上下文(否则我们将收不到动画帧)
- 我们必须有一个 Canvas 元素(否则我们将无法获得 GL 上下文)
关于javascript - 在任何网站上监听 Oculus Controller 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72789605/