[Bf-blender-cvs] [01cec3e0c52] blender2.8: Armature: Envelope Bones: Change drawing method.

Clément Foucault noreply at git.blender.org
Wed May 2 20:54:45 CEST 2018


Commit: 01cec3e0c52184f288a9af28b67d09965ebb0b03
Author: Clément Foucault
Date:   Sun Apr 29 19:39:44 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB01cec3e0c52184f288a9af28b67d09965ebb0b03

Armature: Envelope Bones: Change drawing method.

We now use a more pleasant and efficient way to display enveloppe bones
and their radius.

For this we use a capsule geometry that is displaced (in the vertex shader)
to a signed distance field that represents the bone shape.

The bone distance radius are now drawn in 3D using a "pseudo-fresnel" effect.
This gives a better understanding of what is inside the radius of influence.

When capsules are not needed, we switch to default raytraced points.
The capsules are not distorded by the bone's matrix (same as their actual
influence radius) and are correctly displayed even with complex scaled
parents hierarchy.

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

M	source/blender/draw/CMakeLists.txt
M	source/blender/draw/intern/draw_armature.c
M	source/blender/draw/intern/draw_cache.c
M	source/blender/draw/intern/draw_cache.h
M	source/blender/draw/intern/draw_common.c
M	source/blender/draw/intern/draw_common.h
M	source/blender/draw/modes/edit_armature_mode.c
M	source/blender/draw/modes/object_mode.c
M	source/blender/draw/modes/pose_mode.c
A	source/blender/draw/modes/shaders/armature_envelope_frag.glsl
A	source/blender/draw/modes/shaders/armature_envelope_vert.glsl

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

diff --git a/source/blender/draw/CMakeLists.txt b/source/blender/draw/CMakeLists.txt
index 3ab0ea40990..3ccbf967b87 100644
--- a/source/blender/draw/CMakeLists.txt
+++ b/source/blender/draw/CMakeLists.txt
@@ -224,6 +224,8 @@ data_to_c_simple(modes/shaders/common_fullscreen_vert.glsl SRC)
 data_to_c_simple(modes/shaders/armature_sphere_vert.glsl SRC)
 data_to_c_simple(modes/shaders/armature_sphere_frag.glsl SRC)
 data_to_c_simple(modes/shaders/armature_sphere_outline_vert.glsl SRC)
+data_to_c_simple(modes/shaders/armature_envelope_vert.glsl SRC)
+data_to_c_simple(modes/shaders/armature_envelope_frag.glsl SRC)
 data_to_c_simple(modes/shaders/armature_shape_outline_vert.glsl SRC)
 data_to_c_simple(modes/shaders/armature_shape_outline_geom.glsl SRC)
 data_to_c_simple(modes/shaders/edit_mesh_overlay_frag.glsl SRC)
diff --git a/source/blender/draw/intern/draw_armature.c b/source/blender/draw/intern/draw_armature.c
index 756cc3e6992..6b685f07de1 100644
--- a/source/blender/draw/intern/draw_armature.c
+++ b/source/blender/draw/intern/draw_armature.c
@@ -95,6 +95,9 @@ static struct {
 	DRWPass *pass_bone_envelope;
 } g_data = {NULL};
 
+/* Prototype */
+static void drw_shgroup_bone_point_solid(const float (*bone_mat)[4], const float color[4]);
+
 /* -------------------------------------------------------------------- */
 
 /** \name Shader Groups (DRW_shgroup)
@@ -171,13 +174,22 @@ static void drw_shgroup_bone_envelope_distance(
 {
 	if (g_data.pass_bone_envelope != NULL) {
 		if (g_data.bone_envelope_distance == NULL) {
-			struct Gwn_Batch *geom = DRW_cache_bone_envelope_distance_outline_get();
-			/* Note: bone_wire draw pass is not really working, think we need another one here? */
-			g_data.bone_envelope_distance = shgroup_instance_bone_envelope_wire(g_data.pass_bone_envelope, geom);
+			g_data.bone_envelope_distance = shgroup_instance_bone_envelope_solid(g_data.pass_bone_envelope);
+			/* pass_bone_envelope should have the DRW_STATE_CULL_FRONT state enabled. */
 		}
