浏览代码

Merge pull request #1994 from RaananW/WebVR-1.0

Webvr 1 0
David Catuhe 8 年之前
父节点
当前提交
92ab030b50
共有 4 个文件被更改,包括 90 次插入33 次删除
  1. 79 25
      src/Cameras/VR/babylon.webVRCamera.ts
  2. 2 0
      src/Cameras/babylon.camera.ts
  3. 8 8
      src/Math/babylon.math.ts
  4. 1 0
      src/babylon.mixins.ts

+ 79 - 25
src/Cameras/VR/babylon.webVRCamera.ts

@@ -42,6 +42,7 @@ module BABYLON {
         public _vrDevice = null;
         public rawPose: DevicePose = null;
         private _vrEnabled = false;
+        private _specsVersion: number = 1.1;
         private _attached: boolean = false;
 
         private _oldSize: BABYLON.Size;
@@ -98,7 +99,15 @@ module BABYLON {
             if (!this.getEngine().vrDisplaysPromise) {
                 Tools.Error("WebVR is not enabled on your browser");
             } else {
-                this._frameData = new VRFrameData();
+                //check specs version
+                if (!window.VRFrameData) {
+                    this._specsVersion = 1.0;
+                    this._frameData = {
+                    };
+                } else {
+                    this._frameData = new VRFrameData();
+                }
+
                 this.getEngine().vrDisplaysPromise.then((devices) => {
                     if (devices.length > 0) {
                         this._vrEnabled = true;
@@ -121,7 +130,7 @@ module BABYLON {
                         }
 
                         //reset the rig parameters.
-                        this.setCameraRigMode(Camera.RIG_MODE_WEBVR, { parentCamera: this, vrDisplay: this._vrDevice, frameData: this._frameData });
+                        this.setCameraRigMode(Camera.RIG_MODE_WEBVR, { parentCamera: this, vrDisplay: this._vrDevice, frameData: this._frameData, specs: this._specsVersion });
 
                         if (this._attached) {
                             this.getEngine().enableVR(this._vrDevice)
@@ -170,9 +179,17 @@ module BABYLON {
         }
 
         public _checkInputs(): void {
-            if (this._vrEnabled && this._vrDevice.getFrameData(this._frameData)) {
-                var currentPose = this._frameData.pose;
-                this.updateFromDevice(currentPose);
+            if (this._vrEnabled) {
+                if (this._specsVersion === 1.1) {
+                    this._vrDevice.getFrameData(this._frameData);
+                } else {
+                    //backwards comp
+                    let pose = this._vrDevice.getPose();
+                    this._frameData.pose = pose;
+                    // calculate view and projection matrix
+                }
+
+                this.updateFromDevice(this._frameData.pose);
             }
 
             super._checkInputs();
@@ -251,16 +268,52 @@ module BABYLON {
          * 'this' is the left or right camera (and NOT (!!!) the WebVRFreeCamera instance)
          */
         protected _getWebVRViewMatrix(): Matrix {
-            var viewArray = this._cameraRigParams["left"] ? this._cameraRigParams["frameData"].leftViewMatrix : this._cameraRigParams["frameData"].rightViewMatrix;
+            //WebVR 1.0
+            if (this._cameraRigParams["specs"] === 1.0) {
+                this._updateCameraRotationMatrix();
 
-            Matrix.FromArrayToRef(viewArray, 0, this._webvrViewMatrix);
+                Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
 
-            if (!this.getScene().useRightHandedSystem) {
-                [2, 6, 8, 9, 14].forEach((num) => {
-                    this._webvrViewMatrix.m[num] *= -1;
-                });
+                // Computing target and final matrix
+                this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
+
+                if (this.getScene().useRightHandedSystem) {
+                    Matrix.LookAtRHToRef(this.position, this._currentTarget, this.upVector, this._webvrViewMatrix);
+                } else {
+                    Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._webvrViewMatrix);
+                }
+
+                //now move the eye in the right direction
+                var eyeParams = this._cameraRigParams["eyeParameters"];
+                let offset = eyeParams.offset;
+                // it will actually always be 0, but just in case
+                if (this.getScene().useRightHandedSystem) {
+                    offset[2] *= -1;
+                }
+                Matrix.TranslationToRef(-offset[0], offset[1], -offset[2], Tmp.Matrix[0]);
+
+                this._webvrViewMatrix.multiplyToRef(Tmp.Matrix[0], this._webvrViewMatrix);
+
+            } else /* WebVR 1.1 */ {
+                var viewArray = this._cameraRigParams["left"] ? this._cameraRigParams["frameData"].leftViewMatrix : this._cameraRigParams["frameData"].rightViewMatrix;
+
+                Matrix.FromArrayToRef(viewArray, 0, this._webvrViewMatrix);
+
+                if (!this.getScene().useRightHandedSystem) {
+                    [2, 6, 8, 9, 14].forEach((num) => {
+                        this._webvrViewMatrix.m[num] *= -1;
+                    });
+                }
+
+                // update the camera rotation matrix
+                this._webvrViewMatrix.getRotationMatrixToRef(this._cameraRotationMatrix);
+                Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
+
+                // Computing target and final matrix
+                this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
             }
 
+
             let parentCamera: WebVRFreeCamera = this._cameraRigParams["parentCamera"];
 
             // should the view matrix be updated with scale and position offset?
@@ -276,25 +329,26 @@ module BABYLON {
                 this._webvrViewMatrix.invert();
             }
 
-            // update the camera rotation matrix
-            this._webvrViewMatrix.getRotationMatrixToRef(this._cameraRotationMatrix);
-            Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
-
-            // Computing target and final matrix
-            this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
             return this._webvrViewMatrix;
         }
 
         protected _getWebVRProjectionMatrix(): Matrix {
-            var projectionArray = this._cameraRigParams["left"] ? this._cameraRigParams["frameData"].leftProjectionMatrix : this._cameraRigParams["frameData"].rightProjectionMatrix;
-            Matrix.FromArrayToRef(projectionArray, 0, this._projectionMatrix);
-
-            //babylon compatible matrix
-            if (!this.getScene().useRightHandedSystem) {
-                [8, 9, 10, 11].forEach((num) => {
-                    this._projectionMatrix.m[num] *= -1;
-                });
+            if (this._cameraRigParams["specs"] === 1.0) {
+                var eyeParams = this._cameraRigParams["eyeParameters"];
+                // deprecated!!
+                Matrix.PerspectiveFovWebVRToRef(eyeParams.fieldOfView, 0.1, 1000, this._projectionMatrix, this.getScene().useRightHandedSystem);
+            } else /*WebVR 1.1*/ {
+                var projectionArray = this._cameraRigParams["left"] ? this._cameraRigParams["frameData"].leftProjectionMatrix : this._cameraRigParams["frameData"].rightProjectionMatrix;
+                Matrix.FromArrayToRef(projectionArray, 0, this._projectionMatrix);
+
+                //babylon compatible matrix
+                if (!this.getScene().useRightHandedSystem) {
+                    [8, 9, 10, 11].forEach((num) => {
+                        this._projectionMatrix.m[num] *= -1;
+                    });
+                }
             }
+
             return this._projectionMatrix;
         }
 

+ 2 - 0
src/Cameras/babylon.camera.ts

@@ -613,6 +613,7 @@
                         //Left eye
                         this._rigCameras[0].viewport = new Viewport(0, 0, 0.5, 1.0);
                         this._rigCameras[0].setCameraRigParameter("left", true);
+                        this._rigCameras[0].setCameraRigParameter("specs", rigParams.specs);
                         this._rigCameras[0].setCameraRigParameter("eyeParameters", leftEye);
                         this._rigCameras[0].setCameraRigParameter("frameData", rigParams.frameData);
                         this._rigCameras[0].setCameraRigParameter("parentCamera", rigParams.parentCamera);
@@ -624,6 +625,7 @@
                         //Right eye
                         this._rigCameras[1].viewport = new Viewport(0.5, 0, 0.5, 1.0);
                         this._rigCameras[1].setCameraRigParameter('eyeParameters', rightEye);
+                        this._rigCameras[1].setCameraRigParameter("specs", rigParams.specs);
                         this._rigCameras[1].setCameraRigParameter("frameData", rigParams.frameData);
                         this._rigCameras[1].setCameraRigParameter("parentCamera", rigParams.parentCamera);
                         this._rigCameras[1]._cameraRigParams.vrWorkMatrix = new Matrix();

+ 8 - 8
src/Math/babylon.math.ts

@@ -516,7 +516,7 @@
          * Returns the updated Color4.  
          */
         public set(r: number, g: number, b: number, a: number): Color4 {
-            return this.copyFromFloats(r, g, b,a);
+            return this.copyFromFloats(r, g, b, a);
         }
         /**
          * Returns a string containing the hexadecimal Color4 code.  
@@ -2144,7 +2144,7 @@
         /**
          * Returns a new Vector4 set from the starting index of the passed array.  
          */
-        public static FromArray(array: number[]  | Float32Array, offset?: number): Vector4 {
+        public static FromArray(array: number[] | Float32Array, offset?: number): Vector4 {
             if (!offset) {
                 offset = 0;
             }
@@ -2328,7 +2328,7 @@
          */
         public set(width: number, height: number): Size {
             return this.copyFromFloats(width, height);
-        }        
+        }
         /**
          * Returns a new Size set with the multiplication result of the current Size and the passed floats.  
          */
@@ -2465,7 +2465,7 @@
          */
         public set(x: number, y: number, z: number, w: number): Quaternion {
             return this.copyFromFloats(x, y, z, w);
-        }        
+        }
         /**
          * Returns a new Quaternion as the addition result of the passed one and the current Quaternion.  
          */
@@ -3949,10 +3949,10 @@
             result.m[1] = result.m[2] = result.m[3] = result.m[4] = 0.0;
             result.m[5] = yScale;
             result.m[6] = result.m[7] = 0.0;
-            result.m[8] = ((leftTan - rightTan) * xScale * 0.5) * rightHandedFactor;
-            result.m[9] = -((upTan - downTan) * yScale * 0.5) * rightHandedFactor;
-            result.m[10] = -(znear + zfar) / (zfar - znear) * rightHandedFactor;
-            // result.m[10] = -zfar / (znear - zfar);
+            result.m[8] = ((leftTan - rightTan) * xScale * 0.5)// * rightHandedFactor;
+            result.m[9] = -((upTan - downTan) * yScale * 0.5)// * rightHandedFactor;
+            //result.m[10] = -(znear + zfar) / (zfar - znear) * rightHandedFactor;
+            result.m[10] = -zfar / (znear - zfar);
             result.m[11] = 1.0 * rightHandedFactor;
             result.m[12] = result.m[13] = result.m[15] = 0.0;
             result.m[14] = -(2.0 * zfar * znear) / (zfar - znear);

+ 1 - 0
src/babylon.mixins.ts

@@ -25,6 +25,7 @@ interface Window {
     Float32Array: Float32ArrayConstructor;
     mozURL: any;
     msURL: any;
+    VRFrameData: any; // WebVR, from specs 1.1
 }
 
 interface WebGLVertexArrayObject {