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
}
// 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
}
// 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);
}
}
}
|