+		float head_sphere[4] = {0.0f, 0.0f, 0.0f, 1.0f}, tail_sphere[4] = {0.0f, 1.0f, 0.0f, 1.0f};
 		float final_bonemat[4][4];
 		mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
-		DRW_shgroup_call_dynamic_add(g_data.bone_envelope_distance, final_bonemat, color, radius_head, radius_tail, distance);
+		/* We need matrix mul because we need shear applied. */
+		/* NOTE: could be done in shader if that becomes a bottleneck. */
+		mul_m4_v4(final_bonemat, head_sphere);
+		mul_m4_v4(final_bonemat, tail_sphere);
+		head_sphere[3]  = *radius_head;
+		head_sphere[3] += *distance;
+		tail_sphere[3]  = *radius_tail;
+		tail_sphere[3] += *distance;
+
+		DRW_shgroup_call_dynamic_add(g_data.bone_envelope_distance, head_sphere, tail_sphere, color, final_bonemat[0]);
 	}
 }
 
@@ -186,12 +198,63 @@ static void drw_shgroup_bone_envelope_solid(
         const float *radius_head, const float *radius_tail)
 {
 	if (g_data.bone_envelope_solid == NULL) {
-		struct Gwn_Batch *geom = DRW_cache_bone_envelope_solid_get();
-		g_data.bone_envelope_solid = shgroup_instance_bone_envelope_solid(g_data.pass_bone_solid, geom);
+		g_data.bone_envelope_solid = shgroup_instance_bone_envelope_solid(g_data.pass_bone_solid);
+		/* We can have a lot of overdraw if we don't do this. Also envelope are not subject to
+		 * inverted matrix. */
+		DRW_shgroup_state_enable(g_data.bone_envelope_solid, DRW_STATE_CULL_BACK);
+	}
+	if (g_data.bone_point_solid == NULL) {
+		g_data.bone_point_solid = shgroup_instance_armature_sphere(g_data.pass_bone_solid);
 	}
+
+	float head_sphere[4] = {0.0f, 0.0f, 0.0f, 1.0f}, tail_sphere[4] = {0.0f, 1.0f, 0.0f, 1.0f};
 	float final_bonemat[4][4];
 	mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
-	DRW_shgroup_call_dynamic_add(g_data.bone_envelope_solid, final_bonemat, color, radius_head, radius_tail);
+	mul_m4_v4(final_bonemat, head_sphere);
+	mul_m4_v4(final_bonemat, tail_sphere);
+	head_sphere[3] = *radius_head;
+	tail_sphere[3] = *radius_tail;
+
+	if (head_sphere[3] < 0.0f) {
+		/* Draw Tail only */
+		float tmp[4][4] = {{0.0f}};
+		tmp[0][0] = tmp[1][1] = tmp[2][2] = tail_sphere[3] / 0.05f;
+		tmp[3][3] = 1.0f;
+		copy_v3_v3(tmp[3], tail_sphere);
+		DRW_shgroup_call_dynamic_add(g_data.bone_point_solid, tmp, color);
+	}
+	else if (tail_sphere[3] < 0.0f) {
+		/* Draw Head only */
+		float tmp[4][4] = {{0.0f}};
+		tmp[0][0] = tmp[1][1] = tmp[2][2] = head_sphere[3] / 0.05f;
+		tmp[3][3] = 1.0f;
+		copy_v3_v3(tmp[3], head_sphere);
+		DRW_shgroup_call_dynamic_add(g_data.bone_point_solid, tmp, color);
+	}
+	else {
+		/* Draw Body */
+		float tmp_sphere[4];
+		float len = len_v3v3(tail_sphere, head_sphere);
+		float fac_head = (len - head_sphere[3]) / len;
+		float fac_tail = (len - tail_sphere[3]) / len;
+
+		/* Small epsilon to avoid problem with float precison in shader. */
+		if (len > (tail_sphere[3] + head_sphere[3]) + 1e-8f) {
+			copy_v4_v4(tmp_sphere, head_sphere);
+			interp_v4_v4v4(head_sphere, tail_sphere, head_sphere, fac_head);
+			interp_v4_v4v4(tail_sphere, tmp_sphere,  tail_sphere, fac_tail);
+			DRW_shgroup_call_dynamic_add(g_data.bone_envelope_solid, head_sphere, tail_sphere, color, final_bonemat[0]);
+		}
+		else {
+			float tmp[4][4] = {{0.0f}};
+			float fac = max_ff(fac_head, 1.0f - fac_tail);
+			interp_v4_v4v4(tmp_sphere, tail_sphere, head_sphere, clamp_f(fac, 0.0f, 1.0f));
+			tmp[0][0] = tmp[1][1] = tmp[2][2] = tmp_sphere[3] / 0.05f;
+			tmp[3][3] = 1.0f;
+			copy_v3_v3(tmp[3], tmp_sphere);
+			DRW_shgroup_call_dynamic_add(g_data.bone_point_solid, tmp, color);
+		}
+	}
 }
 
 static void drw_shgroup_bone_envelope_wire(
diff --git a/source/blender/draw/intern/draw_cache.c b/source/blender/draw/intern/draw_cache.c
index 1dd588afc9f..1b41ff311a8 100644
--- a/source/blender/draw/intern/draw_cache.c
+++ b/source/blender/draw/intern/draw_cache.c
@@ -1878,43 +1878,24 @@ Gwn_Batch *DRW_cache_bone_wire_wire_outline_get(void)
 	return SHC.drw_bone_wire_wire;
 }
 
