1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#version 450
layout(set = 0, binding = 0) uniform material_Block {
vec3 albedo;
float metallic;
float roughness;
} material;
layout(set = 0, binding = 1) uniform light_dir_Block {
vec3 light_dir;
};
layout(set = 0, binding = 2) uniform camera_pos_Block {
vec3 camera_pos;
};
layout(set = 0, binding = 3) uniform sampler2D albedo_tex;
vec3 fresnel_schlick(float cos_theta, vec3 f0) {
return (f0 + ((vec3(1.0, 1.0, 1.0) - f0) * pow((1.0 - cos_theta), 5.0)));
}
vec3 compute_lighting(vec3 normal, vec3 view_dir, vec3 albedo) {
vec3 n = normalize(normal);
vec3 l = normalize(light_dir);
vec3 h = normalize((l + view_dir));
float ndotl = max(dot(n, l), 0.0);
float ndoth = max(dot(n, h), 0.0);
vec3 f0 = mix(vec3(0.04, 0.04, 0.04), albedo, material.metallic);
vec3 f = fresnel_schlick(max(dot(h, view_dir), 0.0), f0);
vec3 specular = (f * pow(ndoth, ((1.0 - material.roughness) * 256.0)));
vec3 diffuse = ((albedo * ndotl) * (vec3(1.0, 1.0, 1.0) - f));
return (diffuse + specular);
}
layout(location = 0) in vec2 in_uv;
layout(location = 1) in vec3 in_normal;
layout(location = 2) in vec3 in_world_pos;
layout(location = 0) out vec4 out_color;
void main() {
vec4 tex_color = texture(albedo_tex, in_uv);
vec3 albedo = (tex_color.rgb * material.albedo);
vec3 view_dir = normalize((camera_pos - in_world_pos));
vec3 color = compute_lighting(in_normal, view_dir, albedo);
out_color = vec4(color, tex_color.a);
}