Fix T58113 Multiple problems with bevel harden normals.
authorHoward Trickey <howard.trickey@gmail.com>
Thu, 3 Jan 2019 18:39:52 +0000 (13:39 -0500)
committerHoward Trickey <howard.trickey@gmail.com>
Thu, 3 Jan 2019 18:39:52 +0000 (13:39 -0500)
Move the bevel hardening code all into bmesh_bevel.c.
Based on user feedback, rewrote the bevel hardening algorithm
to be more what users want.
Based on user feedback, changed the UI, removing some
not-useful options. Now hardening normals while beveling
is enabled by a simple checkbox.
Now setting face strength gives options for which faces
get their face strength set.

12 files changed:
release/datafiles/locale
release/scripts/addons
release/scripts/startup/bl_ui/properties_data_modifier.py
source/blender/bmesh/intern/bmesh_opdefines.c
source/blender/bmesh/intern/bmesh_operators.h
source/blender/bmesh/operators/bmo_bevel.c
source/blender/bmesh/tools/bmesh_bevel.c
source/blender/bmesh/tools/bmesh_bevel.h
source/blender/editors/mesh/editmesh_bevel.c
source/blender/makesdna/DNA_modifier_types.h
source/blender/makesrna/intern/rna_modifier.c
source/blender/modifiers/intern/MOD_bevel.c

index fec9a7e88c8686830e9210099818a61489e699e4..345b7d27872d87ae5074a83d8b9995b37bc56f1b 160000 (submodule)
@@ -1 +1 @@
-Subproject commit fec9a7e88c8686830e9210099818a61489e699e4
+Subproject commit 345b7d27872d87ae5074a83d8b9995b37bc56f1b
index e3c9be92624d93ba3eb78731887455fbf41040d6..25ae9e134472c5bca62add0a1db3cdfc2d86aaa7 160000 (submodule)
@@ -1 +1 @@
-Subproject commit e3c9be92624d93ba3eb78731887455fbf41040d6
+Subproject commit 25ae9e134472c5bca62add0a1db3cdfc2d86aaa7
index 3d4f3037a84977f6c0b7d3e9781215c8e209d1f2..52af0512a0c7881963579ff816c84077e664d9e3 100644 (file)
@@ -147,6 +147,7 @@ class DATA_PT_modifiers(ModifierButtonsPanel, Panel):
         col.prop(md, "loop_slide")
         col.prop(md, "mark_seam")
         col.prop(md, "mark_sharp")
+        col.prop(md, "harden_normals")
 
         layout.label(text="Limit Method:")
         layout.row().prop(md, "limit_method", expand=True)
@@ -159,10 +160,8 @@ class DATA_PT_modifiers(ModifierButtonsPanel, Panel):
         layout.label(text="Width Method:")
         layout.row().prop(md, "offset_type", expand=True)
 
-        layout.label(text="Normal Mode")
-        layout.row().prop(md, "hnmode", expand=True)
-        layout.prop(md, "hn_strength")
-        layout.prop(md, "set_wn_strength")
+        layout.label(text="Set Face Strength Mode")
+        layout.row().prop(md, "face_strength_mode", expand=True)
 
     def BOOLEAN(self, layout, ob, md):
         split = layout.split()
index 519ffa5ed59b7d054f161386f9cf29f852d9356d..7a6cb814cc9ed05994b6866e7840e811677ec6c0 100644 (file)
@@ -1734,11 +1734,11 @@ static BMO_FlagSet bmo_enum_bevel_offset_type[] = {
        {0, NULL},
 };
 
-static BMO_FlagSet bmo_enum_bevel_harden_normal_type[] = {
-       {BEVEL_HN_NONE, "NONE"},
-       {BEVEL_HN_FACE, "FACE"},
-       {BEVEL_HN_ADJ, "ADJACENT"},
-       {BEVEL_HN_FIX_SHA, "FIXED_NORMAL_SHADING"},
+static BMO_FlagSet bmo_enum_bevel_face_strength_type[] = {
+       {BEVEL_FACE_STRENGTH_NONE, "NONE"},
+       {BEVEL_FACE_STRENGTH_NEW, "NEW"},
+       {BEVEL_FACE_STRENGTH_AFFECTED, "AFFECTED"},
+       {BEVEL_FACE_STRENGTH_ALL, "ALL"},
        {0, NULL},
 };
 
@@ -1761,15 +1761,15 @@ static BMOpDefine bmo_bevel_def = {
         {"loop_slide", BMO_OP_SLOT_BOOL},      /* prefer to slide along edges to having even widths */
         {"mark_seam", BMO_OP_SLOT_BOOL},               /* extend edge data to allow seams to run across bevels */
         {"mark_sharp", BMO_OP_SLOT_BOOL},              /* extend edge data to allow sharp edges to run across bevels */
-        {"strength", BMO_OP_SLOT_FLT},                 /* strength of calculated normal in range (0, 1) for custom clnors */
-        {"hnmode", BMO_OP_SLOT_INT, {(int)BMO_OP_SLOT_SUBTYPE_INT_ENUM}, bmo_enum_bevel_harden_normal_type}, /* harden normals mode used in bevel, if enabled */
+        {"harden_normals", BMO_OP_SLOT_BOOL},  /* harden normals */
+        {"face_strength_mode", BMO_OP_SLOT_INT, {(int)BMO_OP_SLOT_SUBTYPE_INT_ENUM},
+               bmo_enum_bevel_face_strength_type}, /* whether to set face strength, and which faces to set if so */
         {{'\0'}},
        },
        /* slots_out */
        {{"faces.out", BMO_OP_SLOT_ELEMENT_BUF, {BM_FACE}}, /* output faces */
         {"edges.out", BMO_OP_SLOT_ELEMENT_BUF, {BM_EDGE}}, /* output edges */
         {"verts.out", BMO_OP_SLOT_ELEMENT_BUF, {BM_VERT}}, /* output verts */
-        {"normals.out", BMO_OP_SLOT_MAPPING,  {(int)BMO_OP_SLOT_SUBTYPE_MAP_INTERNAL}}, /* output normals per vertex for beveled edges */
         {{'\0'}},
        },
 
index 21b356155ed498cdec0ba629e77b95160e928016..365b5eb468166ee6aff8bdbdb28712986b3e5b39 100644 (file)
@@ -117,11 +117,12 @@ enum {
        BEVEL_AMT_PERCENT
 };
 
+/* Bevel face_strength_mode values: should match face_str mode enum in DNA_modifer_types.h */
 enum {
-       BEVEL_HN_NONE,                  /* Disable harden normals */
-       BEVEL_HN_FACE,                  /* harden normals according to face area */
-       BEVEL_HN_ADJ,                   /* harden normals according to adjacent 'beveled' faces */
-       BEVEL_HN_FIX_SHA,               /* Special mode to fix normal shading continuity */
+       BEVEL_FACE_STRENGTH_NONE,
+       BEVEL_FACE_STRENGTH_NEW,
+       BEVEL_FACE_STRENGTH_AFFECTED,
+       BEVEL_FACE_STRENGTH_ALL,
 };
 
 extern const BMOpDefine *bmo_opdefines[];
index 656a446d5ab036ce9deb4d0f4a145cbcda5b37f6..1ef8531397f4d93ac3994f8fefb5347da8113f29 100644 (file)
@@ -45,7 +45,8 @@ void bmo_bevel_exec(BMesh *bm, BMOperator *op)
        const bool  loop_slide    = BMO_slot_bool_get(op->slots_in,  "loop_slide");
        const bool      mark_seam         = BMO_slot_bool_get(op->slots_in, "mark_seam");
        const bool      mark_sharp        = BMO_slot_bool_get(op->slots_in, "mark_sharp");
-       const int hnmode                  = BMO_slot_int_get(op->slots_in, "hnmode");
+       const bool  harden_normals = BMO_slot_bool_get(op->slots_in, "harden_normals");
+       const int   face_strength_mode = BMO_slot_int_get(op->slots_in, "face_strength_mode");
 
        if (offset > 0) {
                BMOIter siter;
@@ -71,7 +72,7 @@ void bmo_bevel_exec(BMesh *bm, BMOperator *op)
 
                BM_mesh_bevel(
                        bm, offset, offset_type, seg, profile, vonly, false, clamp_overlap, NULL, -1, material,
-                       loop_slide, mark_seam, mark_sharp, hnmode, op);
+                       loop_slide, mark_seam, mark_sharp, harden_normals, face_strength_mode);
 
                BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "faces.out", BM_FACE, BM_ELEM_TAG);
                BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "edges.out", BM_EDGE, BM_ELEM_TAG);
