[Bf-blender-cvs] [1fc71bf2eae] greasepencil-object: GPencil: Basic spread point functionality

Antonio Vazquez noreply at git.blender.org
Wed Jun 24 16:01:37 CEST 2020


Commit: 1fc71bf2eaec676f1162aed2b3bd4d11b1c16976
Author: Antonio Vazquez
Date:   Mon Jun 22 18:51:32 2020 +0200
Branches: greasepencil-object
https://developer.blender.org/rB1fc71bf2eaec676f1162aed2b3bd4d11b1c16976

GPencil: Basic spread point functionality

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

M	release/scripts/startup/bl_ui/space_view3d_toolbar.py
M	source/blender/editors/gpencil/gpencil_paint.c
M	source/blender/editors/gpencil/gpencil_utils.c
M	source/blender/editors/include/ED_gpencil.h
M	source/blender/makesdna/DNA_brush_types.h
M	source/blender/makesrna/intern/rna_brush.c

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

diff --git a/release/scripts/startup/bl_ui/space_view3d_toolbar.py b/release/scripts/startup/bl_ui/space_view3d_toolbar.py
index a951025166e..254b92ba493 100644
--- a/release/scripts/startup/bl_ui/space_view3d_toolbar.py
+++ b/release/scripts/startup/bl_ui/space_view3d_toolbar.py
@@ -1623,6 +1623,9 @@ class VIEW3D_PT_tools_grease_pencil_brush_random(View3DPanel, Panel):
             col.template_curve_mapping(gp_settings, "curve_jitter", brush=True,
                                 use_negative_slope=True)
 
+        row = col.row(align=True)
+        row.prop(gp_settings, "pen_spread")
+
 
 class VIEW3D_PT_tools_grease_pencil_brush_paint_falloff(GreasePencilBrushFalloff, Panel, View3DPaintPanel):
     bl_context = ".greasepencil_paint"
diff --git a/source/blender/editors/gpencil/gpencil_paint.c b/source/blender/editors/gpencil/gpencil_paint.c
index e3512b8660f..eacf0d80be7 100644
--- a/source/blender/editors/gpencil/gpencil_paint.c
+++ b/source/blender/editors/gpencil/gpencil_paint.c
@@ -911,6 +911,11 @@ static short gp_stroke_addpoint(tGPsdata *p, const float mval[2], float pressure
       }
     }
 
+    /* Spread points. */
+    if (brush_settings->flag & GP_BRUSH_GROUP_RANDOM) {
+      ED_gpencil_stroke_buffer_spread(brush, &p->gsc);
+    }
+
     /* Update evaluated data. */
     ED_gpencil_sbuffer_update_eval(gpd, p->ob_eval);
 
diff --git a/source/blender/editors/gpencil/gpencil_utils.c b/source/blender/editors/gpencil/gpencil_utils.c
index 422b5e0c1b9..7508cec8eac 100644
--- a/source/blender/editors/gpencil/gpencil_utils.c
+++ b/source/blender/editors/gpencil/gpencil_utils.c
@@ -2978,3 +2978,134 @@ bool ED_gpencil_stroke_point_is_inside(bGPDstroke *gps,
 
   return hit;
 }
