Code Review

1 using System;
2 using
UnityEngine;
3
4 namespace
UnityStandardAssets.ImageEffects
5 {
6     
[ExecuteInEditMode]
7     
[RequireComponent (typeof(Camera))]
8     
public class PostEffectsBase : MonoBehaviour
9     {
10         
protected bool supportHDRTextures = true;
11         
protected bool supportDX11 = false;
12         
protected bool isSupported = true;
13
14         
protected Material CheckShaderAndCreateMaterial ( Shader s, Material m2Create)
15         {
16             
if (!s)
17             {
18                 Debug.Log(
"Missing shader in " + ToString ());
19                 enabled =
false;
20                 
return null;
21             }
22
23             
if (s.isSupported && m2Create && m2Create.shader == s)
24                 
return m2Create;
25
26             
if (!s.isSupported)
27             {
28                 NotSupported ();
29                 Debug.Log(
"The shader " + s.ToString() + " on effect "+ToString()+" is not supported on this platform!");
30                 
return null;
31             }
32             
else
33             {
34                 m2Create =
new Material (s);
35                 m2Create.hideFlags = HideFlags.DontSave;
36                 
if (m2Create)
37                     
return m2Create;
38                 
else return null;
39             }
40         }
41
42
43         
protected Material CreateMaterial (Shader s, Material m2Create)
44         {
45             
if (!s)
46             {
47                 Debug.Log (
"Missing shader in " + ToString ());
48                 
return null;
49             }
50
51             
if (m2Create && (m2Create.shader == s) && (s.isSupported))
52                 
return m2Create;
53
54             
if (!s.isSupported)
55             {
56                 
return null;
57             }
58             
else
59             {
60                 m2Create =
new Material (s);
61                 m2Create.hideFlags = HideFlags.DontSave;
62                 
if (m2Create)
63                     
return m2Create;
64                 
else return null;
65             }
66         }
67
68         
void OnEnable ()
69         {
70             isSupported =
true;
71         }
72
73         
protected bool CheckSupport ()
74         {
75             
return CheckSupport (false);
76         }
77
78
79         
public virtual bool CheckResources ()
80         {
81             Debug.LogWarning (
"CheckResources () for " + ToString() + " should be overwritten.");
82             
return isSupported;
83         }
84
85
86         
protected void Start ()
87         {
88             CheckResources ();
89         }
90
91         
protected bool CheckSupport (bool needDepth)
92         {
93             isSupported =
true;
94             supportHDRTextures = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf);
95             supportDX11 = SystemInfo.graphicsShaderLevel >=
50 && SystemInfo.supportsComputeShaders;
96
97             
if (!SystemInfo.supportsImageEffects || !SystemInfo.supportsRenderTextures)
98             {
99                 NotSupported ();
100                 
return false;
101             }
102
103             
if (needDepth && !SystemInfo.SupportsRenderTextureFormat (RenderTextureFormat.Depth))
104             {
105                 NotSupported ();
106                 
return false;
107             }
108
109             
if (needDepth)
110                 GetComponent<Camera>().depthTextureMode |= DepthTextureMode.Depth;
111
112             
return true;
113         }
114
115         
protected bool CheckSupport (bool needDepth, bool needHdr)
116         {
117             
if (!CheckSupport(needDepth))
118                 
return false;
119
120             
if (needHdr && !supportHDRTextures)
121             {
122                 NotSupported ();
123                 
return false;
124             }
125
126             
return true;
127         }
128
129
130         
public bool Dx11Support ()
131         {
132             
return supportDX11;
133         }
134
135
136         
protected void ReportAutoDisable ()
137         {
138             Debug.LogWarning (
"The image effect " + ToString() + " has been disabled as it's not supported on the current platform.");
139         }
140
141         
// deprecated but needed for old effects to survive upgrading
142         
bool CheckShader (Shader s)
143         {
144             Debug.Log(
"The shader " + s.ToString () + " on effect "+ ToString () + " is not part of the Unity 3.2+ effects suite anymore. For best performance and quality, please ensure you are using the latest Standard Assets Image Effects (Pro only) package.");
145             
if (!s.isSupported)
146             {
147                 NotSupported ();
148                 
return false;
149             }
150             
else
151             {
152                 
return false;
153             }
154         }
155
156
157         
protected void NotSupported ()
158         {
159             enabled =
false;
160             isSupported =
false;
161             
return;
162         }
163
164
165         
protected void DrawBorder (RenderTexture dest, Material material)
166         {
167             
float x1;
168             
float x2;
169             
float y1;
170             
float y2;
171
172             RenderTexture.active = dest;
173             
bool invertY = true; // source.texelSize.y < 0.0ff;
174             
// Set up the simple Matrix
175             GL.PushMatrix();
176             GL.LoadOrtho();
177
178             
for (int i = 0; i < material.passCount; i++)
179             {
180                 material.SetPass(i);
181
182                 
float y1_; float y2_;
183                 
if (invertY)
184                 {
185                     y1_ =
1.0f; y2_ = 0.0f;
186                 }
187                 
else
188                 {
189                     y1_ =
0.0f; y2_ = 1.0f;
190                 }
191
192                 
// left
193                 x1 =
0.0f;
194                 x2 =
0.0f + 1.0f/(dest.width*1.0f);
195                 y1 =
0.0f;
196                 y2 =
1.0f;
197                 GL.Begin(GL.QUADS);
198
199                 GL.TexCoord2(
0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
200                 GL.TexCoord2(
1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
201                 GL.TexCoord2(
1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
202                 GL.TexCoord2(
0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
203
204                 
// right
205                 x1 =
1.0f - 1.0f/(dest.width*1.0f);
206                 x2 =
1.0f;
207                 y1 =
0.0f;
208                 y2 =
1.0f;
209
210                 GL.TexCoord2(
0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
211                 GL.TexCoord2(
1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
212                 GL.TexCoord2(
1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
213                 GL.TexCoord2(
0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
214
215                 
// top
216                 x1 =
0.0f;
217                 x2 =
1.0f;
218                 y1 =
0.0f;
219                 y2 =
0.0f + 1.0f/(dest.height*1.0f);
220
221                 GL.TexCoord2(
0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
222                 GL.TexCoord2(
1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
223                 GL.TexCoord2(
1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
224                 GL.TexCoord2(
0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
225
226                 
// bottom
227                 x1 =
0.0f;
228                 x2 =
1.0f;
229                 y1 =
1.0f - 1.0f/(dest.height*1.0f);
230                 y2 =
1.0f;
231
232                 GL.TexCoord2(
0.0f, y1_); GL.Vertex3(x1, y1, 0.1f);
233                 GL.TexCoord2(
1.0f, y1_); GL.Vertex3(x2, y1, 0.1f);
234                 GL.TexCoord2(
1.0f, y2_); GL.Vertex3(x2, y2, 0.1f);
235                 GL.TexCoord2(
0.0f, y2_); GL.Vertex3(x1, y2, 0.1f);
236
237                 GL.End();
238             }
239
240             GL.PopMatrix();
241         }
242     }
243 }


Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *