[Bf-blender-cvs] [1ceeac2cab] clay-engine: Use overall matcap colors as AO color avoiding completelly dark areas.

Clément Foucault noreply at git.blender.org
Tue Jan 10 23:59:57 CET 2017


Commit: 1ceeac2cab8c1ead7edb79aeea49433cb99d291e
Author: Clément Foucault
Date:   Tue Jan 10 22:03:57 2017 +0100
Branches: clay-engine
https://developer.blender.org/rB1ceeac2cab8c1ead7edb79aeea49433cb99d291e

Use overall matcap colors as AO color avoiding completelly dark areas.

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

M	source/blender/draw/engines/clay/clay.c
M	source/blender/draw/engines/clay/shaders/clay_frag.glsl

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

diff --git a/source/blender/draw/engines/clay/clay.c b/source/blender/draw/engines/clay/clay.c
index c28c569330..f903e55cf3 100644
--- a/source/blender/draw/engines/clay/clay.c
+++ b/source/blender/draw/engines/clay/clay.c
@@ -50,6 +50,7 @@ static struct CLAY_data {
 
 	/* Matcap textures */
 	struct GPUTexture *matcap_array;
+	float matcap_colors[24][3];
 	int matcap_id;
 
 	/* Ssao */
@@ -99,6 +100,19 @@ static void add_icon_to_rect(PreviewImage *prv, float *final_rect, int layer)
 
 	IMB_buffer_float_from_byte(new_rect, (unsigned char *)prv->rect[0], IB_PROFILE_SRGB, IB_PROFILE_SRGB,
 	                           false, prv->w[0], prv->h[0], prv->w[0], prv->w[0]);
+
+	/* Find overall color */
+	for (int y = 0; y < 4; ++y)	{
+		for (int x = 0; x < 4; ++x) {
+			data.matcap_colors[layer][0] += new_rect[y * 512 * 128 * 4 + x * 128 * 4 + 0];
+			data.matcap_colors[layer][1] += new_rect[y * 512 * 128 * 4 + x * 128 * 4 + 1];
+			data.matcap_colors[layer][2] += new_rect[y * 512 * 128 * 4 + x * 128 * 4 + 2];
+		}
+	}
+
+	data.matcap_colors[layer][0] /= 16.0f;
+	data.matcap_colors[layer][1] /= 16.0f;
+	data.matcap_colors[layer][2] /= 16.0f;
 }
 
 static struct GPUTexture *load_matcaps(PreviewImage *prv[24], int nbr)
@@ -283,6 +297,8 @@ static void clay_populate_passes(CLAY_PassList *passes, const struct bContext *C
 		DRW_batch_uniform_ivec2(batch, "screenres", DRW_viewport_size_get(), 1);
 		DRW_batch_uniform_buffer(batch, "depthtex", SCENE_DEPTH, depthloc);
 		DRW_batch_uniform_texture(batch, "matcaps", data.matcap_array, matcaploc);
+		DRW_batch_uniform_vec3(batch, "matcaps_color", (float *)data.matcap_colors, 24);
+		DRW_batch_uniform_vec3(batch, "matcaps_color", (float *)data.matcap_colors, 24);
 		DRW_batch_uniform_int(batch, "matcap_index", &data.matcap_id, 1);
 
 		/* SSAO */
diff --git a/source/blender/draw/engines/clay/shaders/clay_frag.glsl b/source/blender/draw/engines/clay/shaders/clay_frag.glsl
index 1321d4bd84..f40fe63db2 100644
--- a/source/blender/draw/engines/clay/shaders/clay_frag.glsl
+++ b/source/blender/draw/engines/clay/shaders/clay_frag.glsl
@@ -6,6 +6,7 @@ uniform mat4 WinMatrix;
 uniform sampler2DArray matcaps;
 uniform int matcap_index;
 uniform vec2 matcap_rotation;
+uniform vec3 matcaps_color[24];
 uniform float matcap_hue;
 
 /* Screen Space Occlusion */
@@ -57,7 +58,7 @@ vec3 calculate_view_space_normal(in vec3 viewposition)
 	return normalize(normal);
 }
 
