纳金网
标题:
设置摄像机的非对称投影矩阵
[打印本页]
作者:
王者再临
时间:
2015-10-30 00:02
标题:
设置摄像机的非对称投影矩阵
设置摄像机的非对称投影矩阵
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CamProjectionMatrix : MonoBehaviour
{
public float left;
public float right;
public float top;
public float bottom;
public Camera mCam;
public GameObject obj;
bool init;
void Start()
{
RateTrasform(mCam.gameObject, obj, mCam.nearClipPlane, out left, out right, out top, out bottom);
init = true;
}
void LateUpdate()
{
if(init)
{
obj.SetActive(false);
}
//RateTrasform(mCam.gameObject, obj, mCam.nearClipPlane, out left, out right, out top, out bottom);
Matrix4x4 m = PerspectiveOffCenter(left, right, top, bottom, mCam.nearClipPlane, mCam.farClipPlane);
mCam.projectionMatrix = m;
}
public static Matrix4x4 PerspectiveOffCenter(float left, float right, float top, float bottom, float near, float far)
{
// Debug.Log(" " + left + " " + right + " " + top + " " + bottom);
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 RateTrasform(GameObject cam, GameObject obj, float near, out float left, out float right, out float top, out float bottom)
{
float xSize = obj.GetComponent<MeshFilter>().mesh.bounds.size.x * obj.transform.localScale.x/2.0f;
float ySize = obj.GetComponent<MeshFilter>().mesh.bounds.size.y * obj.transform.localScale.y / 2.0f;
float leftx = obj.transform.position.x - xSize;
float rightx = obj.transform.position.x + xSize;
float topy = obj.transform.position.y + ySize;
float bottomy = obj.transform.position.y - ySize;
left = leftx - cam.transform.position.x;
right = rightx - cam.transform.position.x;
top = topy - cam.transform.position.y;
bottom = bottomy - cam.transform.position.y;
float zlenght = obj.transform.position.z - cam.transform.position.z - obj.transform.localScale.z / 2.0f;
left = near * left / zlenght;
right = near * right / zlenght;
top = near * top / zlenght;
bottom = near * bottom / zlenght;
}
}
复制代码
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5