[Bf-blender-cvs] [3504b27c507] soc-2018-bevel: Patch to fix shading continuity.

Rohan Rathi noreply at git.blender.org
Thu Jun 28 04:50:13 CEST 2018


Commit: 3504b27c5074d49ed19f86c212c584f37a343d33
Author: Rohan Rathi
Date:   Wed Jun 27 20:19:15 2018 +0530
Branches: soc-2018-bevel
https://developer.blender.org/rB3504b27c5074d49ed19f86c212c584f37a343d33

Patch to fix shading continuity.

Added it as extension to harden. Tried out different methods to fix normals,
Though as with width and segments changes shape, orientation of new polys
a non-smooth method of fix was not possible. Current method aggregates
vertex normals into a smooth fan without affecting edge shading.
Still need to fix the crease at new vertex edges

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

M	source/blender/bmesh/intern/bmesh_operators.h
M	source/blender/bmesh/tools/bmesh_bevel.c
M	source/blender/makesdna/DNA_modifier_types.h
M	source/blender/makesrna/intern/rna_modifier.c
M	source/blender/modifiers/intern/MOD_bevel.c

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

diff --git a/source/blender/bmesh/intern/bmesh_operators.h b/source/blender/bmesh/intern/bmesh_operators.h
index 1b5694c3ee8..9f6ac50a3e5 100644
--- a/source/blender/bmesh/intern/bmesh_operators.h
+++ b/source/blender/bmesh/intern/bmesh_operators.h
@@ -131,6 +131,7 @@ enum {
 	BEVEL_HN_NONE,
 	BEVEL_HN_FACE,
 	BEVEL_HN_ADJ,
+	BEVEL_HN_FIX_SHA,
 };
 
 extern const BMOpDefine *bmo_opdefines[];
diff --git a/source/blender/bmesh/tools/bmesh_bevel.c b/source/blender/bmesh/tools/bmesh_bevel.c
index 186d3a3c8e0..132436c70f0 100644
--- a/source/blender/bmesh/tools/bmesh_bevel.c
+++ b/source/blender/bmesh/tools/bmesh_bevel.c
@@ -44,6 +44,7 @@
 
 #include "BKE_customdata.h"
 #include "BKE_deform.h"
+#include "BKE_mesh.h"
 
 #include "eigen_capi.h"
 
@@ -1721,6 +1722,85 @@ static void bevel_harden_normals_mode(BMesh *bm, BevelParams *bp, BevVert *bv, B
 	} while (bcur != bstart);
 }
 
+static void bevel_fix_normal_shading_continuity(BevelParams *bp, BMesh *bm, BevVert *bv, GHash *faceHash)
+{
+	VMesh *vm = bv->vmesh;
+	BoundVert *bcur = bv->vmesh->boundstart, *start = bcur;
+	int ns = vm->seg;
+	int ns2 = ns / 2;
+	int count = 0;
+
+	BMFace *f;
+	BMLoop *l;
+	BMIter liter, fiter;
+
+	int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
+
+	BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) {
+
+		if (BLI_ghash_haskey(faceHash, f)) {
+			BM_ITER_ELEM(l, &liter, f, BM_LOOPS_OF_FACE) {
+				const int l_index = BM_elem_index_get(l);
+				short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset);
+				BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], l->v->no, clnors);
+			}
+		}
+	}
+
+	do {
+		for (int i = 0; i <= ns; i++) {
+			BMVert *v1 = mesh_vert(vm, bcur->index, 0, i)->v, *v2;
+			BMEdge *e;
+			BMIter eiter;
+
+			BM_ITER_ELEM(e, &eiter, v1, BM_EDGES_OF_VERT) {
+				BMFace *f_a, *f_b;
+				BM_edge_face_pair(e, &f_a, &f_b);
+
+				bool _f_a = false, _f_b = false;
+				if (f_a)
+					_f_a = BLI_ghash_haskey(faceHash, f_a);
+				if (f_b)
+					_f_b = BLI_ghash_haskey(faceHash, f_b);
+				if (_f_a ^ _f_b) {
+
+					BM_ITER_ELEM(l, &liter, v1, BM_LOOPS_OF_VERT) {
+
+						if (l->f == f_a || l->f == f_b) {
+							const int l_index = BM_elem_index_get(l);
+							short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset);
+							float res[3];
+							copy_v3_v3(res, f_a->no);
+							add_v3_v3(res, f_b->no);
+							mul_v3_fl(res, 0.5f);
+							normalize_v3(res);
+
+							BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], res, clnors);
+						}
+					}
+				}
+			}
+
+			float res_n[3];
+			zero_v3(res_n);
+			BM_ITER_ELEM(l, &liter, v1, BM_LOOPS_OF_VERT) {
+				if (!BLI_ghash_haskey(faceHash, l->f)) {
+					add_v3_v3(res_n, l->f->no);
+				}
+			}
+			normalize_v3(res_n);
+			BM_ITER_ELEM(l, &liter, v1, BM_LOOPS_OF_VERT) {
+				if (BLI_ghash_haskey(faceHash, l->f)) {
+					const int l_index = BM_elem_index_get(l);
+					short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset);
+					BKE_lnor_space_custom_normal_to_data(bm	->lnor_spacearr->lspacearr[l_index], res_n, clnors);
+				}
+			}
+		}
+		bcur = bcur->next;
+	} while (bcur != start);
+}
+
 /* Set the any_seam property for a BevVert and all its BoundVerts */
 static void set_bound_vert_seams(BevVert *bv, bool mark_seam, bool mark_sharp)
 {
@@ -5648,6 +5728,17 @@ void BM_mesh_bevel(
 			}
 		}
 
