added license to scripts and shaders
[dd2d.git] / data / shaders / srlight_blur_occ.frag
blob5614fe94c1472fe55f6e0748c06dea2936745378
1 /* DooM2D: Midnight on the Firing Line
2  * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3  * Understanding is not required. Only obedience.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 #version 130
20 #define PI 3.1415926
22 #define SEARCH_RADIUS  (4)
23 #define MIN_ALPHA  (0.05)
25 uniform sampler2D texLMap; // light texture of lightTexSize
26 uniform sampler2D texBg; // background
27 uniform sampler2D texOcc; // occluders
28 uniform vec2 lightTexSize;
29 uniform vec2 mapPixSize;
30 uniform vec4 lightColor;
31 uniform vec2 lightPos;
34 bool hasLight (in vec2 txo, in float pixelStep) {
35   /*
36   vec2 txc = txo;
37   for (int dy = SEARCH_RADIUS*2; dy > 0; --dy) {
38     for (int dx = SEARCH_RADIUS*2; dx > 0; --dx) {
39       if (texture2D(texLMap, txc).a > MIN_ALPHA) return true;
40       txc.x += pixelStep;
41     }
42     txc.x = txo.x;
43     txc.y += pixelStep;
44   }
45   return false;
46   */
47   float pxs = pixelStep*(SEARCH_RADIUS*2);
48   vec2 txclu = txo-pxs;
49   vec2 txcld = vec2(txo.x-pxs, txo.y+pxs);
50   vec2 txcru = vec2(txo.x+pxs, txo.y-pxs);
51   vec2 txcrd = txo+pxs;
52   vec2 txcu = vec2(txo.x, txo.y-pxs);
53   vec2 txcd = vec2(txo.x, txo.y+pxs);
54   vec2 txcl = vec2(txo.x-pxs, txo.y);
55   vec2 txcr = vec2(txo.x+pxs, txo.y);
56   vec2 txcrStep = vec2(pixelStep, -pixelStep);
57   float pst2 = pixelStep*2;
58   for (int d = SEARCH_RADIUS; d > 0; --d) {
59     if (texture2D(texLMap, txclu).a > MIN_ALPHA) return true;
60     txclu += pst2;
61     if (texture2D(texLMap, txcld).a > MIN_ALPHA) return true;
62     txcld.x += pst2;
63     txcld.y -= pst2;
65     if (texture2D(texLMap, txcru).a > MIN_ALPHA) return true;
66     txcru.x -= pst2;
67     txcru.y += pst2;
68     if (texture2D(texLMap, txcrd).a > MIN_ALPHA) return true;
69     txcrd -= pst2;
71     if (texture2D(texLMap, txcu).a > MIN_ALPHA) return true;
72     txcu.y += pst2;
73     if (texture2D(texLMap, txcd).a > MIN_ALPHA) return true;
74     txcd.y -= pst2;
76     if (texture2D(texLMap, txcl).a > MIN_ALPHA) return true;
77     txcl.x += pst2;
78     if (texture2D(texLMap, txcr).a > MIN_ALPHA) return true;
79     txcr.x -= pst2;
80   }
81   return false;
85 //gl_FragCoord: in background
87 void main (void) {
88   vec2 vTexCoord0 = gl_TexCoord[0].xy; // in light texture (texLMap)
89   vec2 btxy = gl_FragCoord.xy/mapPixSize;
90   vec2 ocxy = vec2(btxy.x, 1.0-btxy.y);
91   vec4 color;
93   if (texture2D(texOcc, ocxy).a <= 0.75) discard; // empty space
95   // occluder hit; look for light in 8px radius
96   float pixelStep = 1.0/lightTexSize.x;
97   if (!hasLight(vTexCoord0, pixelStep)) discard;
98   // background
99   vec4 bcolor = texture2D(texBg, btxy);
100   // light is y-mirrored
101   vec2 ourpos = vec2(gl_FragCoord.x, mapPixSize.y-gl_FragCoord.y-1.0);
102   // distance from this point to light origin
103   float lit = clamp(1.0-(distance(ourpos, lightPos)/(lightTexSize.x/2.0)), 0.0, 1.0);
104   lit = smoothstep(0.1, 1.0, lit);
105   if (lightColor.a == 0.0) {
106     color = bcolor*vec4(vec3(1.0), lit);
107   } else {
108     color = lightColor*lit;
109     color += bcolor*lit;
110     color.a = lit/2.0;
111   }
112   gl_FragColor = color;