index e48616ea2312728d173e9fa5cbff6f27b5a8e4f8..7149df7dc4e297b5fc1d2613043242aa80434159 100644 (file)
 
 #include "DNA_meshdata_types.h"
 #include "DNA_modifier_types.h"
+#include "DNA_scene_types.h"
 
 #include "BLI_array.h"
 #include "BLI_alloca.h"
 #include "BLI_gsqueue.h"
+#include "BLI_linklist_stack.h"
 #include "BLI_math.h"
 #include "BLI_memarena.h"
+#include "BLI_utildefines.h"
 
 #include "BKE_customdata.h"
 #include "BKE_deform.h"
@@ -167,7 +170,6 @@ typedef struct VMesh {
                M_POLY,         /* a simple polygon */
                M_ADJ,          /* "adjacent edges" mesh pattern */
                M_TRI_FAN,      /* a simple polygon - fan filled */
-               M_QUAD_STRIP,   /* a simple polygon - cut into parallel strips */
        } mesh_kind;
 //     int _pad;
 } VMesh;
@@ -186,13 +188,21 @@ typedef struct BevVert {
        VMesh *vmesh;           /* mesh structure for replacing vertex */
 } BevVert;
 
+/* face classification: note depend on F_RECON > F_EDGE > F_VERT */
+typedef enum {
+       F_NONE,                         /* used when there is no face at all */
+       F_ORIG,                         /* original face, not touched */
+       F_VERT,                         /* face for construction aroun a vert */
+       F_EDGE,                         /* face for a beveled edge */
+       F_RECON,                        /* reconstructed original face with some new verts */
+} FKind;
+
+// static const char* fkind_names[] = {"F_NONE", "F_ORIG", "F_VERT", "F_EDGE", "F_RECON"}; /* DEBUG */
+
 /* Bevel parameters and state */
 typedef struct BevelParams {
-       /* hash of BevVert for each vertex involved in bevel
-        * GHash: (key=(BMVert *), value=(BevVert *)) */
-       GHash    *vert_hash;
-       /* Hash set used to store resultant beveled faces for VMesh when poly is ring */
-       GHash    *faceHash;
+       GHash    *vert_hash;    /* records BevVerts made: key BMVert*, value BevVert* */
+       GHash    *face_hash;    /* records new faces: key BMFace*, value one of {VERT/EDGE/RECON}_POLY */
        MemArena *mem_arena;    /* use for all allocs while bevel runs, if we need to free we can switch to mempool */
        ProfileSpacing pro_spacing; /* parameter values for evenly spaced profiles */
 
@@ -205,12 +215,13 @@ typedef struct BevelParams {
        bool loop_slide;            /* should bevel prefer to slide along edges rather than keep widths spec? */
        bool limit_offset;      /* should offsets be limited by collisions? */
        bool offset_adjust;     /* should offsets be adjusted to try to get even widths? */
-       bool mark_seam;
-       bool mark_sharp;
+       bool mark_seam;         /* should we propagate seam edge markings? */
+       bool mark_sharp;        /* should we propagate sharp edge markings? */
+       bool harden_normals;    /* should we harden normals? */
        const struct MDeformVert *dvert; /* vertex group array, maybe set if vertex_only */
        int vertex_group;       /* vertex group index, maybe set if vertex_only */
        int mat_nr;             /* if >= 0, material number for bevel; else material comes from adjacent faces */
-       int hnmode;
+       int face_strength_mode; /* setting face strength if > 0 */
 } BevelParams;
 
 // #pragma GCC diagnostic ignored "-Wpadded"
@@ -223,8 +234,10 @@ static int bev_debug_flags = 0;
 #define DEBUG_OLD_PROJ_TO_PERP_PLANE (bev_debug_flags & 2)
 #define DEBUG_OLD_FLAT_MID (bev_debug_flags & 4)
 
+/* use the unused _BM_ELEM_TAG_ALT flag to flag the 'long' loops (parallel to beveled edge) of edge-polygons */
+#define BM_ELEM_LONG_TAG (1<<6)
 
-/* this flag values will get set on geom we want to return in 'out' slots for edges and verts */
+/* these flag values will get set on geom we want to return in 'out' slots for edges and verts */
 #define EDGE_OUT 4
 #define VERT_OUT 8
 
@@ -247,6 +260,18 @@ static void disable_flag_out_edge(BMesh *bm, BMEdge *bme)
                BMO_edge_flag_disable(bm, bme, EDGE_OUT);
 }
 
+static void record_face_kind(BevelParams *bp, BMFace *f, FKind fkind)
+{
+       if (bp->face_hash)
+               BLI_ghash_insert(bp->face_hash, f, POINTER_FROM_INT(fkind));
+}
+
+static FKind get_face_kind(BevelParams *bp, BMFace *f)
+{
+       void *val = BLI_ghash_lookup(bp->face_hash, f);
+       return val ? (FKind)POINTER_AS_INT(val) : F_ORIG;
+}
+
 /* Are d1 and d2 parallel or nearly so? */
 static bool nearly_parallel(const float d1[3], const float d2[3])
 {
@@ -1678,76 +1703,166 @@ static void bevel_extend_edge_data(BevVert *bv)
        } while (bcur != start);
 }
 
