浏览代码

Merge pull request #9161 from RaananW/fixPlaneDetection

[XR] Adjust plane detection to the new API
David Catuhe 4 年之前
父节点
当前提交
749538bc3b

+ 1 - 0
dist/preview release/what's new.md

@@ -220,6 +220,7 @@
 - XR tracking state was added to the camera ([#9076](https://github.com/BabylonJS/Babylon.js/issues/9076)) ([RaananW](https://github.com/RaananW))
 - XR tracking state was added to the camera ([#9076](https://github.com/BabylonJS/Babylon.js/issues/9076)) ([RaananW](https://github.com/RaananW))
 - Individual post processing can be applied to the XR rig cameras ([#9038](https://github.com/BabylonJS/Babylon.js/issues/9038)) ([RaananW](https://github.com/RaananW))
 - Individual post processing can be applied to the XR rig cameras ([#9038](https://github.com/BabylonJS/Babylon.js/issues/9038)) ([RaananW](https://github.com/RaananW))
 - Pointer selection improvements - single/dual hand selection, max ray distance and more ([#7974](https://github.com/BabylonJS/Babylon.js/issues/7974)) ([RaananW](https://github.com/RaananW))
 - Pointer selection improvements - single/dual hand selection, max ray distance and more ([#7974](https://github.com/BabylonJS/Babylon.js/issues/7974)) ([RaananW](https://github.com/RaananW))
+- Updated Plane Detection API ([RaananW](https://github.com/RaananW))
 
 
 ### Collisions
 ### Collisions
 
 

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

@@ -5,6 +5,8 @@ import { Observable } from "../../Misc/observable";
 import { Vector3, Matrix } from "../../Maths/math.vector";
 import { Vector3, Matrix } from "../../Maths/math.vector";
 import { WebXRAbstractFeature } from "./WebXRAbstractFeature";
 import { WebXRAbstractFeature } from "./WebXRAbstractFeature";
 
 
+declare const XRPlane: XRPlane;
+
 /**
 /**
  * Options used in the plane detector module
  * Options used in the plane detector module
  */
  */
@@ -90,6 +92,7 @@ export class WebXRPlaneDetector extends WebXRAbstractFeature {
      */
      */
     constructor(_xrSessionManager: WebXRSessionManager, private _options: IWebXRPlaneDetectorOptions = {}) {
     constructor(_xrSessionManager: WebXRSessionManager, private _options: IWebXRPlaneDetectorOptions = {}) {
         super(_xrSessionManager);
         super(_xrSessionManager);
+        this.xrNativeFeatureName = "plane-detection";
         if (this._xrSessionManager.session) {
         if (this._xrSessionManager.session) {
             this._init();
             this._init();
         } else {
         } else {
@@ -132,6 +135,14 @@ export class WebXRPlaneDetector extends WebXRAbstractFeature {
         this.onPlaneUpdatedObservable.clear();
         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) {
     protected _onXRFrame(frame: XRFrame) {
         if (!this.attached || !this._enabled || !frame) {
         if (!this.attached || !this._enabled || !frame) {
             return;
             return;
@@ -177,15 +188,23 @@ export class WebXRPlaneDetector extends WebXRAbstractFeature {
     }
     }
 
 
     private _init() {
     private _init() {
+        const internalInit = () => {
+            this._enabled = true;
+            if (this._detectedPlanes.length) {
+                this._detectedPlanes.length = 0;
+            }
+        };
         if (!this._xrSessionManager.session.updateWorldTrackingState) {
         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
             // fail silently
             return;
             return;
         }
         }
         this._xrSessionManager.session.updateWorldTrackingState({ planeDetectionState: { enabled: true } });
         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 {
     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 (options.optionalFeatures) {
                         if (typeof options.optionalFeatures === "boolean") {
                         if (typeof options.optionalFeatures === "boolean") {
-                            uiOptions.optionalFeatures = ["hit-test", "anchors", "planes", "hand-tracking"];
+                            uiOptions.optionalFeatures = ["hit-test", "anchors", "plane-detection", "hand-tracking"];
                         } else {
                         } else {
                             uiOptions.optionalFeatures = options.optionalFeatures;
                             uiOptions.optionalFeatures = options.optionalFeatures;
                         }
                         }

+ 8 - 0
src/XR/webXREnterExitUI.ts

@@ -62,6 +62,11 @@ export class WebXREnterExitUIOptions {
      * A list of optional features to init the session with
      * A list of optional features to init the session with
      */
      */
     requiredFeatures?: string[];
     requiredFeatures?: string[];
+
+    /**
+     * If defined, this function will be executed if the UI encounters an error when entering XR
+     */
+    onError?: (error: any) => void;
 }
 }
 /**
 /**
  * UI to allow the user to enter/exit XR mode
  * UI to allow the user to enter/exit XR mode
@@ -180,6 +185,9 @@ export class WebXREnterExitUI implements IDisposable {
                                     const prevTitle = element.title;
                                     const prevTitle = element.title;
                                     element.title = "Error entering XR session : " + prevTitle;
                                     element.title = "Error entering XR session : " + prevTitle;
                                     element.classList.add("xr-error");
                                     element.classList.add("xr-error");
+                                    if (options.onError) {
+                                        options.onError(e);
+                                    }
                                 }
                                 }
                             }
                             }
                         }
                         }