浏览代码

View matrix now correctly calculated for RH

Raanan Weber 8 年之前
父节点
当前提交
7101f38e21
共有 3 个文件被更改,包括 70 次插入17 次删除
  1. 40 1
      src/Cameras/VR/babylon.webVRCamera.ts
  2. 29 15
      src/Cameras/babylon.camera.ts
  3. 1 1
      src/Cameras/babylon.targetCamera.ts

+ 40 - 1
src/Cameras/VR/babylon.webVRCamera.ts

@@ -23,7 +23,7 @@ module BABYLON {
 
         private _quaternionCache: Quaternion;
 
-        private _positionOffset: Vector3;
+        private _positionOffset: Vector3 = Vector3.Zero();
 
         constructor(name: string, position: Vector3, scene: Scene, compensateDistortion = false, private webVROptions: WebVROptions = {}) {
             super(name, position, scene);
@@ -84,8 +84,20 @@ module BABYLON {
                 if (currentPost && currentPost.orientation) {
                     this._cacheState = currentPost;
                     this.rotationQuaternion.copyFromFloats(this._cacheState.orientation[0], this._cacheState.orientation[1], -this._cacheState.orientation[2], -this._cacheState.orientation[3]);
+
+                    if (this.getScene().useRightHandedSystem) {
+                        this.rotationQuaternion.z *= -1;
+                        this.rotationQuaternion.w *= -1;
+                        /*let m = Matrix.Identity();
+                        this.rotationQuaternion.toRotationMatrix(m);
+                        m.multiplyToRef(Matrix.RotationAxis(Axis.Y, Math.PI), m);
+                        this.rotationQuaternion.fromRotationMatrix(m);*/
+                    }
                     if (this.webVROptions.trackPosition && this._cacheState.position) {
                         this.position.copyFromFloats(this._cacheState.position[0], this._cacheState.position[1], -this._cacheState.position[2]);
+                        if (this.getScene().useRightHandedSystem) {
+                            this.position.z *= -1;
+                        }
                         //scale the position accordingly
                         this.webVROptions.positionScale && this.position.scaleInPlace(this.webVROptions.positionScale);
                         //add the position offset
@@ -147,6 +159,33 @@ module BABYLON {
                 this._positionOffset.copyFrom(this.position);
             }
         }
+
+        private _tmpMat = Matrix.Identity();
+
+        protected _getWebVRViewMatrix(): Matrix {
+            /*
+            Teleport - you have to invert the viewmatrix first
+            then add the position (last row)
+            and invert again
+            */
+            var viewArray = this._cameraRigParams["left"] ? this._cameraRigParams["frameData"].leftViewMatrix : this._cameraRigParams["frameData"].rightViewMatrix;
+
+            if (!this.getScene().useRightHandedSystem) {
+                [2, 6, 8, 9, 14].forEach(function (num) {
+                    viewArray[num] *= -1;
+                });
+            }
+            Matrix.FromArrayToRef(viewArray, 0, this._webvrViewMatrix);
+
+            this._webvrViewMatrix.invert();
+            this._webvrViewMatrix.m[12] += 0; //this._positionOffset.x;
+            this._webvrViewMatrix.m[13] += -0.5// this._positionOffset.y;
+            this._webvrViewMatrix.m[14] += - 0.5;// this._positionOffset.z;
+            this._webvrViewMatrix.invert();
+
+
+            return this._webvrViewMatrix;
+        }
     }
 
     export class WebVRGamepadCamera extends WebVRFreeCamera {

+ 29 - 15
src/Cameras/babylon.camera.ts

@@ -119,6 +119,8 @@
         public _cameraRigParams: any;
         public _rigCameras = new Array<Camera>();
         public _rigPostProcess: PostProcess;
+        protected _webvrViewMatrix = Matrix.Identity();
+
 
         // Cache
         private _computedViewMatrix = Matrix.Identity();
@@ -127,7 +129,6 @@
         private _worldMatrix: Matrix;
         public _postProcesses = new Array<PostProcess>();
         private _transformMatrix = Matrix.Zero();
-        private _webvrViewMatrix = Matrix.Identity();
 
         public _activeMeshes = new SmartArray<Mesh>(256);
 
@@ -602,17 +603,22 @@
                         this._rigCameras[0].viewport = new Viewport(0, 0, 0.5, 1.0);
                         this._rigCameras[0].setCameraRigParameter("left", true);
                         this._rigCameras[0].setCameraRigParameter("frameData", rigParams.frameData);
+                        this._rigCameras[0].setCameraRigParameter('eyeParameters', leftEye);
                         //this._rigCameras[0].setCameraRigParameter("vrOffsetMatrix", Matrix.Translation(-leftEye.offset[0], leftEye.offset[1], -leftEye.offset[2]));
                         this._rigCameras[0]._cameraRigParams.vrWorkMatrix = new Matrix();
                         this._rigCameras[0].getProjectionMatrix = this._getWebVRProjectionMatrix;
-                        this._rigCameras[0]._cameraRigParams.vrPreViewMatrix = Matrix.Translation(-leftEye.offset[0], 0, 0);
-
+                        this._rigCameras[0]._getViewMatrix = this._getWebVRViewMatrix;
+                        //this._rigCameras[0]._cameraRigParams.vrPreViewMatrix = Matrix.Translation(-leftEye.offset[0], leftEye.offset[1], leftEye.offset[2]);
+                        //this._rigCameras[0]._cameraRigParams.getViewMatrix = this._getViewMatrix;
                         this._rigCameras[1].viewport = new Viewport(0.5, 0, 0.5, 1.0);
+                        this._rigCameras[1].setCameraRigParameter('eyeParameters', rightEye);
                         this._rigCameras[1].setCameraRigParameter("frameData", rigParams.frameData);
                         //this._rigCameras[1].setCameraRigParameter("vrOffsetMatrix", Matrix.Translation(-rightEye.offset[0], rightEye.offset[1], -rightEye.offset[2]));
                         this._rigCameras[1]._cameraRigParams.vrWorkMatrix = new Matrix();
                         this._rigCameras[1].getProjectionMatrix = this._getWebVRProjectionMatrix;
-                        this._rigCameras[1]._cameraRigParams.vrPreViewMatrix = Matrix.Translation(-rightEye.offset[0], 0, 0);
+                        this._rigCameras[1]._getViewMatrix = this._getWebVRViewMatrix;
+                        //this._rigCameras[1]._cameraRigParams.vrPreViewMatrix = Matrix.Translation(-rightEye.offset[0], rightEye.offset[1], rightEye.offset[2]);
+                        //this._rigCameras[1]._cameraRigParams.getViewMatrix = this._getViewMatrix;
 
                     }
                     break;
@@ -633,22 +639,30 @@
         private _getWebVRProjectionMatrix(): Matrix {
             var projectionArray = this._cameraRigParams["left"] ? this._cameraRigParams["frameData"].leftProjectionMatrix : this._cameraRigParams["frameData"].rightProjectionMatrix;
             //babylon compatible matrix
-            [8, 9, 10, 11].forEach(function (num) {
-                projectionArray[num] *= -1;
-            });
+            if (!this.getScene().useRightHandedSystem) {
+                [8, 9, 10, 11].forEach(function (num) {
+                    projectionArray[num] *= -1;
+                });
+            }
             Matrix.FromArrayToRef(projectionArray, 0, this._projectionMatrix);
+            //var eyeParams = this._cameraRigParams['eyeParameters'];
+            //Matrix.PerspectiveFovWebVRToRef(eyeParams.fieldOfView, this.minZ, this.maxZ, this._projectionMatrix, this.getScene().useRightHandedSystem)
             return this._projectionMatrix;
         }
 
         //Can be used, but we'll use the free camera's view matrix calculation
-        private _getWebVRViewMatrix(): Matrix {
-            var projectionArray = this._cameraRigParams["left"] ? this._cameraRigParams["frameData"].leftViewMatrix : this._cameraRigParams["frameData"].rightViewMatrix;
-            //babylon compatible matrix
-            [10].forEach(function (num) {
-                projectionArray[num] *= -1;
-            });
-            Matrix.FromArrayToRef(projectionArray, 0, this._webvrViewMatrix);
-            return this._webvrViewMatrix;
+        protected _getWebVRViewMatrix(): Matrix {
+            /*var viewArray = this._cameraRigParams["left"] ? this._cameraRigParams["frameData"].leftViewMatrix : this._cameraRigParams["frameData"].rightViewMatrix;
+
+            if (!this.getScene().useRightHandedSystem) {
+                [2, 6, 8, 9, 14].forEach(function (num) {
+                    viewArray[num] *= -1;
+                });
+            }
+            Matrix.FromArrayToRef(viewArray, 0, this._webvrViewMatrix);
+
+            return this._webvrViewMatrix;*/
+            return Matrix.Identity();
         }
 
         public setCameraRigParameter(name: string, value: any) {

+ 1 - 1
src/Cameras/babylon.targetCamera.ts

@@ -247,7 +247,7 @@ module BABYLON {
             } else {
                 Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
             }
-            
+
             return this._viewMatrix;
         }