Browse Source

moved the attached flad to the implementation

Raanan Weber 5 năm trước cách đây
mục cha
commit
223f03e5d6

+ 7 - 4
src/Cameras/XR/features/WebXRAnchorSystem.ts

@@ -81,11 +81,14 @@ export class WebXRAnchorSystem implements IWebXRFeature {
      */
     public onAnchorRemovedObservable: Observable<IWebXRAnchor> = new Observable();
 
+    /**
+     * Set to true when attached
+     */
+    public attached: boolean = false;
     private _planeDetector: WebXRPlaneDetector;
     private _hitTestModule: WebXRHitTestLegacy;
 
     private _enabled: boolean = false;
-    private _attached: boolean = false;
     private _trackedAnchors: Array<IWebXRAnchor> = [];
     private _lastFrameDetected: XRAnchorSet = new Set();
     private _observerTracked: Nullable<Observer<XRFrame>>;
@@ -125,7 +128,7 @@ export class WebXRAnchorSystem implements IWebXRFeature {
     attach(): boolean {
         this._observerTracked = this._xrSessionManager.onXRFrameObservable.add(() => {
             const frame = this._xrSessionManager.currentFrame;
-            if (!this._attached || !this._enabled || !frame) { return; }
+            if (!this.attached || !this._enabled || !frame) { return; }
             // const timestamp = this.xrSessionManager.currentTimestamp;
 
             const trackedAnchors = frame.trackedAnchors;
@@ -163,7 +166,7 @@ export class WebXRAnchorSystem implements IWebXRFeature {
             this._xrSessionManager.session.addEventListener('select', this._onSelect, false);
         }
 
-        this._attached = true;
+        this.attached = true;
         return true;
     }
 
@@ -174,7 +177,7 @@ export class WebXRAnchorSystem implements IWebXRFeature {
      * @returns true if successful.
      */
     detach(): boolean {
-        this._attached = false;
+        this.attached = false;
 
         this._xrSessionManager.session.removeEventListener('select', this._onSelect);
 

+ 10 - 0
src/Cameras/XR/features/WebXRBackgroundRemover.ts

@@ -55,6 +55,11 @@ export class WebXRBackgroundRemover implements IWebXRFeature {
     public onBackgroundStateChangedObservable: Observable<boolean> = new Observable();
 
     /**
+     * Set to true when attached
+     */
+    public attached: boolean = false;
+
+    /**
      * constructs a new background remover module
      * @param _xrSessionManager the session manager for this module
      * @param options read-only options to be used in this module
@@ -76,6 +81,8 @@ export class WebXRBackgroundRemover implements IWebXRFeature {
     attach(): boolean {
         this._setBackgroundState(false);
 
+        this.attached = true;
+
         return true;
     }
 
@@ -88,6 +95,8 @@ export class WebXRBackgroundRemover implements IWebXRFeature {
     detach(): boolean {
         this._setBackgroundState(true);
 
+        this.attached = false;
+
         return true;
     }
 
@@ -126,6 +135,7 @@ export class WebXRBackgroundRemover implements IWebXRFeature {
      * Dispose this feature and all of the resources attached
      */
     dispose(): void {
+        this.detach();
         this.onBackgroundStateChangedObservable.clear();
     }
 }

+ 12 - 7
src/Cameras/XR/features/WebXRHitTestLegacy.ts

@@ -59,6 +59,11 @@ export class WebXRHitTestLegacy implements IWebXRFeature {
     public static readonly Version = 1;
 
     /**
+     * Set to true when attached
+     */
+    public attached: boolean = false;
+
+    /**
      * Execute a hit test on the current running session using a select event returned from a transient input (such as touch)
      * @param event the (select) event to use to select with
      * @param referenceSpace the reference space to use for this hit test
@@ -95,6 +100,8 @@ export class WebXRHitTestLegacy implements IWebXRFeature {
      */
     public onHitTestResultObservable: Observable<IWebXRHitResult[]> = new Observable();
 
+    private _onSelectEnabled = false;
+    private _xrFrameObserver: Nullable<Observer<XRFrame>>;
     /**
      * Creates a new instance of the (legacy version) hit test feature
      * @param _xrSessionManager an instance of WebXRSessionManager
@@ -104,11 +111,9 @@ export class WebXRHitTestLegacy implements IWebXRFeature {
         /**
          * options to use when constructing this feature
          */
-        public readonly options: IWebXRHitTestOptions = {}) { }
+        public readonly options: IWebXRHitTestOptions = {}) {
 
-    private _onSelectEnabled = false;
-    private _xrFrameObserver: Nullable<Observer<XRFrame>>;
-    private _attached: boolean = false;
+    }
 
     /**
      * Populated with the last native XR Hit Results
@@ -132,7 +137,7 @@ export class WebXRHitTestLegacy implements IWebXRFeature {
             const mat = new Matrix();
             this._xrFrameObserver = this._xrSessionManager.onXRFrameObservable.add((frame) => {
                 // make sure we do nothing if (async) not attached
-                if (!this._attached) {
+                if (!this.attached) {
                     return;
                 }
                 let pose = frame.getViewerPose(this._xrSessionManager.referenceSpace);
@@ -149,7 +154,7 @@ export class WebXRHitTestLegacy implements IWebXRFeature {
                 WebXRHitTestLegacy.XRHitTestWithRay(this._xrSessionManager.session, ray, this._xrSessionManager.referenceSpace).then(this._onHitTestResults);
             });
         }
-        this._attached = true;
+        this.attached = true;
 
         return true;
     }
@@ -168,7 +173,7 @@ export class WebXRHitTestLegacy implements IWebXRFeature {
             this._xrSessionManager.onXRFrameObservable.remove(this._xrFrameObserver);
             this._xrFrameObserver = null;
         }
-        this._attached = false;
+        this.attached = false;
         return true;
     }
 

+ 7 - 4
src/Cameras/XR/features/WebXRPlaneDetector.ts

@@ -74,8 +74,11 @@ export class WebXRPlaneDetector implements IWebXRFeature {
      */
     public onPlaneUpdatedObservable: Observable<IWebXRPlane> = new Observable();
 
+    /**
+     * Set to true when attached
+     */
+    public attached: boolean = false;
     private _enabled: boolean = false;
-    private _attached: boolean = false;
     private _detectedPlanes: Array<IWebXRPlane> = [];
     private _lastFrameDetected: XRPlaneSet = new Set();
     private _observerTracked: Nullable<Observer<XRFrame>>;
@@ -107,7 +110,7 @@ export class WebXRPlaneDetector implements IWebXRFeature {
 
         this._observerTracked = this._xrSessionManager.onXRFrameObservable.add(() => {
             const frame = this._xrSessionManager.currentFrame;
-            if (!this._attached || !this._enabled || !frame) { return; }
+            if (!this.attached || !this._enabled || !frame) { return; }
             // const timestamp = this.xrSessionManager.currentTimestamp;
 
             const detectedPlanes = frame.worldInformation.detectedPlanes;
@@ -142,7 +145,7 @@ export class WebXRPlaneDetector implements IWebXRFeature {
             }
         });
 
-        this._attached = true;
+        this.attached = true;
         return true;
     }
 
@@ -153,7 +156,7 @@ export class WebXRPlaneDetector implements IWebXRFeature {
      * @returns true if successful.
      */
     detach(): boolean {
-        this._attached = false;
+        this.attached = false;
 
         if (this._observerTracked) {
             this._xrSessionManager.onXRFrameObservable.remove(this._observerTracked);

+ 5 - 4
src/Cameras/XR/features/webXRMotionControllerTeleportation.ts

@@ -53,7 +53,7 @@ export class WebXRMotionControllerTeleportation implements IWebXRFeature {
     public static readonly Version = 1;
 
     private _observerTracked: Nullable<Observer<XRFrame>>;
-    private _attached: boolean = false;
+    public attached: boolean = false;
     private _tmpRay = new Ray(new Vector3(), new Vector3());
     private _tmpVector = new Vector3();
 
@@ -98,7 +98,7 @@ export class WebXRMotionControllerTeleportation implements IWebXRFeature {
         this._observerTracked = this._xrSessionManager.onXRFrameObservable.add(() => {
             const frame = this._xrSessionManager.currentFrame;
             const scene = this._xrSessionManager.scene;
-            if (!this._attached || !frame) { return; }
+            if (!this.attached || !frame) { return; }
 
             // render target if needed
             const targetMesh = this._options.teleportationTargetMesh;
@@ -153,7 +153,7 @@ export class WebXRMotionControllerTeleportation implements IWebXRFeature {
             }
         });
 
-        this._attached = true;
+        this.attached = true;
         return true;
     }
 
@@ -417,7 +417,7 @@ export class WebXRMotionControllerTeleportation implements IWebXRFeature {
      * @returns true if successful.
      */
     detach(): boolean {
-        this._attached = false;
+        this.attached = false;
 
         if (this._observerTracked) {
             this._xrSessionManager.onXRFrameObservable.remove(this._observerTracked);
@@ -435,6 +435,7 @@ export class WebXRMotionControllerTeleportation implements IWebXRFeature {
      */
     dispose(): void {
         this.detach();
+        this._options.teleportationTargetMesh?.dispose(false, true);
     }
 }
 

+ 12 - 10
src/Cameras/XR/webXRFeaturesManager.ts

@@ -6,6 +6,10 @@ import { IDisposable } from '../../scene';
  */
 export interface IWebXRFeature extends IDisposable {
     /**
+     * Is this feature attached
+     */
+    attached: boolean;
+    /**
      * Attach the feature to the session
      * Will usually be called by the features manager
      *
@@ -121,8 +125,7 @@ export class WebXRFeaturesManager implements IDisposable {
         [name: string]: {
             featureImplementation: IWebXRFeature,
             version: number,
-            enabled: boolean,
-            attached: boolean
+            enabled: boolean
         }
     } = {};
 
@@ -136,7 +139,7 @@ export class WebXRFeaturesManager implements IDisposable {
         this._xrSessionManager.onXRSessionInit.add(() => {
             this.getEnabledFeatures().forEach((featureName) => {
                 const feature = this._features[featureName];
-                if (feature.enabled && !feature.attached) {
+                if (feature.enabled && !feature.featureImplementation.attached) {
                     this.attachFeature(featureName);
                 }
             });
@@ -146,7 +149,7 @@ export class WebXRFeaturesManager implements IDisposable {
         this._xrSessionManager.onXRSessionEnded.add(() => {
             this.getEnabledFeatures().forEach((featureName) => {
                 const feature = this._features[featureName];
-                if (feature.enabled && feature.attached) {
+                if (feature.enabled && feature.featureImplementation.attached) {
                     // detach, but don't disable!
                     this.detachFeature(featureName);
                 }
@@ -193,7 +196,6 @@ export class WebXRFeaturesManager implements IDisposable {
 
             this._features[name] = {
                 featureImplementation: constructFunction(),
-                attached: false,
                 enabled: true,
                 version: versionToLoad
             };
@@ -203,7 +205,7 @@ export class WebXRFeaturesManager implements IDisposable {
         }
 
         // if session started already, request and enable
-        if (this._xrSessionManager.session && !feature.attached && attachIfPossible) {
+        if (this._xrSessionManager.session && !feature.featureImplementation.attached && attachIfPossible) {
             // enable feature
             this.attachFeature(name);
         }
@@ -234,9 +236,9 @@ export class WebXRFeaturesManager implements IDisposable {
      */
     public attachFeature(featureName: string) {
         const feature = this._features[featureName];
-        if (feature && feature.enabled && !feature.attached) {
+        if (feature && feature.enabled && !feature.featureImplementation.attached) {
             feature.featureImplementation.attach();
-            feature.attached = true;
+            feature.featureImplementation.attached = true;
         }
     }
 
@@ -246,9 +248,9 @@ export class WebXRFeaturesManager implements IDisposable {
      */
     public detachFeature(featureName: string) {
         const feature = this._features[featureName];
-        if (feature && feature.attached) {
+        if (feature && feature.featureImplementation.attached) {
             feature.featureImplementation.detach();
-            feature.attached = false;
+            feature.featureImplementation.attached = false;
         }
     }