二、中製作原理
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | using UnityEngine; using System.Collections; [ExecuteInEditMode] public class ViewPlane : MonoBehaviour { public GameObject mirrorPlane; //鏡子屏幕 public bool estimateViewFrustum = true ; public bool setNearClipPlane = false ; //是否設置近剪切平面 public float nearClipDistanceOffset = -0.01f; //近剪切平面的距離 private Camera mirrorCamera; //鏡像攝像機 // Use this for initialization void Start () { mirrorCamera = GetComponent<Camera>(); } // Update is called once per frame void Update () { if ( null != mirrorPlane && null != mirrorCamera) { //世界座標系的左下角 Vector3 pa = mirrorPlane.transform.TransformPoint( new Vector3(-5.0f, 0.0f, -5.0f)); //世界座標系的右下角 Vector3 pb = mirrorPlane.transform.TransformPoint( new Vector3(5.0f, 0.0f, -5.0f)); //世界座標系的左上角 Vector3 pc = mirrorPlane.transform.TransformPoint( new Vector3(-5.0f, 0.0f, 5.0f)); //鏡像觀察角度的世界座標位置 Vector3 pe = transform.position; //鏡像攝像機的近剪切面的距離 float n = mirrorCamera.nearClipPlane; //鏡像攝像機的遠剪切面的距離 float f = mirrorCamera.farClipPlane; //從鏡像攝像機到左下角 Vector3 va = pa - pe; //從鏡像攝像機到右下角 Vector3 vb = pb - pe; //從鏡像攝像機到左上角 Vector3 vc = pc - pe; //屏幕的右側旋轉軸 Vector3 vr = pb - pa; //屏幕的上側旋轉軸 Vector3 vu = pc - pa; //屏幕的法線 Vector3 vn; //到屏幕左邊緣的距離 float l; //到屏幕右邊緣的距離 float r; //到屏幕下邊緣的距離 float b; //到屏幕上邊緣的距離 float t; //從鏡像攝像機到屏幕的距離 float d; //如果看向鏡子的背面 if (Vector3.Dot(-Vector3.Cross(va, vc), vb) < 0.0f) { // vu = -vu; pa = pc; pb = pa + vr; pc = pa + vu; va = pa - pe; vb = pb - pe; vc = pc - pe; } vr.Normalize(); vu.Normalize(); //兩個向量的叉乘,最後在取負,因爲Unity是使用左手座標系 vn = -Vector3.Cross(vr, vu); vn.Normalize(); d = -Vector3.Dot(va, vn); if (setNearClipPlane) { n = d + nearClipDistanceOffset; mirrorCamera.nearClipPlane = n; } l = Vector3.Dot(vr, va) * n / d; r = Vector3.Dot(vr, vb) * n / d; b = Vector3.Dot(vu, va) * n / d; t = Vector3.Dot(vu, vc) * n / d; //投影矩陣 Matrix4x4 p = new Matrix4x4(); p[0, 0] = 2.0f * n / (r - l); p[0, 1] = 0.0f; p[0, 2] = (r + l) / (r - l); p[0, 3] = 0.0f; p[1, 0] = 0.0f; p[1, 1] = 2.0f * n / (t - b); p[1, 2] = (t + b) / (t - b); p[1, 3] = 0.0f; p[2, 0] = 0.0f; p[2, 1] = 0.0f; p[2, 2] = (f + n) / (n - f); p[2, 3] = 2.0f * f * n / (n - f); p[3, 0] = 0.0f; p[3, 1] = 0.0f; p[3, 2] = -1.0f; p[3, 3] = 0.0f; //旋轉矩陣 Matrix4x4 rm = new Matrix4x4(); rm[0, 0] = vr.x; rm[0, 1] = vr.y; rm[0, 2] = vr.z; rm[0, 3] = 0.0f; rm[1, 0] = vu.x; rm[1, 1] = vu.y; rm[1, 2] = vu.z; rm[1, 3] = 0.0f; rm[2, 0] = vn.x; rm[2, 1] = vn.y; rm[2, 2] = vn.z; rm[2, 3] = 0.0f; rm[3, 0] = 0.0f; rm[3, 1] = 0.0f; rm[3, 2] = 0.0f; rm[3, 3] = 1.0f; Matrix4x4 tm = new Matrix4x4(); tm[0, 0] = 1.0f; tm[0, 1] = 0.0f; tm[0, 2] = 0.0f; tm[0, 3] = -pe.x; tm[1, 0] = 0.0f; tm[1, 1] = 1.0f; tm[1, 2] = 0.0f; tm[1, 3] = -pe.y; tm[2, 0] = 0.0f; tm[2, 1] = 0.0f; tm[2, 2] = 1.0f; tm[2, 3] = -pe.z; tm[3, 0] = 0.0f; tm[3, 1] = 0.0f; tm[3, 2] = 0.0f; tm[3, 3] = 1.0f; //矩陣組 // mirrorCamera.projectionMatrix = p; mirrorCamera.worldToCameraMatrix = rm * tm; if (estimateViewFrustum) { //旋轉攝像機 Quaternion q = new Quaternion(); q.SetLookRotation((0.5f * (pb + pc) - pe), vu); //聚焦到屏幕的中心點 mirrorCamera.transform.rotation = q; //保守估計fieldOfView的值 if (mirrorCamera.aspect >= 1.0) { mirrorCamera.fieldOfView = Mathf.Rad2Deg Mathf.Atan(((pb - pa).magnitude + (pc - pa).magnitude) / va.magnitude); } else { //在攝像機角度考慮,保證視錐足夠寬 mirrorCamera.fieldOfView = Mathf.Rad2Deg / mirrorCamera.aspect Mathf.Atan(((pb - pa).magnitude + (pc - pa).magnitude) / va.magnitude); } } } } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | using UnityEngine; using System.Collections; [ExecuteInEditMode] public class Mirrors2 : MonoBehaviour { public GameObject mirrorPlane; //鏡子 public Camera mainCamera; //主攝像機 private Camera mirrorCamera; //鏡像攝像機 // Use this for initialization void Start () { mirrorCamera = GetComponent<Camera>(); } // Update is called once per frame void Update () { if ( null !=mirrorPlane&& null !=mirrorCamera&& null !=mainCamera) { //將主攝像機的世界座標位置轉換爲鏡子的局部座標位置 Vector3 postionInMirrorSpace = mirrorPlane.transform.InverseTransformPoint(mainCamera.transform.position); //一般y爲鏡面的法線方向 postionInMirrorSpace.y = -postionInMirrorSpace.y; //轉回到世界座標系的位置 mirrorCamera.transform.position = mirrorPlane.transform.TransformPoint(postionInMirrorSpace); } } } |