-static void bevel_harden_normals_mode(BevelParams *bp, BevVert *bv, BMOperator *op)
+/*
+ * Harden normals for bevel.
+ * The desired effect is that the newly created F_EDGE and F_VERT faces appear smoothly shaded
+ * with the the normals at the boundaries with F_RECON faces matching those recon faces.
+ * And at boundaries between F_EDGE and F_VERT faces, the normals should match the F_EDGE ones.
+ * Assumes custom loop normals are in use.
+ */
+static void bevel_harden_normals(BMesh *bm, BevelParams *bp)
 {
-       if (bp->hnmode == BEVEL_HN_NONE)
+       BMIter liter, fiter;
+       BMFace *f;
+       BMLoop *l, *lnext, *lprev, *lprevprev, *lnextnext;
+       BMEdge *estep;
+       FKind fkind, fprevkind, fnextkind, fprevprevkind, fnextnextkind;
+       int cd_clnors_offset, l_index;
+       short *clnors;
+       float *pnorm, norm[3];
+
+       if (bp->offset == 0.0 || !bp->harden_normals)
                return;
 
-       VMesh *vm = bv->vmesh;
-       BoundVert *bcur = vm->boundstart, *bstart = bcur;
-
-       BMEdge *e;
-       BMIter eiter;
-
-       BMOpSlot *nslot = BMO_slot_get(op->slots_out, "normals.out");
-       float n_final[3] = { 0.0f, 0.0f, 0.0f };
-
-       if (bp->hnmode == BEVEL_HN_FACE) {
-               GHash *tempfaceHash = BLI_ghash_int_new(__func__);
-
-               /* Iterate through all faces of current BMVert and add their normal*face_area to n_final */
-               BM_ITER_ELEM(e, &eiter, bv->v, BM_EDGES_OF_VERT) {
-                       if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
+       /* recalculate all face and vertex normals; side effect: ensures vertex, edge, face indices */
+       BM_mesh_normals_update(bm);
 
-                               BMFace *f_a, *f_b;
-                               BM_edge_face_pair(e, &f_a, &f_b);
+       /* ensure that bm->lnor_spacearr has properly stored loop normals; side effect: ensures loop indices */
+       BM_lnorspace_update(bm);
+       cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
 
-                               if (f_a && !BLI_ghash_haskey(tempfaceHash, POINTER_FROM_UINT(BM_elem_index_get(f_a)))) {
-                                       int f_area = BM_face_calc_area(f_a);
-                                       float f_no[3];
-                                       copy_v3_v3(f_no, f_a->no);
-                                       mul_v3_fl(f_no, f_area);
-                                       add_v3_v3(n_final, f_no);
-                                       BLI_ghash_insert(tempfaceHash, POINTER_FROM_UINT(BM_elem_index_get(f_a)), NULL);
+       BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) {
+               fkind = get_face_kind(bp, f);
+               if (fkind == F_ORIG || fkind == F_RECON)
+                       continue;
+               BM_ITER_ELEM(l, &liter, f, BM_LOOPS_OF_FACE) {
+                       estep = l->prev->e;  /* causes CW walk around l->v fan */
+                       lprev = BM_vert_step_fan_loop(l, &estep);
+                       estep = l->e;  /* causes CCW walk around l->v fan */
+                       lnext = BM_vert_step_fan_loop(l, &estep);
+                       fprevkind = lprev ? get_face_kind(bp, lprev->f) : F_NONE;
+                       fnextkind = lnext ? get_face_kind(bp, lnext->f) : F_NONE;
+                       pnorm = NULL;
+                       if (fkind == F_EDGE) {
+                               if (fprevkind == F_EDGE && BM_elem_flag_test(l, BM_ELEM_LONG_TAG)) {
+                                       add_v3_v3v3(norm, f->no, lprev->f->no);
+                                       pnorm = norm;
+                               }
+                               else if (fnextkind == F_EDGE && BM_elem_flag_test(lnext, BM_ELEM_LONG_TAG)) {
+                                       add_v3_v3v3(norm, f->no, lnext->f->no);
+                                       pnorm = norm;
+                               }
+                               else if (fprevkind == F_RECON && BM_elem_flag_test(l, BM_ELEM_LONG_TAG)) {
+                                       pnorm = lprev->f->no;
                                }
-                               if (f_b && !BLI_ghash_haskey(tempfaceHash, POINTER_FROM_UINT(BM_elem_index_get(f_b)))) {
-                                       int f_area = BM_face_calc_area(f_b);
-                                       float f_no[3];
-                                       copy_v3_v3(f_no, f_b->no);
-                                       mul_v3_fl(f_no, f_area);
-                                       add_v3_v3(n_final, f_no);
-                                       BLI_ghash_insert(tempfaceHash, POINTER_FROM_UINT(BM_elem_index_get(f_b)), NULL);
+                               else if (fnextkind == F_RECON && BM_elem_flag_test(l->prev, BM_ELEM_LONG_TAG)) {
+                                       pnorm = lnext->f->no;
+                               }
+                               else {
+                                       /* printf("unexpected harden case (edge)\n"); */
                                }
                        }
-               }
-               BLI_ghash_free(tempfaceHash, NULL, NULL);
-               normalize_v3(n_final);
-       }
-       else if (bp->hnmode == BEVEL_HN_ADJ) {
-               BM_ITER_ELEM(e, &eiter, bv->v, BM_EDGES_OF_VERT) {
-                       if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
-                               if (e->v1 == bv->v) {
-                                       add_v3_v3(n_final, e->v2->no);
+                       else if (fkind == F_VERT) {
+                               if (fprevkind == F_VERT && fnextkind == F_VERT) {
+                                       pnorm = l->v->no;
+                               }
+                               else if (fprevkind == F_RECON) {
+                                       pnorm = lprev->f->no;
+                               }
+                               else if (fnextkind == F_RECON) {
+                                       pnorm = lnext->f->no;
                                }
                                else {
-                                       add_v3_v3(n_final, e->v1->no);
+                                       if (lprev) {
+                                               estep = lprev->prev->e;
+                                               lprevprev = BM_vert_step_fan_loop(lprev, &estep);
+                                       }
+                                       else {
+                                               lprevprev = NULL;
+                                       }
+                                       if (lnext) {
+                                               estep = lnext->e;
+                                               lnextnext = BM_vert_step_fan_loop(lnext, &estep);
+                                       }
+                                       else {
+                                               lnextnext = NULL;
+                                       }
+                                       fprevprevkind = lprevprev ? get_face_kind(bp, lprevprev->f) : F_NONE;
+                                       fnextnextkind = lnextnext ? get_face_kind(bp, lnextnext->f) : F_NONE;
+                                       if (fprevkind == F_EDGE && fprevprevkind == F_RECON) {
+                                               pnorm = lprevprev->f->no;
+                                       }
+                                       else if (fprevkind == F_EDGE && fnextkind == F_VERT && fprevprevkind == F_EDGE) {
+                                               add_v3_v3v3(norm, lprev->f->no, lprevprev->f->no);
+                                               pnorm = norm;
+                                       }
+                                       else if (fnextkind == F_EDGE && fprevkind == F_VERT && fnextnextkind == F_EDGE) {
+                                               add_v3_v3v3(norm, lnext->f->no, lnextnext->f->no);
+                                               pnorm = norm;
+                                       }
+                                       else {
+                                               /* printf("unexpected harden case (vert)\n"); */
+                                       }
                                }
                        }
+                       if (pnorm) {
+                               if (pnorm == norm)
+                                       normalize_v3(norm);
+                               l_index = BM_elem_index_get(l);
+                               clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset);
+                               BKE_lnor_space_custom_normal_to_data(
+                                       bm->lnor_spacearr->lspacearr[l_index], pnorm, clnors);
+                       }
                }
-               normalize_v3(n_final);
        }
+}
 
-       do {
-               /* Set normals.out for vertices as computed earlier */
-               if (BMO_slot_map_contains(nslot, bcur->nv.v) != true) {
-
-                       float *vert_normal = MEM_mallocN(sizeof(*vert_normal) * 3, __func__);
-                       normalize_v3_v3(vert_normal, n_final);
-
-                       BMO_slot_map_insert(op, nslot, bcur->nv.v, vert_normal);
+static void bevel_set_weighted_normal_face_strength(BMesh *bm, BevelParams *bp)
+{
+       BMFace *f;
+       BMIter fiter;
+       FKind fkind;
+       int strength;
+       int mode = bp->face_strength_mode;
+       bool do_set_strength;
+       const char *wn_layer_id = MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID;
+       int cd_prop_int_idx = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT, wn_layer_id);
+
+       if (cd_prop_int_idx == -1) {
+               BM_data_layer_add_named(bm, &bm->pdata, CD_PROP_INT, wn_layer_id);
+               cd_prop_int_idx = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT, wn_layer_id);
+       }
+       cd_prop_int_idx -= CustomData_get_layer_index(&bm->pdata, CD_PROP_INT);
+       const int cd_prop_int_offset = CustomData_get_n_offset(&bm->pdata, CD_PROP_INT, cd_prop_int_idx);
+
+       BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) {
+               fkind = get_face_kind(bp, f);
+               do_set_strength = true;
+               switch (fkind) {
+                       case F_VERT:
+                               strength = FACE_STRENGTH_WEAK;
+                               do_set_strength = (mode >= BEVEL_FACE_STRENGTH_NEW);
+                               break;
+                       case F_EDGE:
+                               strength = FACE_STRENGTH_MEDIUM;
+                               do_set_strength = (mode >= BEVEL_FACE_STRENGTH_NEW);
+                               break;
+                       case F_RECON:
+                               strength = FACE_STRENGTH_STRONG;
+                               do_set_strength = (mode >= BEVEL_FACE_STRENGTH_AFFECTED);
+                               break;
+                       case F_ORIG:
+                               strength = FACE_STRENGTH_STRONG;
+                               do_set_strength = (mode == BEVEL_FACE_STRENGTH_ALL);
+                               break;
+                       default:
+                               do_set_strength = false;
                }
-               bcur = bcur->next;
-       } while (bcur != bstart);
+               if (do_set_strength) {
+                       int *strength_ptr = BM_ELEM_CD_GET_VOID_P(f, cd_prop_int_offset);
+                       *strength_ptr = strength;
+               }
+       }
 }
 
 /* Set the any_seam property for a BevVert and all its BoundVerts */
@@ -1972,22 +2087,6 @@ static void build_boundary_terminal_edge(BevelParams *bp, BevVert *bv, EdgeHalf
        }
 }
 
