[Bf-blender-cvs] [f4900b3d02c] sculpt-dev: Sculpt: Smooth Directional deform mode

Pablo Dobarro noreply at git.blender.org
Thu Dec 24 19:58:32 CET 2020


Commit: f4900b3d02cb1d30f5218257d6968a52225c14f8
Author: Pablo Dobarro
Date:   Thu Dec 24 19:57:58 2020 +0100
Branches: sculpt-dev
https://developer.blender.org/rBf4900b3d02cb1d30f5218257d6968a52225c14f8

Sculpt: Smooth Directional deform mode

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

M	source/blender/editors/sculpt_paint/sculpt.c
M	source/blender/editors/sculpt_paint/sculpt_intern.h
M	source/blender/editors/sculpt_paint/sculpt_smooth.c
M	source/blender/makesdna/DNA_brush_enums.h
M	source/blender/makesrna/intern/rna_brush.c

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

diff --git a/source/blender/editors/sculpt_paint/sculpt.c b/source/blender/editors/sculpt_paint/sculpt.c
index adf235fd6f8..b588ef1cb55 100644
--- a/source/blender/editors/sculpt_paint/sculpt.c
+++ b/source/blender/editors/sculpt_paint/sculpt.c
@@ -6244,6 +6244,9 @@ static void do_brush_action(Sculpt *sd, Object *ob, Brush *brush, UnifiedPaintSe
         else if (brush->smooth_deform_type == BRUSH_SMOOTH_DEFORM_SURFACE) {
           SCULPT_do_surface_smooth_brush(sd, ob, nodes, totnode);
         }
+        else if (brush->smooth_deform_type == BRUSH_SMOOTH_DEFORM_DIRECTIONAL) {
+          SCULPT_do_directional_smooth_brush(sd, ob, nodes, totnode);
+        }
         break;
       case SCULPT_TOOL_CREASE:
         do_crease_brush(sd, ob, nodes, totnode);
diff --git a/source/blender/editors/sculpt_paint/sculpt_intern.h b/source/blender/editors/sculpt_paint/sculpt_intern.h
index 9abe1028868..17c474e0ce2 100644
--- a/source/blender/editors/sculpt_paint/sculpt_intern.h
+++ b/source/blender/editors/sculpt_paint/sculpt_intern.h
@@ -563,6 +563,9 @@ void SCULPT_surface_smooth_displace_step(SculptSession *ss,
                                          const float fade);
 void SCULPT_do_surface_smooth_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode);
 
+/* Directional Smooth Brush. */
+void SCULPT_do_directional_smooth_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode);
+
 /* Slide/Relax */
 void SCULPT_relax_vertex(struct SculptSession *ss,
                          struct PBVHVertexIter *vd,
diff --git a/source/blender/editors/sculpt_paint/sculpt_smooth.c b/source/blender/editors/sculpt_paint/sculpt_smooth.c
index 87ee7480c92..45e8aa6d627 100644
--- a/source/blender/editors/sculpt_paint/sculpt_smooth.c
+++ b/source/blender/editors/sculpt_paint/sculpt_smooth.c
@@ -564,3 +564,93 @@ void SCULPT_do_surface_smooth_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, in
         0, totnode, &data, SCULPT_do_surface_smooth_brush_displace_task_cb_ex, &settings);
   }
 }
