light experiments
[dd2d.git] / srblurlight.frag
blob32677f162b4008f367a3bad3b0d22e59acb34452
1 #version 120
3 #define PI 3.1415926
5 uniform sampler2D tex0; // 1D distance
6 uniform sampler2D tex1; // background
7 uniform vec2 lightTexSize;
8 uniform bool shadowDoBloor;
11 const float softShadows = 1.0f;
14 // sample from the 1D distance map
15 float sample (vec2 coord, float r) {
16   return step(r, texture2D(tex0, coord).r);
20 void main (void) {
21   vec2 vTexCoord0 = gl_TexCoord[0].xy;
22   vec2 btxy = gl_FragCoord.xy/(100.0*8.0);
23   float ltsxi = 1.0/lightTexSize.x;
25   // rectangular to polar
26   vec2 norm = vTexCoord0.st*2.0-1.0;
27   float theta = atan(norm.y, norm.x);
28   float r = length(norm);
29   float coord = (theta+PI)/(2.0*PI);
31   // the tex0 coord to sample our 1D lookup texture
32   // always 0.0 on y axis
33   vec2 tc = vec2(coord, 0.0);
35   // the center tex0 coord, which gives us hard shadows
36   float center = sample(vec2(tc.x, tc.y), r);
38   // we multiply the blur amount by our distance from center
39   // this leads to more blurriness as the shadow "fades away"
40   float blur = ltsxi*smoothstep(0.0, 1.0, r);
42   // now we use a simple gaussian blur
43   float sum = 0.0;
44   if (shadowDoBloor) {
45     sum += sample(vec2(tc.x-4.0*blur, tc.y), r)*0.05;
46     sum += sample(vec2(tc.x-3.0*blur, tc.y), r)*0.09;
47     sum += sample(vec2(tc.x-2.0*blur, tc.y), r)*0.12;
48     sum += sample(vec2(tc.x-1.0*blur, tc.y), r)*0.15;
49     sum += center*0.16;
50     sum += sample(vec2(tc.x+1.0*blur, tc.y), r)*0.15;
51     sum += sample(vec2(tc.x+2.0*blur, tc.y), r)*0.12;
52     sum += sample(vec2(tc.x+3.0*blur, tc.y), r)*0.09;
53     sum += sample(vec2(tc.x+4.0*blur, tc.y), r)*0.05;
54   } else {
55     sum = center;
56   }
58   // sum of 1.0 -> in light, 0.0 -> in shadow
59   float lit = mix(center, sum, softShadows);
61   // multiply the summed amount by our distance, which gives us a radial falloff
62   // then multiply by vertex (light) color
64   //vec4 color = gl_Color*vec4(vec3(1.0), lit*smoothstep(1.0, 0.0, r));
65   lit = lit*smoothstep(1.0, 0.0, r);
66   vec4 bcolor = texture2D(tex1, btxy);
67   if (bcolor.a == 0.0) bcolor = vec4(0.0, 0.0, 0.0, 1.0);
68   vec4 color;
69   if (gl_Color.a == 0.0) {
70     color = bcolor*vec4(vec3(1.0), lit);
71   } else {
72     //lit /= 2;
73     //vec4 color = bcolor*vec4(vec3(1.0), lit);
74     //vec4 color = bcolor*vec4(vec3(1.0), lit);
75     //color += gl_Color*vec4(vec3(1.0), lit);
76     //
77     //color = bcolor;
78     //color *= gl_Color*vec4(vec3(1.0), lit);
79     //color += bcolor*vec4(vec3(1.0), lit);
80     //lit /= 2;
81     /*
82     vec4 ltv = vec4(vec3(1.0), lit);
83     color = gl_Color*ltv;
84     bcolor = bcolor*ltv;
85     bcolor.a = 1.0;
86     color.a = lit;
87     color = mix(color, bcolor, lit);
88     color.a = lit;
89     */
90     /*
91     color = gl_Color;
92     color.a = 1.0;
93     bcolor *= lit;
94     bcolor.a = 1.0;
95     color = mix(bcolor, color, lit);
96     */
97     color = gl_Color;
98     color += bcolor*lit;
99     color.a = lit;
100   }
101   gl_FragColor = color;
102   //float intens = (0.2125*color.x)+(0.7154*color.y)+(0.0721*color.z);
103   //gl_FragColor = vec4(intens, intens, intens, color.w);
104   //gl_FragColor = color;