-#if 0
-/* Return a value that is v if v is within BEVEL_MAX_ADJUST_PCT of the spec (assumed positive),
- * else clamp to make it at most that far away from spec */
-static float clamp_adjust(float v, float spec)
-{
-       float allowed_delta = spec * (BEVEL_MAX_ADJUST_PCT / 100.0f);
-
-       if (v - spec > allowed_delta)
-               return spec + allowed_delta;
-       else if (spec - v > allowed_delta)
-               return spec - allowed_delta;
-       else
-               return v;
-}
-#endif
-
 /* Make a circular list of BoundVerts for bv, each of which has the coordinates
  * of a vertex on the boundary of the beveled vertex bv->v.
  * This may adjust some EdgeHalf widths, and there might have to be
@@ -3419,12 +3518,12 @@ static float snap_face_dist_squared(float *co, BMFace *f, BMEdge **r_snap_e, flo
        return beste_d2;
 }
 
-static void build_center_ngon(BMesh *bm, BevVert *bv, int mat_nr)
+static void build_center_ngon(BevelParams *bp, BMesh *bm, BevVert *bv, int mat_nr)
 {
        VMesh *vm = bv->vmesh;
        BoundVert *v;
        int i, ns2;
-       BMFace *frep;
+       BMFace *frep, *f;
        BMEdge *frep_e1, *frep_e2, *frep_e;
        BMVert **vv = NULL;
        BMFace **vf = NULL;
@@ -3456,7 +3555,8 @@ static void build_center_ngon(BMesh *bm, BevVert *bv, int mat_nr)
                        BLI_array_append(ve, NULL);
                }
        } while ((v = v->next) != vm->boundstart);
-       bev_create_ngon(bm, vv, BLI_array_len(vv), vf, frep, ve, mat_nr, true);
+       f = bev_create_ngon(bm, vv, BLI_array_len(vv), vf, frep, ve, mat_nr, true);
+       record_face_kind(bp, f, F_VERT);
 
        BLI_array_free(vv);
        BLI_array_free(vf);
@@ -3496,7 +3596,7 @@ static void build_square_in_vmesh(BevelParams *bp, BMesh *bm, BevVert *bv, VMesh
                for (i = 0; i < n; i++) {
                        mesh_vert(vm, i, ns2, ns2)->v = mesh_vert(vm, i, 0, ns2)->v;
                }
-               build_center_ngon(bm, bv, bp->mat_nr);
+               build_center_ngon(bp, bm, bv, bp->mat_nr);
        }
 }
 
@@ -3722,8 +3822,6 @@ static void bevel_build_rings(BevelParams *bp, BMesh *bm, BevVert *bv)
                vm1 = adj_vmesh(bp, bv);
        }
 
-       bool do_fix_shading_bv = bp->faceHash != NULL;
-
        /* copy final vmesh into bv->vmesh, make BMVerts and BMFaces */
        vm = bv->vmesh;
        for (i = 0; i < n; i++) {
@@ -3810,8 +3908,7 @@ static void bevel_build_rings(BevelParams *bp, BMesh *bm, BevVert *bv)
                                                                   NULL, bme1, bme2, bme3, mat_nr);
                                        }
                                }
-                               if (do_fix_shading_bv)
-                                       BLI_ghash_insert(bp->faceHash, r_f, NULL);
+                               record_face_kind(bp, r_f, F_VERT);
                        }
                }
        } while ((v = v->next) != vm->boundstart);
@@ -3837,7 +3934,7 @@ static void bevel_build_rings(BevelParams *bp, BMesh *bm, BevVert *bv)
 
        /* center ngon */
        if (odd) {
-               build_center_ngon(bm, bv, mat_nr);
+               build_center_ngon(bp, bm, bv, mat_nr);
        }
 }
 
@@ -3884,8 +3981,6 @@ static BMFace *bevel_build_poly(BevelParams *bp, BMesh *bm, BevVert *bv)
        BLI_array_staticdeclare(vf, BM_DEFAULT_NGON_STACK_SIZE);
        BLI_array_staticdeclare(ve, BM_DEFAULT_NGON_STACK_SIZE);
 
-       bool do_fix_shading_bv = bp->faceHash != NULL;
-
        if (bv->any_seam) {
                frep = boundvert_rep_face(vm->boundstart, &frep2);
                if (frep2 && frep && is_bad_uv_poly(bv, frep)) {
@@ -3931,8 +4026,7 @@ static BMFace *bevel_build_poly(BevelParams *bp, BMesh *bm, BevVert *bv)
        } while ((v = v->next) != vm->boundstart);
        if (n > 2) {
                f = bev_create_ngon(bm, vv, n, vf, frep, ve, bp->mat_nr, true);
-               if (do_fix_shading_bv)
-                       BLI_ghash_insert(bp->faceHash, f, NULL);
+               record_face_kind(bp, f, F_VERT);
        }
        else {
                f = NULL;
@@ -3947,7 +4041,6 @@ static void bevel_build_trifan(BevelParams *bp, BMesh *bm, BevVert *bv)
 {
        BMFace *f;
        BLI_assert(next_bev(bv, NULL)->seg == 1 || bv->selcount == 1);
-       bool do_fix_shading_bv = bp->faceHash != NULL;
 
        f = bevel_build_poly(bp, bm, bv);
 
@@ -3956,11 +4049,6 @@ static void bevel_build_trifan(BevelParams *bp, BMesh *bm, BevVert *bv)
                BMLoop *l_fan = BM_FACE_FIRST_LOOP(f)->prev;
                BMVert *v_fan = l_fan->v;
 
-               if (f->len == 3) {
-                       if (do_fix_shading_bv)
-                               BLI_ghash_insert(bp->faceHash, f, NULL);
-               }
-
                while (f->len > 3) {
                        BMLoop *l_new;
                        BMFace *f_new;
@@ -3981,56 +4069,7 @@ static void bevel_build_trifan(BevelParams *bp, BMesh *bm, BevVert *bv)
                                else if (l_fan->prev->v == v_fan) { l_fan = l_fan->prev; }
                                else { BLI_assert(0); }
                        }
-                       if (do_fix_shading_bv)
-                               BLI_ghash_insert(bp->faceHash, f_new, NULL);
-               }
-       }
-}
-
-static void bevel_build_quadstrip(BevelParams *bp, BMesh *bm, BevVert *bv)
-{
-       BMFace *f;
-       BLI_assert(bv->selcount == 2);
-       bool do_fix_shading_bv = bp->faceHash != NULL;
-
-       f = bevel_build_poly(bp, bm, bv);
-
-       if (f) {
-               /* we have a polygon which we know starts at this vertex, make it into strips */
-               EdgeHalf *eh_a = bv->vmesh->boundstart->elast;
-               EdgeHalf *eh_b = next_bev(bv, eh_a->next);  /* since (selcount == 2) we know this is valid */
-               BMLoop *l_a = BM_face_vert_share_loop(f, eh_a->rightv->nv.v);
-               BMLoop *l_b = BM_face_vert_share_loop(f, eh_b->leftv->nv.v);
-               int split_count = bv->vmesh->seg + 1;  /* ensure we don't walk past the segments */
-
-               if (f->len == 4) {
-                       if (do_fix_shading_bv)
-                               BLI_ghash_insert(bp->faceHash, f, NULL);
-               }
-
-               while (f->len > 4 && split_count > 0) {
-                       BMLoop *l_new;
-                       BLI_assert(l_a->f == f);
-                       BLI_assert(l_b->f == f);
-
-                       if (l_a-> v == l_b->v || l_a->next == l_b) {
-                               /* l_a->v and l_b->v can be the same or such that we'd make a 2-vertex poly */
-                               l_a = l_a->prev;
-                               l_b = l_b->next;
-                       }
-                       else {
-                               BM_face_split(bm, f, l_a, l_b, &l_new, NULL, false);
-                               f = l_new->f;
-                               flag_out_edge(bm, l_new->e);
-
-                               /* walk around the new face to get the next verts to split */
-                               l_a = l_new->prev;
-                               l_b = l_new->next->next;
-
-                               if (do_fix_shading_bv)
-                                       BLI_ghash_insert(bp->faceHash, f, NULL);
-                       }
-                       split_count--;
+                       record_face_kind(bp, f_new, F_VERT);
                }
        }
 }
@@ -4091,13 +4130,6 @@ static void bevel_vert_two_edges(BevelParams *bp, BMesh *bm, BevVert *bv)
                                flag_out_edge(bm, bme);
                }
        }
-       else if (bp->faceHash) {
-               BMFace *f;
-               BMIter fiter;
-               BM_ITER_ELEM(f, &fiter, bv->v, BM_FACES_OF_VERT) {
-                       BLI_ghash_insert(bp->faceHash, f, NULL);
-               }
-       }
 }
 
 /* Given that the boundary is built, now make the actual BMVerts
@@ -4200,9 +4232,6 @@ static void build_vmesh(BevelParams *bp, BMesh *bm, BevVert *bv)
                case M_TRI_FAN:
                        bevel_build_trifan(bp, bm, bv);
                        break;
-               case M_QUAD_STRIP:
-                       bevel_build_quadstrip(bp, bm, bv);
-                       break;
        }
 }
 
@@ -4827,6 +4856,7 @@ static bool bev_rebuild_polygon(BMesh *bm, BevelParams *bp, BMFace *f)
 
                /* don't select newly or return created boundary faces... */
                if (f_new) {
+                       record_face_kind(bp, f_new, F_RECON);
                        BM_elem_flag_disable(f_new, BM_ELEM_TAG);
                        /* Also don't want new edges that aren't part of a new bevel face */
                        BM_ITER_ELEM(bme, &eiter, f_new, BM_EDGES_OF_FACE) {
@@ -5016,11 +5046,11 @@ static void bevel_build_edge_polygons(BMesh *bm, BevelParams *bp, BMEdge *bme)
        BMVert *verts[4];
        BMFace *faces[4];
        BMEdge *edges[4];
+       BMLoop *l;
+       BMIter iter;
        int k, nseg, i1, i2, odd, mid;
        int mat_nr = bp->mat_nr;
 
-       bool do_fix_shading_bv = bp->faceHash != NULL;
-
        if (!BM_edge_is_manifold(bme))
                return;
 
@@ -5102,8 +5132,12 @@ static void bevel_build_edge_polygons(BMesh *bm, BevelParams *bp, BMEdge *bme)
                        f = (k <= mid) ? f1 : f2;
                        r_f = bev_create_ngon(bm, verts, 4, NULL, f, NULL, mat_nr, true);
                }
-               if (do_fix_shading_bv)
-                       BLI_ghash_insert(bp->faceHash, r_f, NULL);
+               record_face_kind(bp, r_f, F_EDGE);
+               /* tag the long edges: those out of verts[0] and verts[2] */
+               BM_ITER_ELEM(l, &iter, r_f, BM_LOOPS_OF_FACE) {
+                       if (l->v == verts[0] || l->v == verts[2])
+                               BM_elem_flag_enable(l, BM_ELEM_LONG_TAG);
+               }
                verts[0] = verts[3];
                verts[1] = verts[2];
        }