+		GHash *faceHash;
+		if (!bm->use_toolflags && bp.hnmode == BEVEL_HN_FIX_SHA) {
+			faceHash = BLI_ghash_ptr_new(__func__);
+			BMFace *f;
+			BM_ITER_MESH(f, &iter, bm, BM_FACES_OF_MESH) {
+				if (BM_elem_flag_test(f, BM_ELEM_TAG)) {
+					BLI_ghash_insert(faceHash, f, NULL);
+				}
+			}
+		}
+
 		/* Build polygons for edges */
 		if (!bp.vertex_only) {
 			BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
@@ -5664,6 +5755,7 @@ void BM_mesh_bevel(
 				bevel_harden_normals_mode(bm, &bp, bv, op);
 		}
 
+
 		/* Rebuild face polygons around affected vertices */
 		BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
 			if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
@@ -5672,6 +5764,16 @@ void BM_mesh_bevel(
 			}
 		}
 
+		if (!bm->use_toolflags && bp.hnmode == BEVEL_HN_FIX_SHA) {
+			BM_mesh_normals_update(bm);
+			BM_lnorspace_update(bm);
+			GHASH_ITER(giter, bp.vert_hash) {
+				bv = BLI_ghashIterator_getValue(&giter);
+				if (!bm->use_toolflags)
+					bevel_fix_normal_shading_continuity(&bp, bm, bv, faceHash);
+			}
+			BLI_ghash_free(faceHash, NULL, NULL);
+		}
 
 		BM_ITER_MESH_MUTABLE (v, v_next, &iter, bm, BM_VERTS_OF_MESH) {
 			if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h
index 83f98c1181e..7ae9383a907 100644
--- a/source/blender/makesdna/DNA_modifier_types.h
+++ b/source/blender/makesdna/DNA_modifier_types.h
@@ -379,6 +379,7 @@ enum {
 	MOD_BEVEL_HN_NONE,
 	MOD_BEVEL_HN_FACE,
 	MOD_BEVEL_HN_ADJ,
+	MOD_BEVEL_FIX_SHA,
 };
 
 typedef struct SmokeModifierData {
diff --git a/source/blender/makesrna/intern/rna_modifier.c b/source/blender/makesrna/intern/rna_modifier.c
index 497125476a2..87c28115325 100644
--- a/source/blender/makesrna/intern/rna_modifier.c
+++ b/source/blender/makesrna/intern/rna_modifier.c
@@ -3028,6 +3028,7 @@ static void rna_def_modifier_bevel(BlenderRNA *brna)
 		{ MOD_BEVEL_HN_NONE, "HN_NONE", 0, "Off", "Do not use Harden Normals" },
 		{ MOD_BEVEL_HN_FACE, "HN_FACE", 0, "Face Area", "Use faces as weight" },
 		{ MOD_BEVEL_HN_ADJ, "HN_ADJ", 0, "Vertex average", "Use adjacent vertices as weight" },
+		{ MOD_BEVEL_FIX_SHA, "FIX_SHA", 0, "Fix shading", "Fix normal shading continuity" },
 		{ 0, NULL, 0, NULL, NULL },
 	};
 
diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c
index 77b306466c3..735436f3971 100644
--- a/source/blender/modifiers/intern/MOD_bevel.c
+++ b/source/blender/modifiers/intern/MOD_bevel.c
@@ -303,7 +303,7 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes
 	              vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp,
 	              dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp, bmd->hnmode, NULL);
 
-	if (bmd->hnmode != MOD_BEVEL_HN_NONE)
+	if (bmd->hnmode != MOD_BEVEL_HN_NONE && bmd->hnmode != MOD_BEVEL_FIX_SHA)
 		bevel_mod_harden_normals(bmd, bm, bmd->hn_strength, bmd->hnmode, dvert, vgroup);
 
 	if(set_wn_strength)



More information about the Bf-blender-cvs mailing list