1#version 450 core
2
3layout(location=0) in vec3 v_position;
4layout(location=1) in vec3 v_color;
5layout(location=2) in vec3 v_normal;
6
7layout(location=0) out vec4 f_color;
8
9layout(set=0, binding=0)
10uniform Globals {
11    mat4 u_view_proj;
12    vec3 u_view_position;
13};
14
15layout(set = 1, binding = 0) uniform Light {
16    vec3 u_position;
17    vec3 u_color;
18};
19
20layout(set=2, binding=0)
21uniform Locals {
22    mat4 u_transform;
23    vec2 u_min_max;
24};
25
26layout (set = 2, binding = 1) uniform texture2D t_color;
27layout (set = 2, binding = 2) uniform sampler s_color;
28
29float invLerp(float from, float to, float value){
30    return (value - from) / (to - from);
31}
32
33void main() {
34    vec3 object_color =
35        texture(sampler2D(t_color,s_color), vec2(invLerp(u_min_max.x,u_min_max.y,length(v_position)),0.0)).xyz;
36
37    float ambient_strength = 0.1;
38    vec3 ambient_color = u_color * ambient_strength;
39
40    vec3 normal = normalize(v_normal);
41    vec3 light_dir = normalize(u_position - v_position);
42
43    float diffuse_strength = max(dot(normal, light_dir), 0.0);
44    vec3 diffuse_color = u_color * diffuse_strength;
45
46    vec3 view_dir = normalize(u_view_position - v_position);
47    vec3 half_dir = normalize(view_dir + light_dir);
48
49    float specular_strength = pow(max(dot(normal, half_dir), 0.0), 32);
50
51    vec3 specular_color = specular_strength * u_color;
52
53    vec3 result = (ambient_color + diffuse_color + specular_color) * object_color;
54
55    f_color = vec4(result, 1.0);
56}
57