]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/cooktorrance.glsl
Refactor vertex builders
[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
24 layout(constant_id=auto) const bool use_base_color_map = false;
25 layout(constant_id=auto) const bool use_metalness_map = false;
26 layout(constant_id=auto) const bool use_roughness_map = false;
27 layout(constant_id=auto) const bool use_occlusion_map = false;
28 layout(constant_id=auto) const bool use_emission = false;
29 layout(constant_id=auto) const bool use_emission_map = false;
30
31 const float PI = 3.1415926535;
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);
38         else
39                 return pbr_material.base_color;
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 /* Computes the diffuse reflection of the macrosurface */
75 vec3 lambert_diffuse(vec3 base_color)
76 {
77         // Scale by pi to get a result per steradian, suitable for integration
78         return base_color/PI;
79 }
80
81 /* Computes the fraction of microfacets aligned at the halfway vector
82 (Trowbridge-Reitz GGX) */
83 float normal_distribution_ggxtr(vec3 normal, vec3 halfway, float roughness)
84 {
85         float n_dot_h = max(dot(normal, halfway), 0.0);
86         float rough_q = roughness * roughness;
87         rough_q *= rough_q;
88         float denom = n_dot_h*n_dot_h*(rough_q-1)+1;
89         // Scale by pi to get a result per steradian, suitable for integration
90         return rough_q/(PI*denom*denom);
91 }
92
93 /* Computes shadowing and masking of a microfacet surface from a given
94 direction */
95 float geometry_schlick_ggx(vec3 normal, vec3 view, float k)
96 {
97         float n_dot_v = max(dot(normal, view), 0.0);
98         return n_dot_v/(n_dot_v*(1.0-k)+k);
99 }
100
101 /* Computes shadowing and masking of a microfacet surface for a combination of
102 look and light directions */
103 float geometry_smith(vec3 normal, vec3 look, vec3 light, float roughness)
104 {
105         float k = (roughness+1.0)*(roughness+1.0)/8.0;
106         float ggx_look = geometry_schlick_ggx(normal, look, k);
107         float ggx_light = geometry_schlick_ggx(normal, light, k);
108         return ggx_look*ggx_light;
109 }
110
111 /* Computes the reflectance of the material at a given reflection angle */
112 vec3 fresnel_schlick(vec3 halfway, vec3 look, vec3 base_color, float metalness)
113 {
114         // 0.04 is a decent approximation for dielectric base reflectivity
115         vec3 f0 = mix(vec3(0.04), base_color, metalness);
116         return mix(f0, vec3(1.0), pow(1.0-dot(halfway, look), 5.0));
117 }
118
119 /* Computes the full contribution of a single light */
120 vec3 cooktorrance_one_light_direct(vec3 normal, vec3 look, vec3 light, vec3 light_color, vec3 base_color, float metalness, float roughness)
121 {
122         vec3 halfway = normalize(light-look);
123         float ndist = normal_distribution_ggxtr(normal, halfway, roughness);
124         float geom = geometry_smith(normal, -look, light, roughness);
125
126         vec3 k_spec = fresnel_schlick(halfway, light, base_color, metalness);
127         vec3 k_diff = (1.0-k_spec)*(1.0-metalness);
128
129         float denom = max(4.0*max(dot(normal, -look), 0.0)*max(dot(normal, light), 0.0), 0.001);
130         return max(dot(normal, light), 0.0)*light_color*(k_diff*lambert_diffuse(base_color)+k_spec*ndist*geom/denom);
131 }
132
133 vec3 cooktorrance_lighting(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness)
134 {
135         vec3 light;
136         if(use_normal_map)
137                 light = normalize(tbn_light_dir);
138         else
139                 light = normalize(eye_light_dir);
140
141         float shadow = get_shadow_factor(0);
142         vec3 color = cooktorrance_one_light_direct(normal, look, light, light_sources[0].diffuse.rgb, base_color, metalness, roughness)*shadow;
143
144         color *= get_occlusion_value();
145
146         if(use_emission)
147                 color += get_emission_color();
148
149         return color;
150 }
151
152 void main()
153 {
154         vec3 normal;
155         vec3 look;
156         if(use_normal_map)
157         {
158                 normal = get_fragment_normal();
159                 look = normalize(tbn_look_dir);
160         }
161         else
162         {
163                 normal = normalize(eye_normal);
164                 look = normalize(eye_look_dir);
165         }
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 }