XRWebGLLayer.getViewport() - Web APIs 编辑

Secure context

This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The XRWebGLLayer interface's getViewport() method returns the XRViewport that should be used to render the specified XRView into the WebGL layer. For WebXR devices which use a single framebuffer for both the left and right eyes, the returned viewport represents the region of the framebuffer into which the scene should be rendered for the eye represented by the view.

Syntax

let viewport = xrWebGLLayer.getViewport(view);

Parameters

view
An XRView object indicating the view for which the viewport is to be returned.

Return value

A XRViewport object representing the viewport which will restrict drawing to the portion of the layer corresponding to the specified view.

Exceptions

InvalidStateError
Either the specified view is not in an active XRFrame or that XRFrame and the XRWebGLLayer are not part of the same WebXR session.

Example

This example demonstrates in part what the callback for the requestAnimationFrame() function might look like, using getViewport() to get the viewport so that drawing can be constrained to the area set aside for the eye whose viewpoint is currently being rendered.

This works because the set of views returned by an XRViewerPose each represent one eye's perspective on the scene. Since the framebuffer is split in half, one half for each eye, setting the WebGL viewport to match the WebXR layer's viewport will ensure that when rendering the scene for the current eye's pose, it is rendered into the correct half of the framebuffer.

<<<--- add link to appropriate section in the Cameras and views article --->>>

function drawFrame(time, frame) {
  let session = frame.session;

  let pose = frame.getViewerPose(mainReferenceSpace);

  if (pose) {
    let glLayer = session.renderState.baseLayer;
    gl.bindFramebuffer(gl.FRAMEBUFFER, glLayer.framebuffer);

    gl.clearColor(0, 0, 0, 1.0);
    gl.clearDepth(1.0);
    gl.clear(gl.COLOR_BUFFER_BIT, gl.DEPTH_COLOR_BIT);

    for (let view of pose.views) {
      let viewport = glLayer.getViewport(view);
      gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height);

      /* Render the scene now */
  }
}

Specifications

SpecificationStatusComment
WebXR Device API
The definition of 'XRWebGLLayer.getViewport()' in that specification.
Working DraftInitial definition.

Browser compatibility

BCD tables only load in the browser

See also

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

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

发布评论

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

词条统计

浏览:135 次

字数:4910

最后编辑:7年前

编辑次数:0 次

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