@@ -5137,7 +5171,6 @@ static void bevel_build_edge_polygons(BMesh *bm, BevelParams *bp, BMEdge *bme)
        }
 }
 
-
 /* Find xnew > x0 so that distance((x0,y0), (xnew, ynew)) = dtarget.
  * False position Illinois method used because the function is somewhat linear
  * -> linear interpolation converges fast.
@@ -5644,18 +5677,17 @@ void BM_mesh_bevel(
         const bool vertex_only, const bool use_weights, const bool limit_offset,
         const struct MDeformVert *dvert, const int vertex_group, const int mat,
         const bool loop_slide, const bool mark_seam, const bool mark_sharp,
-        const int hnmode, void *mod_bmop_customdata)
+        const bool harden_normals, const int face_strength_mode)
 {
-       BMIter iter;
+       BMIter iter, liter;
        BMVert *v, *v_next;
        BMEdge *e;
+       BMFace *f;
+       BMLoop *l;
        BevVert *bv;
        BevelParams bp = {NULL};
        GHashIterator giter;
 
-       BMOperator *op = NULL;
-       BevelModNorEditData *clnordata;
-
        bp.offset = offset;
        bp.offset_type = offset_type;
        bp.seg    = segments;
@@ -5670,8 +5702,9 @@ void BM_mesh_bevel(
        bp.mat_nr = mat;
        bp.mark_seam = mark_seam;
        bp.mark_sharp = mark_sharp;
-       bp.hnmode = hnmode;
-       bp.faceHash = NULL;
+       bp.harden_normals = harden_normals;
+       bp.face_strength_mode = face_strength_mode;
+       bp.face_hash = NULL;
 
        if (profile >= 0.950f) {  /* r ~ 692, so PRO_SQUARE_R is 1e4 */
                bp.pro_super_r = PRO_SQUARE_R;
@@ -5684,15 +5717,8 @@ void BM_mesh_bevel(
                BLI_memarena_use_calloc(bp.mem_arena);
                set_profile_spacing(&bp);
 
-               /* Stores BMOp if executed through tool else stores BevelModNorEditData */
-               if (bm->use_toolflags)
-                       op = mod_bmop_customdata;
-               else {
-                       clnordata = mod_bmop_customdata;
-                       clnordata->faceHash = BLI_ghash_ptr_new(__func__);
-                       BLI_ghash_flag_set(clnordata->faceHash, GHASH_FLAG_ALLOW_DUPES);
-                       bp.faceHash = clnordata->faceHash;
-               }
+               bp.face_hash = BLI_ghash_ptr_new(__func__);
+               BLI_ghash_flag_set(bp.face_hash, GHASH_FLAG_ALLOW_DUPES);
 
                /* Analyze input vertices, sorting edges and assigning initial new vertex positions */
                BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
@@ -5742,9 +5768,6 @@ void BM_mesh_bevel(
                GHASH_ITER(giter, bp.vert_hash) {
                        bv = BLI_ghashIterator_getValue(&giter);
                        bevel_extend_edge_data(bv);
-                       if (bm->use_toolflags) {
-                               bevel_harden_normals_mode(&bp, bv, op);
-                       }
                }
 
                /* Rebuild face polygons around affected vertices */
@@ -5762,6 +5785,13 @@ void BM_mesh_bevel(
                        }
                }
 
+               if (bp.harden_normals) {
+                       bevel_harden_normals(bm, &bp);
+               }
+               if (bp.face_strength_mode != BEVEL_FACE_STRENGTH_NONE) {
+                       bevel_set_weighted_normal_face_strength(bm, &bp);
+               }
+
                /* When called from operator (as opposed to modifier), bm->use_toolflags
                 * will be set, and we to transfer the oflags to BM_ELEM_TAGs */
                if (bm->use_toolflags) {
@@ -5776,8 +5806,18 @@ void BM_mesh_bevel(
                        }
                }
 
+               /* clear the BM_ELEM_LONG_TAG tags, which were only set on some edges in F_EDGE faces */
+               BM_ITER_MESH(f, &iter, bm, BM_FACES_OF_MESH) {
+                       if (get_face_kind(&bp, f) != F_EDGE)
+                               continue;
+                       BM_ITER_ELEM(l, &liter, f, BM_LOOPS_OF_FACE) {
+                               BM_elem_flag_disable(l, BM_ELEM_LONG_TAG);
+                       }
+               }
+
                /* primary free */
                BLI_ghash_free(bp.vert_hash, NULL, NULL);
+               BLI_ghash_free(bp.face_hash, NULL, NULL);
                BLI_memarena_free(bp.mem_arena);
        }
 }
index f8a77d431cc7fae61e5b92c001406dca68412610..2bfa2be8f1cb334fee380c6acaf1fe1c4ce6f705 100644 (file)
@@ -34,6 +34,6 @@ void BM_mesh_bevel(
         const float profile, const bool vertex_only, const bool use_weights,
         const bool limit_offset, const struct MDeformVert *dvert, const int vertex_group,
         const int mat, const bool loop_slide, const bool mark_seam, const bool mark_sharp,
-        const int hnmode, void *mod_bmop_customdata);
+        const bool harden_normals, const int face_strength_mode);
 
 #endif /* __BMESH_BEVEL_H__ */
index 4e398c36a2fea04bb1ac59b5ae704569dfc093cd..ff259f01f641e75cdd2d70ea7408a8356e5083e6 100644 (file)
@@ -136,98 +136,6 @@ static void edbm_bevel_update_header(bContext *C, wmOperator *op)
        }
 }
 
