]> gitweb.ps.run Git - sporegirl/blob - shd/quad.glsl
move shader, update build.zig
[sporegirl] / shd / quad.glsl
1 /* quad vertex shader */
2 @vs vs
3 const vec2 positions[4] = { vec2(0, 1), vec2(1, 1), vec2(1, 0), vec2(0, 0) };
4 const vec2 uvs[4] = { vec2(0, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1) };
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 in vec2 inst_pos;
14 in ivec2 inst_size;
15 out vec2 uv;
16
17 void main() {
18     int idx = indices[gl_VertexIndex];
19     vec2 pos = positions[idx];
20     uv = uvs[idx];
21
22     vec2 inst_pos_abs = inst_pos + inst_size * pos - cam - screen / 2;
23     gl_Position = vec4(inst_pos_abs / screen * 2, 1, 1);
24 }
25 @end
26
27 /* quad fragment shader */
28 @fs fs
29 layout(binding=0) uniform texture2D tex;
30 layout(binding=0) uniform sampler smp;
31
32 in vec2 uv;
33 out vec4 frag_color;
34
35 void main() {
36     vec4 tex_color = texture(sampler2D(tex, smp), uv);
37     if (tex_color.a < 1)
38         discard;
39     frag_color = tex_color;
40 }
41 @end
42
43 /* quad shader program */
44 @program quad vs fs
45
46
47
48
49
50 @vs vs_shadow
51 const vec2 positions[4] = { vec2(0, 1), vec2(1, 1), vec2(1, 0), vec2(0, 0) };
52 const vec2 uvs[4] = { vec2(0, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1) };
53 const int indices[6] = { 0, 1, 2, 0, 2, 3 };
54
55 layout(binding=0) uniform Game
56 {
57     vec2 screen;
58     vec2 cam;
59 };
60
61 layout(binding=1) uniform Light
62 {
63     vec2 light_pos;
64 };
65
66 in vec2 inst_pos;
67 in ivec2 inst_size;
68 out vec2 uv;
69
70 vec2 rotate(vec2 v, float a) {
71         float s = sin(a);
72         float c = cos(a);
73         mat2 m = mat2(c, s, -s, c);
74         return m * v;
75 }
76
77 void main() {
78     int idx = indices[gl_VertexIndex];
79     vec2 pos = positions[idx];
80     uv = uvs[idx];
81
82     if (idx == 2 || idx == 3) { // keep pos for lower vertices (base)
83         vec2 inst_pos_abs = inst_pos + inst_size * pos - cam - screen / 2;
84         gl_Position = vec4(inst_pos_abs / screen * 2, 1, 1);
85     }
86     else {
87         vec2 anchor = vec2(0.5, 0);
88         vec2 anchor_abs = inst_pos + inst_size * anchor - cam - screen / 2;
89         vec2 light_pos_abs = light_pos - cam - screen / 2;
90         vec2 dir = anchor_abs - light_pos_abs;
91
92         vec2 left_right[] = {
93             rotate(dir, 1.5708),
94             rotate(dir, -1.5708),
95         };
96
97         vec2 inst_pos_abs = anchor_abs + dir + left_right[idx] / 2;
98         gl_Position = vec4(inst_pos_abs / screen * 2, 1, 1);
99     }
100 }
101 @end
102
103 @fs fs_shadow
104 layout(binding=0) uniform texture2D tex;
105 layout(binding=0) uniform sampler smp;
106
107 in vec2 uv;
108 out vec4 frag_color;
109
110 void main() {
111     vec4 tex_color = texture(sampler2D(tex, smp), uv);
112     // if (tex_color.a < 1)
113     //     discard;
114     frag_color = vec4(0, 0, 0, 1);
115 }
116 @end
117
118 @program shadow vs_shadow fs_shadow
119
120
121
122 @vs vs_light
123 const vec2 positions[4] = { vec2(-0.5, 0.5), vec2(0.5, 0.5), vec2(0.5, -0.5), vec2(-0.5, -0.5) };
124 const int indices[6] = { 0, 1, 2, 0, 2, 3 };
125
126 layout(binding=0) uniform Game
127 {
128     vec2 screen;
129     vec2 cam;
130 };
131
132 in vec2 light_pos;
133
134 void main() {
135     int idx = indices[gl_VertexIndex];
136     vec2 pos = positions[idx];
137
138     vec2 light_size = vec2(10, 10);
139
140     vec2 inst_pos_abs = light_pos + light_size * pos - cam - screen / 2;
141     gl_Position = vec4(inst_pos_abs / screen * 2, 1, 1);
142 }
143 @end
144
145 @fs fs_light
146 out vec4 frag_color;
147
148 void main() {
149     frag_color = vec4(1, 0, 0, 1);
150 }
151 @end
152
153 @program light vs_light fs_light