new lights
[dd2d.git] / data / shaders / srlight_blur.frag
blob012cfae03b3ecc3c241a360f91536474accc4e0d
1 #version 130
3 #define PI 3.1415926
5 uniform sampler2D tex0; // 1D distance
6 uniform sampler2D tex1; // background
7 uniform sampler2D tex2; // occluders
8 uniform vec2 lightTexSize;
9 uniform vec2 mapPixSize;
10 uniform vec4 lightColor;
11 uniform vec2 lightPos;
12 #define SHADOW_DO_BLUR
15 #define SOFT_SHADOWS  (1.0f)
18 // sample from the 1D distance map
19 #define sample(cc_)  step(r, texture2D(tex0, vec2(tc.x+(cc_)*blur, /*tc.y*/0.0)).r)
21 //gl_FragCoord: [0..lightTexSize)
23 void main (void) {
24   vec2 vTexCoord0 = gl_TexCoord[0].xy;
25   vec2 btxy = vec2(lightPos.x-lightTexSize.x/2.0+gl_FragCoord.x, (lightPos.y-1)+lightTexSize.y/2.0-gl_FragCoord.y);
26   btxy.y = mapPixSize.y-btxy.y-1.0;
27   vec2 ocxy = vec2(btxy.x, mapPixSize.y-btxy.y)/mapPixSize;
28   vec4 color;
30   if (texture2D(tex2, ocxy).a > 0.75) {
31     // occluder hit, this will take care of blended light (due to blur)
32     color = vec4(0.0, 0.0, 0.0, 0.0);
33   } else {
34     float ltsxi = 1.0/lightTexSize.x;
36     // rectangular to polar
37     vec2 norm = vTexCoord0.st*2.0-1.0;
38     float theta = atan(norm.y, norm.x);
39     float r = length(norm);
40     float coord = (theta+PI)/(2.0*PI);
42     // the tex0 coord to sample our 1D lookup texture
43     // always 0.0 on y axis
44     vec2 tc = vec2(coord, 0.0);
46     // the center tex0 coord, which gives us hard shadows
47     float center = step(r, texture2D(tex0, tc.xy).r); //sample(vec2(tc.x, tc.y), r);
49     // now we use a simple gaussian blur
50     #ifdef SHADOW_DO_BLUR
51       // we multiply the blur amount by our distance from center
52       // this leads to more blurriness as the shadow "fades away"
53       float blur = ltsxi*smoothstep(0.0, 1.0, r);
55       float sum  = sample(-4.0)*0.05;
56             sum += sample(-3.0)*0.09;
57             sum += sample(-2.0)*0.12;
58             sum += sample(-1.0)*0.15;
59             sum += center*0.16;
60             sum += sample( 1.0)*0.15;
61             sum += sample( 2.0)*0.12;
62             sum += sample( 3.0)*0.09;
63             sum += sample( 4.0)*0.05;
64       // sum of 1.0 -> in light, 0.0 -> in shadow
65       float lit = mix(center, sum, SOFT_SHADOWS);
66     #else
67       float lit = center;
68     #endif
70     // multiply the summed amount by our distance, which gives us a radial falloff
71     lit = lit*smoothstep(1.0, 0.0, r);
73     if (lit == 0.0) {
74       color = vec4(0.0, 0.0, 0.0, 0.0);
75     } else {
76       // has some light here
77       vec4 bcolor = texture2D(tex1, btxy/mapPixSize);
78       //if (bcolor.a == 0.0) bcolor = vec4(0.0, 0.0, 0.0, 1.0);
79       if (lightColor.a == 0.0) {
80         color = bcolor*vec4(vec3(1.0), lit);
81       } else {
82         color = lightColor*lit;
83         color += bcolor*lit;
84         color.a = lit/2.0;
85       }
86     }
87   }
89   gl_FragColor = color;