攝像機的矩陣
https://www.cnblogs.com/w-wfy/p/7243459.html
//關於矩陣我建議還是看一下上面這個網址 理解一下什麼是unity裏的矩陣
代碼如下:
static Matrix4x4 PerspectiveOffCenter(float left, float right, float bottom, float top, float near, float far)
{
float x = 2.0F * near / (right - left);
float y = 2.0F * near / (top - bottom);
float a = (right + left) / (right - left);
float b = (top + bottom) / (top - bottom);
float c = -(far + near) / (far - near);
float d = -(2.0F * far * near) / (far - near);
float e = -1.0F;
Matrix4x4 m = new Matrix4x4();
m[0, 0] = x;
m[0, 1] = 0;
m[0, 2] = a;
m[0, 3] = 0;
m[1, 0] = 0;
m[1, 1] = y;
m[1, 2] = b;
m[1, 3] = 0;
m[2, 0] = 0;
m[2, 1] = 0;
m[2, 2] = c;
m[2, 3] = d;
m[3, 0] = 0;
m[3, 1] = 0;
m[3, 2] = e;
m[3, 3] = 0;
return m;
}
void UpdateLeftRightEyeCameraAndProjMatrix()
{
//更新投影矩陣
//左眼
float Cam_y = Left_Cam.transform.position.y;
float left = (-x_left - Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;
float right = (-x_left + Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;
float top = (-z_left + Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;
float bottom = (-z_left - Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;
Matrix4x4 m = PerspectiveOffCenter(left, right, bottom, top, Left_Cam.nearClipPlane, Left_Cam.farClipPlane);
Left_Cam.projectionMatrix = m;
//主攝像機 右眼
Cam_y = Right_Cam.transform.position.y;
left = (-x_right - Len_Rect / 2) / Cam_y * Right_Cam.nearClipPlane;
right = (-x_right + Len_Rect / 2) / Cam_y * Right_Cam.nearClipPlane;
top = (-z_right + Len_Rect / 2) / Cam_y * Right_Cam.nearClipPlane;
bottom = (-z_right - Len_Rect / 2) / Cam_y * Right_Cam.nearClipPlane;
m = PerspectiveOffCenter(left, right, bottom, top, Right_Cam.nearClipPlane, Right_Cam.farClipPlane);
Right_Cam.projectionMatrix = m;
}
說一下 以左眼爲例子
//左眼
float Cam_y = Left_Cam.transform.position.y;
float left = (-x_left - Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;
float right = (-x_left + Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;
float top = (-z_left + Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;
float bottom = (-z_left - Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;
Matrix4x4 m = PerspectiveOffCenter(left, right, bottom, top, Left_Cam.nearClipPlane, Left_Cam.farClipPlane);
Left_Cam.projectionMatrix = m;
-x_Left是我左眼的座標的x值,Len_Rect是我矩陣投影的邊長,計算完再乘一個比例就是我近切面的左邊座標,right top bottom 相同,關係如下圖:求float left
這樣兩個攝像頭的矩陣投影是重合的