Просмотр исходного кода

Merge pull request #3388 from TrevorDev/viveControllers

remove duplicated hand setting logic, set hand even if only 1 connect…
David Catuhe 7 лет назад
Родитель
Сommit
7c7a64b146

+ 26 - 18
src/Cameras/VR/babylon.vrExperienceHelper.ts

@@ -338,24 +338,7 @@ module BABYLON {
 
         // Raised when one of the controller has loaded successfully its associated default mesh
         private _onDefaultMeshLoaded(webVRController: WebVRController) {
-            if (webVRController.hand === "left") {
-                this._leftControllerReady = true;
-                if (this._interactionsRequested && !this._interactionsEnabledOnLeftController) {
-                    this._enableInteractionOnController(webVRController);
-                }
-                if (this._teleportationRequested && !this._teleportationEnabledOnLeftController) {
-                    this._enableTeleportationOnController(webVRController);
-                }
-            }
-            if (webVRController.hand === "right") {
-                this._rightControllerReady = true;
-                if (this._interactionsRequested && !this._interactionsEnabledOnRightController) {
-                    this._enableInteractionOnController(webVRController);
-                }
-                if (this._teleportationRequested && !this._teleportationEnabledOnRightController) {
-                    this._enableTeleportationOnController(webVRController);
-                }
-            }
+            this._tryEnableInteractionOnController(webVRController);
             try {
                 this.onControllerMeshLoaded.notifyObservers(webVRController);
             }
@@ -692,6 +675,31 @@ module BABYLON {
                         }
                     });
                 }
+            }else{
+                var webVRController = <WebVRController>gamepad;
+                this._tryEnableInteractionOnController(webVRController);
+            }
+        }
+
+        // This only succeeds if the controller's mesh exists for the controller so this must be called whenever new controller is connected or when mesh is loaded
+        private _tryEnableInteractionOnController = (webVRController:WebVRController) => {
+            if (webVRController.hand === "left") {
+                this._leftControllerReady = true;
+                if (this._interactionsRequested && !this._interactionsEnabledOnLeftController) {
+                    this._enableInteractionOnController(webVRController);
+                }
+                if (this._teleportationRequested && !this._teleportationEnabledOnLeftController) {
+                    this._enableTeleportationOnController(webVRController);
+                }
+            }
+            if (webVRController.hand === "right") {
+                this._rightControllerReady = true;
+                if (this._interactionsRequested && !this._interactionsEnabledOnRightController) {
+                    this._enableInteractionOnController(webVRController);
+                }
+                if (this._teleportationRequested && !this._teleportationEnabledOnRightController) {
+                    this._enableTeleportationOnController(webVRController);
+                }
             }
         }
 

+ 17 - 24
src/Cameras/VR/babylon.webVRCamera.ts

@@ -192,13 +192,6 @@ module BABYLON {
                                 }
                             });
                         }
-                        // Move starting headset position by standing matrix
-                        this._deviceToWorld.multiplyToRef(this._standingMatrix, this._deviceToWorld);
-
-                        // Correct for default height added originally
-                        var pos =  this._deviceToWorld.getTranslation()
-                        pos.y -= this._defaultHeight;
-                        this._deviceToWorld.setTranslation(pos)
                         callback(true)
                     }
                 })
@@ -508,25 +501,25 @@ module BABYLON {
                         //add to the controllers array
                         this.controllers.push(webVrController);
 
-                        //did we find enough controllers? Great! let the developer know.
-                        if (this.controllers.length >= 2) {
-                            // Forced to add some control code for Vive as it doesn't always fill properly the "hand" property
-                            // Sometimes, both controllers are set correctly (left and right), sometimes none, sometimes only one of them...
-                            // So we're overriding setting left & right manually to be sure
-                            let firstViveWandDetected = false;
-
-                            for (let i = 0; i < this.controllers.length; i++) {
-                                if (this.controllers[i].controllerType === PoseEnabledControllerType.VIVE) {
-                                    if (!firstViveWandDetected) {
-                                        firstViveWandDetected = true;
-                                        this.controllers[i].hand = "left";
-                                    }
-                                    else {
-                                        this.controllers[i].hand = "right";
-                                    }
+                        // Forced to add some control code for Vive as it doesn't always fill properly the "hand" property
+                        // Sometimes, both controllers are set correctly (left and right), sometimes none, sometimes only one of them...
+                        // So we're overriding setting left & right manually to be sure
+                        let firstViveWandDetected = false;
+
+                        for (let i = 0; i < this.controllers.length; i++) {
+                            if (this.controllers[i].controllerType === PoseEnabledControllerType.VIVE) {
+                                if (!firstViveWandDetected) {
+                                    firstViveWandDetected = true;
+                                    this.controllers[i].hand = "left";
+                                }
+                                else {
+                                    this.controllers[i].hand = "right";
                                 }
                             }
-
+                        }
+                        
+                            //did we find enough controllers? Great! let the developer know.
+                        if (this.controllers.length >= 2) {
                             this.onControllersAttachedObservable.notifyObservers(this.controllers);
                         }
                     }

+ 0 - 6
src/Gamepad/Controllers/babylon.webVRController.ts

@@ -13,8 +13,6 @@ module BABYLON {
 
         protected _buttons: Array<MutableGamepadButton>;
 
-        private static handCounter = 0;
-
         private _onButtonStateChange: (controlledIndex: number, buttonIndex: number, state: ExtendedGamepadButton) => void;
 
         public onButtonStateChange(callback: (controlledIndex: number, buttonIndex: number, state: ExtendedGamepadButton) => void) {
@@ -33,10 +31,6 @@ module BABYLON {
             super(vrGamepad);
             this._buttons = new Array<ExtendedGamepadButton>(vrGamepad.buttons.length);
             this.hand = vrGamepad.hand;
-            if(!this.hand){
-                this.hand = (WebVRController.handCounter % 2 == 0) ? "left" : "right";
-                WebVRController.handCounter++;
-            }
         }
 
         public update() {