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

瀏覽器相容性

另見