--- title: WebVR API slug: Web/API/WebVR_API tags: - API - VR - WebVR - 虚拟现实 translation_of: Web/API/WebVR_API ---
{{DefaultAPISidebar("WebVR API")}}{{SeeCompatTable}}

WebVR API 能为虚拟现实设备的渲染提供支持 — 例如像Oculus Rift或者HTC Vive 这样的头戴式设备与 Web apps 的连接。它能让开发者将位置和动作信息转换成3D场景中的运动。基于这项技术能产生很多有趣的应用, 比如虚拟的产品展示,可交互的培训课程,以及超强沉浸感的第一人称游戏。

概念及使用方法

【K】

Sketch of a person in a chair with wearing goggles labelled "Head mounted display (HMD)" facing a monitor with a webcam labelled "Position sensor"

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.

Using controllers: Combining WebVR with the Gamepad API 

使用控制器:将WebVR与Gamepad API相结合

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 posehaptic actuators, and more.

Note: Our Using VR controllers with WebVR article explains the basics of how to use VR controllers with WebVR apps.

 

WebVR接口

{{domxref("Navigator.getVRDevices")}}
Returns a promise that resolves to an array of objects representing the VR devices attached to the computer.
返回一个Promise对象,并通过resolve方式返回参数,参数为链接到电脑的VR设备数组。
{{domxref("VRDevice")}}
A generic VR device, includes information such as device IDs and descriptions. Inherited by HMDVRDevice and PositionSensorVRDevice.
返回一个包括了VR设备IDs,描述等信息的类。HMDVRDevice 和 PositionSensorVRDevice 继承了 VRDevice。
{{domxref("HMDVRDevice")}}
Represents a head mounted display, providing access to information about each eye, and the current field of view.
头戴设备。提供设备双眼、当前FOV(field of view)信息。
{{domxref("PositionSensorVRDevice")}}
Represents the position sensor for the VR hardware, allowing access to information such as position and orientation.
VR设备的位置传感器。获取位置、方向信息。
{{domxref("VRPose")}}
Represents the position state at a given timestamp (which includes orientation, position, velocity, and acceleration.)
根据一个时间戳返回包括(方向、位置、速度、加速度)的状态。
{{domxref("VREyeParameters")}}
Provides access to all the information required to correctly render a scene for each given eye, including field of view information.
给双眼提供正确渲染场景的所有信息,包括FOV。
{{domxref("VRFieldOfView")}}
Represents a field of view defined by 4 different degree values describing the view from a center point.
返回以视窗的中心点为基点的,表示FOV的4个角度值(downDegrees, leftDegrees, rightDegrees, upDegrees)。
{{domxref("VRFieldOfViewReadOnly")}}
Contains the raw definition for the degree value properties required to define a field of view. Inherited by VRFieldOfView.
定义一个FOV必须的角度属性。VRFieldOfView 继承了 VRFieldOfViewReadOnly。

示例

【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}}

相关文章