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

Merge pull request #1271 from RaananW/deviceorientation-initialrotation

resetToCurrentRotation to device orientation cameras
David Catuhe 9 лет назад
Родитель
Сommit
08cbc94c46

+ 1 - 2
Tools/Gulp/config.json

@@ -48,7 +48,6 @@
       "../../src/Cameras/Inputs/babylon.freecamera.input.keyboard.js",
       "../../src/Cameras/Inputs/babylon.freecamera.input.touch.js",
       "../../src/Cameras/Inputs/babylon.freecamera.input.deviceorientation.js",
-      "../../src/Cameras/Inputs/babylon.freecamera.input.vrdeviceorientation.js",
       "../../src/Cameras/Inputs/babylon.freecamera.input.gamepad.js",
       "../../src/Cameras/Inputs/babylon.arcrotatecamera.input.keyboard.js",
       "../../src/Cameras/Inputs/babylon.arcrotatecamera.input.mousewheel.js",
@@ -159,7 +158,7 @@
       "../../src/Materials/Textures/babylon.fontTexture.js",
       "../../src/Materials/Textures/babylon.mapTexture.js",
       "../../src/Canvas2d/babylon.bounding2d.js",
-      "../../src/Canvas2d/babylon.canvas2dLayoutEngine.js",
+      "../../src/Canvas2d/babylon.canvas2dLayoutEngine.js",
       "../../src/Canvas2d/babylon.brushes2d.js",
       "../../src/Canvas2d/babylon.smartPropertyPrim.js",
       "../../src/Canvas2d/babylon.prim2dBase.js",

+ 2 - 1
src/.gitignore

@@ -1 +1,2 @@
-*.d.ts
+*.d.ts
+.history

+ 0 - 53
src/Cameras/Inputs/babylon.freecamera.input.vrdeviceorientation.js

@@ -1,53 +0,0 @@
-var BABYLON;
-(function (BABYLON) {
-    var FreeCameraVRDeviceOrientationInput = (function () {
-        function FreeCameraVRDeviceOrientationInput() {
-            this.alphaCorrection = 1;
-            this.betaCorrection = 1;
-            this.gammaCorrection = 1;
-            this._alpha = 0;
-            this._beta = 0;
-            this._gamma = 0;
-            this._dirty = false;
-            this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
-        }
-        FreeCameraVRDeviceOrientationInput.prototype.attachControl = function (element, noPreventDefault) {
-            window.addEventListener("deviceorientation", this._deviceOrientationHandler);
-        };
-        FreeCameraVRDeviceOrientationInput.prototype._onOrientationEvent = function (evt) {
-            var camera = this.camera;
-            this._alpha = evt.alpha;
-            this._beta = evt.beta;
-            this._gamma = evt.gamma;
-            this._dirty = true;
-        };
-        FreeCameraVRDeviceOrientationInput.prototype.checkInputs = function () {
-            if (this._dirty) {
-                this._dirty = false;
-                var rotationX = this._gamma;
-                if (rotationX < 0) {
-                    rotationX = 90 + rotationX;
-                }
-                else {
-                    // Incline it in the correct angle.
-                    rotationX = 270 - rotationX;
-                }
-                this.camera.rotation.x = this.gammaCorrection * rotationX / 180.0 * Math.PI;
-                this.camera.rotation.y = this.alphaCorrection * -this._alpha / 180.0 * Math.PI;
-                this.camera.rotation.z = this.betaCorrection * this._beta / 180.0 * Math.PI;
-            }
-        };
-        FreeCameraVRDeviceOrientationInput.prototype.detachControl = function (element) {
-            window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
-        };
-        FreeCameraVRDeviceOrientationInput.prototype.getTypeName = function () {
-            return "FreeCameraVRDeviceOrientationInput";
-        };
-        FreeCameraVRDeviceOrientationInput.prototype.getSimpleName = function () {
-            return "VRDeviceOrientation";
-        };
-        return FreeCameraVRDeviceOrientationInput;
-    })();
-    BABYLON.FreeCameraVRDeviceOrientationInput = FreeCameraVRDeviceOrientationInput;
-    BABYLON.CameraInputTypes["FreeCameraVRDeviceOrientationInput"] = FreeCameraVRDeviceOrientationInput;
-})(BABYLON || (BABYLON = {}));

+ 0 - 65
src/Cameras/Inputs/babylon.freecamera.input.vrdeviceorientation.ts

