Commit: 9e23f80d8289170032c8f48a0395a3eb46989438
Author: Antonio Vazquez
Date:   Wed Mar 7 17:44:41 2018 +0100
Branches: greasepencil-object
https://developer.blender.org/rB9e23f80d8289170032c8f48a0395a3eb46989438

WIP: Smooth while drawing stroke

Initial implementation of active smooth while drawing.

The UI parameter is experimental, so maybe in the future is removed/replaced.

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

M       release/scripts/startup/bl_ui/space_view3d_toolbar.py
M       source/blender/editors/gpencil/gpencil_paint.c
M       source/blender/makesdna/DNA_gpencil_types.h
M       source/blender/makesrna/intern/rna_scene.c

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

diff --git a/release/scripts/startup/bl_ui/space_view3d_toolbar.py 
b/release/scripts/startup/bl_ui/space_view3d_toolbar.py
index aaa532fe353..bcc9641f553 100644
--- a/release/scripts/startup/bl_ui/space_view3d_toolbar.py
+++ b/release/scripts/startup/bl_ui/space_view3d_toolbar.py
@@ -2193,8 +2193,10 @@ class VIEW3D_PT_tools_grease_pencil_brush_option(Panel):
             row = layout.row(align=True)
             row.prop(brush, "uv_random", slider=True)
 
-            row = layout.row(align=True)
+            row = layout.row()
             row.prop(brush, "input_samples")
+            row.prop(brush, "active_smooth_factor")
+
 
 # Grease Pencil drawing brushes mode
 class VIEW3D_PT_tools_grease_pencil_brush_mode(Panel):
diff --git a/source/blender/editors/gpencil/gpencil_paint.c 
b/source/blender/editors/gpencil/gpencil_paint.c
index b4591924cfa..d005e50da86 100644
--- a/source/blender/editors/gpencil/gpencil_paint.c
+++ b/source/blender/editors/gpencil/gpencil_paint.c
@@ -492,6 +492,59 @@ static void gp_brush_angle(bGPdata *gpd, bGPDbrush *brush, 
tGPspoint *pt, const
 
 }
 
+/* Apply smooth to buffer while drawing
+* to smooth point C, use 2 before (A, B) and current point (D):
+*
+*   A----B-----C------D
+*/
+static void gp_smooth_buffer(tGPsdata *p, float inf)
+{
+       bGPdata *gpd = p->gpd;
+       short num_points = gpd->sbuffer_size;
+
+       /* Do nothing if not enough points to smooth out */
+       if ((num_points < 3) && (inf > 0.0f)) {
+               return;
+       }
+
+       tGPspoint *points = (tGPspoint *)gpd->sbuffer;
+       float steps = 4.0f;
+       if (num_points < 4) {
+               steps--;
+       }
+       
+       tGPspoint *pta = num_points >= 4 ? &points[num_points - 4] : NULL;
+       tGPspoint *ptb = num_points >= 3 ? &points[num_points - 3] : NULL;
+       tGPspoint *ptc = num_points >= 2 ? &points[num_points - 2] : NULL;
+       tGPspoint *ptd = &points[num_points - 1];
+
+       float sco[2] = { 0.0f };
+       float a[2], b[2], c[2], d[2];
+       const float average_fac = 1.0f / steps;
+
+       /* Compute smoothed coordinate by taking the ones nearby */
+       if (pta) {
+               copy_v2fl_v2i(a, &pta->x);
+               madd_v2_v2fl(sco, a, average_fac);
+       }
+       if (ptb) {
+               copy_v2fl_v2i(b, &ptb->x);
+               madd_v2_v2fl(sco, b, average_fac);
+       }
+       if (ptc) {
+               copy_v2fl_v2i(c, &ptc->x);
+               madd_v2_v2fl(sco, c, average_fac);
+       }
+       if (ptd) {
+               copy_v2fl_v2i(d, &ptd->x);
+               madd_v2_v2fl(sco, d, average_fac);
+       }
+
+       /* Based on influence factor, blend between original and optimal 
smoothed coordinate */
+       interp_v2_v2v2(c, c, sco, inf);
+       round_v2i_v2fl(&ptc->x, c);
+}
+
 /* add current stroke-point to buffer (returns whether point was successfully 
added) */
 static short gp_stroke_addpoint(
         tGPsdata *p, const int mval[2], float pressure, double curtime)
@@ -656,6 +709,9 @@ static short gp_stroke_addpoint(
                /* increment counters */
                gpd->sbuffer_size++;
 
+               /* smooth while drawing previous point */
+               gp_smooth_buffer(p, 0.8f);
+
                /* check if another operation can still occur */
                if (gpd->sbuffer_size == GP_STROKE_BUFFER_MAX)
                        return GP_STROKEADD_FULL;
@@ -2701,8 +2757,8 @@ static void gpencil_add_missing_events(bContext *C, 
wmOperator *op, const wmEven
                scale = defaultpixsize;
        }
 
-       /* The thickness of the brush is reduced at 50 % of thickness to get 
overlap dots */ 
-       float factor = ((thickness * 0.50f) / scale) * samples;
+       /* The thickness of the brush is reduced of thickness to get overlap 
dots */ 
+       float factor = ((thickness * 0.40f) / scale) * samples;
 
        copy_v2fl_v2i(a, p->mvalo);
        b[0] = event->mval[0] + 1;
diff --git a/source/blender/makesdna/DNA_gpencil_types.h 
b/source/blender/makesdna/DNA_gpencil_types.h
index 214ecb58f59..85de40167fe 100644
--- a/source/blender/makesdna/DNA_gpencil_types.h
+++ b/source/blender/makesdna/DNA_gpencil_types.h
@@ -141,7 +141,7 @@ typedef struct bGPDbrush {
        int   input_samples;      /* maximum distance before generate new point 
for very fast mouse movements */
        int   type;               /* type of brush (draw, fill, erase, etc..) */
        int   eraser_mode;        /* soft, hard or stroke */
-       char  pad[4];
+       float active_smooth;      /* smooth while drawing factor */
 } bGPDbrush;
 
 /* bGPDbrush->flag */
diff --git a/source/blender/makesrna/intern/rna_scene.c 
b/source/blender/makesrna/intern/rna_scene.c
index c1807ac3557..48f15b493b4 100644
--- a/source/blender/makesrna/intern/rna_scene.c
+++ b/source/blender/makesrna/intern/rna_scene.c
@@ -2440,6 +2440,14 @@ static void rna_def_gpencil_brush(BlenderRNA *brna)
        RNA_def_property_ui_text(prop, "Input Samples", "Generate intermediate 
points for very fast mouse movements. Set to 0 to disable");
        RNA_def_property_update(prop, NC_GPENCIL | ND_DATA, NULL);
 
+       /* active smooth factor while drawing */
+       prop = RNA_def_property(srna, "active_smooth_factor", PROP_FLOAT, 
PROP_NONE);
+       RNA_def_property_float_sdna(prop, NULL, "active_smooth");
+       RNA_def_property_range(prop, 0.0f, 1.0f);
+       RNA_def_property_ui_text(prop, "Active Smooth",
+               "Amount of smoothing while drawing ");
+       RNA_def_property_update(prop, NC_GPENCIL | ND_DATA, NULL);
+
        /* Flags */
        prop = RNA_def_property(srna, "use_pressure", PROP_BOOLEAN, PROP_NONE);
        RNA_def_property_boolean_sdna(prop, NULL, "flag", 
GP_BRUSH_USE_PRESSURE);

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to