安全上下文
此特征只可用于
安全上下文
(HTTPS),在某些或所有
支持浏览器
.
草案
此页面不完整。
只读
transform
特性为
XRView
interface is an
XRRigidTransform
object which provides the position and orientation of the viewpoint relative to the
XRReferenceSpace
specified when the
XRFrame.getViewerPose()
method was called to obtain the view object.
With the
transform
, you can then position the view as a camera within the 3D scene. If you instead need the more traditional view matrix, you can get using
view.transform.inverse.matrix
; this gets the underlying
matrix
of the transform's
inverse
.
let viewTransform = xrView.transform;
A
XRRigidTransform
object specifying the position and orientation of the viewpoint represented by the
XRView
.
For each view making up the presented scene, the view's
transform
represents the position and orientation of the viewer or camera relative to the reference space's origin. You can then use the inverse of this matrix to transform the objects in your scene to adjust their placement and orientation to simulate the viewer's movement through space.
In this example, we see an outline of a code fragment used while rendering an
XRFrame
, which makes use of the view transform to place objects in the world during rendering.
const modelViewMatrix = mat4.create();
const normalMatrix = mat4.create();
for (let view of pose.views) {
let viewport = glLayer.getViewport(view);
gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height);
for (let obj of world.objects) {
mat4.multiply(modelViewMatrix, view.transform.inverse.matrix, obj.matrix);
mat4.invert(normalMatrix, modelViewMatrix);
mat4.transpose(normalMatrix, normalMatrix);
obj.render(modelViewMatrix, normalMatrix);
}
}
Two matrices are created outside the rendering loop; this avoids repeatedly allocating and deallocating the matrices, and generally reduces overhead by reusing the same matrix for each object rendered.
Then we iterate over each
XRView
found in the
XRViewerPose
's list of
views
. There will usually be two: one for the left eye and one for the right, but there may be only one if in monoscopic mode. Currently, WebXR doesn't support more than two views per pose, although room has been left to extend the specification to support that in the future with some additions to the API.
For each view, we obtain its viewport and pass that to WebGL using
gl.viewport()
. For the left eye, this will be the left half of the canvas, while the right eye will use the right half.
Then we iterate over each object that makes up the scene. Each object's model view matrix is computed by multiplying its own matrix which describes the object's own position and orientation by the additional position and orientation adjustments needed to match the camera's movement. To convert the "camera focused" transform matrix into an "object focused" transform, we use the transform's inverse, thus taking the matrix returned by
view.transform.inverse.matrix
. The resulting model view matrix will apply all the transforms needed to move and rotate the object based on the relative positions of the object and the camera. This will simulate the movement of the camera even though we're actually moving the object.
We then compute the normals for the model view matrix by inverting it, then transposing it.
Finally, we call the object's
render()
routine, passing along the
modelViewMatrix
and
normalMatrix
so the renderer can place and light the object properly.
注意: This example is derived from a larger example... <<<--- finish and add link --->>>
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebXR 设备 API
The definition of 'XRView.transform' in that specification. |
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
transform
|
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 |
完整支持
不支持
XRView
eye
projectionMatrix
transform
Navigator.xr
WebGLRenderingContext.makeXRCompatible()
XR
XRBoundedReferenceSpace
XRFrame
XRInputSource
XRInputSourceArray
XRInputSourceEvent
XRInputSourcesChangeEvent
XRPose
XRReferenceSpace
XRReferenceSpaceEvent
XRRenderState
XRRigidTransform
XRSession
XRSessionEvent
XRSpace
XRViewerPose
XRViewport
XRWebGLLayer