5 layout(set=1) uniform sampler2D fresnel_lookup;
7 layout(constant_id=auto) const bool use_image_based_lighting = false;
9 #pragma MSP stage(fragment)
10 /* Computes the diffuse reflection of the macrosurface */
11 vec3 lambert_diffuse(vec3 base_color)
13 /* Scale by pi (cosine-weighted area of a hemisphere) because the light
14 scatters in every direction */
18 /* Computes the fraction of microfacets aligned at the halfway vector
19 (Trowbridge-Reitz GGX) */
20 float normal_distribution_ggxtr(vec3 normal, vec3 halfway, float roughness)
22 float n_dot_h = max(dot(normal, halfway), 0.0);
23 float rough_q = roughness * roughness;
25 float denom = n_dot_h*n_dot_h*(rough_q-1)+1;
26 /* Scale by pi to normalize the total area of the microfacets as projected
27 to the macrosurface */
28 return rough_q/(PI*denom*denom);
31 /* Computes shadowing and masking of a microfacet surface from a given
33 float geometry_schlick_ggx(vec3 normal, vec3 view, float k)
35 float n_dot_v = max(dot(normal, view), 0.0);
36 return n_dot_v/(n_dot_v*(1.0-k)+k);
39 /* Computes shadowing and masking of a microfacet surface for a combination of
40 look and light directions */
41 float geometry_smith(vec3 normal, vec3 look, vec3 light, float roughness)
43 float k = (roughness+1.0)*(roughness+1.0)/8.0;
44 float ggx_look = geometry_schlick_ggx(normal, look, k);
45 float ggx_light = geometry_schlick_ggx(normal, light, k);
46 return ggx_look*ggx_light;
49 /* Computes the reflectance of the material at a given reflection angle */
50 vec3 fresnel_schlick(vec3 halfway, vec3 look, vec3 base_color, float metalness)
52 // 0.04 is a decent approximation for dielectric base reflectivity
53 vec3 f0 = mix(vec3(0.04), base_color, metalness);
54 return mix(f0, vec3(1.0), pow(max(1.0-dot(halfway, look), 0.0), 5.0));
57 /* Computes the full contribution of a single light */
58 vec3 cooktorrance_one_light_direct(vec3 normal, vec3 look, vec3 light, vec3 base_color, float metalness, float roughness)
60 vec3 halfway = normalize(light-look);
61 float ndist = normal_distribution_ggxtr(normal, halfway, roughness);
62 float geom = geometry_smith(normal, -look, light, roughness);
64 vec3 k_spec = fresnel_schlick(halfway, light, base_color, metalness);
65 vec3 k_diff = (1.0-k_spec)*(1.0-metalness);
67 float spec_denom = max(4.0*max(dot(normal, -look), 0.0)*max(dot(normal, light), 0.0), 0.001);
68 return max(dot(normal, light), 0.0)*(k_diff*lambert_diffuse(base_color)+k_spec*ndist*geom/spec_denom);
71 vec3 cooktorrance_environment(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness)
73 vec3 f0 = mix(vec3(0.04), base_color, metalness);
74 vec2 scale_bias = texture(fresnel_lookup, vec2(roughness, max(dot(normal, -look), 0.0))).rg;
75 vec3 k_spec = f0*scale_bias.x+scale_bias.y;
76 vec3 k_diff = (1.0-k_spec)*(1.0-metalness);
78 if(use_image_based_lighting)
80 vec3 irradiance = get_irradiance_sample(normal);
81 vec3 reflection = get_environment_sample(reflect(look, normal), roughness).rgb;
83 return k_diff*irradiance*base_color+k_spec*reflection;
86 return (k_diff*base_color+k_spec)*ambient_color.rgb;
89 vec3 cooktorrance_lighting(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness, float occlusion)
91 vec3 color = vec3(0.0);
92 for(int i=0; i<max_lights; ++i)
93 if(light_sources[i].type!=0)
95 IncomingLight incoming = get_incoming_light(i, world_vertex.xyz);
96 float shadow = get_shadow_factor(i, world_vertex);
97 color += cooktorrance_one_light_direct(normal, look, incoming.direction, base_color, metalness, roughness)*incoming.color*shadow;
100 color += cooktorrance_environment(normal, look, base_color, metalness, roughness)*occlusion;