Переглянути джерело

Make sure attached is executed only once

Raanan Weber 5 роки тому
батько
коміт
c1106e19f8

+ 8 - 2
src/Cameras/XR/features/WebXRAbstractFeature.ts

@@ -34,9 +34,12 @@ export abstract class WebXRAbstractFeature implements IWebXRFeature {
     /**
      * attach this feature
      *
-     * @returns true if successful.
+     * @returns true if successful, false is failed or already attached
      */
     public attach(): boolean {
+        if (this.attached) {
+            return false;
+        }
         this._attached = true;
         this._addNewAttachObserver(this._xrSessionManager.onXRFrameObservable, (frame) => this._onXRFrame(frame));
         return true;
@@ -45,9 +48,12 @@ export abstract class WebXRAbstractFeature implements IWebXRFeature {
     /**
      * detach this feature.
      *
-     * @returns true if successful.
+     * @returns true if successful, false if failed or already detached
      */
     public detach(): boolean {
+        if (!this._attached) {
+            return false;
+        }
         this._attached = false;
         this._removeOnDetach.forEach((toRemove) => {
             toRemove.observable.remove(toRemove.observer);

+ 6 - 2
src/Cameras/XR/features/WebXRAnchorSystem.ts

@@ -122,7 +122,9 @@ export class WebXRAnchorSystem extends WebXRAbstractFeature implements IWebXRFea
      * @returns true if successful.
      */
     attach(): boolean {
-        super.attach();
+        if (!super.attach()) {
+            return false;
+        }
         if (this._options.addAnchorOnSelect) {
             this._xrSessionManager.session.addEventListener('select', this._onSelect, false);
         }
@@ -136,7 +138,9 @@ export class WebXRAnchorSystem extends WebXRAbstractFeature implements IWebXRFea
      * @returns true if successful.
      */
     detach(): boolean {
-        super.detach();
+        if (!super.detach()) {
+            return false;
+        }
 
         this._xrSessionManager.session.removeEventListener('select', this._onSelect);
 

+ 18 - 3
src/Cameras/XR/features/WebXRControllerPointerSelection.ts

@@ -92,6 +92,15 @@ export class WebXRControllerPointerSelection extends WebXRAbstractFeature implem
      */
     public lasterPointerDefaultColor: Color3 = new Color3(0.5, 0.5, 0.5);
 
+    /**
+     * Should the laser pointer be displayed
+     */
+    public displayLaserPointer: boolean = true;
+    /**
+     * Should the selection mesh be displayed (The ring at the end of the laser pointer)
+     */
+    public displaySelectionMesh: boolean = true;
+
     private static _idCounter = 0;
 
     private _tmpRay = new Ray(new Vector3(), new Vector3());
@@ -128,7 +137,9 @@ export class WebXRControllerPointerSelection extends WebXRAbstractFeature implem
      * @returns true if successful.
      */
     attach(): boolean {
-        super.attach();
+        if (!super.attach()) {
+            return false;
+        }
 
         this._options.xrInput.controllers.forEach(this._attachController);
         this._addNewAttachObserver(this._options.xrInput.onControllerAddedObservable, this._attachController);
@@ -147,7 +158,9 @@ export class WebXRControllerPointerSelection extends WebXRAbstractFeature implem
      * @returns true if successful.
      */
     detach(): boolean {
-        super.detach();
+        if (!super.detach()) {
+            return false;
+        }
 
         Object.keys(this._controllers).forEach((controllerId) => {
             this._detachController(controllerId);
@@ -203,7 +216,7 @@ export class WebXRControllerPointerSelection extends WebXRAbstractFeature implem
                     Vector3.RotationFromAxisToRef(axis2, pickNormal, axis1, controllerData.selectionMesh.rotation);
                     controllerData.selectionMesh.position.addInPlace(pickNormal.scale(deltaFighting));
                 }
-                controllerData.selectionMesh.isVisible = true;
+                controllerData.selectionMesh.isVisible = true && this.displaySelectionMesh;
             } else {
                 controllerData.selectionMesh.isVisible = false;
             }
@@ -362,6 +375,8 @@ export class WebXRControllerPointerSelection extends WebXRAbstractFeature implem
                 (<StandardMaterial>controllerData.laserPointer.material).emissiveColor = this.lasterPointerDefaultColor;
             }
 
+            controllerData.laserPointer.isVisible = this.displayLaserPointer;
+
             if (controllerData.pick) {
                 this._scene.simulatePointerMove(controllerData.pick, { pointerId: controllerData.id });
             }

+ 6 - 2
src/Cameras/XR/features/WebXRControllerTeleportation.ts

@@ -205,7 +205,9 @@ export class WebXRMotionControllerTeleportation extends WebXRAbstractFeature imp
     }
 
     public attach(): boolean {
-        super.attach();
+        if (!super.attach()) {
+            return false;
+        }
 
         this._options.xrInput.controllers.forEach(this._attachController);
         this._addNewAttachObserver(this._options.xrInput.onControllerAddedObservable, this._attachController);
@@ -218,7 +220,9 @@ export class WebXRMotionControllerTeleportation extends WebXRAbstractFeature imp
     }
 
     public detach(): boolean {
-        super.detach();
+        if (!super.detach()) {
+            return false;
+        }
 
         Object.keys(this._controllers).forEach((controllerId) => {
             this._detachController(controllerId);

+ 6 - 2
src/Cameras/XR/features/WebXRHitTestLegacy.ts

@@ -121,7 +121,9 @@ export class WebXRHitTestLegacy extends WebXRAbstractFeature implements IWebXRFe
      * @returns true if successful.
      */
     attach(): boolean {
-        super.attach();
+        if (!super.attach()) {
+            return false;
+        }
         if (this.options.testOnPointerDownOnly) {
             this._xrSessionManager.session.addEventListener('select', this._onSelect, false);
         }
@@ -136,7 +138,9 @@ export class WebXRHitTestLegacy extends WebXRAbstractFeature implements IWebXRFe
      * @returns true if successful.
      */
     detach(): boolean {
-        super.detach();
+        if (!super.detach()) {
+            return false;
+        }
         // disable select
         this._onSelectEnabled = false;
         this._xrSessionManager.session.removeEventListener('select', this._onSelect);