1 using System;
2 using
UnityEngine;
3
4 namespace
UnityStandardAssets.ImageEffects
5 {
6     
[ExecuteInEditMode]
7     
[RequireComponent (typeof(Camera))]
8     
class PostEffectsHelper : MonoBehaviour
9     {
10         
void OnRenderImage (RenderTexture source, RenderTexture destination)
11         {
12             Debug.Log(
"OnRenderImage in Helper called ...");
13         }
14
15         
static void DrawLowLevelPlaneAlignedWithCamera (
16             
float dist ,
17             RenderTexture source, RenderTexture dest ,
18             Material material ,
19             Camera cameraForProjectionMatrix )
20         {
21             
// Make the destination texture the target for all rendering
22             RenderTexture.active = dest;
23             
// Assign the source texture to a property from a shader
24             material.SetTexture(
"_MainTex", source);
25             
bool invertY = true; // source.texelSize.y < 0.0f;
26             
// Set up the simple Matrix
27             GL.PushMatrix();
28             GL.LoadIdentity();
29             GL.LoadProjectionMatrix(cameraForProjectionMatrix.projectionMatrix);
30
31             
float fovYHalfRad = cameraForProjectionMatrix.fieldOfView * 0.5f * Mathf.Deg2Rad;
32             
float cotangent = Mathf.Cos(fovYHalfRad) / Mathf.Sin(fovYHalfRad);
33             
float asp = cameraForProjectionMatrix.aspect;
34
35             
float x1 = asp/-cotangent;
36             
float x2 = asp/cotangent;
37             
float y1 = 1.0f/-cotangent;
38             
float y2 = 1.0f/cotangent;
39
40             
float sc = 1.0f; // magic constant (for now)
41
42             x1 *= dist * sc;
43             x2 *= dist * sc;
44             y1 *= dist * sc;
45             y2 *= dist * sc;
46
47             
float z1 = -dist;
48
49             
for (int i = 0; i < material.passCount; i++)
50             {
51                 material.SetPass(i);
52
53                 GL.Begin(GL.QUADS);
54                 
float y1_; float y2_;
55                 
if (invertY)
56                 {
57                     y1_ =
1.0f; y2_ = 0.0f;
58                 }
59                 
else
60                 {
61                     y1_ =
0.0f; y2_ = 1.0f;
62                 }
63                 GL.TexCoord2(
0.0f, y1_); GL.Vertex3(x1, y1, z1);
64                 GL.TexCoord2(
1.0f, y1_); GL.Vertex3(x2, y1, z1);
65                 GL.TexCoord2(
1.0f, y2_); GL.Vertex3(x2, y2, z1);
66                 GL.TexCoord2(
0.0f, y2_); GL.Vertex3(x1, y2, z1);
67                 GL.End();
68             }
69
70             GL.PopMatrix();
71         }
72
73         
static void DrawBorder (
74             RenderTexture dest ,
75             Material material )
76         {
77             
float x1;
78             
float x2;
79             
float y1;
80             
float y2;
81
82             RenderTexture.active = dest;
83             
bool invertY = true; // source.texelSize.y < 0.0ff;
84             
// Set up the simple Matrix
85             GL.PushMatrix();
86             GL.LoadOrtho();
87
88             
for (int i = 0; i < material.passCount; i++)
89             {
90                 material.SetPass(i);
91
92                 
float y1_; float y2_;
93                 
if (invertY)
94                 {
95                     y1_ =
1.0f; y2_ = 0.0f;
96                 }
97                 
else
98                 {
99                     y1_ =
0.0f; y2_ = 1.0f;
100                 }
101
102                 
// left
103                 x1 =
0.0f;
104                 x2 =
0.0f + 1.0f/(dest.width*1.0f);
105                 y1 =
0.0f;
106                 y2 =
1.0f;
107                 GL.Begin(GL.QUADS);
108
109                 GL.TexCoord2(
0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
110                 GL.TexCoord2(
1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
111                 GL.TexCoord2(
1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
112                 GL.TexCoord2(
0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
113
114                 
// right
115                 x1 =
1.0f - 1.0f/(dest.width*1.0f);
116                 x2 =
1.0f;
117                 y1 =
0.0f;
118                 y2 =
1.0f;
119
120                 GL.TexCoord2(
0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
121                 GL.TexCoord2(
1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
122                 GL.TexCoord2(
1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
123                 GL.TexCoord2(
0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
124
125                 
// top
126                 x1 =
0.0f;
127                 x2 =
1.0f;
128                 y1 =
0.0f;
129                 y2 =
0.0f + 1.0f/(dest.height*1.0f);
130
131                 GL.TexCoord2(
0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
132                 GL.TexCoord2(
1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
133                 GL.TexCoord2(
1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
134                 GL.TexCoord2(
0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
135
136                 
// bottom
137                 x1 =
0.0f;
138                 x2 =
1.0f;
139                 y1 =
1.0f - 1.0f/(dest.height*1.0f);
140                 y2 =
1.0f;
141
142                 GL.TexCoord2(
0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
143                 GL.TexCoord2(
1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
144                 GL.TexCoord2(
1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
145                 GL.TexCoord2(
0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
146
147                 GL.End();
148             }
149
150             GL.PopMatrix();
151         }
152
153         
static void DrawLowLevelQuad ( float x1, float x2, float y1, float y2, RenderTexture source, RenderTexture dest, Material material )
154         {
155             
// Make the destination texture the target for all rendering
156             RenderTexture.active = dest;
157             
// Assign the source texture to a property from a shader
158             material.SetTexture(
"_MainTex", source);
159             
bool invertY = true; // source.texelSize.y < 0.0f;
160             
// Set up the simple Matrix
161             GL.PushMatrix();
162             GL.LoadOrtho();
163
164             
for (int i = 0; i < material.passCount; i++)
165             {
166                 material.SetPass(i);
167
168                 GL.Begin(GL.QUADS);
169                 
float y1_; float y2_;
170                 
if (invertY)
171                 {
172                     y1_ =
1.0f; y2_ = 0.0f;
173                 }
174                 
else
175                 {
176                     y1_ =
0.0f; y2_ = 1.0f;
177                 }
178                 GL.TexCoord2(
0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
179                 GL.TexCoord2(
1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
180                 GL.TexCoord2(
1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
181                 GL.TexCoord2(
0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
182                 GL.End();
183             }
184
185             GL.PopMatrix();
186         }
187     }
188 }


Gõ tìm kiếm nhanh...