安全上下文
此特征只可用于
安全上下文
(HTTPS),在某些或所有
支持浏览器
.
The WebXR Device API interface
XRViewerPose
represents the pose (the position and orientation) of a viewer's point of view on the scene. Each
XRViewerPose
can have multiple views to represent, for example, the slight separation between the left and right eye.
This view can represent anything from the point-of-view of a user's XR headset to the viewpoint represented by a player's movement of an avatar using mouse and keyboard, presented on the screen, to a virtual camera capturing the scene for a spectator.
In addition to the properties inherited from
XRPose
,
XRViewerPose
includes the following:
views
只读
XRView
objects, one for each viewpoint on the scene which is needed to represent the scene to the user. A typical headset provides a viewer pose with two views whose
eye
property is either
left
or
right
, indicating which eye that view represents. Taken together, these views can reproduce the 3D effect when displayed on the XR device.
XRViewerPose
object is used to describe the state of a viewer of a WebXR scene as it's tracked by the user's XR hardware. The viewer may be the virtual representation of the user, or it may represent another device or interface which may serve as the source of a position and orientation that make up a view upon the scene. For example, every player in a MMORPG might have an instance of
XRViewerPose
to provide a way to calculate what they can see; if the game provides a mechanism that tells the player if another player sees them, or that they see another player, this information becomes crucial.
An
XRViewerPose
is always obtained and referenced relative to an existing
XRReferenceSpace
. This ensures that positions and orientations are reported using the expected relative coordinate system.
To render a scene using the
XRViewerPose
representing the user's head, one would iterate over the views in the
views
array, rendering them one after another. By calling
viewport()
on the WebGL context, specifying the
XRView
as input, you can get the viewport to use when rendering in order to draw the frame for that eye into the correct part of the drawing surface.
Also, when rendering the scene for spectators or other players in a multiplayer game, the
transform
的
XRViewerPose
can be used to determine both placement and facing direction of the other players in the game, so that they can be drawn in the correct place with the correct facing.
The viewer's pose for the animation frame represented by
XRFrame
can be obtained by calling the frame's
getViewerPose()
method, specifying the reference space in which the origin's position should be computed. The returned
XRViewerPose
tells you where the viewer is and what direction they're facing at the time at which the frame takes place.
In this example—part of the code to render an
XRFrame
,
getViewerPose()
is called to get an
XRViewerPose
using the same reference space the code is using as its base reference space. If a valid pose is returned, the frame is rendered by clearing the backbuffer and then rendering each of the views in the pose; these are most likely the views for the left and right eyes.
let pose = frame.getViewerPose(xrReferenceSpace);
if (pose) {
let glLayer = xrSession.renderState.baseLayer;
gl.bindFrameBuffer(gl.FRAMEBUFFER, glLayer.framebuffer);
gl.clearColor(0, 0, 0, 1);
gl.clearDepth(1);
gl.clear(gl.COLOR_BUFFER_BIT, gl.DEPTH_BUFFER_BIT);
for (let view of pose.views) {
let viewport = glLayer.getViewport(view);
gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height);
/* render the scene for the eye view.eye */
}
}
Passing each
view
to
getViewport()
returns the WebGL viewport to apply in order to cause the rendered output to be positioned correctly in the framebuffer for renderijng to the corresponding eye on the output device.
This code is derived from Drawing a frame in Movement, orientation, and motion: A WebXR example . You can see more context and see much more on that page.
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebXR 设备 API
The definition of 'XRViewerPose' in that specification. |
工作草案 | 初始定义。 |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
XRViewerPose
|
Chrome 79 | Edge 79 | Firefox No | IE No | Opera No | Safari No | WebView Android No | Chrome Android 79 | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android 11.2 |
views
|
Chrome 79 | Edge 79 | Firefox No | IE No | Opera No | Safari No | WebView Android No | Chrome Android 79 | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android 11.2 |
完整支持
不支持
XRViewerPose
Navigator.xr
WebGLRenderingContext.makeXRCompatible()
XR
XRBoundedReferenceSpace
XRFrame
XRInputSource
XRInputSourceArray
XRInputSourceEvent
XRInputSourcesChangeEvent
XRPose
XRReferenceSpace
XRReferenceSpaceEvent
XRRenderState
XRRigidTransform
XRSession
XRSessionEvent
XRSpace
XRView
XRViewport
XRWebGLLayer