]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/pbr_material.glsl
Check the flat qualifier from the correct member
[libs/gl.git] / shaderlib / pbr_material.glsl
1 import msp_interface;
2 import common;
3 import cooktorrance;
4
5 struct PbrMaterialParameters
6 {
7         vec4 base_color;
8         vec4 tint;
9         vec4 emission;
10         float metalness;
11         float roughness;
12 };
13
14 layout(set=1) uniform PbrMaterial
15 {
16         PbrMaterialParameters pbr_material;
17         AlphaCutoffParams alpha_cutoff;
18 };
19
20 layout(set=1) uniform sampler2D base_color_map;
21 layout(set=1) uniform sampler2D metalness_map;
22 layout(set=1) uniform sampler2D roughness_map;
23 layout(set=1) uniform sampler2D occlusion_map;
24 layout(set=1) uniform sampler2D emission_map;
25
26 layout(constant_id=auto) const bool use_base_color_map = false;
27 layout(constant_id=auto) const bool use_metalness_map = false;
28 layout(constant_id=auto) const bool use_roughness_map = false;
29 layout(constant_id=auto) const bool use_occlusion_map = false;
30 layout(constant_id=auto) const bool use_emission = false;
31 layout(constant_id=auto) const bool use_emission_map = false;
32
33 #pragma MSP stage(fragment)
34 virtual vec4 get_base_color()
35 {
36         if(use_base_color_map)
37                 return texture(base_color_map, texcoord.xy)*pbr_material.tint;
38         else
39                 return pbr_material.base_color*pbr_material.tint;
40 }
41
42 virtual float get_metalness_value()
43 {
44         if(use_metalness_map)
45                 return texture(metalness_map, texcoord.xy).r;
46         else
47                 return pbr_material.metalness;
48 }
49
50 virtual float get_roughness_value()
51 {
52         if(use_roughness_map)
53                 return texture(roughness_map, texcoord.xy).r;
54         else
55                 return pbr_material.roughness;
56 }
57
58 virtual float get_occlusion_value()
59 {
60         if(use_occlusion_map)
61                 return texture(occlusion_map, texcoord.xy).r;
62         else
63                 return 1.0;
64 }
65
66 virtual vec3 get_emission_color()
67 {
68         if(use_emission_map)
69                 return texture(emission_map, texcoord.xy).rgb;
70         else
71                 return pbr_material.emission.rgb;
72 }
73
74 void main()
75 {
76         vec4 base_color = get_base_color();
77         float alpha = apply_alpha_cutoff(base_color.a, alpha_cutoff);
78
79         vec3 normal = get_fragment_normal();
80         vec3 look = normalize(world_look_dir);
81
82         float metalness = get_metalness_value();
83         float roughness = get_roughness_value();
84         float occlusion = get_occlusion_value();
85
86         vec3 lit_color = cooktorrance_lighting(normal, look, base_color.rgb, metalness, roughness, occlusion);
87         if(use_emission)
88                 lit_color += get_emission_color();
89
90         frag_color = vec4(lit_color, alpha);
91 }