summary refs log tree commit diff stats
path: root/shaders/bloom2.fragment
blob: 1d9cac41cb8c80bfd0af8e22dd5eea4e919a9ca3 (plain) (blame)
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
#version 330 core

in vec2 UV;

out vec3 color;

uniform sampler2D screenTex;
uniform sampler2D downsampledTex;
uniform float iGlobalTime;
uniform vec2 resolution;

float nrand(vec2 n)
{
  return fract(sin(dot(n.xy, vec2(19.9898, 78.233))) * 43758.5453);
}

vec2 rot2d(vec2 p, float a)
{
  vec2 sc = vec2(sin(a), cos(a));
  return vec2(dot(p, vec2(sc.y, -sc.x)), dot(p, sc.xy));
}

void main()
{
  const int NUM_TAPS = 12;
  float max_siz = 1.0;
  
  vec2 fTaps_Poisson[NUM_TAPS];
  fTaps_Poisson[0]  = vec2(-.326, -.406);
  fTaps_Poisson[1]  = vec2(-.840, -.074);
  fTaps_Poisson[2]  = vec2(-.696,  .457);
  fTaps_Poisson[3]  = vec2(-.203,  .621);
  fTaps_Poisson[4]  = vec2( .962, -.195);
  fTaps_Poisson[5]  = vec2( .473, -.480);
  fTaps_Poisson[6]  = vec2( .519,  .767);
  fTaps_Poisson[7]  = vec2( .185, -.893);
  fTaps_Poisson[8]  = vec2( .507,  .064);
  fTaps_Poisson[9]  = vec2( .896,  .412);
  fTaps_Poisson[10] = vec2(-.322, -.933);
  fTaps_Poisson[11] = vec2(-.792, -.598);
  
  vec4 sum = vec4(0);
  float rnd = 6.28 * nrand(UV + fract(iGlobalTime));
  vec4 basis = vec4(rot2d(vec2(1,0), rnd), rot2d(vec2(0,1), rnd));
  for (int i=0; i<NUM_TAPS; i++)
  {
    vec2 ofs = fTaps_Poisson[i];
    ofs = vec2(dot(ofs, basis.xz), dot(ofs, basis.yw));
    
    vec2 texcoord = UV + max_siz * ofs / (resolution / vec2(16.0));
    sum += texture(downsampledTex, texcoord, -10.0);
  }
  
  vec3 bloom_c = (sum / vec4(NUM_TAPS)).rgb;
  bloom_c *= bloom_c;
  //bloom_c = vec3(bloom_c.r*bloom_c.r, bloom_c.g*bloom_c.g, bloom_c.b*bloom_c.b);
  //bloom_c = vec3(bloom_c.r*bloom_c.r, bloom_c.g*bloom_c.g, bloom_c.b*bloom_c.b);
  //bloom_c = max(bloom_c - 0.25, vec3(0,0,0));
  
  color = texture(screenTex, UV).rgb + bloom_c;
}