-static void bevel_harden_normals(BMEditMesh *em, BMOperator *bmop, float face_strength)
-{
-       BKE_editmesh_lnorspace_update(em);
-       BM_normals_loops_edges_tag(em->bm, true);
-       const int cd_clnors_offset = CustomData_get_offset(&em->bm->ldata, CD_CUSTOMLOOPNORMAL);
-
-       BMesh *bm = em->bm;
-       BMFace *f;
-       BMLoop *l, *l_cur, *l_first;
-       BMIter fiter;
-
-       BMOpSlot *nslot = BMO_slot_get(bmop->slots_out, "normals.out");         /* Per vertex normals depending on hn_mode */
-
-       /* Similar functionality to bm_mesh_loops_calc_normals... Edges that can be smoothed are tagged */
-       BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) {
-               l_cur = l_first = BM_FACE_FIRST_LOOP(f);
-               do {
-                       if ((BM_elem_flag_test(l_cur->v, BM_ELEM_SELECT)) &&
-                           ((!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG)) ||
-                            (!BM_elem_flag_test(l_cur, BM_ELEM_TAG) && BM_loop_check_cyclic_smooth_fan(l_cur))))
-                       {
-                               /* Both adjacent loops are sharp, set clnor to face normal */
-                               if (!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG) && !BM_elem_flag_test(l_cur->prev->e, BM_ELEM_TAG)) {
-                                       const int loop_index = BM_elem_index_get(l_cur);
-                                       short *clnors = BM_ELEM_CD_GET_VOID_P(l_cur, cd_clnors_offset);
-                                       BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[loop_index], f->no, clnors);
-                               }
-                               else {
-                                       /* Find next corresponding sharp edge in this smooth fan */
-                                       BMVert *v_pivot = l_cur->v;
-                                       float *calc_n = BLI_ghash_lookup(nslot->data.ghash, v_pivot);
-
-                                       BMEdge *e_next;
-                                       const BMEdge *e_org = l_cur->e;
-                                       BMLoop *lfan_pivot, *lfan_pivot_next;
-
-                                       lfan_pivot = l_cur;
-                                       e_next = lfan_pivot->e;
-                                       BLI_SMALLSTACK_DECLARE(loops, BMLoop *);
-                                       float cn_wght[3] = { 0.0f, 0.0f, 0.0f }, cn_unwght[3] = { 0.0f, 0.0f, 0.0f };
-
-                                       /* Fan through current vert and accumulate normals and loops */
-                                       while (true) {
-                                               lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot, &e_next);
-                                               if (lfan_pivot_next) {
-                                                       BLI_assert(lfan_pivot_next->v == v_pivot);
-                                               }
-                                               else {
-                                                       e_next = (lfan_pivot->e == e_next) ? lfan_pivot->prev->e : lfan_pivot->e;
-                                               }
-
-                                               BLI_SMALLSTACK_PUSH(loops, lfan_pivot);
-                                               float cur[3];
-                                               mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f));
-                                               add_v3_v3(cn_wght, cur);
-
-                                               if (BM_elem_flag_test(lfan_pivot->f, BM_ELEM_SELECT))
-                                                       add_v3_v3(cn_unwght, cur);
-
-                                               if (!BM_elem_flag_test(e_next, BM_ELEM_TAG) || (e_next == e_org)) {
-                                                       break;
-                                               }
-                                               lfan_pivot = lfan_pivot_next;
-                                       }
-
-                                       normalize_v3(cn_wght);
-                                       normalize_v3(cn_unwght);
-                                       if (calc_n) {
-                                               mul_v3_fl(cn_wght, face_strength);
-                                               mul_v3_fl(calc_n, 1.0f - face_strength);
-                                               add_v3_v3(calc_n, cn_wght);
-                                               normalize_v3(calc_n);
-                                       }
-                                       while ((l = BLI_SMALLSTACK_POP(loops))) {
-                                               const int l_index = BM_elem_index_get(l);
-                                               short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset);
-                                               if (calc_n) {
-                                                       BKE_lnor_space_custom_normal_to_data(
-                                                               bm->lnor_spacearr->lspacearr[l_index], calc_n, clnors);
-                                               }
-                                               else {
-                                                       BKE_lnor_space_custom_normal_to_data(
-                                                               bm->lnor_spacearr->lspacearr[l_index], cn_unwght, clnors);
-                                               }
-                                       }
-                                       BLI_ghash_remove(nslot->data.ghash, v_pivot, NULL, MEM_freeN);
-                               }
-                       }
-               } while ((l_cur = l_cur->next) != l_first);
-       }
-}
-
 static bool edbm_bevel_init(bContext *C, wmOperator *op, const bool is_modal)
 {
        Scene *scene = CTX_data_scene(C);
@@ -320,9 +228,8 @@ static bool edbm_bevel_calc(wmOperator *op)
        const bool loop_slide = RNA_boolean_get(op->ptr, "loop_slide");
        const bool mark_seam = RNA_boolean_get(op->ptr, "mark_seam");
        const bool mark_sharp = RNA_boolean_get(op->ptr, "mark_sharp");
-       const float hn_strength = RNA_float_get(op->ptr, "strength");
-       const int hnmode = RNA_enum_get(op->ptr, "hnmode");
-
+       const bool harden_normals = RNA_boolean_get(op->ptr, "harden_normals");
+       const int face_strength_mode = RNA_enum_get(op->ptr, "face_strength_mode");
 
        for (uint ob_index = 0; ob_index < opdata->ob_store_len; ob_index++) {
                em = opdata->ob_store[ob_index].em;
@@ -339,9 +246,9 @@ static bool edbm_bevel_calc(wmOperator *op)
                EDBM_op_init(
                        em, &bmop, op,
                        "bevel geom=%hev offset=%f segments=%i vertex_only=%b offset_type=%i profile=%f clamp_overlap=%b "
-                       "material=%i loop_slide=%b mark_seam=%b mark_sharp=%b strength=%f hnmode=%i",
+                       "material=%i loop_slide=%b mark_seam=%b mark_sharp=%b harden_normals=%b face_strength_mode=%i",
                        BM_ELEM_SELECT, offset, segments, vertex_only, offset_type, profile,
-                       clamp_overlap, material, loop_slide, mark_seam, mark_sharp, hn_strength, hnmode);
+                       clamp_overlap, material, loop_slide, mark_seam, mark_sharp, harden_normals, face_strength_mode);
 
                BMO_op_exec(em->bm, &bmop);
 
@@ -352,10 +259,6 @@ static bool edbm_bevel_calc(wmOperator *op)
                        BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "faces.out", BM_FACE, BM_ELEM_SELECT, true);
                }
 
-               if (hnmode != BEVEL_HN_NONE) {
-                       bevel_harden_normals(em, &bmop, hn_strength);
-               }
-
                /* no need to de-select existing geometry */
                if (!EDBM_op_finish(em, &bmop, op, true)) {
                        continue;
@@ -766,11 +669,12 @@ void MESH_OT_bevel(wmOperatorType *ot)
                {0, NULL, 0, NULL, NULL},
        };
 
