Bläddra i källkod

Merge pull request #1273 from RaananW/webvr-initialquat

WebVR initial quaternion
David Catuhe 9 år sedan
förälder
incheckning
46381d09ee
1 ändrade filer med 28 tillägg och 0 borttagningar
  1. 28 0
      src/Cameras/VR/babylon.webVRCamera.ts

+ 28 - 0
src/Cameras/VR/babylon.webVRCamera.ts

@@ -8,6 +8,9 @@ module BABYLON {
         private _cacheState = null;
         public _vrEnabled = false;
 
+        private _initialQuaternion: Quaternion;
+        private _quaternionCache: Quaternion;
+
         constructor(name: string, position: Vector3, scene: Scene, compensateDistortion = true, vrCameraMetrics: VRCameraMetrics = VRCameraMetrics.GetDefault()) {
             super(name, position, scene);
 
@@ -17,6 +20,7 @@ module BABYLON {
             this._getWebVRDevices = this._getWebVRDevices.bind(this);
 
             this.rotationQuaternion = new Quaternion();
+            this._quaternionCache = new Quaternion();
         }
 
         private _getWebVRDevices(devices: Array<any>): void {
@@ -54,6 +58,10 @@ module BABYLON {
                 //Flip in XY plane
                 this.rotationQuaternion.z *= -1;
                 this.rotationQuaternion.w *= -1;
+                if (this._initialQuaternion) {
+                    this._quaternionCache.copyFrom(this.rotationQuaternion);
+                    this._initialQuaternion.multiplyToRef(this.rotationQuaternion, this.rotationQuaternion);
+                }
             }
 
             super._checkInputs();
@@ -85,6 +93,26 @@ module BABYLON {
         public getTypeName(): string {
             return "WebVRFreeCamera";
         }
+
+        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();
+        }
     }
 }