]> gitweb.ps.run Git - sporegirl/blob - shd/main.glsl
changezzz
[sporegirl] / shd / main.glsl
1 // === combine ===
2 @vs vs_combine
3 const vec2 positions[4] = { vec2(-1, 1), vec2(1, 1), vec2(1, -1), vec2(-1, -1) };
4 const vec2 uvs[4] = { vec2(0, 1), vec2(1, 1), vec2(1, 0), vec2(0, 0) };
5 const int indices[6] = { 0, 1, 2, 0, 2, 3 };
6
7 layout(binding=0) uniform Game
8 {
9     vec2 screen;
10     vec2 cam;
11 };
12
13 out vec2 uv;
14
15 void main() {
16     int idx = indices[gl_VertexIndex];
17     vec2 pos = positions[idx];
18     uv = uvs[idx];
19
20     gl_Position = vec4(pos, 1, 1);
21 }
22 @end
23
24 @fs fs_combine
25 layout(binding=0) uniform texture2D tex_tex;
26 layout(binding=1) uniform texture2D tex_shadow;
27 layout(binding=2) uniform texture2D tex_light;
28 layout(binding=0) uniform sampler smp;
29
30 in vec2 uv;
31 out vec4 frag_color;
32
33
34 void main() {
35     vec4 tb = vec4(0.8,0.3,0.3,1);
36     if (mod(floor(uv.x*10), 2) != mod(floor(uv.y*10),2)) {
37         tb = vec4(0.3,0.3,0.8,1);
38     }
39     vec4 tt = texture(sampler2D(tex_tex, smp), uv);
40     vec4 ts = texture(sampler2D(tex_shadow, smp), uv);
41     vec4 tl = texture(sampler2D(tex_light, smp), uv);
42     
43     vec3 l = tt.a * (tt.rgb * tl.rgb) + (1-tt.a) * (tl.rgb * tl.a);
44     l = ts.a * (l * ts.rgb) + (1-ts.a) * (l);
45     frag_color = vec4(l, 1);
46
47     vec3 a = (tb.rgb + tl.rgb) * tt.rgb * ts.rgb; // + tl.rgb * tl.a;
48     frag_color = vec4(a, 1);
49
50
51     vec3 bg = tb.rgb * tl.rgb;
52     vec3 bg_s = bg * (1-ts.a) + bg * ts.rgb * 0.5 * ts.a;
53     vec3 t = tt.rgb * tl.rgb;
54     vec3 t_s = t * (1-ts.a) + t * ts.rgb * 0.5 * ts.a;
55     vec3 bg_s_t_s = bg_s * (1-tt.a) + t_s * tt.a;
56     frag_color = vec4(bg_s_t_s, 1);
57
58     // frag_color = vec4(tl);
59 }
60 @end
61
62 @program combine vs_combine fs_combine
63
64
65 // === tex ===
66 @vs vs_tex
67 const vec2 positions[4] = { vec2(0, 1), vec2(1, 1), vec2(1, 0), vec2(0, 0) };
68 const vec2 uvs[4] = { vec2(0, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1) };
69 const int indices[6] = { 0, 1, 2, 0, 2, 3 };
70
71 layout(binding=0) uniform Game
72 {
73     vec2 screen;
74     vec2 cam;
75 };
76
77 in vec2 inst_pos;
78 in ivec2 inst_size;
79 out vec2 uv;
80
81 void main() {
82     int idx = indices[gl_VertexIndex];
83     vec2 pos = positions[idx];
84     uv = uvs[idx];
85
86     vec2 inst_pos_abs = inst_pos + inst_size * pos - cam - screen / 2;
87     gl_Position = vec4(inst_pos_abs / screen * 2, 1, 1);
88 }
89 @end
90
91 @fs fs_tex
92 layout(binding=0) uniform texture2D tex;
93 layout(binding=0) uniform sampler smp;
94
95 in vec2 uv;
96 out vec4 frag_color;
97
98 void main() {
99     vec4 tex_color = texture(sampler2D(tex, smp), uv);
100     if (tex_color.a < 1)
101         discard;
102     frag_color = tex_color;
103 }
104 @end
105
106 @program tex vs_tex fs_tex
107
108
109 // === shadows ===
110 @vs vs_shadow
111 const vec2 positions[3] = { vec2(0, 1), vec2(1, 1), vec2(0.5, 0) };
112 const vec2 uvs[3] = { vec2(0, 0), vec2(1, 0), vec2(0.5, 1) };
113
114 layout(binding=0) uniform Game
115 {
116     vec2 screen;
117     vec2 cam;
118 };
119
120 layout(binding=1) uniform Light
121 {
122     vec3 color;
123     vec2 pos;
124     float reach;
125     float radius_glow;
126     float intensity;
127     float shadow_brightness;
128 } light;
129
130 in vec2 inst_pos;
131 in ivec2 inst_size;
132 out vec2 uv;
133 out vec3 light_color;
134 out float shadow_brightness;
135
136 // https://gist.github.com/yiwenl/3f804e80d0930e34a0b33359259b556c
137 vec2 rotate(vec2 v, float a) {
138         float s = sin(a);
139         float c = cos(a);
140         mat2 m = mat2(c, s, -s, c);
141         return m * v;
142 }
143
144 void main() {
145     int idx = gl_VertexIndex;
146     vec2 pos = positions[idx];
147     uv = uvs[idx];
148     light_color = light.color;
149     shadow_brightness = light.shadow_brightness;
150
151     if (idx == 2) { // keep pos for base
152         vec2 inst_pos_abs = inst_pos + inst_size * pos - cam - screen / 2;
153         gl_Position = vec4(inst_pos_abs / screen * 2, 1, 1);
154     }
155     else {
156         vec2 anchor = vec2(0.5, 0);
157         vec2 anchor_abs = inst_pos + inst_size * anchor - cam - screen / 2;
158         vec2 light_pos_abs = light.pos - cam - screen / 2;
159         vec2 dir = anchor_abs - light_pos_abs;
160
161         vec2 left_right[] = {
162             rotate(dir, 1.5708),
163             rotate(dir, -1.5708),
164         };
165
166         float light_radius_factor = 1.5 / log(light.reach);
167
168         vec2 inst_pos_abs = anchor_abs + dir + left_right[idx] * light_radius_factor;
169         gl_Position = vec4(inst_pos_abs / screen * 2, 1, 1);
170     }
171 }
172 @end
173
174 @fs fs_shadow
175 layout(binding=0) uniform texture2D tex;
176 layout(binding=0) uniform sampler smp;
177
178 in vec2 uv;
179 in vec3 light_color;
180 in float shadow_brightness;
181 out vec4 frag_color;
182
183 vec3 brighten(vec3 c, float f) {
184     vec3 d = vec3(1) - c;
185     return c + d * f;
186 }
187
188 vec4 blur(float radius, float resolution) {
189     float blur = radius/resolution; 
190     
191     float hstep = 1;
192     float vstep = 1;
193     
194     vec4 sum = vec4(0.0);
195     
196     sum += texture(sampler2D(tex, smp), vec2(uv.x - 4.0*blur*hstep, uv.y - 4.0*blur*vstep)) * 0.0162162162;
197     sum += texture(sampler2D(tex, smp), vec2(uv.x - 3.0*blur*hstep, uv.y - 3.0*blur*vstep)) * 0.0540540541;
198     sum += texture(sampler2D(tex, smp), vec2(uv.x - 2.0*blur*hstep, uv.y - 2.0*blur*vstep)) * 0.1216216216;
199     sum += texture(sampler2D(tex, smp), vec2(uv.x - 1.0*blur*hstep, uv.y - 1.0*blur*vstep)) * 0.1945945946;
200     
201     sum += texture(sampler2D(tex, smp), vec2(uv.x, uv.y)) * 0.2270270270;
202     
203     sum += texture(sampler2D(tex, smp), vec2(uv.x + 1.0*blur*hstep, uv.y + 1.0*blur*vstep)) * 0.1945945946;
204     sum += texture(sampler2D(tex, smp), vec2(uv.x + 2.0*blur*hstep, uv.y + 2.0*blur*vstep)) * 0.1216216216;
205     sum += texture(sampler2D(tex, smp), vec2(uv.x + 3.0*blur*hstep, uv.y + 3.0*blur*vstep)) * 0.0540540541;
206     sum += texture(sampler2D(tex, smp), vec2(uv.x + 4.0*blur*hstep, uv.y + 4.0*blur*vstep)) * 0.0162162162;
207
208     return sum;
209 }
210
211 void main() {
212     vec4 tex_color = blur(10, 1);
213     tex_color = texture(sampler2D(tex, smp), uv);
214     
215     vec3 brighter = brighten(light_color, shadow_brightness);
216     frag_color = vec4(brighter * tex_color.a + vec3(1) * (1-tex_color.a), tex_color.a);
217 }
218 @end
219
220 @program shadow vs_shadow fs_shadow
221
222
223 // === lights ===
224 @vs vs_light
225 const vec2 positions[4] = { vec2(-0.5, 0.5), vec2(0.5, 0.5), vec2(0.5, -0.5), vec2(-0.5, -0.5) };
226 const int indices[6] = { 0, 1, 2, 0, 2, 3 };
227
228 layout(binding=0) uniform Game
229 {
230     vec2 screen;
231     vec2 cam;
232 };
233
234 layout(binding=1) uniform Light
235 {
236     vec3 color;
237     vec2 pos;
238     float reach;
239     float radius_glow;
240     float intensity;
241     float shadow_brightness;
242 } light;
243
244 out vec3 light_col;
245 out vec2 light_pos;
246 out float light_rea;
247 out float light_glo;
248 out float light_intensity;
249 out vec2 fragpos;
250
251 void main() {
252     int idx = indices[gl_VertexIndex];
253     vec2 pos = positions[idx];
254
255     vec2 inst_pos_ap = light.pos + light.reach * 2 * pos;
256     vec2 inst_pos_rp = inst_pos_ap - cam - screen / 2;
257     vec2 inst_pos_rw = inst_pos_rp / screen * 2;
258     gl_Position = vec4(inst_pos_rw, 1, 1);
259
260     light_col = light.color;
261     light_pos = light.pos;
262     light_rea = light.reach;
263     light_glo = light.radius_glow;
264     light_intensity = light.intensity;
265     fragpos = inst_pos_ap;
266 }
267 @end
268
269 @fs fs_light
270 layout(binding=0) uniform Game
271 {
272     vec2 screen;
273     vec2 cam;
274 };
275
276 in vec3 light_col;
277 in vec2 light_pos;
278 in float light_rea;
279 in float light_glo;
280 in float light_intensity;
281 in vec2 fragpos;
282 out vec4 frag_color;
283
284 float dist(vec2 a, vec2 b) {
285     float d1 = a.x - b.x;
286     float d2 = a.y - b.y;
287     return sqrt(d1*d1 + d2*d2);
288 }
289
290 void main() {
291     // vec2 frag_coord_abs = gl_FragCoord.xy * v_screen + v_cam + v_screen / 2;
292     // float factor = dist(light_pos, frag_coord_abs) / light_rad;
293     float factor_rea = 1 - (dist(light_pos, fragpos) / light_rea);
294     float factor_glo = 1 - (dist(light_pos, fragpos) / light_glo);
295     if (factor_rea > 1.0) discard;
296     frag_color = vec4(light_col.rgb * factor_rea, factor_glo * light_intensity);
297     // frag_color = vec4(vec3(mod(factor, 1.0)) * light_col, factor);
298 }
299 @end
300
301 @program light vs_light fs_light