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