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