dom / latest / xrview / transform.html /

XRView.transform

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.

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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
transform
79
79
No
No
No
No
No
79
No
No
No
11.2

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/XRView/transform