]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/cooktorrance.glsl
Add support for alpha to coverage
[libs/gl.git] / shaderlib / cooktorrance.glsl
1 import msp_interface;
2 import common;
3 import environment;
4 import shadow;
5
6 struct PbrMaterialParameters
7 {
8         vec4 base_color;
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 layout(set=1) uniform sampler2D fresnel_lookup;
26
27 layout(constant_id=auto) const bool use_base_color_map = false;
28 layout(constant_id=auto) const bool use_metalness_map = false;
29 layout(constant_id=auto) const bool use_roughness_map = false;
30 layout(constant_id=auto) const bool use_occlusion_map = false;
31 layout(constant_id=auto) const bool use_emission = false;
32 layout(constant_id=auto) const bool use_emission_map = false;
33 layout(constant_id=auto) const bool use_image_based_lighting = false;
34
35 #pragma MSP stage(fragment)
36 virtual vec4 get_base_color()
37 {
38         if(use_base_color_map)
39                 return texture(base_color_map, texcoord.xy);
40         else
41                 return pbr_material.base_color;
42 }
43
44 virtual float get_metalness_value()
45 {
46         if(use_metalness_map)
47                 return texture(metalness_map, texcoord.xy).r;
48         else
49                 return pbr_material.metalness;
50 }
51
52 virtual float get_roughness_value()
53 {
54         if(use_roughness_map)
55                 return texture(roughness_map, texcoord.xy).r;
56         else
57                 return pbr_material.roughness;
58 }
59
60 virtual float get_occlusion_value()
61 {
62         if(use_occlusion_map)
63                 return texture(occlusion_map, texcoord.xy).r;
64         else
65                 return 1.0;
66 }
67
68 virtual vec3 get_emission_color()
69 {
70         if(use_emission_map)
71                 return texture(emission_map, texcoord.xy).rgb;
72         else
73                 return pbr_material.emission.rgb;
74 }
75
76 /* Computes the diffuse reflection of the macrosurface */
77 vec3 lambert_diffuse(vec3 base_color)
78 {
79         /* Scale by pi (cosine-weighted area of a hemisphere) because the light
80         scatters in every direction */
81         return base_color/PI;
82 }
83
84 /* Computes the fraction of microfacets aligned at the halfway vector
85 (Trowbridge-Reitz GGX) */
86 float normal_distribution_ggxtr(vec3 normal, vec3 halfway, float roughness)
87 {
88         float n_dot_h = max(dot(normal, halfway), 0.0);
89         float rough_q = roughness * roughness;
90         rough_q *= rough_q;
91         float denom = n_dot_h*n_dot_h*(rough_q-1)+1;
92         /* Scale by pi to normalize the total area of the microfacets as projected
93         to the macrosurface */
94         return rough_q/(PI*denom*denom);
95 }
96
97 /* Computes shadowing and masking of a microfacet surface from a given
98 direction */
99 float geometry_schlick_ggx(vec3 normal, vec3 view, float k)
100 {
101         float n_dot_v = max(dot(normal, view), 0.0);
102         return n_dot_v/(n_dot_v*(1.0-k)+k);
103 }
104
105 /* Computes shadowing and masking of a microfacet surface for a combination of
106 look and light directions */
107 float geometry_smith(vec3 normal, vec3 look, vec3 light, float roughness)
108 {
109         float k = (roughness+1.0)*(roughness+1.0)/8.0;
110         float ggx_look = geometry_schlick_ggx(normal, look, k);
111         float ggx_light = geometry_schlick_ggx(normal, light, k);
112         return ggx_look*ggx_light;
113 }
114
115 /* Computes the reflectance of the material at a given reflection angle */
116 vec3 fresnel_schlick(vec3 halfway, vec3 look, vec3 base_color, float metalness)
117 {
118         // 0.04 is a decent approximation for dielectric base reflectivity
119         vec3 f0 = mix(vec3(0.04), base_color, metalness);
120         return mix(f0, vec3(1.0), pow(max(1.0-dot(halfway, look), 0.0), 5.0));
121 }
122
123 /* Computes the full contribution of a single light */
124 vec3 cooktorrance_one_light_direct(vec3 normal, vec3 look, vec3 light, vec3 base_color, float metalness, float roughness)
125 {
126         vec3 halfway = normalize(light-look);
127         float ndist = normal_distribution_ggxtr(normal, halfway, roughness);
128         float geom = geometry_smith(normal, -look, light, roughness);
129
130         vec3 k_spec = fresnel_schlick(halfway, light, base_color, metalness);
131         vec3 k_diff = (1.0-k_spec)*(1.0-metalness);
132
133         float spec_denom = max(4.0*max(dot(normal, -look), 0.0)*max(dot(normal, light), 0.0), 0.001);
134         return max(dot(normal, light), 0.0)*(k_diff*lambert_diffuse(base_color)+k_spec*ndist*geom/spec_denom);
135 }
136
137 vec3 cooktorrance_environment(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness)
138 {
139         vec3 f0 = mix(vec3(0.04), base_color, metalness);
140         vec2 scale_bias = texture(fresnel_lookup, vec2(roughness, max(dot(normal, -look), 0.0))).rg;
141         vec3 k_spec = f0*scale_bias.x+scale_bias.y;
142         vec3 k_diff = (1.0-k_spec)*(1.0-metalness);
143
144         if(use_image_based_lighting)
145         {
146                 vec3 irradiance = get_irradiance_sample(normal);
147                 vec3 reflection = get_environment_sample(reflect(look, normal), roughness).rgb;
148
149                 return k_diff*irradiance*base_color+k_spec*reflection;
150         }
151         else
152                 return (k_diff*base_color+k_spec)*ambient_color.rgb;
153 }
154
155 vec3 cooktorrance_lighting(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness)
156 {
157         vec3 color = vec3(0.0);
158         for(int i=0; i<max_lights; ++i)
159                 if(light_sources[i].type!=0)
160                 {
161                         IncomingLight incoming = get_incoming_light(i, world_vertex.xyz);
162                         float shadow = get_shadow_factor(i, world_vertex);
163                         color += cooktorrance_one_light_direct(normal, look, incoming.direction, base_color, metalness, roughness)*incoming.color*shadow;
164                 }
165
166         color += cooktorrance_environment(normal, look, base_color, metalness, roughness);
167
168         color *= get_occlusion_value();
169
170         if(use_emission)
171                 color += get_emission_color();
172
173         return color;
174 }
175
176 void main()
177 {
178         vec4 base_color = get_base_color();
179         float alpha = apply_alpha_cutoff(base_color.a, alpha_cutoff);
180
181         vec3 normal = get_fragment_normal();
182         vec3 look = normalize(world_look_dir);
183
184         float metalness = get_metalness_value();
185         float roughness = get_roughness_value();
186
187         vec3 lit_color = cooktorrance_lighting(normal, look, base_color.rgb, metalness, roughness);
188
189         frag_color = vec4(lit_color, alpha);
190 }