tiny_game_framework/graphics/
SHADERS.rs

1pub static DEFAULT_MESH_SHADER_VS: &str = r#"
2#version 330 core
3layout (location = 0) in vec3 aPos;
4layout (location = 1) in vec4 aColor;
5layout (location = 2) in vec2 aTexCoord; // New input for texture coordinates
6layout (location = 3) in vec2 aNormal;
7
8uniform mat4 model;
9uniform mat4 view;
10uniform mat4 proj;
11
12uniform vec3 pos;
13
14uniform float time;
15
16out vec4 fColor;
17out vec2 TexCoord; // Pass texture coordinates to the fragment shader
18
19void main() {
20    gl_Position = proj * view * model * vec4(aPos, 1.0);
21    fColor = aColor;
22    TexCoord = aTexCoord; // Pass texture coordinates
23}
24"#;
25
26pub static DEFAULT_MESH_SHADER_FS: &str = r#"
27#version 330 core
28out vec4 FragColor;
29
30in vec4 fColor;
31in vec2 TexCoord; // Receive texture coordinates
32
33uniform sampler2D texture1; // Texture sampler
34
35void main()
36{
37    vec4 texColor = texture(texture1, TexCoord); // Sample the texture
38    FragColor = texColor * fColor; // Combine texture color and vertex color
39}
40"#;
41
42pub static LIGHT_MESH_SHADER_VS: &str = r#"
43#version 330 core
44layout (location = 0) in vec3 aPos;
45layout (location = 1) in vec4 aColor; 
46layout (location = 2) in vec2 aTexCoords;
47layout (location = 3) in vec3 aNormal;
48
49uniform mat4 model;
50uniform mat4 view;
51uniform mat4 proj;
52
53uniform vec3 pos;
54
55uniform float  time;
56
57out vec4 fColor;
58out vec3 Normal;
59out vec3 FragPos;
60
61void main() {
62    gl_Position = proj * view * model * vec4(aPos, 1.0);
63    FragPos = vec3(model * vec4(aPos, 1.0));
64    fColor = aColor;
65    Normal = mat3(transpose(inverse(model))) * aNormal;  
66}
67"#;
68
69pub static LIGHT_MESH_SHADER_FS: &str = r#"
70#version 330 core
71
72out vec4 FragColor;
73
74in vec4 fColor;
75in vec3 Normal;
76in vec3 FragPos;  
77
78uniform vec3 lightColor;
79uniform vec3 lightPos;
80uniform vec3 viewPos;
81
82void main()
83{
84    float ambientStrength = 0.1;
85    vec3 ambient = ambientStrength * lightColor;
86
87    vec3 norm = normalize(Normal);
88    vec3 lightDir = normalize(lightPos - FragPos); 
89
90    float diff = max(dot(norm, lightDir), 0.0);
91
92    vec3 diffuse = diff * lightColor;
93    float specularStrength = 0.5;
94
95    vec3 viewDir = normalize(viewPos - FragPos);
96    vec3 reflectDir = reflect(-lightDir, norm); 
97
98    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
99    vec3 specular = specularStrength * spec * lightColor;  
100    
101    vec3 result = (ambient + diffuse + specular) * vec3(fColor);
102    FragColor = vec4(result, 1.0);
103}
104"#;
105
106pub static FULL_SHADER_VS: &str = r#"
107#version 330 core
108layout (location = 0) in vec3 aPos;
109layout (location = 1) in vec4 aColor;
110layout (location = 2) in vec2 aTexCoord;
111layout (location = 3) in vec3 aNormal;
112
113uniform mat4 model;
114uniform mat4 view;
115uniform mat4 proj;
116
117out vec4 fColor;
118out vec3 Normal;
119out vec3 FragPos;
120out vec2 TexCoord; // Pass texture coordinates to the fragment shader
121
122void main() {
123    gl_Position = proj * view * model * vec4(aPos, 1.0);
124    fColor = aColor;
125    TexCoord = aTexCoord; // Pass texture coordinates
126    FragPos = vec3(model * vec4(aPos, 1.0));
127    Normal = mat3(transpose(inverse(model))) * aNormal;  
128}
129"#;
130
131pub static FULL_SHADER_FS: &str = r#"
132#version 330 core
133out vec4 FragColor;
134
135in vec4 fColor;
136in vec2 TexCoord;
137in vec3 Normal;
138in vec3 FragPos;  
139
140uniform vec3 lightColor[5];
141uniform vec3 lightPos[5];
142uniform vec3 viewPos;
143
144uniform int has_texture;
145uniform int num_lights;
146
147uniform sampler2D texture1;
148
149void main()
150{
151    // vec4 texColor = fColor;
152
153    //if (has_texture == 1) {
154        // }
155    vec4 texColor = texture(texture1, TexCoord) * fColor;
156
157    vec3 ambientStrength = vec3(0.1); 
158    vec3 specularStrength = vec3(0.5);
159
160    vec3 result = vec3(0.0);
161
162    for (int i = 0; i < num_lights; ++i) {
163        vec3 lightDir = normalize(lightPos[i] - FragPos); 
164        vec3 norm = normalize(Normal);
165
166        float diff = max(dot(norm, lightDir), 0.0);
167        vec3 diffuse = diff * lightColor[i];
168
169        vec3 viewDir = normalize(viewPos - FragPos);
170        vec3 reflectDir = reflect(-lightDir, norm); 
171
172        float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
173        vec3 specular = specularStrength * spec * lightColor[i];
174
175        result += (ambientStrength + diffuse + specular) * texColor.rgb;
176    }
177
178    FragColor = vec4(result, texColor.a);
179}
180"#;
181
182pub static INSTANCE_MESH_SHADER_VS: &str = r#"
183#version 330 core
184layout (location = 0) in vec3 aPos;
185layout (location = 1) in vec4 aColor;
186layout (location = 2) in mat4 model;
187
188uniform mat4 view;
189uniform mat4 proj;
190
191out vec4 fColor;
192
193void main() {
194    gl_Position = proj * view * model * vec4(aPos, 1.0);
195    // gl_Position = model * vec4(aPos, 1.0);
196    fColor = aColor;
197}
198"#;
199
200pub static INSTANCE_MESH_SHADER_FS: &str = r#"
201#version 330 core
202out vec4 FragColor;
203
204in vec4 fColor;
205
206uniform mat4 view;
207
208void main()
209{
210    FragColor = vec4(fColor);
211}
212"#;
213
214
215pub static PARTICLE_SHADER_VS: &str = r#"
216#version 330 core
217layout (location = 0) in vec3 aPos;
218layout (location = 1) in vec4 aColor;
219layout (location = 2) in vec3 position;
220
221uniform mat4 view;
222uniform mat4 proj;
223uniform mat4 model;
224
225out vec4 fColor;
226
227void main() {
228    // extract the right and up vectors from the view matrix
229    vec3 right = vec3(view[0][0], view[1][0], view[2][0]);
230    vec3 up = vec3(view[0][1], view[1][1], view[2][1]);
231
232    vec3 billboardPos = position + aPos.x * right + aPos.y * up;
233
234    gl_Position = proj * view * model * vec4(billboardPos, 1.0);
235
236    fColor = aColor;
237}
238
239"#;
240
241pub static PARTICLE_SHADER_FS: &str = r#"
242#version 330 core
243out vec4 FragColor;
244
245in vec4 fColor;
246
247void main()
248{
249    FragColor = vec4(fColor);
250}
251"#;