-       static EnumPropertyItem harden_normals_items[] = {
-               { BEVEL_HN_NONE, "HN_NONE", 0, "Off", "Do not use Harden Normals" },
-               { BEVEL_HN_FACE, "HN_FACE", 0, "Face Area", "Use faces as weight" },
-               { BEVEL_HN_ADJ, "HN_ADJ", 0, "Vertex average", "Use adjacent vertices as weight" },
-               { 0, NULL, 0, NULL, NULL },
+       static const EnumPropertyItem face_strength_mode_items[] = {
+               {BEVEL_FACE_STRENGTH_NONE, "NONE", 0, "None", "Do not set face strength"},
+               {BEVEL_FACE_STRENGTH_NEW, "NEW", 0, "New", "Set face strength on new faces only"},
+               {BEVEL_FACE_STRENGTH_AFFECTED, "AFFECTED", 0, "Affected", "Set face strength on new and modified faces only"},
+               {BEVEL_FACE_STRENGTH_ALL, "ALL", 0, "All", "Set face strength on all faces"},
+               {0, NULL, 0, NULL, NULL},
        };
 
        /* identifiers */
@@ -802,10 +706,9 @@ void MESH_OT_bevel(wmOperatorType *ot)
        RNA_def_boolean(ot->srna, "mark_sharp", false, "Mark Sharp", "Mark beveled edges as sharp");
        RNA_def_int(ot->srna, "material", -1, -1, INT_MAX, "Material",
                "Material for bevel faces (-1 means use adjacent faces)", -1, 100);
-       RNA_def_float(ot->srna, "strength", 0.5f, 0.0f, 1.0f, "Normal Strength",
-               "Strength of calculated normal", 0.0f, 1.0f);
-       RNA_def_enum(ot->srna, "hnmode", harden_normals_items, BEVEL_HN_NONE, "Normal Mode",
-               "Weighting mode for Harden Normals");
+       RNA_def_boolean(ot->srna, "harden_normals", false, "Harden Normals", "Match normals of new faces to adjacent faces");
+       RNA_def_enum(ot->srna, "face_strength_mode", face_strength_mode_items, BEVEL_FACE_STRENGTH_NONE,
+               "Face Strength Mode", "Whether to set face strength, and which faces to set face strength on");
        prop = RNA_def_boolean(ot->srna, "release_confirm", 0, "Confirm on Release", "");
        RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
 
index 748c68c3fd450b6c47c0310ebedd4adc8cfe3edc..37157248aa6a71d00dc7fd36af0e16127d435717 100644 (file)
@@ -357,13 +357,12 @@ typedef struct BevelModifierData {
        short e_flags;        /* flags to direct how edge weights are applied to verts */
        short mat;            /* material index if >= 0, else material inherited from surrounding faces */
        short edge_flags;
-       int pad2;
+       short face_str_mode;
+       short pad2;
        float profile;        /* controls profile shape (0->1, .5 is round) */
        /* if the MOD_BEVEL_ANGLE is set, this will be how "sharp" an edge must be before it gets beveled */
        float bevel_angle;
        /* if the MOD_BEVEL_VWEIGHT option is set, this will be the name of the vert group, MAX_VGROUP_NAME */
-       int hnmode;
-       float hn_strength;
        char defgrp_name[64];
        struct BevelModNorEditData clnordata;
 } BevelModifierData;
@@ -386,7 +385,7 @@ enum {
 /*     MOD_BEVEL_DIST          = (1 << 12), */  /* same as above */
        MOD_BEVEL_OVERLAP_OK    = (1 << 13),
        MOD_BEVEL_EVEN_WIDTHS   = (1 << 14),
-       MOD_BEVEL_SET_WN_STR    = (1 << 15),
+       MOD_BEVEL_HARDEN_NORMALS = (1 << 15),
 };
 
 /* BevelModifierData->val_flags (not used as flags any more) */
@@ -403,12 +402,12 @@ enum {
        MOD_BEVEL_MARK_SHARP = (1 << 1),
 };
 
-/* BevelModifierData->hnmode */
+/* BevelModifierData->face_str_mode */
 enum {
-       MOD_BEVEL_HN_NONE,
-       MOD_BEVEL_HN_FACE,
-       MOD_BEVEL_HN_ADJ,
-       MOD_BEVEL_FIX_SHA,
+       MOD_BEVEL_FACE_STRENGTH_NONE,
+       MOD_BEVEL_FACE_STRENGTH_NEW,
+       MOD_BEVEL_FACE_STRENGTH_AFFECTED,
+       MOD_BEVEL_FACE_STRENGTH_ALL,
 };
 
 typedef struct SmokeModifierData {
index 541e5c772f8d7d8ce1d3f85ba4fd16c7d8ac43e7..5bc925ea9695ef781335b6290e2c602754764cc8 100644 (file)
@@ -3049,10 +3049,11 @@ static void rna_def_modifier_bevel(BlenderRNA *brna)
        };
 
        static EnumPropertyItem prop_harden_normals_items[] = {
-               { 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" },
+               { MOD_BEVEL_FACE_STRENGTH_NONE, "FSTR_NONE", 0, "None", "Do not set face strength" },
+               { MOD_BEVEL_FACE_STRENGTH_NEW, "FSTR_NEW", 0, "New", "Set face strength on new faces only" },
+               { MOD_BEVEL_FACE_STRENGTH_AFFECTED, "FSTR_AFFECTED", 0, "Affected",
+                       "Set face strength on new and affected faces only" },
+               { MOD_BEVEL_FACE_STRENGTH_ALL, "FSTR_ALL", 0, "All", "Set face strength on all faces" },
                { 0, NULL, 0, NULL, NULL },
        };
 
@@ -3143,22 +3144,16 @@ static void rna_def_modifier_bevel(BlenderRNA *brna)
        RNA_def_property_ui_text(prop, "Mark Sharp", "Mark beveled edges as sharp");
        RNA_def_property_update(prop, 0, "rna_Modifier_update");
 
-       prop = RNA_def_property(srna, "hnmode", PROP_ENUM, PROP_NONE);
-       RNA_def_property_enum_items(prop, prop_harden_normals_items);
-       RNA_def_property_ui_text(prop, "Normal Mode", "Weighting mode for Harden Normals");
+       prop = RNA_def_property(srna, "harden_normals", PROP_BOOLEAN, PROP_NONE);
+       RNA_def_property_boolean_sdna(prop, NULL, "flags", MOD_BEVEL_HARDEN_NORMALS);
+       RNA_def_property_ui_text(prop, "Harden Normals",
+                                "Match normals of new faces to adjacent faces");
        RNA_def_property_update(prop, 0, "rna_Modifier_update");
 
-       prop = RNA_def_property(srna, "hn_strength", PROP_FLOAT, PROP_NONE);
-       RNA_def_property_float_default(prop, 0.5f);
-       RNA_def_property_range(prop, 0, 1);
-       RNA_def_property_ui_range(prop, 0, 1, 1, 2);
-       RNA_def_property_ui_text(prop, "Normal Strength", "Strength of calculated normal");
-       RNA_def_property_update(prop, 0, "rna_Modifier_update");
-
-       prop = RNA_def_property(srna, "set_wn_strength", PROP_BOOLEAN, PROP_NONE);
-       RNA_def_property_boolean_sdna(prop, NULL, "flags", MOD_BEVEL_SET_WN_STR);
-       RNA_def_property_ui_text(prop, "Face Strength",
-                                "Set face strength of beveled faces for use in Weighted Normal modifier");
+       prop = RNA_def_property(srna, "face_strength_mode", PROP_ENUM, PROP_NONE);
+       RNA_def_property_enum_sdna(prop, NULL, "face_str_mode");
+       RNA_def_property_enum_items(prop, prop_harden_normals_items);
+       RNA_def_property_ui_text(prop, "Set Face Strength", "Whether to set face strength, and which faces to set it on");
        RNA_def_property_update(prop, 0, "rna_Modifier_update");
 }
 
index ee2b08fe824e93b9e140f014497980c036ebf002..e63b5407a74b2bb75d2154500f10fa0e6fcec70d 100644 (file)
@@ -66,12 +66,11 @@ static void initData(ModifierData *md)
        bmd->lim_flags = 0;
        bmd->e_flags = 0;
        bmd->edge_flags = 0;
+       bmd->face_str_mode = MOD_BEVEL_FACE_STRENGTH_NONE;
        bmd->mat = -1;
        bmd->profile = 0.5f;
        bmd->bevel_angle = DEG2RADF(30.0f);
        bmd->defgrp_name[0] = '\0';
-       bmd->hnmode = MOD_BEVEL_HN_NONE;
-       bmd->hn_strength = 0.5f;
        bmd->clnordata.faceHash = NULL;
 }
 
@@ -95,254 +94,6 @@ static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
        return dataMask;
 }
 
