简体   繁体   English

侦听任何网站中的 Oculus 控制器事件

[英]Listen for Oculus controller events in any website

Is it possible for a webpage to listen for button presses of a Oculus controller simply using some generic javascript code like document.addEventListener ?网页是否可以简单地使用一些通用的 JavaScript 代码(如document.addEventListener )来监听 Oculus 控制器的按钮按下?

In VR browsers by default the primary thumbstick scrolls the page up and down.在 VR 浏览器中,默认情况下,主摇杆会上下滚动页面。 The idea is to re-map it to trigger different actions.这个想法是重新映射它以触发不同的动作。

From my research it looks like I need to use an A-frame but I'm looking for a generic solution that works across different websites with just Vanilla Javascript, not inside an immersive context.从我的研究来看,我似乎需要使用 A 框架,但我正在寻找一种通用的解决方案,该解决方案可以在不同的网站上使用 Vanilla Javascript,而不是在沉浸式上下文中。


在此处输入图像描述

In short, no.简而言之,没有。

It's not possible to interface with an Oculus controller outside of an immersive WebXR context.在沉浸式 WebXR 上下文之外,无法与 Oculus 控制器进行交互。

The Inputs and Sources section of the WebXR API states: WebXR API 的 Inputs and Sources 部分指出:

While the Gamepad record is defined by the Gamepad API specification, it's not actually managed by the Gamepad API虽然 Gamepad 记录由 Gamepad API 规范定义,但它实际上并不由 Gamepad API 管理

So the Gamepad API will not work, and we're forced to use the WebXR API.所以 Gamepad API 将不起作用,我们不得不使用 WebXR API。 Futhermore, we are forced to use one of the immersive modes for our webxr session.此外,我们被迫为我们的 webxr 会话使用一种沉浸式模式。 The WebXR Controller Sample states (confirmed this, see comments in rules below): WebXR 控制器示例状态(确认了这一点,请参阅下面规则中的注释):

WebXR gamepads are not available during inline sessions. WebXR 游戏手柄在内联会话期间不可用。

That being said, you can still ...话虽如此,您仍然可以...

Do it with minimal (vanilla) Javascript.用最少的(香草)Javascript 来做。

Depending on your definition of vanilla, this will work ( Codesandbox ):根据您对香草的定义,这将起作用( 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

Minimum rules to follow to get Oculus or any VR/AR/XR controllers/gamepads to show up:让 Oculus 或任何 VR/AR/XR 控制器/游戏手柄出现的最低规则:

  • Main rules:主要规则:
    • We must not be in an inline context, only immersive-vr or immersive-ar (otherwise source.gamepad will be null)我们不能在内inline上下文中,只能在immersive-vrimmersive-ar环境中(否则source.gamepad将为空)
    • We must be in a frame context (otherwise source.gamepad will be null)我们必须在框架上下文中(否则source.gamepad将为空)
  • Collary rules:拼贴规则:
    • We must connect the render state to the GL context (otherwise we won't receive animation frames)我们必须将渲染状态连接到 GL 上下文(否则我们将不会收到动画帧)
    • We must have a canvas element (otherwise we won't be able to get a GL context)我们必须有一个画布元素(否则我们将无法获得 GL 上下文)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM