浏览代码

option to enable horizontal dragging when orientation camera is using sensor

Trevor Baron 6 年之前
父节点
当前提交
ff2e68ab72
共有 2 个文件被更改,包括 37 次插入6 次删除
  1. 14 5
      src/Cameras/Inputs/freeCameraMouseInput.ts
  2. 23 1
      src/Cameras/deviceOrientationCamera.ts

+ 14 - 5
src/Cameras/Inputs/freeCameraMouseInput.ts

@@ -1,4 +1,4 @@
-import { Observer, EventState } from "../../Misc/observable";
+import { Observer, EventState, Observable } from "../../Misc/observable";
 import { serialize } from "../../Misc/decorators";
 import { serialize } from "../../Misc/decorators";
 import { Nullable } from "../../types";
 import { Nullable } from "../../types";
 import { ICameraInput, CameraInputTypes } from "../../Cameras/cameraInputsManager";
 import { ICameraInput, CameraInputTypes } from "../../Cameras/cameraInputsManager";
@@ -30,7 +30,9 @@ export class FreeCameraMouseInput implements ICameraInput<FreeCamera> {
     private _onMouseMove: Nullable<(e: MouseEvent) => any>;
     private _onMouseMove: Nullable<(e: MouseEvent) => any>;
     private _observer: Nullable<Observer<PointerInfo>>;
     private _observer: Nullable<Observer<PointerInfo>>;
     private previousPosition: Nullable<{ x: number, y: number }> = null;
     private previousPosition: Nullable<{ x: number, y: number }> = null;
-
+    
+    public _onMouseMoved = new Observable<{ offsetX: number, offsetY: number }>();
+    public _moveCamera = true;
     /**
     /**
      * Manage the mouse inputs to control the movement of a free camera.
      * Manage the mouse inputs to control the movement of a free camera.
      * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
      * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
@@ -105,12 +107,15 @@ export class FreeCameraMouseInput implements ICameraInput<FreeCamera> {
                     }
                     }
 
 
                     var offsetX = evt.clientX - this.previousPosition.x;
                     var offsetX = evt.clientX - this.previousPosition.x;
+                    var offsetY = evt.clientY - this.previousPosition.y;
                     if (this.camera.getScene().useRightHandedSystem) { offsetX *= -1; }
                     if (this.camera.getScene().useRightHandedSystem) { offsetX *= -1; }
                     if (this.camera.parent && this.camera.parent._getWorldMatrixDeterminant() < 0) { offsetX *= -1; }
                     if (this.camera.parent && this.camera.parent._getWorldMatrixDeterminant() < 0) { offsetX *= -1; }
-                    this.camera.cameraRotation.y += offsetX / this.angularSensibility;
 
 
-                    var offsetY = evt.clientY - this.previousPosition.y;
-                    this.camera.cameraRotation.x += offsetY / this.angularSensibility;
+                    if(this._moveCamera){
+                        this.camera.cameraRotation.y += offsetX / this.angularSensibility;
+                        this.camera.cameraRotation.x += offsetY / this.angularSensibility;
+                    }
+                    this._onMouseMoved.notifyObservers({offsetX:offsetX, offsetY:offsetY})
 
 
                     this.previousPosition = {
                     this.previousPosition = {
                         x: evt.clientX,
                         x: evt.clientX,
@@ -179,6 +184,10 @@ export class FreeCameraMouseInput implements ICameraInput<FreeCamera> {
                 element.removeEventListener("contextmenu", <EventListener>this.onContextMenu);
                 element.removeEventListener("contextmenu", <EventListener>this.onContextMenu);
             }
             }
 
 
+            if (this._onMouseMoved) {
+                this._onMouseMoved.clear();
+            }
+
             this._observer = null;
             this._observer = null;
             this._onMouseMove = null;
             this._onMouseMove = null;
             this.previousPosition = null;
             this.previousPosition = null;

+ 23 - 1
src/Cameras/deviceOrientationCamera.ts

@@ -18,6 +18,7 @@ export class DeviceOrientationCamera extends FreeCamera {
 
 
     private _initialQuaternion: Quaternion;
     private _initialQuaternion: Quaternion;
     private _quaternionCache: Quaternion;
     private _quaternionCache: Quaternion;
+    private _tmpDragQuaternion = new Quaternion();
 
 
     /**
     /**
      * Creates a new device orientation camera
      * Creates a new device orientation camera
@@ -33,11 +34,32 @@ export class DeviceOrientationCamera extends FreeCamera {
         // When the orientation sensor fires it's first event, disable mouse input
         // When the orientation sensor fires it's first event, disable mouse input
         if (this.inputs._deviceOrientationInput) {
         if (this.inputs._deviceOrientationInput) {
             this.inputs._deviceOrientationInput._onDeviceOrientationChangedObservable.addOnce(() => {
             this.inputs._deviceOrientationInput._onDeviceOrientationChangedObservable.addOnce(() => {
-                this.inputs.removeMouse();
+                if(this.inputs._mouseInput){
+                    this.inputs._mouseInput._moveCamera = false;
+                    this.inputs._mouseInput._onMouseMoved.add((e)=>{
+                        if(this._dragFactor != 0){
+                            if (!this._initialQuaternion) {
+                                this._initialQuaternion = new Quaternion();
+                            }
+                            // Rotate the initial space around the y axis to allow users to "turn around" via touch/mouse
+                            Quaternion.FromEulerAnglesToRef(0,e.offsetX*this._dragFactor,0, this._tmpDragQuaternion);
+                            this._initialQuaternion.multiplyToRef(this._tmpDragQuaternion, this._initialQuaternion);
+                        }
+                    })
+                }
             });
             });
         }
         }
     }
     }
 
 
+    private _dragFactor = 0;
+    /**
+     * Enabled turning on the y axis when the orientation sensor is active
+     * @param dragFactor the factor that controls the turn speed (default: 1/300)
+     */
+    public enableHorizontalDragging(dragFactor=1/300){
+        this._dragFactor = dragFactor;
+    }
+
     /**
     /**
      * Gets the current instance class name ("DeviceOrientationCamera").
      * Gets the current instance class name ("DeviceOrientationCamera").
      * This helps avoiding instanceof at run time.
      * This helps avoiding instanceof at run time.