-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ls_Glint.2.glsl
67 lines (57 loc) · 2.26 KB
/
Ls_Glint.2.glsl
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
// Glint pass 2: Gaussian blur horizontal
// lewis@lewissaunders.com
uniform sampler2D adsk_results_pass1;
uniform float blursize, blursizer, blursizeg, blursizeb;
uniform float adsk_result_w, adsk_result_h;
uniform int downsample;
// Return a 1D Gaussian blur
// sampler is hard-coded to "adsk_results_pass1" to avoid a Baselight bug when passing it as an arg
// xy: centre of blur in pixels
// res: pixel size of mipmap level selected by lod param
// sizes: sigma of blurs, in pixels
// dir: direction of blur, usually vec2(1.0, 0.0) for horizontal followed by
// another pass for vertical
vec4 gaussianblur(vec2 xy, vec2 res, float sizered, float sizegreen, float sizeblue, float sizealpha, vec2 dir) {
vec4 sigmas = vec4(sizered, sizegreen, sizeblue, sizealpha);
// Set up state for incremental coefficient calculation, see GPU Gems
// We use vec4s to store four copies of the state, for different size
// red/green/blue/alpha blurs
vec4 gx = vec4(0.0);
vec4 gy = vec4(0.0);
vec4 gz = vec4(0.0);
gx = 1.0 / (sqrt(2.0 * 3.141592653589793238) * sigmas);
gy = exp(-0.5 / (sigmas * sigmas));
gz = gy * gy;
// vec4 a, centre, sample1, sample2 = vec4(0.0);
vec4 a = vec4(0.0);
vec4 centre = vec4(0.0);
vec4 sample1 = vec4(0.0);
vec4 sample2 = vec4(0.0);
// First take the centre sample
centre = texture2D(adsk_results_pass1, xy / res);
a += gx * centre;
vec4 energy = gx;
gx *= gy;
gy *= gz;
// Now the other samples
float support = max(max(max(sigmas.r, sigmas.g), sigmas.b), sigmas.a) * 3.0;
for(float i = 1.0; i <= support; i++) {
sample1 = texture2D(adsk_results_pass1, (xy - i * dir) / res);
sample2 = texture2D(adsk_results_pass1, (xy + i * dir) / res);
a += gx * sample1;
a += gx * sample2;
energy += 2.0 * gx;
gx *= gy;
gy *= gz;
}
a /= energy;
if(sizered < 0.1) a.r = centre.r;
if(sizegreen < 0.1) a.g = centre.g;
if(sizeblue < 0.1) a.b = centre.b;
return a;
}
void main() {
vec2 res = vec2(adsk_result_w, adsk_result_h);
vec2 xy = gl_FragCoord.xy / float(downsample+1);
gl_FragColor = gaussianblur(xy, res, blursize*blursizer, blursize*blursizeg, blursize*blursizeb, 0.0, vec2(1.0, 0.0));
}