瀏覽代碼

disable mouse interaction on orientation camera if sensor is active

Trevor Baron 6 年之前
父節點
當前提交
c29ba066cf

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

@@ -130,6 +130,7 @@
 - Added `MeshExploder` class ([danjpar](https://github.com/danjpar))
 - Observables can now make observers top or bottom priority ([TrevorDev](https://github.com/TrevorDev))
 - Mesh outline no longer is shown through the mesh when it's transparent ([TrevorDev](https://github.com/TrevorDev))
+- DeviceOrientationCamera will no longer be modified by mouse input if the orientation sensor is active ([TrevorDev](https://github.com/TrevorDev))
 
 ### OBJ Loader
 - Add color vertex support (not part of standard) ([brianzinn](https://github.com/brianzinn))

+ 17 - 1
src/Cameras/Inputs/freeCameraDeviceOrientationInput.ts

@@ -4,11 +4,16 @@ import { FreeCamera } from "../../Cameras/freeCamera";
 import { Quaternion } from "../../Maths/math";
 import { Tools } from "../../Misc/tools";
 import { FreeCameraInputsManager } from "../../Cameras/freeCameraInputsManager";
+import { Observable } from '../../Misc/observable';
 
 // Module augmentation to abstract orientation inputs from camera.
 declare module "../../Cameras/freeCameraInputsManager" {
     export interface FreeCameraInputsManager {
         /**
+         * @hidden
+         */
+        _deviceOrientationInput: Nullable<FreeCameraDeviceOrientationInput>;
+        /**
          * Add orientation input support to the input manager.
          * @returns the current input manager
          */
@@ -21,7 +26,11 @@ declare module "../../Cameras/freeCameraInputsManager" {
  * @returns the current input manager
  */
 FreeCameraInputsManager.prototype.addDeviceOrientation = function(): FreeCameraInputsManager {
-    this.add(new FreeCameraDeviceOrientationInput());
+    if (!this._deviceOrientationInput) {
+        this._deviceOrientationInput = new FreeCameraDeviceOrientationInput();
+        this.add(this._deviceOrientationInput);
+    }
+
     return this;
 };
 
@@ -43,6 +52,10 @@ export class FreeCameraDeviceOrientationInput implements ICameraInput<FreeCamera
     private _gamma: number = 0;
 
     /**
+     * @hidden
+     */
+    public _onDeviceOrientationChangedObservable = new Observable<void>();
+    /**
      * Instantiates a new input
      * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
      */
@@ -88,6 +101,9 @@ export class FreeCameraDeviceOrientationInput implements ICameraInput<FreeCamera
         this._alpha = evt.alpha !== null ? evt.alpha : 0;
         this._beta = evt.beta !== null ? evt.beta : 0;
         this._gamma = evt.gamma !== null ? evt.gamma : 0;
+        if (evt.alpha !== null) {
+            this._onDeviceOrientationChangedObservable.notifyObservers();
+        }
     }
 
     /**

+ 7 - 0
src/Cameras/deviceOrientationCamera.ts

@@ -29,6 +29,13 @@ export class DeviceOrientationCamera extends FreeCamera {
         super(name, position, scene);
         this._quaternionCache = new Quaternion();
         this.inputs.addDeviceOrientation();
+
+        // When the orientation sensor fires it's first event, disable mouse input
+        if (this.inputs._deviceOrientationInput) {
+            this.inputs._deviceOrientationInput._onDeviceOrientationChangedObservable.addOnce(() => {
+                this.inputs.removeMouse();
+            });
+        }
     }
 
     /**

+ 25 - 1
src/Cameras/freeCameraInputsManager.ts

@@ -3,6 +3,7 @@ import { CameraInputsManager } from "./cameraInputsManager";
 import { FreeCameraKeyboardMoveInput } from "../Cameras/Inputs/freeCameraKeyboardMoveInput";
 import { FreeCameraMouseInput } from "../Cameras/Inputs/freeCameraMouseInput";
 import { FreeCameraTouchInput } from "../Cameras/Inputs/freeCameraTouchInput";
+import { Nullable } from '../types';
 
 /**
  * Default Inputs manager for the FreeCamera.
@@ -11,6 +12,15 @@ import { FreeCameraTouchInput } from "../Cameras/Inputs/freeCameraTouchInput";
  */
 export class FreeCameraInputsManager extends CameraInputsManager<FreeCamera> {
     /**
+     * @hidden
+     */
+    public _keyboardInput: Nullable<FreeCameraKeyboardMoveInput> = null;
+
+    /**
+     * @hidden
+     */
+    public _mouseInput: Nullable<FreeCameraMouseInput> = null;
+    /**
      * Instantiates a new FreeCameraInputsManager.
      * @param camera Defines the camera the inputs belong to
      */
@@ -33,7 +43,21 @@ export class FreeCameraInputsManager extends CameraInputsManager<FreeCamera> {
      * @returns the current input manager
      */
     addMouse(touchEnabled = true): FreeCameraInputsManager {
-        this.add(new FreeCameraMouseInput(touchEnabled));
+        if (!this._mouseInput) {
+            this._mouseInput = new FreeCameraMouseInput(touchEnabled);
+            this.add(this._mouseInput);
+        }
+        return this;
+    }
+
+    /**
+     * Removes the mouse input support from the manager
+     * @returns the current input manager
+     */
+    removeMouse(): FreeCameraInputsManager {
+        if (this._mouseInput) {
+            this.remove(this._mouseInput);
+        }
         return this;
     }