1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#define FXPRO_EFFECT
//#define BLOOMPRO_EFFECT
#if FXPRO_EFFECT
#define BLOOMPRO_EFFECT
#endif
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Object = UnityEngine.Object;
#if FXPRO_EFFECT
namespace FxProNS {
#elif BLOOMPRO_EFFECT
namespace BloomProNS {
#endif
[Serializable]
public class BloomHelperParams {
public EffectsQuality Quality;
public Color BloomTint = new Color(110.0f/255.0f,230.0f/255.0f,1.0f,1.0f);
[Range( 0f, .99f )]
public float BloomThreshold = .8f;
[Range( 0f, 3f )]
public float BloomIntensity = 0.7f;
[Range( 0.01f, 3f )]
public float BloomSoftness = .5f;
}
public class BloomHelper : Singleton<BloomHelper>, IDisposable
{
private static Material _mat;
public static Material Mat
{
get
{
if (null == _mat)
{
Shader shader = XUtliPoolLib.ShaderManager.singleton.FindShader("BloomPro", "Hidden/BloomPro");
_mat = new Material(shader)
{
hideFlags = HideFlags.HideAndDontSave
};
}
return _mat;
}
}
private BloomHelperParams p;
private int bloomSamples = 5;
private float bloomBlurRadius = 5f;
#if ((UNITY_EDITOR) || (UNITY_STANDALONE)|| (UNITY_IOS))
public void SetParams(BloomHelperParams _p)
{
p = _p;
}
public void Init() {
float realBloomIntensity = Mathf.Exp( p.BloomIntensity ) - 1f;
//if (Application.platform == RuntimePlatform.IPhonePlayer)
// p.BloomThreshold *= .75f;
Mat.SetFloat( "_BloomThreshold", p.BloomThreshold );
Mat.SetFloat( "_BloomIntensity", realBloomIntensity );
Mat.SetColor( "_BloomTint", p.BloomTint );
// Debug.Log( p.BloomTint );
//Bloom samples settings
if (p.Quality == EffectsQuality.High || p.Quality == EffectsQuality.Normal) {
bloomSamples = 5;
Mat.EnableKeyword( "BLOOM_SAMPLES_5" );
} if (p.Quality == EffectsQuality.Fast || p.Quality == EffectsQuality.Fastest) {
bloomSamples = 3;
Mat.DisableKeyword( "BLOOM_SAMPLES_5" );
}
//Blur radius settings
if (p.Quality == EffectsQuality.High) {
bloomBlurRadius = 10f;
Mat.EnableKeyword( "BLUR_RADIUS_10" );
Mat.DisableKeyword( "BLUR_RADIUS_5" );
} else {
bloomBlurRadius = 5f;
Mat.EnableKeyword( "BLUR_RADIUS_5" );
Mat.DisableKeyword( "BLUR_RADIUS_10" );
}
float[] bloomTexFactors = CalculateBloomTexFactors( Mathf.Exp( p.BloomSoftness ) - 1f );
if (bloomTexFactors.Length == 5) {
Mat.SetVector( "_BloomTexFactors1",
new Vector4( bloomTexFactors[0], bloomTexFactors[1], bloomTexFactors[2], bloomTexFactors[3] ) );
Mat.SetVector( "_BloomTexFactors2", new Vector4( bloomTexFactors[4], 0f, 0f, 0f ) );
} else if (bloomTexFactors.Length == 3) {
Mat.SetVector(
"_BloomTexFactors1",
new Vector4( bloomTexFactors[0], bloomTexFactors[1], bloomTexFactors[2], 0f ) );
} else {
Debug.LogError( "Unsupported bloomTexFactors.Length: " + bloomTexFactors.Length );
}
RenderTextureManager.Instance.Dispose();
}
public void RenderBloomTexture( RenderTexture source, RenderTexture dest )
{
RenderTexture curRenderTex = RenderTextureManager.Instance.RequestRenderTexture( source.width, source.height, source.depth, source.format );
//Prepare by extracting blooming pixels
Graphics.Blit( source, curRenderTex, Mat, 0 ); //Is it ok to extract blooming pixels at 1/4 res??????
//Downsample & blur
for (int i = 1; i <= bloomSamples; i++) {
float curSpread = Mathf.Lerp( 1f, 2f, (float)(i - 1) / (float)(bloomSamples) );
#if FXPRO_EFFECT
RenderTextureManager.Instance.SafeAssign( ref curRenderTex, FxPro.DownsampleTex( curRenderTex, 2f ) );
#elif BLOOMPRO_EFFECT
RenderTextureManager.Instance.SafeAssign( ref curRenderTex, BloomPro.DownsampleTex( curRenderTex, 2f ) );
#elif DOFPRO_EFFECT
RenderTextureManager.Instance.SafeAssign( ref curRenderTex, DOFPro.DownsampleTex( curRenderTex, 2f ) );
#endif
RenderTextureManager.Instance.SafeAssign( ref curRenderTex, BlurTex( curRenderTex, curSpread ) );
//Set blurred bloom texture
Mat.SetTexture( "_DsTex" + i, curRenderTex );
}
//Bloom composite pass
Graphics.Blit( null, dest, Mat, 1 );
RenderTextureManager.Instance.ReleaseRenderTexture(curRenderTex);
}
public RenderTexture BlurTex( RenderTexture _input, float _spread ) {
//Blur
float curBlurSize = _spread * 10f / bloomBlurRadius; //Normalization - smaller blur = higher step
RenderTexture tempRenderTex1 = RenderTextureManager.Instance.RequestRenderTexture( _input.width, _input.height, _input.depth, _input.format );
RenderTexture tempRenderTex2 = RenderTextureManager.Instance.RequestRenderTexture( _input.width, _input.height, _input.depth, _input.format );
//Horizontal blur
Mat.SetVector( "_SeparableBlurOffsets", new Vector4( 1f, 0f, 0f, 0f ) * curBlurSize );
Graphics.Blit( _input, tempRenderTex1, Mat, 2 );
//Vertical blur
Mat.SetVector( "_SeparableBlurOffsets", new Vector4( 0f, 1f, 0f, 0f ) * curBlurSize );
Graphics.Blit( tempRenderTex1, tempRenderTex2, Mat, 2 );
tempRenderTex1 = RenderTextureManager.Instance.ReleaseRenderTexture( tempRenderTex1 );
return tempRenderTex2;
}
private float[] CalculateBloomTexFactors( float softness ) {
var bloomTexFactors = new float[bloomSamples];
for (int i = 0; i < bloomTexFactors.Length; i++) {
float t = (float)(i) / (float)(bloomTexFactors.Length - 1);
bloomTexFactors[i] = Mathf.Lerp( 1f, softness, t );
}
bloomTexFactors = MakeSumOne( bloomTexFactors );
return bloomTexFactors;
}
private float[] MakeSumOne( IList<float> _in ) {
float sum = _in.Sum();
var res = new float[_in.Count];
for (int i = 0; i < _in.Count; i++) {
res[i] = _in[i] / sum;
}
return res;
}
public void Dispose()
{
if (null != Mat)
Object.DestroyImmediate(Mat);
RenderTextureManager.Instance.Dispose();
}
#else
public void Dispose() {
}
#endif
}
}
|