-static void bevel_set_weighted_normal_face_strength(BMesh *bm, Scene *scene)
-{
-       BMFace *f;
-       BMIter fiter;
-       const char *wn_layer_id = MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID;
-       int cd_prop_int_idx = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT, wn_layer_id);
-
-       if (cd_prop_int_idx == -1) {
-               BM_data_layer_add_named(bm, &bm->pdata, CD_PROP_INT, wn_layer_id);
-               cd_prop_int_idx = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT, wn_layer_id);
-       }
-       cd_prop_int_idx -= CustomData_get_layer_index(&bm->pdata, CD_PROP_INT);
-       const int cd_prop_int_offset = CustomData_get_n_offset(&bm->pdata, CD_PROP_INT, cd_prop_int_idx);
-
-       const int face_strength = scene->toolsettings->face_strength;
-
-       BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) {
-               if (BM_elem_flag_test(f, BM_ELEM_TAG)) {
-                       int *strength = BM_ELEM_CD_GET_VOID_P(f, cd_prop_int_offset);
-                       *strength = face_strength;
-               }
-       }
-}
-
-static void bevel_mod_harden_normals(
-        BevelModifierData *bmd, BMesh *bm, const float hn_strength,
-        const int hnmode, MDeformVert *dvert, int vgroup)
-{
-       if (bmd->res > 20 || bmd->value == 0)
-               return;
-
-       BM_mesh_normals_update(bm);
-       BM_lnorspace_update(bm);
-       BM_normals_loops_edges_tag(bm, true);
-
-       const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0;
-       const int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
-       const bool do_normal_to_recon = (hn_strength == 1.0f);
-
-       BMFace *f;
-       BMLoop *l, *l_cur, *l_first;
-       BMIter fiter;
-       GHash *faceHash = bmd->clnordata.faceHash;
-
-       /* Iterate throught all loops of a face */
-       BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) {
-
-               l_cur = l_first = BM_FACE_FIRST_LOOP(f);
-               do {
-                       if ((!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG)) ||
-                           (!BM_elem_flag_test(l_cur, BM_ELEM_TAG) && BM_loop_check_cyclic_smooth_fan(l_cur)))
-                       {
-
-                               /* previous and next edge is sharp, accumulate face normals into loop */
-                               if (!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG) && !BM_elem_flag_test(l_cur->prev->e, BM_ELEM_TAG)) {
-                                       const int loop_index = BM_elem_index_get(l_cur);
-                                       short *clnors = BM_ELEM_CD_GET_VOID_P(l_cur, cd_clnors_offset);
-                                       BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[loop_index], f->no, clnors);
-                               }
-                               else {
-                                       BMVert *v_pivot = l_cur->v;
-                                       BMEdge *e_next;
-                                       const BMEdge *e_org = l_cur->e;
-                                       BMLoop *lfan_pivot, *lfan_pivot_next;
-                                       UNUSED_VARS_NDEBUG(v_pivot);
-
-                                       lfan_pivot = l_cur;
-                                       e_next = lfan_pivot->e;
-                                       BLI_SMALLSTACK_DECLARE(loops, BMLoop *);
-                                       float cn_wght[3] = { 0.0f, 0.0f, 0.0f };
-                                       int recon_face_count = 0;               /* Counts number of reconstructed faces current vert is connected to */
-                                       BMFace *recon_face = NULL;              /* Reconstructed face */
-
-                                       while (true) {
-                                               lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot, &e_next);
-                                               if (lfan_pivot_next) {
-                                                       BLI_assert(lfan_pivot_next->v == v_pivot);
-                                               }
-                                               else {
-                                                       e_next = (lfan_pivot->e == e_next) ? lfan_pivot->prev->e : lfan_pivot->e;
-                                               }
-
-                                               BLI_SMALLSTACK_PUSH(loops, lfan_pivot);
-
-                                               if (bmd->lim_flags & MOD_BEVEL_WEIGHT) {
-                                                       int weight = BM_elem_float_data_get(&bm->edata, lfan_pivot->f, CD_BWEIGHT);
-                                                       if (weight) {
-                                                               if (hnmode == MOD_BEVEL_HN_FACE) {
-                                                                       float cur[3];                                   //Add area weighted face normals
-                                                                       mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f));
-                                                                       add_v3_v3(cn_wght, cur);
-                                                               }
-                                                               else
-                                                                       add_v3_v3(cn_wght, lfan_pivot->f->no);          //Else simply add face normals
-                                                       }
-                                                       else
-                                                               add_v3_v3(cn_wght, lfan_pivot->f->no);
-
-                                               }
-                                               else if (bmd->lim_flags & MOD_BEVEL_VGROUP) {
-                                                       const bool has_vgroup = dvert != NULL;
-                                                       const bool vert_of_group = (
-                                                               has_vgroup &&
-                                                               (defvert_find_index(&dvert[BM_elem_index_get(lfan_pivot->v)], vgroup) != NULL));
-
-                                                       if (vert_of_group && hnmode == MOD_BEVEL_HN_FACE) {
-                                                               float cur[3];
-                                                               mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f));
-                                                               add_v3_v3(cn_wght, cur);
-                                                       }
-                                                       else
-                                                               add_v3_v3(cn_wght, lfan_pivot->f->no);
-                                               }
-                                               else {
-                                                       float cur[3];
-                                                       mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f));
-                                                       add_v3_v3(cn_wght, cur);
-                                               }
-                                               if (!BLI_ghash_haskey(faceHash, lfan_pivot->f)) {
-                                                       recon_face = lfan_pivot->f;
-                                                       recon_face_count++;
-                                               }
-                                               if (!BM_elem_flag_test(e_next, BM_ELEM_TAG) || (e_next == e_org)) {
-                                                       break;
-                                               }
-                                               lfan_pivot = lfan_pivot_next;
-                                       }
-
-                                       normalize_v3(cn_wght);
-                                       mul_v3_fl(cn_wght, hn_strength);
-                                       float n_final[3];
-
-                                       while ((l = BLI_SMALLSTACK_POP(loops))) {
-                                               const int l_index = BM_elem_index_get(l);
-                                               short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset);
-
-                                               /* If vertex is edge vert with 1 reconnected face */
-                                               if (recon_face_count == 1 || (recon_face != NULL && do_normal_to_recon)) {
-                                                       BKE_lnor_space_custom_normal_to_data(
-                                                               bm->lnor_spacearr->lspacearr[l_index], recon_face->no, clnors);
-                                               }
-                                               else if (vertex_only == false || recon_face_count == 0) {
-                                                       copy_v3_v3(n_final, l->f->no);
-                                                       mul_v3_fl(n_final, 1.0f - hn_strength);
-                                                       add_v3_v3(n_final, cn_wght);
-                                                       normalize_v3(n_final);
-                                                       BKE_lnor_space_custom_normal_to_data(
-                                                               bm->lnor_spacearr->lspacearr[l_index], n_final, clnors);
-                                               }
-                                               else if (BLI_ghash_haskey(faceHash, l->f)) {
-                                                       BKE_lnor_space_custom_normal_to_data(
-                                                               bm->lnor_spacearr->lspacearr[l_index], l->v->no, clnors);
-                                               }
-                                       }
-                               }
-                       }
-               } while ((l_cur = l_cur->next) != l_first);
-       }
-}
-
-static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *bm)
-{
-       const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0;
-       if (bmd->value == 0 || (bmd->clnordata.faceHash == NULL && vertex_only))
-               return;
-
-       BM_mesh_normals_update(bm);
-       BM_lnorspace_update(bm);
-
-       GHash *faceHash = bmd->clnordata.faceHash;
-       BMEdge *e;
-       BMLoop *l;
-       BMIter liter, eiter;
-
-       const int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
-       const float hn_strength = bmd->hn_strength;
-       float ref = 10.0f;
-
-       BM_ITER_MESH(e, &eiter, bm, BM_EDGES_OF_MESH) {
-               BMFace *f_a, *f_b;
-               BM_edge_face_pair(e, &f_a, &f_b);
-
-               bool has_f_a = false, has_f_b = false;
-               if (f_a)
-                       has_f_a = BLI_ghash_haskey(faceHash, f_a);
-               if (f_b)
-                       has_f_b = BLI_ghash_haskey(faceHash, f_b);
-               if (has_f_a ^ has_f_b) {
-                       /* If one of both faces is present in faceHash then we are at a border
-                        * between new vmesh created and reconstructed face */
-
-                       for (int i = 0; i < 2; i++) {
-                               BMVert *v = (i == 0) ? e->v1 : e->v2;
-                               BM_ITER_ELEM(l, &liter, v, 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 n_final[3], pow_a[3], pow_b[3];
-
-                                               zero_v3(n_final);
-                                               copy_v3_v3(pow_a, f_a->no);
-                                               copy_v3_v3(pow_b, f_b->no);
-                                               if (has_f_a) {
-                                                       mul_v3_fl(pow_a, bmd->res / ref);
-                                                       mul_v3_fl(pow_b, ref / bmd->res);
-                                               }
-                                               else {
-                                                       mul_v3_fl(pow_b, bmd->res / ref);
-                                                       mul_v3_fl(pow_a, ref / bmd->res);
-                                               }
-                                               add_v3_v3(n_final, pow_a);
-                                               add_v3_v3(n_final, pow_b);
-                                               normalize_v3(n_final);
-
-                                               BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], n_final, clnors);
-                                       }
-                               }
-                       }
-               }
-               else if (has_f_a == true  && has_f_b == true) {
-                       /* Else if both faces are present we assign clnor corresponding
-                        * to vert normal and face normal */
-                       for (int i = 0; i < 2; i++) {
-                               BMVert *v = (i == 0) ? e->v1 : e->v2;
-                               BM_ITER_ELEM(l, &liter, v, 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 n_final[3], cn_wght[3];
-
-                                               copy_v3_v3(n_final, v->no);
-                                               mul_v3_fl(n_final, hn_strength);
-
-                                               copy_v3_v3(cn_wght, l->f->no);
-                                               mul_v3_fl(cn_wght, 1.0f - hn_strength);
-
-                                               add_v3_v3(n_final, cn_wght);
-                                               normalize_v3(n_final);
-                                               BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], n_final, clnors);
-                                       }
-                               }
-                       }
-               }
-       }
-}
-
 /*
  * This calls the new bevel code (added since 2.64)
  */
@@ -365,9 +116,8 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes
        const bool loop_slide = (bmd->flags & MOD_BEVEL_EVEN_WIDTHS) == 0;
        const bool mark_seam = (bmd->edge_flags & MOD_BEVEL_MARK_SEAM);
        const bool mark_sharp = (bmd->edge_flags & MOD_BEVEL_MARK_SHARP);
-       const bool set_wn_strength = (bmd->flags & MOD_BEVEL_SET_WN_STR);
-
-       struct Scene *scene = DEG_get_evaluated_scene(ctx->depsgraph);
+       const bool harden_normals = (bmd->flags & MOD_BEVEL_HARDEN_NORMALS);
+       const int face_strength_mode = bmd->face_str_mode;
 
        bm = BKE_mesh_to_bmesh_ex(
                mesh,
@@ -439,15 +189,8 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes
 
        BM_mesh_bevel(bm, bmd->value, offset_type, bmd->res, bmd->profile,
                      vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp,
-                     dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp, bmd->hnmode, &bmd->clnordata);
-
-       if (bmd->hnmode != BEVEL_HN_FIX_SHA && bmd->hnmode != MOD_BEVEL_HN_NONE) {
-               bevel_mod_harden_normals(bmd, bm, bmd->hn_strength, bmd->hnmode, dvert, vgroup);
-       }
-       if (bmd->hnmode == BEVEL_HN_FIX_SHA)
-               bevel_fix_normal_shading_continuity(bmd, bm);
-       if (set_wn_strength)
-               bevel_set_weighted_normal_face_strength(bm, scene);
+                     dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp,
+                     harden_normals, face_strength_mode);
 
        result = BKE_mesh_from_bmesh_for_eval_nomain(bm, 0);