1
+ using UnityEngine ;
2
+ using System . Collections ;
3
+
4
+ namespace SecondStar . Effects
5
+ {
6
+ /// <summary>
7
+ /// Port of standard blur effect
8
+ /// </summary>
9
+ public class BlurEffect : ImageEffectBase {
10
+
11
+ public enum BlurType {
12
+ StandardGauss = 0 ,
13
+ SgxGauss = 1 ,
14
+ }
15
+
16
+ [ Range ( 0 , 2 ) ]
17
+ public int downsample ;
18
+ [ Range ( 0f , 10f ) ]
19
+ public float blurSize = 3 ;
20
+ [ Range ( 1 , 4 ) ]
21
+ public int blurIterations = 2 ;
22
+ public BlurType blurType = BlurType . StandardGauss ;
23
+
24
+ public LayerMask maskLayer ;
25
+ Camera effectsCamera ;
26
+
27
+ Material mixMaterial ;
28
+
29
+ void Reset ( )
30
+ {
31
+ shader = Shader . Find ( "KBD/Fast Blur" ) ;
32
+ }
33
+
34
+ private Material MixMaterial
35
+ {
36
+ get
37
+ {
38
+ if ( mixMaterial == null )
39
+ {
40
+ mixMaterial = new Material ( Shader . Find ( "KBD/Mix Shader" ) ) ;
41
+ mixMaterial . hideFlags = HideFlags . HideAndDontSave ;
42
+ }
43
+ return mixMaterial ;
44
+ }
45
+ }
46
+
47
+ protected override void OnDisable ( )
48
+ {
49
+ base . OnDisable ( ) ;
50
+ if ( mixMaterial != null )
51
+ DestroyImmediate ( mixMaterial ) ;
52
+ if ( effectsCamera != null )
53
+ DestroyImmediate ( effectsCamera ) ;
54
+ }
55
+
56
+ protected override void OnPreRender ( )
57
+ {
58
+ if ( maskLayer . value != 0 && effectsCamera == null )
59
+ {
60
+ GameObject effectCamObject = new GameObject ( ) ;
61
+ effectCamObject . hideFlags = HideFlags . HideAndDontSave ;
62
+ effectsCamera = effectCamObject . AddComponent < Camera > ( ) ;
63
+ effectsCamera . enabled = false ;
64
+ }
65
+ }
66
+
67
+ protected override void OnRenderImage ( RenderTexture source , RenderTexture destination )
68
+ {
69
+ float widthMod = 1.0f / ( 1.0f * ( 1 << downsample ) ) ;
70
+ Vector4 blurParams = new Vector4 ( blurSize * widthMod , - blurSize * widthMod , 0.0f , 0.0f ) ;
71
+ material . SetVector ( "_Parameter" , blurParams ) ;
72
+ source . filterMode = FilterMode . Bilinear ;
73
+
74
+ int renderWidth = source . width >> downsample ;
75
+ int renderHeight = source . height >> downsample ;
76
+
77
+ // downsample
78
+ RenderTexture tempRender = RenderTexture . GetTemporary ( renderWidth , renderHeight , 0 , source . format ) ;
79
+ tempRender . filterMode = FilterMode . Bilinear ;
80
+ Graphics . Blit ( source , tempRender , material , 0 ) ;
81
+
82
+ int passOffs = blurType == BlurType . StandardGauss ? 0 : 2 ;
83
+
84
+ for ( int i = 0 ; i < blurIterations ; i ++ )
85
+ {
86
+ float iterationOffs = i ;
87
+ blurParams = new Vector4 ( blurSize * widthMod + iterationOffs , - blurSize * widthMod - iterationOffs , 0.0f , 0.0f ) ;
88
+ material . SetVector ( "_Parameter" , blurParams ) ;
89
+
90
+ // vertical blur
91
+ RenderTexture tempRender2 = RenderTexture . GetTemporary ( renderWidth , renderHeight , 0 , source . format ) ;
92
+ tempRender2 . filterMode = FilterMode . Bilinear ;
93
+ Graphics . Blit ( tempRender , tempRender2 , material , 1 + passOffs ) ;
94
+ RenderTexture . ReleaseTemporary ( tempRender ) ;
95
+ tempRender = tempRender2 ;
96
+
97
+ // horizontal blur
98
+ tempRender2 = RenderTexture . GetTemporary ( renderWidth , renderHeight , 0 , source . format ) ;
99
+ tempRender2 . filterMode = FilterMode . Bilinear ;
100
+ Graphics . Blit ( tempRender , tempRender2 , material , 2 + passOffs ) ;
101
+ RenderTexture . ReleaseTemporary ( tempRender ) ;
102
+ tempRender = tempRender2 ;
103
+ }
104
+
105
+ if ( maskLayer . value == 0 )
106
+ {
107
+ Graphics . Blit ( tempRender , destination ) ;
108
+ }
109
+ else
110
+ {
111
+ RenderTexture maskBuffer = RenderTexture . GetTemporary ( renderWidth , renderHeight , 0 , source . format ) ;
112
+
113
+ effectsCamera . CopyFrom ( camera ) ;
114
+ effectsCamera . cullingMask = maskLayer ;
115
+ effectsCamera . backgroundColor = Color . black ;
116
+ effectsCamera . transparencySortMode = camera . transparencySortMode ;
117
+ effectsCamera . targetTexture = maskBuffer ;
118
+ effectsCamera . Render ( ) ;
119
+
120
+ MixMaterial . SetTexture ( "_OverLayer" , tempRender ) ;
121
+ MixMaterial . SetTexture ( "_Blend" , maskBuffer ) ;
122
+ Graphics . Blit ( source , destination , MixMaterial ) ;
123
+
124
+ RenderTexture . ReleaseTemporary ( maskBuffer ) ;
125
+ }
126
+
127
+ RenderTexture . ReleaseTemporary ( tempRender ) ;
128
+ }
129
+
130
+ }
131
+ }
0 commit comments