+
+static void SCULPT_do_directional_smooth_task_cb_ex(void *__restrict userdata,
+                                                    const int n,
+                                                    const TaskParallelTLS *__restrict tls)
+{
+  SculptThreadedTaskData *data = userdata;
+  SculptSession *ss = data->ob->sculpt;
+  const Brush *brush = data->brush;
+  const float bstrength = ss->cache->bstrength;
+
+  PBVHVertexIter vd;
+
+  SculptBrushTest test;
+  SculptBrushTestFn sculpt_brush_test_sq_fn = SCULPT_brush_test_init_with_falloff_shape(
+      ss, &test, data->brush->falloff_shape);
+  const int thread_id = BLI_task_parallel_thread_id(tls);
+
+  BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
+  {
+    if (!sculpt_brush_test_sq_fn(&test, vd.co)) {
+      continue;
+    }
+    const float fade = bstrength * SCULPT_brush_strength_factor(ss,
+                                                                brush,
+                                                                vd.co,
+                                                                sqrtf(test.dist),
+                                                                vd.no,
+                                                                vd.fno,
+                                                                vd.mask ? *vd.mask : 0.0f,
+                                                                vd.index,
+                                                                thread_id);
+
+    float stroke_disp[3];
+    sub_v3_v3v3(stroke_disp, ss->cache->location, ss->cache->last_location);
+    normalize_v3(stroke_disp);
+
+    float avg[3] = {0.0f, 0.0f, 0.0f};
+    int neighbor_count = 0;
+
+    SculptVertexNeighborIter ni;
+    SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, vd.index, ni) {
+      float vertex_neighbor_disp[3];
+      const float *neighbor_co = SCULPT_vertex_co_get(ss, ni.index);
+      sub_v3_v3v3(vertex_neighbor_disp, neighbor_co, vd.co);
+      normalize_v3(vertex_neighbor_disp);
+      if (fabsf(dot_v3v3(stroke_disp, vertex_neighbor_disp)) > 0.6f) {
+        neighbor_count++;
+        add_v3_v3(avg, neighbor_co);
+      }
+    }
+    SCULPT_VERTEX_NEIGHBORS_ITER_END(ni);
+
+    /* Avoid division by 0 when there are no neighbors. */
+    if (neighbor_count == 0) {
+      continue;
+    }
+
+    float smooth_co[3];
+    mul_v3_v3fl(smooth_co, avg, 1.0f / neighbor_count);
+
+    float final_disp[3];
+    sub_v3_v3v3(final_disp, smooth_co, vd.co);
+    madd_v3_v3v3fl(final_disp, vd.co, final_disp, fade);
+    SCULPT_clip(data->sd, ss, vd.co, final_disp);
+
+    if (vd.mvert) {
+      vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
+    }
+    BKE_pbvh_vertex_iter_end;
+  }
+}
+
+void SCULPT_do_directional_smooth_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
+{
+  Brush *brush = BKE_paint_brush(&sd->paint);
+
+  /* Threaded loop over nodes. */
+  SculptThreadedTaskData data = {
+      .sd = sd,
+      .ob = ob,
+      .brush = brush,
+      .nodes = nodes,
+  };
+
+  TaskParallelSettings settings;
+  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
+  for (int i = 0; i < brush->surface_smooth_iterations; i++) {
+    BLI_task_parallel_range(0, totnode, &data, SCULPT_do_directional_smooth_task_cb_ex, &settings);
+  }
+}
diff --git a/source/blender/makesdna/DNA_brush_enums.h b/source/blender/makesdna/DNA_brush_enums.h
index 9b74ddada51..1d6238714b0 100644
--- a/source/blender/makesdna/DNA_brush_enums.h
+++ b/source/blender/makesdna/DNA_brush_enums.h
@@ -230,6 +230,7 @@ typedef enum eBrushClothDeformType {
 typedef enum eBrushSmoothDeformType {
   BRUSH_SMOOTH_DEFORM_LAPLACIAN = 0,
   BRUSH_SMOOTH_DEFORM_SURFACE = 1,
+  BRUSH_SMOOTH_DEFORM_DIRECTIONAL = 2,
 } eBrushSmoothDeformType;
 
 typedef enum eBrushClothForceFalloffType {
diff --git a/source/blender/makesrna/intern/rna_brush.c b/source/blender/makesrna/intern/rna_brush.c
index f0c970830f6..389c6ccde94 100644
--- a/source/blender/makesrna/intern/rna_brush.c
+++ b/source/blender/makesrna/intern/rna_brush.c
@@ -2164,6 +2164,11 @@ static void rna_def_brush(BlenderRNA *brna)
        0,
        "Surface",
        "Smooths the surface of the mesh, preserving the volume"},
+      {BRUSH_SMOOTH_DEFORM_DIRECTIONAL,
+       "DIRECTIONAL",
+       0,
+       "Directional",
+       "Smooths the surface taking into account the direction of the stroke"},
       {0, NULL, 0, NULL, NULL},
   };



More information about the Bf-blender-cvs mailing list