Example #1
0
        public Vector3 GetHMDPosition(VarjoPlugin.PoseType type)
        {
            VarjoPlugin.Matrix matrix;
            GetPose(type, out matrix);

            var unityView = VarjoMatrixUtils.WorldMatrixToUnity(matrix.value);

            return(unityView.GetColumn(3));
        }
Example #2
0
        public Quaternion GetHMDOrientation(VarjoPlugin.PoseType type)
        {
            VarjoPlugin.Matrix matrix;
            GetPose(type, out matrix);

            var unityView = VarjoMatrixUtils.WorldMatrixToUnity(matrix.value);

            return(Quaternion.LookRotation(unityView.GetColumn(2), unityView.GetColumn(1)));
        }
Example #3
0
        public void UpdateFromLayer()
        {
            VerifyCamera();

            VarjoPlugin.PoseType pose = (CameraId == CAMERA_ID.CONTEXT_LEFT || CameraId == CAMERA_ID.FOCUS_LEFT) ? VarjoPlugin.PoseType.LEFT_EYE : VarjoPlugin.PoseType.RIGHT_EYE;

            VarjoPlugin.Matrix centerPoseVRJ, eyePoseVRJ;
            VarjoManager.Instance.GetPose(VarjoPlugin.PoseType.CENTER, out centerPoseVRJ);
            VarjoManager.Instance.GetPose(pose, out eyePoseVRJ);

            var centerPose = VarjoMatrixUtils.WorldMatrixToUnity(centerPoseVRJ.value);
            var eyePose    = VarjoMatrixUtils.WorldMatrixToUnity(eyePoseVRJ.value);

            var centerPoseInv = centerPose.inverse;

            var worldMatrix = centerPoseInv * eyePose;

            // TODO: for the first few frames we can receive invalid data which causes annoying debug log output.
            // Let's just do a quick and dirty validation and skip the buffer info if the data doesn't seem to be correct.
            for (int i = 0; i < 16; i++)
            {
                if (float.IsNaN(worldMatrix[i]))
                {
                    Debug.Log("Received invalid data.");
                    return;
                }
            }

            transform.localPosition = worldMatrix.GetColumn(3);
            transform.localRotation = Quaternion.LookRotation(worldMatrix.GetColumn(2), worldMatrix.GetColumn(1));

            var viewInfo = VarjoManager.Instance.GetViewInfo((int)CameraId);

            viewInfo.projectionMatrix.CopyTo(m_SubmissionProjMatrix, 0);
            viewInfo.invViewMatrix.CopyTo(m_SubmissionViewMatrix, 0);

            // Plug the near and far clip values to the submission projection matrix as well, for depth reconstruction
            float near = cam.nearClipPlane;
            float far  = cam.farClipPlane;

            m_SubmissionProjMatrix[2 * 4 + 2] = -far / (near - far) - 1;
            m_SubmissionProjMatrix[3 * 4 + 2] = -(far * near) / (near - far);
            cam.usePhysicalProperties         = true;

            var projMat = VarjoMatrixUtils.ProjectionMatrixToUnity(viewInfo.projectionMatrix, near, far);

            float aspect = projMat.m11 / projMat.m00;

            cam.sensorSize = new Vector2(cam.sensorSize.x, cam.sensorSize.x / aspect);
            // Calculate FOV and shift
            float       t       = projMat.m11;
            const float Rad2Deg = 180.0f / (float)Math.PI;
            float       fov     = ((float)Math.Atan(1.0f / t)) * 2.0f * Rad2Deg;

            if (m_Owner.flipY)
            {
                projMat.m12 *= -1.0f;
            }

            float lensX = projMat.m02 / 2.0f;
            float lensY = projMat.m12 / 2.0f;

            cam.fieldOfView = fov;
            cam.lensShift   = new Vector2(lensX, lensY);

#if UNITY_2018_3_OR_NEWER
            cam.gateFit = Camera.GateFitMode.None;
#else
            // Pre-2018.3, we couldn't get exact 1:1 match with the projection matrix by tweaking camera parameters, so override the matrix directly
            cam.projectionMatrix = projMat;
#endif

            if (m_Owner.useOcclusionMesh && (m_OcclusionMesh != null))
            {
                // Draw the occlusion mesh; move it to the camera near plane so that it won't get frustum culled.
                Matrix4x4 occMeshMtx = new Matrix4x4();
                occMeshMtx.SetTRS(new Vector3(0.0f, 0.0f, cam.nearClipPlane), Quaternion.identity, Vector3.one);
                occMeshMtx = transform.localToWorldMatrix * occMeshMtx;
                Graphics.DrawMesh(m_OcclusionMesh, occMeshMtx, m_Owner.occlusionMaterial, 0, cam);
            }
        }
Example #4
0
 public void GetPose(VarjoPlugin.PoseType type, out VarjoPlugin.Matrix matrix)
 {
     matrix = latestFramePose.eyePoses[(int)type - 1];
 }