From 0c4ce8e55e93c76c81f70db64dc8dee52b30a9f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Fri, 17 May 2019 18:17:14 +0200 Subject: Eevee / Workbench: Fix hair normals Hair normals were not behaving correctly. This corrects their looks and fix the node shader geometry that was showing the flat normal. --- .../blender/draw/engines/eevee/shaders/lit_surface_vert.glsl | 4 +--- .../engines/workbench/shaders/workbench_prepass_vert.glsl | 11 +++++------ source/blender/draw/modes/shaders/common_hair_lib.glsl | 4 ++-- source/blender/gpu/shaders/gpu_shader_material.glsl | 2 +- source/blender/nodes/shader/nodes/node_shader_geometry.c | 2 +- 5 files changed, 10 insertions(+), 13 deletions(-) (limited to 'source') diff --git a/source/blender/draw/engines/eevee/shaders/lit_surface_vert.glsl b/source/blender/draw/engines/eevee/shaders/lit_surface_vert.glsl index 0a2785bafca..2b9a325e313 100644 --- a/source/blender/draw/engines/eevee/shaders/lit_surface_vert.glsl +++ b/source/blender/draw/engines/eevee/shaders/lit_surface_vert.glsl @@ -47,9 +47,7 @@ void main() hairTime, hairThickness, hairThickTime); - - hairTangent = normalize(hairTangent); - worldNormal = cross(binor, hairTangent); + worldNormal = cross(hairTangent, binor); worldPosition = pos; #else worldPosition = point_object_to_world(pos); diff --git a/source/blender/draw/engines/workbench/shaders/workbench_prepass_vert.glsl b/source/blender/draw/engines/workbench/shaders/workbench_prepass_vert.glsl index 5d4153999c0..7872c1380ed 100644 --- a/source/blender/draw/engines/workbench/shaders/workbench_prepass_vert.glsl +++ b/source/blender/draw/engines/workbench/shaders/workbench_prepass_vert.glsl @@ -46,12 +46,11 @@ vec3 srgb_to_linear_attr(vec3 c) vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) { /* To "simulate" anisotropic shading, randomize hair normal per strand. */ - tan = normalize(tan); - vec3 nor = normalize(cross(binor, tan)); - // nor = normalize(mix(nor, -tan, rand * 0.1)); - // float cos_theta = (rand * 2.0 - 1.0) * 0.2; - // float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); - // nor = nor * sin_theta + binor * cos_theta; + vec3 nor = cross(tan, binor); + nor = normalize(mix(nor, -tan, rand * 0.1)); + float cos_theta = (rand * 2.0 - 1.0) * 0.2; + float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); + nor = nor * sin_theta + binor * cos_theta; return nor; } diff --git a/source/blender/draw/modes/shaders/common_hair_lib.glsl b/source/blender/draw/modes/shaders/common_hair_lib.glsl index 1c0a31c59fd..f9c3df34658 100644 --- a/source/blender/draw/modes/shaders/common_hair_lib.glsl +++ b/source/blender/draw/modes/shaders/common_hair_lib.glsl @@ -160,9 +160,9 @@ void hair_get_pos_tan_binor_time(bool is_persp, } wpos = (hairDupliMatrix * vec4(wpos, 1.0)).xyz; - wtan = mat3(hairDupliMatrix) * wtan; + wtan = -normalize(mat3(hairDupliMatrix) * wtan); - vec3 camera_vec = (is_persp) ? wpos - camera_pos : -camera_z; + vec3 camera_vec = (is_persp) ? camera_pos - wpos : camera_z; wbinor = normalize(cross(camera_vec, wtan)); thickness = hair_shaperadius(hairRadShape, hairRadRoot, hairRadTip, time); diff --git a/source/blender/gpu/shaders/gpu_shader_material.glsl b/source/blender/gpu/shaders/gpu_shader_material.glsl index 94770aa2ebf..b94a7d6cc0a 100644 --- a/source/blender/gpu/shaders/gpu_shader_material.glsl +++ b/source/blender/gpu/shaders/gpu_shader_material.glsl @@ -2038,7 +2038,7 @@ void node_geometry(vec3 I, position = worldPosition; # ifndef VOLUMETRICS - normal = normalize(gl_FrontFacing ? worldNormal : -worldNormal); + normal = normalize(N); vec3 B = dFdx(worldPosition); vec3 T = dFdy(worldPosition); true_normal = normalize(cross(B, T)); diff --git a/source/blender/nodes/shader/nodes/node_shader_geometry.c b/source/blender/nodes/shader/nodes/node_shader_geometry.c index 6f97efe9f73..df9a8ac8318 100644 --- a/source/blender/nodes/shader/nodes/node_shader_geometry.c +++ b/source/blender/nodes/shader/nodes/node_shader_geometry.c @@ -51,7 +51,7 @@ static int node_shader_gpu_geometry(GPUMaterial *mat, in, out, GPU_builtin(GPU_VIEW_POSITION), - GPU_builtin(GPU_VIEW_NORMAL), + GPU_builtin(GPU_WORLD_NORMAL), GPU_attribute(CD_ORCO, ""), GPU_builtin(GPU_OBJECT_MATRIX), GPU_builtin(GPU_INVERSE_VIEW_MATRIX), -- cgit v1.2.3