浏览代码

Adjusting to the new Plane Detection API

Raanan Weber 4 年之前
父节点
当前提交
5a3f6f233d
共有 2 个文件被更改,包括 24 次插入5 次删除
  1. 23 4
      src/XR/features/WebXRPlaneDetector.ts
  2. 1 1
      src/XR/webXRDefaultExperience.ts

+ 23 - 4
src/XR/features/WebXRPlaneDetector.ts

@@ -5,6 +5,8 @@ import { Observable } from "../../Misc/observable";
 import { Vector3, Matrix } from "../../Maths/math.vector";
 import { WebXRAbstractFeature } from "./WebXRAbstractFeature";
 
+declare const XRPlane: XRPlane;
+
 /**
  * Options used in the plane detector module
  */
@@ -90,6 +92,7 @@ export class WebXRPlaneDetector extends WebXRAbstractFeature {
      */
     constructor(_xrSessionManager: WebXRSessionManager, private _options: IWebXRPlaneDetectorOptions = {}) {
         super(_xrSessionManager);
+        this.xrNativeFeatureName = "plane-detection";
         if (this._xrSessionManager.session) {
             this._init();
         } else {
@@ -132,6 +135,14 @@ export class WebXRPlaneDetector extends WebXRAbstractFeature {
         this.onPlaneUpdatedObservable.clear();
     }
 
+    /**
+     * Check if the needed objects are defined.
+     * This does not mean that the feature is enabled, but that the objects needed are well defined.
+     */
+    public isCompatible(): boolean {
+        return typeof XRPlane !== "undefined";
+    }
+
     protected _onXRFrame(frame: XRFrame) {
         if (!this.attached || !this._enabled || !frame) {
             return;
@@ -177,15 +188,23 @@ export class WebXRPlaneDetector extends WebXRAbstractFeature {
     }
 
     private _init() {
+        const internalInit = () => {
+            this._enabled = true;
+            if (this._detectedPlanes.length) {
+                this._detectedPlanes.length = 0;
+            }
+        };
         if (!this._xrSessionManager.session.updateWorldTrackingState) {
+            // check if this was enabled by a flag
+            const alreadyEnabled = (this._xrSessionManager.session as any).worldTrackingState?.planeDetectionState?.enabled;
+            if (alreadyEnabled) {
+                internalInit();
+            }
             // fail silently
             return;
         }
         this._xrSessionManager.session.updateWorldTrackingState({ planeDetectionState: { enabled: true } });
-        this._enabled = true;
-        if (this._detectedPlanes.length) {
-            this._detectedPlanes = [];
-        }
+        internalInit();
     }
 
     private _updatePlaneWithXRPlane(xrPlane: XRPlane, plane: Partial<IWebXRPlane>, xrFrame: XRFrame): IWebXRPlane {

+ 1 - 1
src/XR/webXRDefaultExperience.ts

@@ -141,7 +141,7 @@ export class WebXRDefaultExperience {
                     };
                     if (options.optionalFeatures) {
                         if (typeof options.optionalFeatures === "boolean") {
-                            uiOptions.optionalFeatures = ["hit-test", "anchors", "planes", "hand-tracking"];
+                            uiOptions.optionalFeatures = ["hit-test", "anchors", "plane-detection", "hand-tracking"];
                         } else {
                             uiOptions.optionalFeatures = options.optionalFeatures;
                         }