EEVEE: change Principled BSDF to match Cycles #111754

Merged
Weizhen Huang merged 8 commits from weizhen/blender:eevee-principled into main 2023-09-01 11:28:19 +02:00
6 changed files with 80 additions and 76 deletions

View File

@ -24,7 +24,7 @@ float ior_from_f0(float f0)
}
/* Simplified form of F_eta(eta, 1.0). */
float f0_from_ior(float eta)
float F0_from_ior(float eta)
{
float A = (eta - 1.0) / (eta + 1.0);
return A * A;
@ -69,7 +69,7 @@ float F_eta(float eta, float cos_theta)
/* Fresnel color blend base on fresnel factor */
vec3 F_color_blend(float eta, float fresnel, vec3 f0_color)
{
float f0 = f0_from_ior(eta);
float f0 = F0_from_ior(eta);
float fac = saturate((fresnel - f0) / (1.0 - f0));
return mix(f0_color, vec3(1.0), fac);
}

View File

@ -100,6 +100,7 @@ vec2 brdf_lut(float a, float b);
vec3 F_brdf_multi_scatter(vec3 a, vec3 b, vec2 c);
vec3 F_brdf_single_scatter(vec3 a, vec3 b, vec2 c);
float F_eta(float a, float b);
float F0_from_ior(float a);
#endif
#ifdef VOLUMETRICS

View File

@ -82,7 +82,7 @@ vec2 btdf_lut(float cos_theta, float roughness, float ior, float do_multiscatter
if (ior >= 1.0) {
vec2 split_sum = brdf_lut(cos_theta, roughness);
float f0 = f0_from_ior(ior);
float f0 = F0_from_ior(ior);
/* Gradually increase `f90` from 0 to 1 when IOR is in the range of [1.0, 1.33], to avoid harsh
* transition at `IOR == 1`. */
float f90 = fast_sqrt(saturate(f0 / 0.02));

View File

@ -63,7 +63,7 @@ vec4 screen_space_refraction(vec3 vP, vec3 N, vec3 V, float ior, float roughness
/* Empirical fit for refraction. */
/* TODO: find a better fit or precompute inside the LUT. */
cone_tan *= 0.5 * fast_sqrt(f0_from_ior((ior < 1.0) ? 1.0 / ior : ior));
cone_tan *= 0.5 * fast_sqrt(F0_from_ior((ior < 1.0) ? 1.0 / ior : ior));
float cone_footprint = hit_dist * cone_tan;

View File

@ -53,101 +53,103 @@ void node_bsdf_principled(vec4 base_color,
{
/* Match cycles. */
metallic = clamp(metallic, 0.0, 1.0);
transmission = clamp(transmission, 0.0, 1.0) * (1.0 - metallic);
float diffuse_weight = (1.0 - transmission) * (1.0 - metallic);
float specular_weight = (1.0 - transmission);
float clearcoat_weight = max(clearcoat, 0.0) * 0.25;
specular = max(0.0, specular);
transmission = clamp(transmission, 0.0, 1.0);
clearcoat = max(clearcoat, 0.0) * 0.25;
N = safe_normalize(N);
CN = safe_normalize(CN);
vec3 V = cameraVec(g_data.P);
float NV = dot(N, V);
vec2 glass_bsdf = btdf_lut(NV, roughness, ior, do_multiscatter);
float glass_reflection_weight = glass_bsdf.y * transmission;
float glass_transmission_weight = glass_bsdf.x * transmission;
vec3 base_color_tint = tint_from_color(base_color.rgb);
vec2 split_sum = brdf_lut(NV, roughness);
ClosureTransparency transparency_data;
transparency_data.weight = weight;
transparency_data.transmittance = vec3(1.0 - alpha);
transparency_data.holdout = 0.0;
weight *= alpha;
/* First layer: Sheen */
/* TODO: Maybe sheen should be specular. */
vec3 sheen_color = sheen * sheen_tint.rgb * principled_sheen(NV, sheen_roughness);
ClosureDiffuse diffuse_data;
diffuse_data.color = weight * sheen_color;
diffuse_data.N = N;
/* Attenuate lower layers */
weight *= (1.0 - max_v3(sheen_color));
/* Second layer: Clearcoat */
ClosureReflection clearcoat_data;
clearcoat_data.N = CN;
clearcoat_data.roughness = clearcoat_roughness;
float coat_ior = 1.5;
float coat_NV = dot(clearcoat_data.N, V);
float reflectance = btdf_lut(coat_NV, clearcoat_data.roughness, coat_ior, 0.0).y;
clearcoat_data.weight = weight * clearcoat * reflectance;
clearcoat_data.color = vec3(1.0);
/* Attenuate lower layers */
weight *= (1.0 - reflectance * clearcoat);
/* Attenuated by sheen and clearcoat. */
ClosureEmission emission_data;
emission_data.weight = weight;
emission_data.emission = emission.rgb * emission_strength;
/* Diffuse. */
ClosureDiffuse diffuse_data;
diffuse_data.weight = diffuse_weight * weight;
diffuse_data.color = mix(base_color.rgb, subsurface_color.rgb, subsurface);
/* Sheen Coarse approximation: We reuse the diffuse radiance and just scale it. */
diffuse_data.color += sheen * sheen_tint.rgb * principled_sheen(NV, sheen_roughness);
diffuse_data.N = N;
diffuse_data.sss_radius = subsurface_radius * subsurface;
diffuse_data.sss_id = uint(do_sss);
/* NOTE(@fclem): We need to blend the reflection color but also need to avoid applying the
* weights so we compute the ratio. */
float reflection_weight = specular_weight + glass_reflection_weight;
float reflection_weight_inv = safe_rcp(reflection_weight);
specular_weight *= reflection_weight_inv;
glass_reflection_weight *= reflection_weight_inv;
/* Reflection. */
/* Metallic component */
ClosureReflection reflection_data;
reflection_data.weight = reflection_weight * weight;
reflection_data.N = N;
reflection_data.roughness = roughness;
vec2 split_sum = brdf_lut(NV, roughness);
if (true) {
vec3 dielectric_f0_color = mix(vec3(1.0), base_color_tint, specular_tint);
vec3 metallic_f0_color = base_color.rgb;
vec3 f0 = mix((0.08 * specular) * dielectric_f0_color, metallic_f0_color, metallic);
/* Cycles does this blending using the microfacet fresnel factor. However, our fresnel
* is already baked inside the split sum LUT. We approximate by changing the f90 color
* directly in a non linear fashion. */
vec3 f90 = mix(f0, vec3(1.0), fast_sqrt(specular));
vec3 reflection_brdf = (do_multiscatter != 0.0) ? F_brdf_multi_scatter(f0, f90, split_sum) :
F_brdf_single_scatter(f0, f90, split_sum);
reflection_data.color = reflection_brdf * specular_weight;
}
if (true) {
/* Poor approximation since we baked the LUT using a fixed IOR. */
vec3 f0 = mix(vec3(1.0), base_color.rgb, specular_tint);
vec3 f0 = base_color.rgb;
vec3 f90 = vec3(1.0);
vec3 glass_brdf = (do_multiscatter != 0.0) ? F_brdf_multi_scatter(f0, f90, split_sum) :
F_brdf_single_scatter(f0, f90, split_sum);
/* Avoid 3 glossy evaluation. Use the same closure for glass reflection. */
reflection_data.color += glass_brdf * glass_reflection_weight;
vec3 metallic_brdf = (do_multiscatter != 0.0) ? F_brdf_multi_scatter(f0, f90, split_sum) :
F_brdf_single_scatter(f0, f90, split_sum);
reflection_data.color = weight * metallic * metallic_brdf;
/* Attenuate lower layers */
weight *= (1.0 - metallic);
}
ClosureReflection clearcoat_data;
clearcoat_data.weight = clearcoat_weight * weight;
clearcoat_data.N = CN;
clearcoat_data.roughness = clearcoat_roughness;
if (true) {
float NV = dot(clearcoat_data.N, V);
vec2 split_sum = brdf_lut(NV, clearcoat_data.roughness);
vec3 brdf = F_brdf_single_scatter(vec3(0.04), vec3(1.0), split_sum);
clearcoat_data.color = brdf;
}
/* Refraction. */
/* Transmission component */
ClosureRefraction refraction_data;
refraction_data.weight = glass_transmission_weight * weight;
refraction_data.color = base_color.rgb;
refraction_data.N = N;
refraction_data.roughness = roughness;
refraction_data.ior = ior;
/* TODO: change `specular_tint` to rgb. */
vec3 reflection_tint = mix(vec3(1.0), base_color.rgb, specular_tint);
if (true) {
vec2 bsdf = btdf_lut(NV, roughness, ior, do_multiscatter);
reflection_data.color += weight * transmission * bsdf.y * reflection_tint;
refraction_data.weight = weight * transmission * bsdf.x;
refraction_data.color = base_color.rgb;
refraction_data.N = N;
refraction_data.roughness = roughness;
refraction_data.ior = ior;
/* Attenuate lower layers */
weight *= (1.0 - transmission);
}
/* Specular component */
if (true) {
vec3 f0 = F0_from_ior(ior) * 2.0 * specular * reflection_tint;
vec3 f90 = vec3(1.0);
vec3 specular_brdf = (do_multiscatter != 0.0) ? F_brdf_multi_scatter(f0, f90, split_sum) :
F_brdf_single_scatter(f0, f90, split_sum);
reflection_data.color += weight * specular_brdf;
/* Attenuate lower layers */
weight *= (1.0 - max_v3(specular_brdf));
}
/* Diffuse component */
if (true) {
vec3 diffuse_color = mix(base_color.rgb, subsurface_color.rgb, subsurface);
diffuse_data.sss_radius = subsurface_radius * subsurface;
diffuse_data.sss_id = uint(do_sss);
diffuse_data.color += weight * diffuse_color;
}
/* Adjust the weight of picking the closure. */
reflection_data.weight = avg(reflection_data.color);
reflection_data.color *= safe_rcp(reflection_data.weight);
diffuse_data.weight = avg(diffuse_data.color);
diffuse_data.color *= safe_rcp(diffuse_data.weight);
/* Ref. #98190: Defines are optimizations for old compilers.
* Might become unnecessary with EEVEE-Next. */

View File

@ -170,7 +170,8 @@ static int node_shader_gpu_bsdf_principled(GPUMaterial *mat,
}
#endif
bool use_diffuse = socket_not_one(SOCK_METALLIC_ID) && socket_not_one(SOCK_TRANSMISSION_ID);
bool use_diffuse = socket_not_zero(SOCK_SHEEN_ID) ||
(socket_not_one(SOCK_METALLIC_ID) && socket_not_one(SOCK_TRANSMISSION_ID));
bool use_subsurf = socket_not_zero(SOCK_SUBSURFACE_ID) && use_diffuse;
bool use_refract = socket_not_one(SOCK_METALLIC_ID) && socket_not_zero(SOCK_TRANSMISSION_ID);
bool use_transparency = socket_not_one(SOCK_ALPHA_ID);