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


Blur iterations - larger number means more blur.

Blur spread for each iteration. Lower values

give better looking blur, but require more iterations to

get large blurs. Value is usually between 0.5 and 1.0.

--------------------------------------------------------

The blur iteration shader.

Basically it just takes 4 texture samples and averages them.

By applying it repeatedly and spreading out sample locations

we get a Gaussian blur approximation.

--------------------------------------------------------

Disable if we don't support image effects

Disable if the shader can't run on the users graphics card

Performs one blur iteration.

Downsamples the texture to a quarter resolution.

Called by the camera to apply the image effect

Copy source to the 4x4 smaller texture.

Blur the small texture



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