Przeglądaj źródła

Introducing resetToCurrentRotation to device orientation cameras (non VR and VR)

using the newly introduced function an initial rotation can be applied
to the camera (in any axis, defaults to y) to that the user's "0"
rotation will be the current camera's rotation.
Raanan Weber 9 lat temu
rodzic
commit
96f5028bec

+ 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();
         }
 
 

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

@@ -1,22 +1,9 @@
 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;
+
         constructor(name: string, position: Vector3, scene: Scene) {
             super(name, position, scene);
             this.inputs.addDeviceOrientation();
@@ -25,5 +12,30 @@ module BABYLON {
         public getTypeName(): string {
             return "DeviceOrientationCamera";
         }
+
+        public _checkInputs(): void {
+            super._checkInputs();
+            if(this._initialQuaternion) {
+                this._initialQuaternion.multiplyToRef(this.rotationQuaternion, this.rotationQuaternion);
+            }
+        }
+
+        public resetToCurrentRotation(axis: BABYLON.Vector3 = 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();
+            } else {
+                this._initialQuaternion.copyFrom(this.rotationQuaternion);
+            }
+
+            ['x', 'y', 'z'].forEach(function (axisName) {
+                if (!axis[axisName]) {
+                    this._initialQuaternion[axisName] = 0;
+                }
+            });
+            this._initialQuaternion.normalize();
+        }
     }
 }