--- title: WebXR 设备 接口参考 slug: Web/API/WebXR_Device_API translation_of: Web/API/WebXR_Device_API ---
WebXR 是一组支持将渲染3D场景用来呈现虚拟世界(虚拟现实,也称作VR)或将图形图像添加到现实世界(增强现实,也称作AR)的标准。 WebXR 设备 API 实现了 WebXR 功能集的核心,管理输出设备的选择,以适当的帧速率将3D场景呈现给所选设备,并管理使用输入控制器创建的运动矢量。
WebXR-兼容性设备包括沉浸式3D运动和定位跟踪耳机,通过框架覆盖在真实世界场景之上的眼镜,以及手持移动电话,它们通过用摄像机捕捉世界来增强现实,并通过计算机生成的图像增强场景。
为了完成这些事情,WebXR 设备 API 提供了以下关键功能:
在最基本的层面上,通过计算应用于场景的透视图,以从每个用户的视角呈现场景,从而在3D中呈现场景,考虑到眼睛之间的常规距离,然后渲染场景两次,每只眼睛一次。然后将生成的图像(场景在一个帧上呈现两次,每只眼睛一半)显示给用户。
由于 WebGL 用于将3D世界渲染到WebXR会话中,因此您首先应该熟悉 WebGL 的一般用法以及3D图形的基本知识。您很可能不会直接使用 WebGL API,而是利用在 WebGL 之上构建的框架或库之一来使其使用更加方便。其中最流行的是three.js。
使用库而不是直接使用WebGL API的一个特殊好处是,库取向于实现虚拟相机函数性的接口。OpenGL( WebGL 的扩展)不直接提供照相机视图,使用库模拟一个的话可以使您的工作变得非常非常容易,特别是在构建允许在虚拟世界中自由移动的代码时。
因为本质上来说,创建虚拟3D世界的整个过程是一个技巧,它利用了我们对眼睛如何收集光以及大脑如何解释所收集的数据的理解,因此务必要牢记,软件设计师开发人员有责任比平时更加小心,以确保结果正确。
缺陷,未对准或变形会混淆眼睛和大脑,导致眼睛疼痛或头痛乃至眩晕,头晕或潜在的严重恶心。考虑到 VR 护目镜的全部特性,需要特别注意,开发者对可能引起癫痫发作的任何事物都要保持警惕;如果它引起困扰,则用户可能无法快速将视线从您呈现的图像上移开。
如果您有任何可能对任何用户构成风险的内容,则应提供警告消息。有备无患!
较早的 WebVR API 仅设计为支持虚拟现实(VR),而WebXR在Web上同时支持VR和增强现实(AR)。WebXR增强现实模块增加了对AR功能的支持。
典型的XR设备可以具有3或6个自由度,并且有没有外部位置传感器都可以。
该设备还可以包括加速度计,气压计或其他传感器,用于感测用户何时在空间中移动,旋转其头部等。
使用WebXR的大多数应用程序将遵循类似的总体设计模式:
inline
, immersive-vr
, 或 immersive-ar
, 以确定您希望提供的会话类型是否可用。inline
, immersive-vr
, 或 immersive-ar
后,可以将一个 {{DOMxRef("XRSession")}} 设定在期望的模式下。 requestSession()
返回的 promise 被 resolve 后,使用新的 {{domxref("XRSession")}} 在整个 WebXR 体验期间运行帧循环。
requestAnimationFrame()
的回调都需要使用 WebGL 渲染已提供信息的 3D 世界中的物体。WebXR Device API 受到一系列许可与安全性的控制。这些控制不涉及法律责任,但也需要引起重视。其控制场景主要在于身临其境的 immersive-vr
会话模式和 AR 会话下。
首先,如果域名不支持请求有权限打开沉浸模式,那么 immersive-vr
模式就会被拒绝。这个权限管理来自xr-spatial-tracking
特征策略。
一旦有权限了,申请开启 immersive-vr
模式的请求还需要再检查以下三点,全部满足才能开启:
requestSession()
调用;如果上述三点均满足, requestSession()
返回的 Promise 将被 resolve,新的 {{domxref("XRSession")}} 对象被传入完成时的处理函数中。如果有不满足的情况,将会根据具体场景抛出异常,比如当没有权限进入沉浸式模式情况下, SecurityError
将被抛出。
当你在 inline
模式下发出 {{domxref("XRSession")}} 请求想要请求其他的特性时,浏览器仅允许那些明显由用户意图发起才会执行的代码所调用到的 {{domxref("XR.requestSession", "requestSession()")}}。
特别注意:
requestSession()
调用的发起既不来自用户事件中,也不是在 Web 应用启动时,那该请求将会被驳回,Promise 放返回 false
;false
。一个可信任文档指的是该文档是活跃的、负责任的且有重点的;注意:当调用 requestSession()
时,根据选择对象需要指定的特性不同,将会执行额外的请求。
用户意图指的是用户自身是否想执行某个动作的时候可以通过代码控制实际的执行情况。有两种用户意图类型:显性和隐性。
直接询问用户是否同意执行某个操作,即显性的用户意图 (用户显示的同意操作) 。
当以下情况发生时,我们可以认为出现了隐性的用户意图 (用户暗示同意):
requestSession()
from the button's {{domxref("Element.click_event", "click")}} event handler will permitted.As a new and still in development API, WebXR support is limited to specific devices and browsers; and even on those, it may not be enabled by default. There may be options available to allow you to experiment with WebXR even if you don't have a compatible system, however.
The team designing the WebXR specification has published a WebXR polyfill which you can use to simulate WebXR on browsers which don't have support for the WebXR APIs. If the browser supports the older WebVR API, that is used. Otherwise, the polyfill falls back to an implementation which uses Google's Cardboard VR API.
The polyfill is maintained alongside the specification, and is kept up to date with the specification. Additionally, it is updated to maintain compatiblity with browsers as their support for WebXR and other technologies related to it and to the implementation of the polyfill change over time.
Be sure to read the readme carefully; the polyfill comes in several versions depending on what degree of compatibility with newer JavaScript features your target browsers include.
The Mozilla WebXR team has created a WebXR API Emulator browser extension, compatible with both Firefox and Chrome, which emulates the WebXR API, simulating a variety of compatible devices such as the HTC Vive, the Oculus Go and Oculus Quest, Samsung Gear, and Google Cardboard. With the extension in place, you can open up a developer tools panel that lets you control the position and orientation of the headset and any hand controllers, as well as button presses on the controllers.
While somewhat awkward compared to using an actual headset, this makes it possible to experiment with and developer WebXR code on a desktop computer, where WebXR isn't normally available. It also lets you perform some basic testing before taking your code to a real device. Be aware, however, that the emulator does not yet completely emulate all of the WebXR API, so you may run into problems you're not expecting. Again, carefully read the readme file and make sure you're aware of the limitations before you begin.
Important: You should always test your code on actual AR and/or VR hardware before releasing or shipping a product! Emulated, simulated, or polyfilled environments are not an adequate substitute for actual testing on physical devices.
Download the WebXR API Emulator for your supported browser below:
The source code for the extension is also available on GitHub.
To gain access to the WebXR API within the context of a given window, use the {{domxref("navigator.xr")}} property.
null
, WebXR is not available.XR
interface, you can create {{domxref("XRSession")}}s to represent actual AR and/or VR sessions.XRFrame
. To get an XRFrame
, call the session's {{domxref("XRSession.requestAnimationFrame", "requestAnimationFrame()")}} method, providing a callback which will be called with the XRFrame
once available. Events which communicate tracking states will also use XRFrame
to contain that information.XRSession
is composited. These properties include the range of distances from the viewer within which content should be rendered, the vertical field of view (for inline presentations), and a reference to the {{domxref("XRWebGLLayer")}} being used as the target for rendering the scene prior to it being presented on the XR device's display or displays.XRSession
is obtained from {{domxref("XR.requestSession()")}}, the session can be used to check the position and orientation of the viewer, query the device for environment information, and present the virtual or augmented world to the user.XRSpace
is an opaque base class on which all virtual coordinate system interfaces are based. Positions in WebXR are always expressed in relation to a particular XRSpace
at the time at which a particular {{domxref("XFrame")}} takes place. The space's coordinate system has its origin at the a given physical position.XRReferenceSpace
coordinate system is expected to remain unchanged through the lifespan of the {{domxref("XRSession")}}.The world has no boundaries and extends infinitely in every direction.XRBoundedReferenceSpace
extends the {{domxref("XRReferenceSpace")}} coordinate system to further include support for a finite world with set boundaries. Unlike XRReferenceSpace
, the origin must be located on the floor (that is, y = 0 at the floor). The x and z components of the origin are typically presumed to be located at or near the center of the room or surface.XRView
corresponds to the video display surface used to present the scene to the user. For example, a given XR device might have two views: one for the left eye and one for the right. Each view has an offset used to shift the position of the view relative to the camera, in order to allow for creating stereographic effects.XRViewerPose
specifies the state of a viewer of the WebXR scene as indicated by the XR device. Included is an array of {{domxref("XRView")}} objects, each representing one perspective on the scene. For example, it takes two views to create the stereoscopic view as perceived by human vision—one for the left eye and a second for the right eye. One view is offset to the left slightly from the viewer's position, and the other view is offset to the right by the same distance. The view list can also be used to represent the perspectives of each of the spectators of a scene, in a multi-user environment.XRInputSource
instances.The following interfaces are used to represent the events used by the WebXR API.
The WebGL API is extended by the WebXR specification to augment the WebGL context to allow it to be used to render views for display by a WebXR device.
true
, you must call makeXRCompatible()
prior to attempting to use the WebGL context for WebXR rendering. Returns a {{jsxref("promise")}} which resolves once the context has been prepared, or is rejected if the context cannot be configured for use by WebXR.The following guides and tutorials are a great resource to learn how to comprehend WebXR and the underlying 3D and VR/AR graphics concepts.
Specification | Status | Comment |
---|---|---|
{{SpecName("WebXR")}} | {{Spec2("WebXR")}} | Initial definition. |
{{Compat("api.Navigator.xr")}}