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