XRWebGLBinding: phương thức getDepthInformation()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Phương thức getDepthInformation() của giao diện XRWebGLBinding trả về đối tượng XRWebGLDepthInformation chứa thông tin độ sâu WebGL.

Cú pháp

js
getDepthInformation(view)

Tham số

view

Đối tượng XRView thu được từ tư thế của người xem.

Giá trị trả về

Đối tượng XRWebGLDepthInformation.

Ngoại lệ

NotSupportedError DOMException

Được ném ra nếu "depth-sensing" không có trong danh sách tính năng được kích hoạt cho XRSession này.

InvalidStateError DOMException

Được ném ra nếu XRFrame không hoạt động hoặc không có hoạt hình. Việc lấy thông tin độ sâu chỉ hợp lệ trong callback requestAnimationFrame().

InvalidStateError DOMException

Được ném ra nếu depthUsage của phiên không phải là "gpu-optimized".

Ví dụ

Lấy thông tin độ sâu WebGL

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

// Đảm bảo yêu cầu phiên với depth-sensing được kích hoạt
const session = navigator.xr.requestSession("immersive-ar", {
  requiredFeatures: ["depth-sensing"],
  depthSensing: {
    usagePreference: ["gpu-optimized"],
    formatPreference: ["luminance-alpha"],
  },
});

const glBinding = new XRWebGLBinding(session, gl);

// …

// Lấy thông tin độ sâu trong một khung hoạt động và có hoạt hình
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) {
        // Thực hiện gì đó với thông tin độ sâu
        // gl.bindTexture(gl.TEXTURE_2D, depthInformation.texture);
        // …
      }
    }
  }
}

Thông số kỹ thuật

Specification
WebXR Depth Sensing Module
# dom-xrwebglbinding-getdepthinformation

Tương thích trình duyệt

Xem thêm