shader optimization; won some frames on big lights
[dd2d.git] / srlight_topolar.frag
blobb30edf746a171b19ab79da4380f782330015160d
1 #version 120
3 #define PI 3.1415926
5 uniform sampler2D tex0;
6 uniform vec2 lightTexSize;
9 // alpha threshold for our occlusion map
10 #define THRESHOLD  (0.75)
12 #define inv2  (1/2.0)
15 void main (void) {
16   float ltsyStep = 1.0/lightTexSize.y;
17   float theta = PI*1.5+(gl_TexCoord[0].x*2.0-1.0)*PI;
18   float nsint = -sin(theta);
19   float ncost = -cos(theta);
20   float dst;
21   for (dst = 0.0; dst < 1.0; dst += ltsyStep) {
22     float r = (1.0+(dst*2.0-1.0))*0.5;
23     // sample the occlusion map
24     float dataa = texture2D(tex0, vec2(r*nsint, r*ncost)*inv2+0.5).a;
25     // if we've hit an opaque fragment (occluder), then get new distance
26     // if the new distance is below the current, then we'll use that for our ray
27     if (dataa > THRESHOLD) break; // the current distance is how far from the top we've come
28   }
29   gl_FragColor = vec4(vec3(dst), 1.0);