-float calculate_ssao_factor(float depth, vec3 normal, vec3 position)
+void calculate_ssao_factor(in float depth, in vec3 normal, in vec3 position, out float cavities, out float edges)
 {
 	vec2 uvs = vec2(gl_FragCoord.xy) / vec2(screenres);
 
@@ -65,10 +66,6 @@ float calculate_ssao_factor(float depth, vec3 normal, vec3 position)
 	vec2 rotX = texture2D(ssao_jitter, uvs.xy * jitter_tilling).rg;
 	vec2 rotY = vec2(-rotX.y, rotX.x);
 
-	/* occlusion is zero in full depth */
-	if (depth == 1.0)
-		return 1.0;
-
 	/* find the offset in screen space by multiplying a point
 	 * in camera space at the depth of the point by the projection matrix. */
 	vec2 offset;
@@ -78,8 +75,7 @@ float calculate_ssao_factor(float depth, vec3 normal, vec3 position)
 	/* convert from -1.0...1.0 range to 0.0..1.0 for easy use with texture coordinates */
 	offset *= 0.5;
 
-	float obscurance_cavity = 0.0;
-	float obscurance_edge = 0.0;
+	cavities = edges = 0.0;
 	int x;
 	int num_samples = int(ssao_samples_num);
 
@@ -100,28 +96,27 @@ float calculate_ssao_factor(float depth, vec3 normal, vec3 position)
 			vec3 pos_new = get_view_space_from_depth(uvcoords, viewvecs[0].xyz, viewvecs[1].xyz, depth_new);
 			vec3 dir = pos_new - position;
 			float len = length(dir);
-			float f_cavity = dot(dir, normal);
+			float f_cavities = dot(dir, normal);
 			float f_edge = dot(dir, -normal);
 			float f_bias = 0.05 * len + 0.0001;
 
 			float attenuation = 1.0 / (len * (1.0 + len * len * ssao_attenuation));
 
 			/* use minor bias here to avoid self shadowing */
-			if (f_cavity > f_bias)
-				obscurance_cavity += f_cavity * attenuation;
+			if (f_cavities > f_bias)
+				cavities += f_cavities * attenuation;
 
 			if (f_edge > f_bias)
-				obscurance_edge += f_edge * attenuation;
+				edges += f_edge * attenuation;
 		}
 	}
 
-	obscurance_cavity /= ssao_samples_num;
-	obscurance_edge /= ssao_samples_num;
-
-	obscurance_cavity = clamp(obscurance_cavity * ssao_factor_cavity, 0.0, 1.0);
-	obscurance_edge = clamp(obscurance_edge * ssao_factor_edge, 0.0, 1.0);
+	cavities /= ssao_samples_num;
+	edges /= ssao_samples_num;
 
-	return 1.0 - obscurance_cavity + obscurance_edge;
+	/* don't let cavity wash out the surface appearance */
+	cavities = clamp(cavities * ssao_factor_cavity, 0.0, 0.85);
+	edges = edges * ssao_factor_edge;
 }
 
 
@@ -144,11 +139,15 @@ void main() {
 	vec2 rotY = vec2(-matcap_roation.y, matcap_roation.x);
 	texco = vec2(dot(texco, matcap_roation), dot(texco, rotY));
 #endif
-	vec4 col = texture(matcaps, vec3(texco, float(matcap_index)));
+	vec3 col = texture(matcaps, vec3(texco, float(matcap_index))).rgb;
 
 #ifdef USE_AO
-	col *= calculate_ssao_factor(depth, normal, position);
+	float cavity, edges;
+	calculate_ssao_factor(depth, normal, position, cavity, edges);
+
+	col = mix(col, matcaps_color[int(matcap_index)] * 0.5, cavity);
+	col *= edges + 1.0;
 #endif
 
-	fragColor = vec4(col.rgb, 1.0);
+	fragColor = vec4(col, 1.0);
 }
\ No newline at end of file




More information about the Bf-blender-cvs mailing list