+
+/* Helper to move a point in buffer. */
+void gpencil_spread_point(
+    Brush *brush, GP_SpaceConversion *gsc, const int pt_index, const float x, const float y)
+{
+  RegionView3D *rv3d = gsc->region->regiondata;
+  float pixsize = rv3d->pixsize * 1000.0f;
+  const float factor = brush->size * pixsize;
+  float rand = 1.0f;
+
+  bGPdata *gpd = gsc->gpd;
+  tGPspoint *pt = ((tGPspoint *)(gpd->runtime.sbuffer) + pt_index);
+  rand = BLI_hash_int_01(BLI_hash_int_2d(pt->x + gpd->runtime.sbuffer_used, pt->y)) * 2.0f - 1.0f;
+  pt->x += x * factor * rand;
+
+  rand = BLI_hash_int_01(BLI_hash_int_2d(pt->y + gpd->runtime.sbuffer_used, pt->x)) * 2.0f - 1.0f;
+  pt->y += y * factor * rand;
+
+  /* Randomness to other factors. */
+  rand = BLI_hash_int_01(BLI_hash_int_2d(pt->x, pt->y)) * 2.0f - 1.0f;
+  pt->pressure += rand * 1.5f;
+
+  rand = BLI_hash_int_01(BLI_hash_int_2d(pt->x + pt->y, pt->x)) * 2.0f - 1.0f;
+  pt->strength += rand * 1.5f;
+  CLAMP(pt->strength, 0.1f, 1.0f);
+}
+
+/**
+ * Spread last buffer point to get more topoly
+ * @param brush  Current brush
+ * @param gpd  Current datablock
+ */
+void ED_gpencil_stroke_buffer_spread(Brush *brush, GP_SpaceConversion *gsc)
+{
+  bGPdata *gpd = gsc->gpd;
+  const int spread = brush->gpencil_settings->draw_spread;
+  if (spread == 0) {
+    return;
+  }
+  const int last_index = gpd->runtime.sbuffer_used - 1;
+
+  /* Increase the buffer size to hold the new points.
+   * As the function add 1, add only spread point minus 1. */
+  gpd->runtime.sbuffer_used += spread - 1;
+  gpd->runtime.sbuffer = ED_gpencil_sbuffer_ensure(
+      gpd->runtime.sbuffer, &gpd->runtime.sbuffer_size, &gpd->runtime.sbuffer_used, false);
+  /* Increment counters after expand buffer. */
+  gpd->runtime.sbuffer_used++;
+
+  /* Duplicate las point to the new point. */
+  tGPspoint *pt_orig = ((tGPspoint *)(gpd->runtime.sbuffer) + last_index);
+  for (int i = 0; i < spread; i++) {
+    tGPspoint *pt = ((tGPspoint *)(gpd->runtime.sbuffer) + last_index + i + 1);
+    copy_v2_v2(&pt->x, &pt_orig->x);
+    pt->pressure = pt_orig->pressure;
+    pt->strength = pt_orig->strength;
+    pt->time = pt_orig->time;
+    pt->uv_fac = pt_orig->uv_fac;
+    pt->uv_rot = pt_orig->uv_rot;
+    copy_v3_v3(pt->rnd, pt_orig->rnd);
+    pt->rnd_dirty = pt_orig->rnd_dirty;
+    copy_v4_v4(pt->vert_color, pt_orig->vert_color);
+  }
+  /* Spread the points. */
+  switch (spread) {
+    case 1: {
+      gpencil_spread_point(brush, gsc, last_index, -1.0f, 0.0f);
+      gpencil_spread_point(brush, gsc, last_index + 1, 1.0f, 0.0f);
+      break;
+    }
+    case 2: {
+      gpencil_spread_point(brush, gsc, last_index, -0.7f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 2, 0.7f, -1.0f);
+      break;
+    }
+    case 3: {
+      gpencil_spread_point(brush, gsc, last_index, -1.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 1, 1.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 3, 0.0f, -1.0f);
+      break;
+    }
+    case 4: {
+      gpencil_spread_point(brush, gsc, last_index, -1.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 1, 1.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 3, -1.0f, -1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 4, 1.0f, -1.0f);
+      break;
+    }
+    case 5: {
+      gpencil_spread_point(brush, gsc, last_index, -1.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 1, 0.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 2, 1.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 4, -1.0f, -1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 5, 1.0f, -1.0f);
+      break;
+    }
+    case 6: {
+      gpencil_spread_point(brush, gsc, last_index, -1.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 1, 0.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 2, 1.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 4, -1.0f, -1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 5, 0.0f, -1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 6, 1.0f, -1.0f);
+      break;
+    }
+    case 7: {
+      gpencil_spread_point(brush, gsc, last_index, -1.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 1, 0.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 2, 1.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 3, -0.7f, 0.0f);
+      gpencil_spread_point(brush, gsc, last_index + 4, 0.7f, 0.0f);
+      gpencil_spread_point(brush, gsc, last_index + 5, -1.0f, -1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 6, 0.0f, -1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 7, 1.0f, -1.0f);
+      break;
+    }
+    case 8: {
+      gpencil_spread_point(brush, gsc, last_index, -1.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 1, 0.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 2, 1.0f, 1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 3, -1.0f, 0.0f);
+      gpencil_spread_point(brush, gsc, last_index + 5, 1.0f, 0.0f);
+      gpencil_spread_point(brush, gsc, last_index + 6, -1.0f, -1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 7, 0.0f, -1.0f);
+      gpencil_spread_point(brush, gsc, last_index + 8, 1.0f, -1.0f);
+      break;
+    }
+    default:
+      break;
+  }
+}
diff --git a/source/blender/editors/include/ED_gpencil.h b/source/blender/editors/include/ED_gpencil.h
index 3b0db27e58d..7ebf7501851 100644
--- a/source/blender/editors/include/ED_gpencil.h
+++ b/source/blender/editors/include/ED_gpencil.h
@@ -356,6 +356,8 @@ bool ED_gpencil_stroke_point_is_inside(struct bGPDstroke *gps,
                                        int mouse[2],
                                        const float diff_mat[4][4]);
 
+void ED_gpencil_stroke_buffer_spread(struct Brush *brush, struct GP_SpaceConversion *gsc);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/makesdna/DNA_brush_types.h b/source/blender/makesdna/DNA_brush_types.h
index a1c69af4750..b8ea46d85c1 100644
--- a/source/blender/makesdna/DNA_brush_types.h
+++ b/source/blender/makesdna/DNA_brush_types.h
@@ -50,7 +50,8 @@ typedef struct BrushClone {
 typedef struct BrushGpencilSettings {
   /** Amount of smoothing to apply to newly created strokes. */
   float draw_smoothfac;
-  char _pad2[4];
+  /** Spread of points to increase topology density. */
+  int draw_spread;
   /** Amount of alpha strength to apply to newly created strokes. */
   float draw_strength;
   /** Amount of jitter to apply to newly created strokes. */
diff --git a/source/blender/makesrna/intern/rna_brush.c b/source/blender/makesrna/intern/rna_brush.c
index 59656b48cfe..2e996038c7a 100644
--- a/source/blender/makesrna/intern/rna_brush.c
+++ b/source/blender/makesrna/intern/rna_brush.c
@@ -1206,6 +1206,14 @@ static void rna_def_gpencil_options(BlenderRNA *brna)
   RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
   RNA_def_property_update(prop, NC_GPENCIL | ND_DATA, NULL);
 
+  /* Spread value. */
+  prop = RNA_def_property(srna, "pen_spread", PROP_INT, PROP_NONE);
+  RNA_def_property_int_sdna(prop, NULL, "draw_spread");
+  RNA_def_property_range(prop, 0, 8);
+  RNA_def_property_ui_text(prop, "Spread", "Number of points spread by point");
+  RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+  RNA_def_property_update(prop, NC_GPENCIL | ND_DATA, NULL);
+
   /* Randomnes factor for pressure */
   prop = RNA_def_property(srna, "random_pressure", PROP_FLOAT, PROP_FACTOR);
   RNA_def_property_float_sdna(prop, NULL, "draw_random_press");



More information about the Bf-blender-cvs mailing list