/// <summary> /// Convert a FOVE 4x4 matrix into a Unity 4x4 matrix. /// </summary> /// <param name="fmat">The Fove matrix to convert</param> /// <returns>An equivalent Unity 4x4 matrix</returns> public static Matrix4x4 GetUnityMx(SFVR_Matrix44 fmat) { var m = new Matrix4x4 { m00 = fmat.mat[0 + 0 * 4], m01 = fmat.mat[0 + 1 * 4], m02 = fmat.mat[0 + 2 * 4], m03 = fmat.mat[0 + 3 * 4], m10 = fmat.mat[1 + 0 * 4], m11 = fmat.mat[1 + 1 * 4], m12 = fmat.mat[1 + 2 * 4], m13 = fmat.mat[1 + 3 * 4], m20 = fmat.mat[2 + 0 * 4], m21 = fmat.mat[2 + 1 * 4], m22 = fmat.mat[2 + 2 * 4], m23 = fmat.mat[2 + 3 * 4], m30 = fmat.mat[3 + 0 * 4], m31 = fmat.mat[3 + 1 * 4], m32 = fmat.mat[3 + 2 * 4], m33 = fmat.mat[3 + 3 * 4] }; return(m); }
void OnPreCull() { if (!_isAuthoritative) { return; } if (null == _compositor) { return; } if (_needsResolutionUpdate) { SetOptimalRenderScale(); } WaitForRenderPose_IfNeeded(); SFVR_Matrix44 lViewMat = new SFVR_Matrix44(); SFVR_Matrix44 rViewMat = new SFVR_Matrix44(); Matrix4x4 scale = Matrix4x4.Scale(new Vector3(worldScale, worldScale, worldScale)); // Something about the camera projections or relative translations means that I have to swap what the // x-axis values so positive-x is left (not right, as it typically should be). Vector3 left_translation = new Vector3() { x = usedIOD * 0.5f, y = eyeHeight, z = eyeForward } *worldScale; Vector3 right_translation = new Vector3() { x = -usedIOD * 0.5f, y = eyeHeight, z = eyeForward } *worldScale; Matrix4x4 lxform = Matrix4x4.TRS(left_translation, Quaternion.identity, new Vector3(1, 1, -1)); Matrix4x4 rxform = Matrix4x4.TRS(right_translation, Quaternion.identity, new Vector3(1, 1, -1)); _camera.SetStereoViewMatrix(Camera.StereoscopicEye.Left, lxform * transform.worldToLocalMatrix); _camera.SetStereoViewMatrix(Camera.StereoscopicEye.Right, rxform * transform.worldToLocalMatrix); // Protection junk SFVR_Matrix44 lProjMat = new SFVR_Matrix44(); SFVR_Matrix44 rProjMat = new SFVR_Matrix44(); if (_projectionErrorFree && suppressProjectionUpdates) { return; } bool foundProjectionErrors = false; EFVR_ErrorCode err; err = _sHeadset.GetEyeToHeadMatrices(out lViewMat, out rViewMat); foundProjectionErrors |= err != EFVR_ErrorCode.None; err = _sHeadset.GetProjectionMatricesRH(_camera.nearClipPlane, _camera.farClipPlane, out lProjMat, out rProjMat); foundProjectionErrors |= err != EFVR_ErrorCode.None; if (foundProjectionErrors) { _projectionErrorFree = false; Debug.Log("Giving up on flawed projection matrices..."); return; } _projectionErrorFree = true; _camera.SetStereoProjectionMatrix(Camera.StereoscopicEye.Left, FoveUnityUtils.GetUnityMx(lProjMat)); _camera.SetStereoProjectionMatrix(Camera.StereoscopicEye.Right, FoveUnityUtils.GetUnityMx(rProjMat)); }