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

SpecificationStatusComment
WebXR Device API
The definition of 'XRView.transform' in that specification.
Working DraftInitial definition.

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:39 次

字数:6190

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文