XRView.transform - Web APIs 编辑
Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Draft
This page is not complete.
The read-only transform
property of the 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
.
Syntax
let viewTransform = xrView.transform;
Value
A XRRigidTransform
object specifying the position and orientation of the viewpoint represented by the XRView
.
Examples
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.
Note: This example is derived from a larger example... <<<--- finish and add link --->>>
Specifications
Specification | Status | Comment |
---|---|---|
WebXR Device API The definition of 'XRView.transform' in that specification. | Working Draft | Initial definition. |
Browser compatibility
BCD tables only load in the browser
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论