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

Khả dụng hạn chế

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.

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

Phương thức getDepthInformation() của giao diện XRFrame trả về một đối tượng XRCPUDepthInformation chứa thông tin độ sâu CPU cho khung đang hoạt động và đang được hoạt họa.

Cú pháp

js
getDepthInformation(view)

Tham số

view

Một đối tượng XRView lấy từ tư thế người xem.

Giá trị trả về

Một đối tượng XRCPUDepthInformation.

Ngoại lệ

NotSupportedError DOMException

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

InvalidStateError DOMException

Được ném nếu:

  • XRFrame không đang hoạt động hoặc không đang được hoạt họa. Việc lấy thông tin độ sâu chỉ hợp lệ trong callback của requestAnimationFrame().
  • depthUsage của phiên không phải là "cpu-optimized".

Ví dụ

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

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

// …

// 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 = frame.getDepthInformation(view);
      if (depthInformation) {
        // Do something with the depth information
        renderDepth(depthInformation);
      }
    }
  }
}

Thông số kỹ thuật

Thông số kỹ thuật
WebXR Depth Sensing Module
# dom-xrframe-getdepthinformation

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