diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2017-06-29 16:49:51 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2017-06-29 18:08:13 +0300 |
commit | 95797336f5ab02b9ce7fa7e9a854d34109fd6f66 (patch) | |
tree | da10cf20d1d1bb587c547432c8f3e19a76692e26 | |
parent | 3888227a7bfdb6b3867680ea237152d2929e1bc5 (diff) |
Eevee: Prepare support for future Anisotropic shading.
4 files changed, 49 insertions, 35 deletions
diff --git a/source/blender/draw/engines/eevee/eevee_materials.c b/source/blender/draw/engines/eevee/eevee_materials.c index 3b25277c8ae..b6e2864b517 100644 --- a/source/blender/draw/engines/eevee/eevee_materials.c +++ b/source/blender/draw/engines/eevee/eevee_materials.c @@ -303,8 +303,8 @@ void EEVEE_materials_init(void) for (int i = 0; i < 64 * 64; i++) { texels_layer[i][0] = blue_noise[i][0]; texels_layer[i][1] = blue_noise[i][1] * 0.5 + 0.5; - texels_layer[i][2] = blue_noise[i][2]; - texels_layer[i][3] = blue_noise[i][3]; + texels_layer[i][2] = cosf(blue_noise[i][1] * 2.0 * M_PI); + texels_layer[i][3] = sinf(blue_noise[i][1] * 2.0 * M_PI); } e_data.util_tex = DRW_texture_create_2D_array(64, 64, layers, DRW_TEX_RGBA_16, DRW_TEX_FILTER | DRW_TEX_WRAP, (float *)texels); diff --git a/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl b/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl index 127a5167d0c..8bbdc57678c 100644 --- a/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl +++ b/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl @@ -84,7 +84,7 @@ vec3 eevee_surface_lit(vec3 N, vec3 albedo, vec3 f0, float roughness, float ao) float fade = probe_attenuation_planar(pd, worldPosition, N); if (fade > 0.0) { - vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, rand.a, cameraPos, roughness, fade); + vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, rand.r, cameraPos, roughness, fade); accumulate_light(spec, fade, spec_accum); } } @@ -221,10 +221,10 @@ vec3 eevee_surface_clearcoat_lit( 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); + vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, rand.r, 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); + vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, rand.r, cameraPos, C_roughness, fade); accumulate_light(C_spec, fade, C_spec_accum); } } diff --git a/source/blender/gpu/shaders/gpu_shader_material.glsl b/source/blender/gpu/shaders/gpu_shader_material.glsl index 1598c987143..48f9a5e0ec9 100644 --- a/source/blender/gpu/shaders/gpu_shader_material.glsl +++ b/source/blender/gpu/shaders/gpu_shader_material.glsl @@ -2656,6 +2656,20 @@ 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) { + /* rotate tangent */ + if (anisotropic_rotation != 0.0) { + T = rotate_vector(T, N, anisotropic_rotation * 2.0 * M_PI); + } + + /* calculate the tangent and bitangent */ + vec3 Y = T; + vec3 X = normalize(cross(Y, N)); + + float aspect = sqrt(1.0 - anisotropic * 0.9); + float a = sqr(roughness); + float ax = max(0.001, a / aspect); + float ay = max(0.001, a * aspect); + #ifdef EEVEE_ENGINE vec4 diffuse, f0; convert_metallic_to_specular(base_color, metallic, specular, diffuse, f0); @@ -2663,9 +2677,28 @@ void node_bsdf_principled(vec4 base_color, float subsurface, vec3 subsurface_rad /* 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); +#if 0 /* Wait until temporal AA (aka. denoising) */ + /* Distribute N in anisotropy direction. */ + vec4 surface_color = vec4(0.0); + for (float i = 0.0; i < 5.0; ++i) { + vec4 rand = texture(utilTex, vec3((gl_FragCoord.xy + i) / LUT_SIZE, 2.0)); + + float tmp = sqrt( rand.x / (1.0 - rand.x) ); + float x = (ax > ay ? ax : 0.0) * tmp * rand.z; + float y = (ay > ax ? ay : 0.0) * tmp * rand.w; + vec3 Ht = normalize(vec3(x, y, 1.0)); + N = tangent_to_world(Ht, N, Y, X); + + if (dot(N, cameraVec) > 0) { + surface_color.rgb += eevee_surface_clearcoat_lit(N, diffuse.rgb, f0.rgb, sqrt(min(ax, ay)), CN, clearcoat, clearcoat_roughness, 1.0); + surface_color.a += 1.0; + } + } + result = vec4(surface_color.rgb / surface_color.a, 1.0); +#else + result = vec4(eevee_surface_clearcoat_lit(N, diffuse.rgb, f0.rgb, sqrt(min(ax, ay)), CN, clearcoat * 0.5, clearcoat_roughness, 1.0), 1.0); +#endif - result = vec4(surface_color, 1.0); #else /* ambient light */ // TODO: set ambient light to an appropriate value @@ -2676,30 +2709,6 @@ void node_bsdf_principled(vec4 base_color, float subsurface, vec3 subsurface_rad /* set the viewing vector */ vec3 V = (ProjectionMatrix[3][3] == 0.0) ? -normalize(I) : vec3(0.0, 0.0, 1.0); - /* get the tangent */ - vec3 Tangent = T; - if (T == vec3(0.0)) { - // if no tangent is set, use a default tangent - Tangent = vec3(1.0, 0.0, 0.0); - if (N.x != 0.0 || N.y != 0.0) { - vec3 N_xz = normalize(vec3(N.x, 0.0, N.z)); - - vec3 axis = normalize(cross(vec3(0.0, 0.0, 1.0), N_xz)); - float angle = acos(dot(vec3(0.0, 0.0, 1.0), N_xz)); - - Tangent = normalize(rotate_vector(vec3(1.0, 0.0, 0.0), axis, angle)); - } - } - - /* rotate tangent */ - if (anisotropic_rotation != 0.0) { - Tangent = rotate_vector(Tangent, N, anisotropic_rotation * 2.0 * M_PI); - } - - /* calculate the tangent and bitangent */ - vec3 Y = normalize(cross(N, Tangent)); - vec3 X = cross(Y, N); - /* fresnel normalization parameters */ float F0 = fresnel_dielectric_0(eta); float F0_norm = 1.0 / (1.0 - F0); @@ -2742,10 +2751,6 @@ void node_bsdf_principled(vec4 base_color, float subsurface, vec3 subsurface_rad float ss = 1.25 * (Fss * (1.0 / (NdotL + NdotV) - 0.5) + 0.5); // specular - float aspect = sqrt(1.0 - anisotropic * 0.9); - float a = sqr(roughness); - float ax = max(0.001, a / aspect); - float ay = max(0.001, a * aspect); float Ds = GTR2_aniso(NdotH, dot(H, X), dot(H, Y), ax, ay); //GTR2(NdotH, a); float FH = (fresnel_dielectric_cos(LdotH, eta) - F0) * F0_norm; vec3 Fs = mix(Cspec0, vec3(1.0), FH); diff --git a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.c b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.c index f34c0e603ac..c3940573a29 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.c +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.c @@ -89,6 +89,15 @@ static int node_shader_gpu_bsdf_principled(GPUMaterial *mat, bNode *UNUSED(node) GPU_link(mat, "world_normals_get", &in[18].link); } + /* Tangents */ + if (!in[19].link) { + GPUNodeLink *orco = GPU_attribute(CD_ORCO, ""); + GPU_link(mat, "tangent_orco_z", orco, &in[19].link); + GPU_link(mat, "node_tangent", + GPU_builtin(GPU_VIEW_NORMAL), in[19].link, GPU_builtin(GPU_OBJECT_MATRIX), GPU_builtin(GPU_INVERSE_VIEW_MATRIX), + &in[19].link); + } + return GPU_stack_link(mat, "node_bsdf_principled", in, out, GPU_builtin(GPU_VIEW_POSITION)); } |