@@ -1,65 +0,0 @@
-module BABYLON {
-    export class FreeCameraVRDeviceOrientationInput implements ICameraInput<FreeCamera> {
-        camera: FreeCamera;
-
-        public alphaCorrection = 1;
-        public betaCorrection = 1;
-        public gammaCorrection = 1;
-
-        private _alpha = 0;
-        private _beta = 0;
-        private _gamma = 0;
-        private _dirty = false;
-
-        private _offsetOrientation: { yaw: number; pitch: number; roll: number };
-        private _deviceOrientationHandler;
-
-        constructor() {
-            this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
-        }
-
-        attachControl(element: HTMLElement, noPreventDefault?: boolean) {
-            window.addEventListener("deviceorientation", this._deviceOrientationHandler);
-        }
-
-        public _onOrientationEvent(evt: DeviceOrientationEvent): void {
-            var camera = this.camera;
-            this._alpha = evt.alpha;
-            this._beta = evt.beta;
-            this._gamma = evt.gamma;
-            this._dirty = true;
-        }
-
-        public checkInputs() {
-            if (this._dirty) {
-                this._dirty = false;
-                var rotationX = this._gamma;
-                if (rotationX < 0) {
-                    rotationX = 90 + rotationX;
-                }
-                else {
-                    // Incline it in the correct angle.
-                    rotationX = 270 - rotationX;
-                }
-
-                this.camera.rotation.x = this.gammaCorrection * rotationX / 180.0 * Math.PI;
-                this.camera.rotation.y = this.alphaCorrection * -this._alpha / 180.0 * Math.PI;
-                this.camera.rotation.z = this.betaCorrection * this._beta / 180.0 * Math.PI;
-            }
-        }
-
-        detachControl(element: HTMLElement) {
-            window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
-        }
-
-        getTypeName(): string {
-            return "FreeCameraVRDeviceOrientationInput";
-        }
-
-        getSimpleName() {
-            return "VRDeviceOrientation";
-        }
-    }
-
-    CameraInputTypes["FreeCameraVRDeviceOrientationInput"] = FreeCameraVRDeviceOrientationInput;
-}

+ 1 - 5
src/Cameras/VR/babylon.vrDeviceOrientationCamera.ts

@@ -1,15 +1,11 @@
 module BABYLON {
-    export class VRDeviceOrientationFreeCamera extends FreeCamera {
+    export class VRDeviceOrientationFreeCamera extends DeviceOrientationCamera {
 
         constructor(name: string, position: Vector3, scene: Scene, compensateDistortion = true, vrCameraMetrics: VRCameraMetrics = VRCameraMetrics.GetDefault()) {
             super(name, position, scene);
 
-            this.rotationQuaternion = new Quaternion();
-
             vrCameraMetrics.compensateDistortion = compensateDistortion;
             this.setCameraRigMode(Camera.RIG_MODE_VR, { vrCameraMetrics: vrCameraMetrics });
-
-            this.inputs.addDeviceOrientation();
         }
 
 

+ 32 - 16
src/Cameras/babylon.deviceOrientationCamera.ts

@@ -1,22 +1,10 @@
 module BABYLON {
     // We're mainly based on the logic defined into the FreeCamera code
     export class DeviceOrientationCamera extends FreeCamera {
-        //-- Begin properties for backward compatibility for inputs
-        public get angularSensibility() {
-            return 0;
-        }
-        
-        public set angularSensibility(value) {
-        }
-        
-        public get moveSensibility() {
-                return 0;
-        }
-        
-        public set moveSensibility(value) {
-        }
-        //-- end properties for backward compatibility for inputs
-        
+
+        private _initialQuaternion: Quaternion;
+        private _quaternionCache: Quaternion;
+
         constructor(name: string, position: Vector3, scene: Scene) {
             super(name, position, scene);
             this.inputs.addDeviceOrientation();
@@ -25,5 +13,33 @@ module BABYLON {
         public getTypeName(): string {
             return "DeviceOrientationCamera";
         }
+
+        public _checkInputs(): void {
+            super._checkInputs();
+            if (this._initialQuaternion) {
+                this._quaternionCache.copyFrom(this.rotationQuaternion);
+                this._initialQuaternion.multiplyToRef(this.rotationQuaternion, this.rotationQuaternion);
+            }
+        }
+
+        public resetToCurrentRotation(axis: BABYLON.Axis = BABYLON.Axis.Y) {
+            //can only work if this camera has a rotation quaternion already.
+            if (!this.rotationQuaternion) return;
+
+            if (!this._initialQuaternion) {
+                this._initialQuaternion = new BABYLON.Quaternion();
+            }
+
+            this._initialQuaternion.copyFrom(this._quaternionCache || this.rotationQuaternion);
+
+            ['x', 'y', 'z'].forEach((axisName) => {
+                if (!axis[axisName]) {
+                    this._initialQuaternion[axisName] = 0;
+                } else {
+                    this._initialQuaternion[axisName] *= -1;
+                }
+            });
+            this._initialQuaternion.normalize();
+        }
     }
 }

+ 0 - 5
src/Cameras/babylon.freeCameraInputsManager.ts

@@ -24,11 +24,6 @@ module BABYLON {
             return this;
         }
 
-        addVRDeviceOrientation(): FreeCameraInputsManager {
-            this.add(new FreeCameraVRDeviceOrientationInput());
-            return this;
-        }
-
         addTouch(): FreeCameraInputsManager {
             this.add(new FreeCameraTouchInput());
             return this;