[Bf-blender-cvs] [98e4d548a16] blender2.8: Eevee: Hair: Make SSR works with hairs.

Clément Foucault noreply at git.blender.org
Sat Jun 2 21:22:41 CEST 2018


Commit: 98e4d548a16c6533eadbe619f78cf1dda1a5c7a4
Author: Clément Foucault
Date:   Sat Jun 2 13:02:13 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB98e4d548a16c6533eadbe619f78cf1dda1a5c7a4

Eevee: Hair: Make SSR works with hairs.

===================================================================

M	source/blender/draw/engines/eevee/eevee_materials.c
M	source/blender/draw/engines/eevee/shaders/default_frag.glsl
M	source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl
M	source/blender/gpu/shaders/gpu_shader_material.glsl

===================================================================

diff --git a/source/blender/draw/engines/eevee/eevee_materials.c b/source/blender/draw/engines/eevee/eevee_materials.c
index c4eea12cc03..bad8178e8ab 100644
--- a/source/blender/draw/engines/eevee/eevee_materials.c
+++ b/source/blender/draw/engines/eevee/eevee_materials.c
@@ -896,7 +896,7 @@ static struct DRWShadingGroup *EEVEE_default_shading_group_get(
 		DRWShadingGroup *shgrp = DRW_shgroup_hair_create(ob, psys, md,
 		                                                 vedata->psl->default_pass[options], vedata->psl->hair_tf_pass,
 		                                                 e_data.default_lit[options]);
-		add_standard_uniforms(shgrp, sldata, vedata, NULL, NULL, false, false);
+		add_standard_uniforms(shgrp, sldata, vedata, &ssr_id, NULL, false, false);
 		return shgrp;
 	}
 	else {
@@ -1584,6 +1584,8 @@ void EEVEE_hair_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata,
 	const DRWContextState *draw_ctx = DRW_context_state_get();
 	Scene *scene = draw_ctx->scene;
 
+	bool use_ssr = ((stl->effects->enabled_effects & EFFECT_SSR) != 0);
+
 	if (ob->type == OB_MESH) {
 		if (ob != draw_ctx->object_edit) {
 			for (ModifierData *md = ob->modifiers.first; md; md = md->next) {
@@ -1628,6 +1630,8 @@ void EEVEE_hair_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata,
 
 				shgrp = NULL;
 				if (ma->use_nodes && ma->nodetree) {
+					static int ssr_id;
+					ssr_id = (use_ssr) ? 1 : -1;
 					static float half = 0.5f;
 					static float error_col[3] = {1.0f, 0.0f, 1.0f};
 					static float compile_col[3] = {0.5f, 0.5f, 0.5f};
@@ -1640,7 +1644,7 @@ void EEVEE_hair_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata,
 							        ob, psys, md,
 							        psl->material_pass, psl->hair_tf_pass,
 							        gpumat);
-							add_standard_uniforms(shgrp, sldata, vedata, NULL, NULL, false, false);
+							add_standard_uniforms(shgrp, sldata, vedata, &ssr_id, NULL, false, false);
 							break;
 						}
 						case GPU_MAT_QUEUED:
@@ -1660,7 +1664,6 @@ void EEVEE_hair_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata,
 
 				/* Fallback to default shader */
 				if (shgrp == NULL) {
-					bool use_ssr = ((stl->effects->enabled_effects & EFFECT_SSR) != 0);
 					shgrp = EEVEE_default_shading_group_get(sldata, vedata,
 					                                        ob, psys, md,
 					                                        true, false, use_ssr,
diff --git a/source/blender/draw/engines/eevee/shaders/default_frag.glsl b/source/blender/draw/engines/eevee/shaders/default_frag.glsl
index fd1a10de548..c5dbf29a3f4 100644
--- a/source/blender/draw/engines/eevee/shaders/default_frag.glsl
+++ b/source/blender/draw/engines/eevee/shaders/default_frag.glsl
@@ -6,14 +6,38 @@ uniform float roughness;
 
 Closure nodetree_exec(void)
 {
+#ifdef HAIR_SHADER
+	vec3 B = normalize(cross(worldNormal, hairTangent));
+	float cos_theta;
+	if (hairThicknessRes == 1) {
+		vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0);
+		/* Random cosine normal distribution on the hair surface. */
+		cos_theta = rand.x * 2.0 - 1.0;
+	}
+	else {
+		/* Shade as a cylinder. */
+		cos_theta = hairThickTime / hairThickness;
+	}
+	float sin_theta = sqrt(max(0.0, 1.0f - cos_theta*cos_theta));;
+	vec3 N = normalize(worldNormal * sin_theta + B * cos_theta);
+	vec3 vN = mat3(ViewMatrix) * N;
+#else
+	vec3 N = normalize(gl_FrontFacing ? worldNormal : -worldNormal);
+	vec3 vN = normalize(gl_FrontFacing ? viewNormal : -viewNormal);
+#endif
+
 	vec3 dielectric = vec3(0.034) * specular * 2.0;
 	vec3 albedo = mix(basecol, vec3(0.0), metallic);
 	vec3 f0 = mix(dielectric, basecol, metallic);
-	vec3 N = (gl_FrontFacing) ? worldNormal : -worldNormal;
 	vec3 out_diff, out_spec, ssr_spec;
 	eevee_closure_default(N, albedo, f0, 1, roughness, 1.0, out_diff, out_spec, ssr_spec);
 
-	Closure result = Closure(out_spec + out_diff * albedo, 1.0, vec4(ssr_spec, roughness), normal_encode(normalize(viewNormal), viewCameraVec), 0);
+	Closure result = Closure(
+	        out_spec + out_diff * albedo,
+	        1.0,
+	        vec4(ssr_spec, roughness),
+	        normal_encode(vN, viewCameraVec),
+	        0);
 
 #ifdef LOOKDEV
 	gl_FragDepth = 0.0;
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 daeef1dbf01..4e65834e528 100644
--- a/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl
+++ b/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl
@@ -178,21 +178,6 @@ void CLOSURE_NAME(
 
 	vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0);
 
-#ifdef HAIR_SHADER
-	vec3 B = normalize(cross(worldNormal, hairTangent));
-	float cos_theta;
-	if (hairThicknessRes == 1) {
-		/* Random cosine normal distribution on the hair surface. */
-		cos_theta = rand.x * 2.0 - 1.0;
-	}
-	else {
-		/* Shade as a cylinder. */
-		cos_theta = hairThickTime / hairThickness;
-	}
-	float sin_theta = sqrt(max(0.0, 1.0f - cos_theta*cos_theta));;
-	N = normalize(N * sin_theta + B * cos_theta);
-#endif
-
 	/* ---------------------------------------------------------------- */
 	/* -------------------- SCENE LAMPS LIGHTING ---------------------- */
 	/* ---------------------------------------------------------------- */
@@ -399,12 +384,6 @@ void CLOSURE_NAME(
 	}
 
 	out_spec += spec_accum.rgb * ssr_spec * spec_occlu * float(specToggle);
-
-#  ifdef HAIR_SHADER
-	/* Hack: Overide spec color so that ssr will not be computed
-	 * even if ssr_id match the active ssr. */
-	ssr_spec = vec3(0.0);
-#  endif
 #endif
 
 #ifdef CLOSURE_REFRACTION
diff --git a/source/blender/gpu/shaders/gpu_shader_material.glsl b/source/blender/gpu/shaders/gpu_shader_material.glsl
index 6f253a48f86..4c77dd038af 100644
--- a/source/blender/gpu/shaders/gpu_shader_material.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_material.glsl
@@ -2558,7 +2558,23 @@ void node_output_world(Closure surface, Closure volume, out Closure result)
 /* EEVEE output */
 void world_normals_get(out vec3 N)
 {
+#ifdef HAIR_SHADER
+	vec3 B = normalize(cross(worldNormal, hairTangent));
+	float cos_theta;
+	if (hairThicknessRes == 1) {
+		vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0);
+		/* Random cosine normal distribution on the hair surface. */
+		cos_theta = rand.x * 2.0 - 1.0;
+	}
+	else {
+		/* Shade as a cylinder. */
+		cos_theta = hairThickTime / hairThickness;
+	}
+	float sin_theta = sqrt(max(0.0, 1.0f - cos_theta*cos_theta));;
+	N = normalize(worldNormal * sin_theta + B * cos_theta);
+#else
 	N = gl_FrontFacing ? worldNormal : -worldNormal;
+#endif
 }
 
 void node_eevee_specular(



More information about the Bf-blender-cvs mailing list