Eevee: Add Initial support for Principle BRDF.

Lots of things not working yet but it's comming.
This commit is contained in:
Clément Foucault 2017-06-29 01:22:23 +02:00
parent 8d57f4e3c6
commit e14fd19105
3 changed files with 183 additions and 8 deletions

View File

@ -139,7 +139,7 @@ vec3 eevee_surface_lit(vec3 N, vec3 albedo, vec3 f0, float roughness, float ao)
}
/* World Diffuse */
if (diff_accum.a < 1.0 && grid_count > 0) {
if (diff_accum.a < 0.999 && grid_count > 0) {
vec3 diff = probe_evaluate_world_diff(bent_normal);
accumulate_light(diff, 1.0, diff_accum);
}
@ -148,3 +148,155 @@ vec3 eevee_surface_lit(vec3 N, vec3 albedo, vec3 f0, float roughness, float ao)
return out_light;
}
/* ----------- CLEAR COAT ----------- */
vec3 eevee_surface_clearcoat_lit(
vec3 N, vec3 albedo, vec3 f0, float roughness,
vec3 C_N, float C_intensity, float C_roughness, /* Clearcoat params */
float ao)
{
roughness = clamp(roughness, 1e-8, 0.9999);
float roughnessSquared = roughness * roughness;
C_roughness = clamp(C_roughness, 1e-8, 0.9999);
float C_roughnessSquared = C_roughness * C_roughness;
vec3 V = cameraVec;
N = normalize(N);
C_N = normalize(C_N);
vec4 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0));
/* ---------------- SCENE LAMPS LIGHTING ----------------- */
#ifdef HAIR_SHADER
vec3 norm_view = cross(V, N);
norm_view = normalize(cross(norm_view, N)); /* Normal facing view */
#endif
vec3 diff = vec3(0.0);
vec3 spec = vec3(0.0);
for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) {
LightData ld = lights_data[i];
vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */
l_vector.xyz = ld.l_position - worldPosition;
l_vector.w = length(l_vector.xyz);
vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, l_vector);
#ifdef HAIR_SHADER
vec3 norm_lamp, view_vec;
float occlu_trans, occlu;
light_hair_common(ld, N, V, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec);
diff += l_color_vis * light_diffuse(ld, -norm_lamp, V, l_vector) * occlu_trans;
spec += l_color_vis * light_specular(ld, N, view_vec, l_vector, roughnessSquared, f0) * occlu;
spec += l_color_vis * light_specular(ld, C_N, view_vec, l_vector, C_roughnessSquared, f0) * C_intensity * occlu;
#else
diff += l_color_vis * light_diffuse(ld, N, V, l_vector);
spec += l_color_vis * light_specular(ld, N, V, l_vector, roughnessSquared, f0);
spec += l_color_vis * light_specular(ld, C_N, V, l_vector, C_roughnessSquared, f0) * C_intensity;
#endif
}
/* Accumulate outgoing radiance */
vec3 out_light = diff * albedo + spec * float(specToggle);
#ifdef HAIR_SHADER
N = -norm_view;
#endif
/* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */
/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
vec4 spec_accum = vec4(0.0);
vec4 C_spec_accum = vec4(0.0);
/* Planar Reflections */
for (int i = 0; i < MAX_PLANAR && i < planar_count && spec_accum.a < 0.999; ++i) {
PlanarData pd = planars_data[i];
/* Fade on geometric normal. */
float fade = probe_attenuation_planar(pd, worldPosition, worldNormal);
if (fade > 0.0) {
vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, rand.a, cameraPos, roughness, fade);
accumulate_light(spec, fade, spec_accum);
vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, rand.a, cameraPos, C_roughness, fade);
accumulate_light(C_spec, fade, C_spec_accum);
}
}
/* Specular probes */
vec3 spec_dir = get_specular_dominant_dir(N, V, roughnessSquared);
vec3 C_spec_dir = get_specular_dominant_dir(C_N, V, C_roughnessSquared);
/* Starts at 1 because 0 is world probe */
for (int i = 1; i < MAX_PROBE && i < probe_count && spec_accum.a < 0.999; ++i) {
CubeData cd = probes_data[i];
float fade = probe_attenuation_cube(cd, worldPosition);
if (fade > 0.0) {
vec3 spec = probe_evaluate_cube(float(i), cd, worldPosition, spec_dir, roughness);
accumulate_light(spec, fade, spec_accum);
vec3 C_spec = probe_evaluate_cube(float(i), cd, worldPosition, C_spec_dir, C_roughness);
accumulate_light(C_spec, fade, C_spec_accum);
}
}
/* World Specular */
if (spec_accum.a < 0.999) {
vec3 spec = probe_evaluate_world_spec(spec_dir, roughness);
accumulate_light(spec, 1.0, spec_accum);
vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness);
accumulate_light(C_spec, 1.0, C_spec_accum);
}
/* Ambient Occlusion */
vec3 bent_normal;
float final_ao = occlusion_compute(N, viewPosition, ao, rand.rg, bent_normal);
/* Get Brdf intensity */
vec2 uv = lut_coords(dot(N, V), roughness);
vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg;
out_light += spec_accum.rgb * F_ibl(f0, brdf_lut) * specular_occlusion(dot(N, V), final_ao, roughness) * float(specToggle);
uv = lut_coords(dot(C_N, V), C_roughness);
brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg;
out_light += C_spec_accum.rgb * F_ibl(vec3(0.04), brdf_lut) * specular_occlusion(dot(C_N, V), final_ao, C_roughness) * float(specToggle) * C_intensity;
/* ---------------- DIFFUSE ENVIRONMENT LIGHTING ----------------- */
/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
vec4 diff_accum = vec4(0.0);
/* Start at 1 because 0 is world irradiance */
for (int i = 1; i < MAX_GRID && i < grid_count && diff_accum.a < 0.999; ++i) {
GridData gd = grids_data[i];
vec3 localpos;
float fade = probe_attenuation_grid(gd, worldPosition, localpos);
if (fade > 0.0) {
vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos);
accumulate_light(diff, fade, diff_accum);
}
}
/* World Diffuse */
if (diff_accum.a < 0.999 && grid_count > 0) {
vec3 diff = probe_evaluate_world_diff(bent_normal);
accumulate_light(diff, 1.0, diff_accum);
}
out_light += diff_accum.rgb * albedo * gtao_multibounce(final_ao, albedo);
return out_light;
}

