--- title: WebVR API slug: Web/API/WebVR_API tags: - API - VR - WebVR - 虚拟现实 translation_of: Web/API/WebVR_API ---
WebVR API 能为虚拟现实设备的渲染提供支持 — 例如像Oculus Rift或者HTC Vive 这样的头戴式设备与 Web apps 的连接。它能让开发者将位置和动作信息转换成3D场景中的运动。基于这项技术能产生很多有趣的应用, 比如虚拟的产品展示,可交互的培训课程,以及超强沉浸感的第一人称游戏。
【K】
Any VR devices attached to your computer will be returned by the {{domxref("Navigator.getVRDevices()")}} method. This returns an array of objects to represent the attached devices, which inherit from the general {{domxref("VRDevice")}} object — generally a head mounted display will have two devices — the head mounted display itself, represented by {{domxref("HMDVRDevice")}}, and a position sensor camera that keeps track of your head position, represented by {{domxref("PositionSensorVRDevice")}}.
连接到电脑的所有VR设备都将由 {{domxref("Navigator.getVRDevices()")}} 方法返回。 这个方法将返回一个包含了所有已连接设备的对象数组,每个设备对应一个对象, 该对象继承自 {{domxref("VRDevice")}} — 通常一个头显将包含两个设备 — 头显自身由 {{domxref("HMDVRDevice")}} 表示, 和一个跟踪头部位置的位置捕捉传感器,由 {{domxref("PositionSensorVRDevice")}} 表示。
The {{domxref("PositionSensorVRDevice")}} object contains the {{domxref("PositionSensorVRDevice.getState","getState()")}} method, which returns a {{domxref("VRPositionState")}} object — this represents the sensor’s state at a given timestamp, and includes properties containing useful data such as current velocity, acceleration, and orientation, useful for updating the rendering of a scene on each frame according to the movement of the VR head mounted display.
{{domxref("PositionSensorVRDevice")}} 对象有一个 {{domxref("PositionSensorVRDevice.getState","getState()")}} 方法, 该方法返回一个{{domxref("VRPositionState")}} 对象 — 这个对象代表位置传感器在指定时刻的状态,包含了一些十分有用的信息,例如速度、加速度以及运动方向,可用于根据头部运动刷新画面显示。
The {{domxref("HMDVRDevice.getEyeParameters()")}} method returns a {{domxref("VREyeParameters")}} object, which can be used to return field of view information — how much of the scene the head mounted display can see. The {{domxref("VREyeParameters.currentFieldOfView")}} returns a {{domxref("VRFieldOfView")}} object that contains 4 angles describing the current view from a center point. You can also change the field of view using {{domxref("HMDVRDevice.setFieldOfView()")}}.
{{domxref("HMDVRDevice.getEyeParameters()")}} 方法返回一个 {{domxref("VREyeParameters")}} 对象, 可用于获取显示区域的信息 — 头显可以看到多少画面。 {{domxref("VREyeParameters.currentFieldOfView")}} 返回一个 {{domxref("VRFieldOfView")}} 对象 ,该对象包含了4个角度信息来描述当前的显示区域. 你可以用 {{domxref("HMDVRDevice.setFieldOfView()")}} 来改变当前的显示区域。
Note: To find out more about using these interfaces in your own app, read Using the WebVR API. To learn more about the basic concepts behind VR, read WebVR concepts.
注释:: 要了解更多关于如何在你的应用程序中使用这些接口,请阅读文章Using the WebVR API. 要学习更多关于VR技术背后的基础概念,请阅读文章 WebVR concepts.
Many WebVR hardware setups feature controllers that go along with the headset. These can be used in WebVR apps via the Gamepad API, and specifically the Gamepad Extensions API that adds API features for accessing controller pose, haptic actuators, and more.
HMDVRDevice
and PositionSensorVRDevice
.VRFieldOfView
.【K】
You can find a number of examples at these Github repos:
你可以在Github的协议中找到一系列的案例:
【K】
Specification | Status | Comment |
---|---|---|
{{SpecName('WebVR')}} |
{{Spec2('WebVR')}} 草稿阶段 |
Initial definition 最初的定义 |
【K】
{{CompatibilityTable}}
【K】
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}}[1] | {{CompatGeckoDesktop(39)}}[2] | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
Feature | Android | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | {{CompatNo}} | {{CompatGeckoMobile(39)}}[2] {{CompatGeckoMobile(44)}}[3] |
{{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
about:config
and enable the dom.vr*
prefs. A better option however is to install the WebVR Enabler Add-on, which does this for you and sets up other necessary parts of the environment.dom.vr*
prefs的优先级. 另一个更好的选择是安装WebVR Enabler Add-on, 它能代替你完成这些工作并建立其他运行环境中必需的部分environment.dom.vr*
prefs are enabled by default at this point, in Nightly/Aurora editions.dom.vr*
prefs 在Nightly/Aurora的版本中,目前并不能被执行。