Code Review

1 using System;
2 using
UnityEngine;
3
4 namespace
UnityStandardAssets.ImageEffects
5 {
6     
[ExecuteInEditMode]
7     
[AddComponentMenu("Image Effects/Blur/Blur")]
8     
public class Blur : MonoBehaviour
9     {

10         ///
Blur iterations - larger number means more blur.
11         
[Range(0,10)]
12         
public int iterations = 3;
13
14         ///
Blur spread for each iteration. Lower values
15         ///
give better looking blur, but require more iterations to
16         ///
get large blurs. Value is usually between 0.5 and 1.0.
17         
[Range(0.0f,1.0f)]
18         
public float blurSpread = 0.6f;
19
20
21         
// --------------------------------------------------------
22         
// The blur iteration shader.
23         
// Basically it just takes 4 texture samples and averages them.
24         
// By applying it repeatedly and spreading out sample locations
25         
// we get a Gaussian blur approximation.
26
27         
public Shader blurShader = null;
28
29         
static Material m_Material = null;
30         
protected Material material {
31             
get {
32                 
if (m_Material == null) {
33                     m_Material =
new Material(blurShader);
34                     m_Material.hideFlags = HideFlags.DontSave;
35                 }
36                 
return m_Material;
37             }
38         }
39
40         
protected void OnDisable() {
41             
if ( m_Material ) {
42                 DestroyImmediate( m_Material );
43             }
44         }
45
46         
// --------------------------------------------------------
47
48         
protected void Start()
49         {
50             
// Disable if we don't support image effects
51             
if (!SystemInfo.supportsImageEffects) {
52                 enabled =
false;
53                 
return;
54             }
55             
// Disable if the shader can't run on the users graphics card
56             
if (!blurShader || !material.shader.isSupported) {
57                 enabled =
false;
58                 
return;
59             }
60         }
61
62         
// Performs one blur iteration.
63         
public void FourTapCone (RenderTexture source, RenderTexture dest, int iteration)
64         {
65             
float off = 0.5f + iteration*blurSpread;
66             Graphics.BlitMultiTap (source, dest, material,
67                                    
new Vector2(-off, -off),
68                                    
new Vector2(-off, off),
69                                    
new Vector2( off, off),
70                                    
new Vector2( off, -off)
71                 );
72         }
73
74         
// Downsamples the texture to a quarter resolution.
75         
private void DownSample4x (RenderTexture source, RenderTexture dest)
76         {
77             
float off = 1.0f;
78             Graphics.BlitMultiTap (source, dest, material,
79                                    
new Vector2(-off, -off),
80                                    
new Vector2(-off, off),
81                                    
new Vector2( off, off),
82                                    
new Vector2( off, -off)
83                 );
84         }
85
86         
// Called by the camera to apply the image effect
87         
void OnRenderImage (RenderTexture source, RenderTexture destination) {
88             
int rtW = source.width/4;
89             
int rtH = source.height/4;
90             RenderTexture buffer = RenderTexture.GetTemporary(rtW, rtH,
0);
91
92             
// Copy source to the 4x4 smaller texture.
93             DownSample4x (source, buffer);
94
95             
// Blur the small texture
96             
for(int i = 0; i < iterations; i++)
97             {
98                 RenderTexture buffer2 = RenderTexture.GetTemporary(rtW, rtH,
0);
99                 FourTapCone (buffer, buffer2, i);
100                 RenderTexture.ReleaseTemporary(buffer);
101                 buffer = buffer2;
102             }
103             Graphics.Blit(buffer, destination);
104
105             RenderTexture.ReleaseTemporary(buffer);
106         }
107     }
108 }


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 *