-
 /* Helpers for envelope bone's solid sphere-with-hidden-equatorial-cylinder.
  * Note that here we only encode head/tail in forth component of the vector. */
 static void benv_lat_lon_to_co(const float lat, const float lon, float r_nor[3])
 {
 	/* Poles are along Y axis. */
 	r_nor[0] = sinf(lat) * cosf(lon);
-	r_nor[1] = cosf(lat);
+	r_nor[1] = -cosf(lat);
 	r_nor[2] = sinf(lat) * sinf(lon);
 }
 
-static void benv_add_tri(Gwn_VertBuf *vbo, uint pos_id, uint *v_idx, float *co1, float *co2, float *co3)
-{
-	/* Given tri and its seven other mirrors along X/Y/Z axes. */
-	for (int x = -1; x <= 1; x += 2) {
-		for (int y = -1; y <= 1; y += 2) {
-			const float head_tail = (y == -1) ? 0.0f : 1.0f;
-			for (int z = -1; z <= 1; z += 2) {
-				GWN_vertbuf_attr_set(vbo, pos_id, (*v_idx)++,
-				                     (const float[4]){co1[0] * x, co1[1] * y, co1[2] * z, head_tail});
-				GWN_vertbuf_attr_set(vbo, pos_id, (*v_idx)++,
-				                     (const float[4]){co2[0] * x, co2[1] * y, co2[2] * z, head_tail});
-				GWN_vertbuf_attr_set(vbo, pos_id, (*v_idx)++,
-				                     (const float[4]){co3[0] * x, co3[1] * y, co3[2] * z, head_tail});
-			}
-		}
-	}
-}
-
 Gwn_Batch *DRW_cache_bone_envelope_solid_get(void)
 {
-#define CIRCLE_RESOL 32  /* Must be multiple of 4 */
 	if (!SHC.drw_bone_envelope) {
-		const int lon_res = CIRCLE_RESOL / 4;
-		const int lat_res = CIRCLE_RESOL / 4;
-		const float lon_inc = M_PI_2 / lon_res;
-		const float lat_inc = M_PI_2 / lat_res;
+		const int lon_res = 24;
+		const int lat_res = 16;
+		const float lon_inc = 2.0f * M_PI / lon_res;
+		const float lat_inc = M_PI / lat_res;
+		const float eps = 0.02f;
 		unsigned int v_idx = 0;
 
 		static Gwn_VertFormat format = { 0 };
@@ -1925,92 +1906,59 @@ Gwn_Batch *DRW_cache_bone_envelope_solid_get(void)
 
 		/* Vertices */
 		Gwn_VertBuf *vbo = GWN_vertbuf_create_with_format(&format);
-		GWN_vertbuf_data_alloc(vbo, lat_res * lon_res * 8 * 6);
+		GWN_vertbuf_data_alloc(vbo, ((lat_res + 1) * 2) * lon_res * 2);
 
-		float lon = 0.0f;
+		float lon = lon_inc;
 		for (int i = 0; i < lon_res; i++, lon += lon_inc) {
 			float lat = 0.0f;
-			float co1[3], co2[3], co3[3], co4[3];
+			float co1[4], co2[4];
+			co1[3] = co2[3] = 0.0f;
 
+			/* 1st sphere */
 			for (int j = 0; j < lat_res; j++, lat += lat_inc) {
-				benv_lat_lon_to_co(lat,           lon,           co1);
-				benv_lat_lon_to_co(lat,           lon + lon_inc, co2);
-				benv_lat_lon_to_co(lat + lat_inc, lon + lon_inc, co3);
-				benv_lat_lon_to_co(lat + lat_inc, lon,           co4);
+				benv_lat_lon_to_co(lat, lon,           co1);
+				benv_lat_lon_to_co(lat, lon + lon_inc, co2);
 
-				if (j != 0) {  /* At pole, n1 and n2 are identical. */
-					benv_add_tri(vbo, attr_id.pos, &v_idx, co1, co2, co3);
-				}
-				benv_add_tri(vbo, attr_id.pos, &v_idx, co1, co3, co4);
+				GWN_vertbuf_attr_set(vbo, attr_id.pos, v_idx++, co1);
+				GWN_vertbuf_attr_set(vbo, attr_id.pos, v_idx++, co2);
 			}
-
-			/* lat is at equator (i.e. lat == pi / 2). */
-			/* We need to add 'cylinder' part between the equators (along XZ plane). */
-			for (int x = -1; x <= 1; x += 2) {
-				for (int z = -1; z <= 1; z += 2) {
-					GWN_vertbuf_attr_set(vbo, attr_id.pos, v_idx++,
-					                     (const float[4]){co3[0] * x, co3[1], co3[2] * z, 0.0f});
-					GWN_vertbuf_attr_set(vbo, attr_id.pos, v_idx++,
-					                     (const float[4]){co4[0] * x, co4[1], co4[2] * z, 0.0f});
-					GWN_vertbuf_attr_set(vbo, attr_id.pos, v_idx++,
-					                     (const float[4]){co4[0] * x, co4[1], co4[2] * z, 1.0f});
-
-					GWN_vertbuf_attr_set(vbo, attr_id.pos, v_idx++,
-					                     (const float[4]){co3[0] * x, co3[1], co3[2] * z, 0.0f});
-					GWN_vertbuf_attr_set(vbo, attr_id.pos, v_idx++,
-					                     (const float[4]){co4[0] * x, co4[1], co4[2] * z, 1.0f});
-					GWN_vertbuf_attr_set(vbo, attr_id.pos, v_idx++,
-					                     (const float[4]){co3[0] * x, co3[1], co3[2] * z, 1.0f});
-				}
+			/* Need to close the sphere, but add a small gap to be able
+			 * to distinguish the verts in the vertex shader. */
+			benv_lat_lon_to_co(M_PI - eps, lon,           co1);
+			benv_lat_lon_to_co(M_PI - eps, lon + lon_inc, co2);
+			GWN_vertbuf_attr_set(vbo, at

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list