import environment; import lighting; import shadow; layout(set=1) uniform sampler2D fresnel_lookup; layout(constant_id=auto) const bool use_image_based_lighting = false; #pragma MSP stage(fragment) /* Computes the diffuse reflection of the macrosurface */ vec3 lambert_diffuse(vec3 base_color) { /* Scale by pi (cosine-weighted area of a hemisphere) because the light scatters in every direction */ return base_color/PI; } /* Computes the fraction of microfacets aligned at the halfway vector (Trowbridge-Reitz GGX) */ float normal_distribution_ggxtr(vec3 normal, vec3 halfway, float roughness) { float n_dot_h = max(dot(normal, halfway), 0.0); float rough_q = roughness * roughness; rough_q *= rough_q; float denom = n_dot_h*n_dot_h*(rough_q-1)+1; /* Scale by pi to normalize the total area of the microfacets as projected to the macrosurface */ return rough_q/(PI*denom*denom); } /* Computes shadowing and masking of a microfacet surface from a given direction */ float geometry_schlick_ggx(vec3 normal, vec3 view, float k) { float n_dot_v = max(dot(normal, view), 0.0); return n_dot_v/(n_dot_v*(1.0-k)+k); } /* Computes shadowing and masking of a microfacet surface for a combination of look and light directions */ float geometry_smith(vec3 normal, vec3 look, vec3 light, float roughness) { float k = (roughness+1.0)*(roughness+1.0)/8.0; float ggx_look = geometry_schlick_ggx(normal, look, k); float ggx_light = geometry_schlick_ggx(normal, light, k); return ggx_look*ggx_light; } /* Computes the reflectance of the material at a given reflection angle */ vec3 fresnel_schlick(vec3 halfway, vec3 look, vec3 base_color, float metalness) { // 0.04 is a decent approximation for dielectric base reflectivity vec3 f0 = mix(vec3(0.04), base_color, metalness); return mix(f0, vec3(1.0), pow(max(1.0-dot(halfway, look), 0.0), 5.0)); } /* Computes the full contribution of a single light */ vec3 cooktorrance_one_light_direct(vec3 normal, vec3 look, vec3 light, vec3 base_color, float metalness, float roughness) { vec3 halfway = normalize(light-look); float ndist = normal_distribution_ggxtr(normal, halfway, roughness); float geom = geometry_smith(normal, -look, light, roughness); vec3 k_spec = fresnel_schlick(halfway, light, base_color, metalness); vec3 k_diff = (1.0-k_spec)*(1.0-metalness); float spec_denom = max(4.0*max(dot(normal, -look), 0.0)*max(dot(normal, light), 0.0), 0.001); return max(dot(normal, light), 0.0)*(k_diff*lambert_diffuse(base_color)+k_spec*ndist*geom/spec_denom); } vec3 cooktorrance_environment(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness) { vec3 f0 = mix(vec3(0.04), base_color, metalness); vec2 scale_bias = texture(fresnel_lookup, vec2(roughness, max(dot(normal, -look), 0.0))).rg; vec3 k_spec = f0*scale_bias.x+scale_bias.y; vec3 k_diff = (1.0-k_spec)*(1.0-metalness); if(use_image_based_lighting) { vec3 irradiance = get_irradiance_sample(normal); vec3 reflection = get_environment_sample(reflect(look, normal), roughness).rgb; return k_diff*irradiance*base_color+k_spec*reflection; } else return (k_diff*base_color+k_spec)*ambient_color.rgb; } vec3 cooktorrance_lighting(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness, float occlusion) { vec3 color = vec3(0.0); for(int i=0; i