XRView: transform property
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
The read-onlytransform property of theXRView interface is anXRRigidTransform object whichprovides the position and orientation of the viewpoint relative to theXRReferenceSpace specified when theXRFrame.getViewerPose() method was called to obtain the view object.
With thetransform, you can then position the view as a camera within the3D scene. If you instead need the more traditional view matrix, you can get usingview.transform.inverse.matrix; this gets the underlyingmatrix of the transform'sinverse.
In this article
Value
AXRRigidTransform object specifying the position and orientation of theviewpoint represented by theXRView.
Examples
For each view making up the presented scene, the view'stransformrepresents the position and orientation of the viewer or camera relative to thereference space's origin. You can then use the inverse of this matrix to transform theobjects in your scene to adjust their placement and orientation to simulate the viewer'smovement through space.
In this example, we see an outline of a code fragment used while rendering anXRFrame, which makes use of the view transform to place objects in theworld during rendering.
const modelViewMatrix = mat4.create();const normalMatrix = mat4.create();for (const view of pose.views) { const viewport = glLayer.getViewport(view); gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height); for (const 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 allocatingand deallocating the matrices, and generally reduces overhead by reusing the same matrixfor each object rendered.
Then we iterate over eachXRView found in theXRViewerPose's list ofviews. Therewill usually be two: one for the left eye and one for the right, but there may be onlyone if in monoscopic mode. Currently, WebXR doesn't support more than two views perpose, although room has been left to extend the specification to support that in thefuture with some additions to the API.
For each view, we obtain its viewport and pass that to WebGL usinggl.viewport(). For the left eye, thiswill 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 viewmatrix is computed by multiplying its own matrix which describes the object's ownposition and orientation by the additional position and orientation adjustments neededto 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 matrixreturned byview.transform.inverse.matrix. Theresulting model view matrix will apply all the transforms needed to move and rotate theobject based on the relative positions of the object and the camera. This will simulatethe 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 transposingit.
Finally, we call the object'srender() routine, passing along themodelViewMatrix andnormalMatrix so the renderer can place andlight the object properly.
Note:This example is derived from a larger example…<<<--- finish and add link --->>>
Specifications
| Specification |
|---|
| WebXR Device API> # dom-xrviewgeometry-transform> |