Sfoglia il codice sorgente

auto-attach feature improvement

Raanan Weber 5 anni fa
parent
commit
7f2fee41f0

+ 17 - 4
src/Cameras/XR/features/WebXRAbstractFeature.ts

@@ -15,7 +15,6 @@ export abstract class WebXRAbstractFeature implements IWebXRFeature {
      * @param _xrSessionManager the xr session manager for this feature
      * @param _xrSessionManager the xr session manager for this feature
      */
      */
     constructor(protected _xrSessionManager: WebXRSessionManager) {
     constructor(protected _xrSessionManager: WebXRSessionManager) {
-
     }
     }
 
 
     private _attached: boolean = false;
     private _attached: boolean = false;
@@ -32,14 +31,27 @@ export abstract class WebXRAbstractFeature implements IWebXRFeature {
     }
     }
 
 
     /**
     /**
+     * Should auto-attach be disabled?
+     */
+    public disableAutoAttach: boolean = false;
+
+    /**
      * attach this feature
      * attach this feature
      *
      *
      * @returns true if successful, false is failed or already attached
      * @returns true if successful, false is failed or already attached
      */
      */
-    public attach(): boolean {
-        if (this.attached) {
-            return false;
+    public attach(force?: boolean): boolean {
+        if (!force) {
+            if (this.attached) {
+                return false;
+            }
+        } else {
+            if (this.attached) {
+                // detach first, to be sure
+                this.detach();
+            }
         }
         }
+
         this._attached = true;
         this._attached = true;
         this._addNewAttachObserver(this._xrSessionManager.onXRFrameObservable, (frame) => this._onXRFrame(frame));
         this._addNewAttachObserver(this._xrSessionManager.onXRFrameObservable, (frame) => this._onXRFrame(frame));
         return true;
         return true;
@@ -52,6 +64,7 @@ export abstract class WebXRAbstractFeature implements IWebXRFeature {
      */
      */
     public detach(): boolean {
     public detach(): boolean {
         if (!this._attached) {
         if (!this._attached) {
+            this.disableAutoAttach = true;
             return false;
             return false;
         }
         }
         this._attached = false;
         this._attached = false;

+ 16 - 7
src/Cameras/XR/webXRFeaturesManager.ts

@@ -10,12 +10,17 @@ export interface IWebXRFeature extends IDisposable {
      */
      */
     attached: boolean;
     attached: boolean;
     /**
     /**
+     * Should auto-attach be disabled?
+     */
+    disableAutoAttach: boolean;
+    /**
      * Attach the feature to the session
      * Attach the feature to the session
      * Will usually be called by the features manager
      * Will usually be called by the features manager
      *
      *
+     * @param force should attachment be forced (even when already attached)
      * @returns true if successful.
      * @returns true if successful.
      */
      */
-    attach(): boolean;
+    attach(force?: boolean): boolean;
     /**
     /**
      * Detach the feature from the session
      * Detach the feature from the session
      * Will usually be called by the features manager
      * Will usually be called by the features manager
@@ -151,7 +156,7 @@ export class WebXRFeaturesManager implements IDisposable {
         this._xrSessionManager.onXRSessionInit.add(() => {
         this._xrSessionManager.onXRSessionInit.add(() => {
             this.getEnabledFeatures().forEach((featureName) => {
             this.getEnabledFeatures().forEach((featureName) => {
                 const feature = this._features[featureName];
                 const feature = this._features[featureName];
-                if (feature.enabled && !feature.featureImplementation.attached) {
+                if (feature.enabled && !feature.featureImplementation.attached && !feature.featureImplementation.disableAutoAttach) {
                     this.attachFeature(featureName);
                     this.attachFeature(featureName);
                 }
                 }
             });
             });
@@ -194,7 +199,7 @@ export class WebXRFeaturesManager implements IDisposable {
                 // try loading the number the string represents
                 // try loading the number the string represents
                 versionToLoad = +version;
                 versionToLoad = +version;
             }
             }
-            if (versionToLoad === -1 ||  isNaN(versionToLoad)) {
+            if (versionToLoad === -1 || isNaN(versionToLoad)) {
                 throw new Error(`feature not found - ${name} (${version})`);
                 throw new Error(`feature not found - ${name} (${version})`);
             }
             }
         } else {
         } else {
@@ -219,10 +224,14 @@ export class WebXRFeaturesManager implements IDisposable {
             version: versionToLoad
             version: versionToLoad
         };
         };
 
 
-        // if session started already, request and enable
-        if (this._xrSessionManager.session && !feature.featureImplementation.attached && attachIfPossible) {
-            // enable feature
-            this.attachFeature(name);
+        if (attachIfPossible) {
+            // if session started already, request and enable
+            if (this._xrSessionManager.session && !feature.featureImplementation.attached && attachIfPossible) {
+                // enable feature
+                this.attachFeature(name);
+            }
+        } else {
+            this._features[name].featureImplementation.disableAutoAttach = true;
         }
         }
 
 
         return this._features[name].featureImplementation;
         return this._features[name].featureImplementation;