查看: 720|回复: 0
打印 上一主题 下一主题

[其他] 设置摄像机的非对称投影矩阵

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-10-30 00:02:09 |只看该作者 |倒序浏览

设置摄像机的非对称投影矩阵
  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. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-2-5 12:11 , Processed in 0.062690 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部