files/en-us/web/api/xrwebglbinding/getviewsubimage/index.md
{{APIRef("WebXR Device API")}}{{SeeCompatTable}}
The getViewSubImage() method of the {{domxref("XRWebGLBinding")}} interface returns a {{domxref("XRWebGLSubImage")}} object representing the WebGL texture to render for a view.
getViewSubImage(layer, view)
layer
view
A {{domxref("XRWebGLSubImage")}} object.
A {{jsxref("TypeError")}} is thrown,
layer is not in the session's layer array.XRProjectionLayerThe following example renders an {{domxref("XRProjectionLayer")}} to a view.
const xrGlBinding = new XRWebGLBinding(xrSession, gl);
const layer = xrGlBinding.createProjectionLayer({});
const framebuffer = gl.createFramebuffer();
xrSession.updateRenderState({ layers: [layer] });
xrSession.requestAnimationFrame(onXRFrame);
function onXRFrame(time, xrFrame) {
xrSession.requestAnimationFrame(onXRFrame);
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
for (const view in xrViewerPose.views) {
const subImage = xrGlBinding.getViewSubImage(layer, view);
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D,
subImage.colorTexture,
0,
);
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.DEPTH_ATTACHMENT,
gl.TEXTURE_2D,
subImage.depthStencilTexture,
0,
);
const viewport = subImage.viewport;
gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height);
// Render from the viewpoint of xrView
}
}
{{Specifications}}
{{Compat}}