XRWebGLBinding:getDepthInformation() 方法

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

getDepthInformation() 方法是 XRWebGLBinding 接口的一部分,它返回一个 XRWebGLDepthInformation 对象,其中包含 WebGL 深度信息。

语法

js
getDepthInformation(view)

参数

view

一个从查看器姿势获得的 XRView 对象。

返回值

一个 XRWebGLDepthInformation 对象。

异常

NotSupportedError DOMException

如果 "depth-sensing" 不在此 XRSession 的已启用功能列表中,则抛出此异常。

InvalidStateError DOMException

如果 XRFrame 未激活或未处于动画状态,则抛出此异常。获取深度信息仅在 requestAnimationFrame() 回调中有效。

InvalidStateError DOMException

如果会话的 depthUsage 不是 "gpu-optimized",则抛出此异常。

示例

获取 WebGL 深度信息

js
const canvasElement = document.querySelector(".output-canvas");
const gl = canvasElement.getContext("webgl");
await gl.makeXRCompatible();

// Make sure to request a session with depth-sensing enabled
const session = navigator.xr.requestSession("immersive-ar", {
  requiredFeatures: ["depth-sensing"],
  depthSensing: {
    usagePreference: ["gpu-optimized"],
    formatPreference: ["luminance-alpha"],
  },
});

const glBinding = new XRWebGLBinding(session, gl);

// …

// Obtain depth information in an active and animated frame
function rafCallback(time, frame) {
  session.requestAnimationFrame(rafCallback);
  const pose = frame.getViewerPose(referenceSpace);
  if (pose) {
    for (const view of pose.views) {
      const depthInformation = glBinding.getDepthInformation(view);
      if (depthInformation) {
        // Do something with the depth information
        // gl.bindTexture(gl.TEXTURE_2D, depthInformation.texture);
        // …
      }
    }
  }
}

规范

规范
WebXR Depth Sensing 模块
# dom-xrwebglbinding-getdepthinformation

浏览器兼容性

另见