Unity设置Camera的投影矩阵

时间:2024-10-30 12:46:36
  • public class Cube : MonoBehaviour{
  • public Camera LeftCamera;
  • Matrix4x4 CalcProjection(float fx, float fy, float cx, float cy, float far, float near) {
  • Matrix4x4 P = new Matrix4x4();;
  • P[0, 0] = 2F * fx / 1920.0F;
  • P[0, 2] = (1920.0F - 2F * cx) / 1920F;
  • P[1, 1] = 2F * fy / 1080.0F;
  • P[1, 2] = (2F * cy - 1080.0F) / 1080.0F;
  • P[2, 2] = -(far + near) / (far - near);
  • P[2, 3] = -2F * far * near / (far - near);
  • P[3, 2] = -1.0F;
  • return P;
  • }
  • void Update()
  • {
  • GameObject LeftCameraObject = ("LeftCamera");
  • LeftCamera = <Camera>();
  • float fx; float fy; float cx; float cy; float far; float near;
  • fx=2856.518311F; fy=2856.518311F; cx=924.223999F; cy=625.578186F; far = 2000F; near = 0.2F;
  • = CalcProjection(fx, fy, cx, cy, far, near);
  • }
  • }