纳金网

标题: 设置摄像机的非对称投影矩阵 [打印本页]

作者: 王者再临    时间: 2015-10-30 00:02
标题: 设置摄像机的非对称投影矩阵

设置摄像机的非对称投影矩阵
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;

  4. public class CamProjectionMatrix : MonoBehaviour
  5. {

  6.     public float left;
  7.     public float right;
  8.     public float top;
  9.     public float bottom;

  10.     public Camera mCam;
  11.     public GameObject obj;

  12.     bool init;

  13.     void Start()
  14.     {
  15.         RateTrasform(mCam.gameObject, obj, mCam.nearClipPlane, out left, out right, out top, out bottom);
  16.         init = true;
  17.     }

  18.     void LateUpdate()
  19.     {
  20.         if(init)
  21.         {
  22.             obj.SetActive(false);
  23.         }

  24.         //RateTrasform(mCam.gameObject, obj, mCam.nearClipPlane, out left, out right, out top, out bottom);
  25.         Matrix4x4 m = PerspectiveOffCenter(left, right, top, bottom, mCam.nearClipPlane, mCam.farClipPlane);
  26.         mCam.projectionMatrix = m;
  27.         
  28.     }



  29.     public static Matrix4x4 PerspectiveOffCenter(float left, float right, float top, float bottom, float near, float far)
  30.     {
  31.        // Debug.Log("   " + left + "   " + right + "  " + top + "  " + bottom);
  32.         float x = 2.0F * near / (right - left);
  33.         float y = 2.0F * near / (top - bottom);
  34.         float a = (right + left) / (right - left);
  35.         float b = (top + bottom) / (top - bottom);
  36.         float c = -(far + near) / (far - near);
  37.         float d = -(2.0F * far * near) / (far - near);
  38.         float e = -1.0F;
  39.         Matrix4x4 m = new Matrix4x4();
  40.         m[0, 0] = x;
  41.         m[0, 1] = 0;
  42.         m[0, 2] = a;
  43.         m[0, 3] = 0;
  44.         m[1, 0] = 0;
  45.         m[1, 1] = y;
  46.         m[1, 2] = b;
  47.         m[1, 3] = 0;
  48.         m[2, 0] = 0;
  49.         m[2, 1] = 0;
  50.         m[2, 2] = c;
  51.         m[2, 3] = d;
  52.         m[3, 0] = 0;
  53.         m[3, 1] = 0;
  54.         m[3, 2] = e;
  55.         m[3, 3] = 0;
  56.         return m;
  57.     }

  58.     void RateTrasform(GameObject cam, GameObject obj, float near, out float left, out float right, out float top, out float bottom)
  59.     {
  60.         float xSize = obj.GetComponent<MeshFilter>().mesh.bounds.size.x * obj.transform.localScale.x/2.0f;
  61.         float ySize = obj.GetComponent<MeshFilter>().mesh.bounds.size.y * obj.transform.localScale.y / 2.0f;


  62.         float leftx = obj.transform.position.x - xSize;
  63.         float rightx = obj.transform.position.x + xSize;
  64.         float topy = obj.transform.position.y + ySize;
  65.         float bottomy = obj.transform.position.y - ySize;

  66.         left = leftx - cam.transform.position.x;
  67.         right = rightx - cam.transform.position.x;
  68.         top = topy - cam.transform.position.y;
  69.         bottom = bottomy - cam.transform.position.y;

  70.         float zlenght = obj.transform.position.z - cam.transform.position.z - obj.transform.localScale.z / 2.0f;

  71.         left = near * left / zlenght;
  72.         right = near * right / zlenght;
  73.         top = near * top / zlenght;
  74.         bottom = near * bottom / zlenght;
  75.     }

  76. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5