XRFrame: getDepthInformation() 方法
XRFrame 介面的 getDepthInformation() 方法返回一個 XRCPUDepthInformation 物件,其中包含當前活動和正在動畫的幀的 CPU 深度資訊。
語法
js
getDepthInformation(view)
引數
返回值
一個 XRCPUDepthInformation 物件。
異常
NotSupportedErrorDOMException-
如果
"depth-sensing"不在此XRSession的已啟用功能列表中,則丟擲此異常。 InvalidStateErrorDOMException-
在以下情況下丟擲
XRFrame未處於活動或動畫狀態。僅在requestAnimationFrame()回撥中獲取深度資訊是有效的。- 會話的
depthUsage不是"cpu-optimized"。
示例
獲取 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);
}
}
}
}
規範
| 規範 |
|---|
| WebXR Depth Sensing 模組 # dom-xrframe-getdepthinformation |
瀏覽器相容性
載入中…