View File

@ -2573,6 +2573,12 @@ vec3 rotate_vector(vec3 p, vec3 n, float theta) {
);
}
void convert_metallic_to_specular(vec4 basecol, float metallic, float specular_fac, out vec4 diffuse, out vec4 f0)
{
vec4 dielectric = vec4(0.034) * specular_fac * 2.0;
diffuse = mix(basecol, vec4(0.0), metallic);
f0 = mix(dielectric, basecol, metallic);
}
/*********** NEW SHADER NODES ***************/
@ -2650,6 +2656,17 @@ void node_bsdf_principled(vec4 base_color, float subsurface, vec3 subsurface_rad
float specular_tint, float roughness, float anisotropic, float anisotropic_rotation, float sheen, float sheen_tint, float clearcoat,
float clearcoat_roughness, float ior, float transmission, float transmission_roughness, vec3 N, vec3 CN, vec3 T, vec3 I, out vec4 result)
{
#ifdef EEVEE_ENGINE
vec4 diffuse, f0;
convert_metallic_to_specular(base_color, metallic, specular, diffuse, f0);
/* Original value is 0.25 but this one seems to fit cycles better */
clearcoat *= 0.5;
vec3 surface_color = eevee_surface_clearcoat_lit(N, diffuse.rgb, f0.rgb, roughness, CN, clearcoat, clearcoat_roughness, 1.0);
result = vec4(surface_color, 1.0);
#else
/* ambient light */
// TODO: set ambient light to an appropriate value
vec3 L = vec3(mix(0.1, 0.03, metallic)) * base_color.rgb;
@ -2764,6 +2781,7 @@ void node_bsdf_principled(vec4 base_color, float subsurface, vec3 subsurface_rad
}
result = vec4(L, 1.0);
#endif
}
void node_bsdf_translucent(vec4 color, vec3 N, out vec4 result)
@ -3862,13 +3880,6 @@ void node_output_world(vec4 surface, vec4 volume, out vec4 result)
result = vec4(surface.rgb, backgroundAlpha);
}
void convert_metallic_to_specular(vec4 basecol, float metallic, float specular_fac, out vec4 diffuse, out vec4 f0)
{
vec4 dielectric = vec4(0.034) * specular_fac * 2.0;
diffuse = mix(basecol, vec4(0.0), metallic);
f0 = mix(dielectric, basecol, metallic);
}
/* TODO : clean this ifdef mess */
/* EEVEE output */
#ifdef EEVEE_ENGINE

View File

@ -65,6 +65,7 @@ static void node_shader_init_principled(bNodeTree *UNUSED(ntree), bNode *node)
static int node_shader_gpu_bsdf_principled(GPUMaterial *mat, bNode *UNUSED(node), bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
{
#if 0 /* Old 2.7 glsl viewport */
// normal
if (!in[17].link)
in[17].link = GPU_builtin(GPU_VIEW_NORMAL);
@ -76,6 +77,17 @@ static int node_shader_gpu_bsdf_principled(GPUMaterial *mat, bNode *UNUSED(node)
in[18].link = GPU_builtin(GPU_VIEW_NORMAL);
else
GPU_link(mat, "direction_transform_m4v3", in[18].link, GPU_builtin(GPU_VIEW_MATRIX), &in[18].link);
#endif
/* Normals */
if (!in[17].link) {
GPU_link(mat, "world_normals_get", &in[17].link);
}
/* Clearcoat Normals */
if (!in[18].link) {
GPU_link(mat, "world_normals_get", &in[18].link);
}
return GPU_stack_link(mat, "node_bsdf_principled", in, out, GPU_builtin(GPU_VIEW_POSITION));
}