[Bf-blender-cvs] [8b97a539aa5] greasepencil-object: New Smooth modifer

Antonio Vazquez noreply at git.blender.org
Mon Nov 13 11:02:03 CET 2017


Commit: 8b97a539aa5bb62d65e6a3505600df80253e09ea
Author: Antonio Vazquez
Date:   Mon Nov 13 11:01:44 2017 +0100
Branches: greasepencil-object
https://developer.blender.org/rB8b97a539aa5bb62d65e6a3505600df80253e09ea

New Smooth modifer

Allow to apply smooth to the stroke after drawing.

This is not related to the smooth of the stroke while drawing.

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

M	release/scripts/startup/bl_ui/properties_data_modifier.py
M	source/blender/blenkernel/BKE_gpencil.h
M	source/blender/blenkernel/intern/gpencil.c
M	source/blender/editors/gpencil/gpencil_brush.c
M	source/blender/editors/gpencil/gpencil_paint.c
M	source/blender/editors/gpencil/gpencil_utils.c
M	source/blender/editors/space_outliner/outliner_draw.c
M	source/blender/makesdna/DNA_modifier_types.h
M	source/blender/makesrna/RNA_access.h
M	source/blender/makesrna/intern/rna_modifier.c
M	source/blender/modifiers/CMakeLists.txt
M	source/blender/modifiers/MOD_modifiertypes.h
A	source/blender/modifiers/intern/MOD_gpencilsmooth.c
M	source/blender/modifiers/intern/MOD_util.c

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

diff --git a/release/scripts/startup/bl_ui/properties_data_modifier.py b/release/scripts/startup/bl_ui/properties_data_modifier.py
index e582e90ad22..da68565e571 100644
--- a/release/scripts/startup/bl_ui/properties_data_modifier.py
+++ b/release/scripts/startup/bl_ui/properties_data_modifier.py
@@ -1578,6 +1578,38 @@ class DATA_PT_modifiers(ModifierButtonsPanel, Panel):
         row.prop(md, "affect_strength", text="Strength", icon='COLOR', toggle=True)
         row.prop(md, "affect_thickness", text="Thickness", icon='LINE_DATA', toggle=True)
 
+    def GP_SMOOTH(self, layout, ob, md):
+        gpd = ob.data
+        split = layout.split()
+
+        col = split.column()
+        row = col.row(align=False)
+        row.prop(md, "factor")
+        row.prop(md, "step")
+
+        split = layout.split()
+        col = split.column()
+        col.label("Layer:")
+        row = col.row(align=True)
+        row.prop_search(md, "layer", gpd, "layers", text="", icon='GREASEPENCIL')
+        row.prop(md, "inverse_layers", text="", icon="ARROW_LEFTRIGHT")
+
+        col.label("Vertex Group:")
+        row = col.row(align=True)
+        row.prop_search(md, "vertex_group", ob, "vertex_groups", text="")
+        row.prop(md, "inverse_vertex", text="", icon="ARROW_LEFTRIGHT")
+
+        row = col.row(align=True)
+        row.prop(md, "pass_index", text="Pass")
+        row.prop(md, "inverse_pass", text="", icon="ARROW_LEFTRIGHT")
+
+        row = layout.row(align=True)
+        row.label("Affect:")
+        row = layout.row(align=True)
+        row.prop(md, "affect_position", text="Position", icon='MESH_DATA', toggle=True)
+        row.prop(md, "affect_strength", text="Strength", icon='COLOR', toggle=True)
+        row.prop(md, "affect_thickness", text="Thickness", icon='LINE_DATA', toggle=True)
+
     def GP_SUBDIV(self, layout, ob, md):
         gpd = ob.data
         split = layout.split()
diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index ac242620005..5b4ed72c82d 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -211,5 +211,8 @@ void BKE_gpencil_simplify_stroke(struct bGPDlayer *gpl, struct bGPDstroke *gps,
 
 void BKE_gpencil_transform(struct bGPdata *gpd, float mat[4][4]);
 
+bool BKE_gp_smooth_stroke(struct bGPDstroke *gps, int i, float inf, bool affect_pressure);
+bool BKE_gp_smooth_stroke_strength(struct bGPDstroke *gps, int i, float inf);
+bool BKE_gp_smooth_stroke_thickness(struct bGPDstroke *gps, int i, float inf);
 
 #endif /*  __BKE_GPENCIL_H__ */
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index a710bd344eb..be9d62f7b9c 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -2349,3 +2349,160 @@ bool BKE_gpencil_vgroup_remove_point_weight(bGPDspoint *pt, int index)
 
 
 /* ************************************************** */
+/**
+* Apply smooth to stroke point
+* \param gps              Stroke to smooth
+* \param i                Point index
+* \param inf              Amount of smoothing to apply
+* \param affect_pressure  Apply smoothing to pressure values too?
+*/
+bool BKE_gp_smooth_stroke(bGPDstroke *gps, int i, float inf, bool affect_pressure)
+{
+	bGPDspoint *pt = &gps->points[i];
+	float pressure = 0.0f;
+	float sco[3] = { 0.0f };
+
+	/* Do nothing if not enough points to smooth out */
+	if (gps->totpoints <= 2) {
+		return false;
+	}
+
+	/* Only affect endpoints by a fraction of the normal strength,
+	* to prevent the stroke from shrinking too much
+	*/
+	if ((i == 0) || (i == gps->totpoints - 1)) {
+		inf *= 0.1f;
+	}
+
+	/* Compute smoothed coordinate by taking the ones nearby */
+	/* XXX: This is potentially slow, and suffers from accumulation error as earlier points are handled before later ones */
+	{
+		// XXX: this is hardcoded to look at 2 points on either side of the current one (i.e. 5 items total)
+		const int   steps = 2;
+		const float average_fac = 1.0f / (float)(steps * 2 + 1);
+		int step;
+
+		/* add the point itself */
+		madd_v3_v3fl(sco, &pt->x, average_fac);
+
+		if (affect_pressure) {
+			pressure += pt->pressure * average_fac;
+		}
+
+		/* n-steps before/after current point */
+		// XXX: review how the endpoints are treated by this algorithm
+		// XXX: falloff measures should also introduce some weighting variations, so that further-out points get less weight
+		for (step = 1; step <= steps; step++) {
+			bGPDspoint *pt1, *pt2;
+			int before = i - step;
+			int after = i + step;
+
+			CLAMP_MIN(before, 0);
+			CLAMP_MAX(after, gps->totpoints - 1);
+
+			pt1 = &gps->points[before];
+			pt2 = &gps->points[after];
+
+			/* add both these points to the average-sum (s += p[i]/n) */
+			madd_v3_v3fl(sco, &pt1->x, average_fac);
+			madd_v3_v3fl(sco, &pt2->x, average_fac);
+
+#if 0
+			/* XXX: Disabled because get weird result */
+			/* do pressure too? */
+			if (affect_pressure) {
+				pressure += pt1->pressure * average_fac;
+				pressure += pt2->pressure * average_fac;
+			}
+#endif
+		}
+	}
+
+	/* Based on influence factor, blend between original and optimal smoothed coordinate */
+	interp_v3_v3v3(&pt->x, &pt->x, sco, inf);
+
+#if 0
+	/* XXX: Disabled because get weird result */
+	if (affect_pressure) {
+		pt->pressure = pressure;
+	}
+#endif
+
+	return true;
+}
+
+/**
+* Apply smooth for strength to stroke point
+* \param gps              Stroke to smooth
+* \param i                Point index
+* \param inf              Amount of smoothing to apply
+*/
+bool BKE_gp_smooth_stroke_strength(bGPDstroke *gps, int i, float inf)
+{
+	bGPDspoint *ptb = &gps->points[i];
+
+	/* Do nothing if not enough points */
+	if (gps->totpoints <= 2) {
+		return false;
+	}
+
+	/* Compute theoretical optimal value using distances */
+	bGPDspoint *pta, *ptc;
+	int before = i - 1;
+	int after = i + 1;
+
+	CLAMP_MIN(before, 0);
+	CLAMP_MAX(after, gps->totpoints - 1);
+
+	pta = &gps->points[before];
+	ptc = &gps->points[after];
+
+	/* the optimal value is the corresponding to the interpolation of the strength
+	*  at the distance of point b
+	*/
+	const float fac = line_point_factor_v3(&ptb->x, &pta->x, &ptc->x);
+	const float optimal = (1.0f - fac) * pta->strength + fac * ptc->strength;
+
+	/* Based on influence factor, blend between original and optimal */
+	ptb->strength = (1.0f - inf) * ptb->strength + inf * optimal;
+
+	return true;
+}
+
+/**
+* Apply smooth for thickness to stroke point (use pressure)
+* \param gps              Stroke to smooth
+* \param i                Point index
+* \param inf              Amount of smoothing to apply
+*/
+bool BKE_gp_smooth_stroke_thickness(bGPDstroke *gps, int i, float inf)
+{
+	bGPDspoint *ptb = &gps->points[i];
+
+	/* Do nothing if not enough points */
+	if (gps->totpoints <= 2) {
+		return false;
+	}
+
+	/* Compute theoretical optimal value using distances */
+	bGPDspoint *pta, *ptc;
+	int before = i - 1;
+	int after = i + 1;
+
+	CLAMP_MIN(before, 0);
+	CLAMP_MAX(after, gps->totpoints - 1);
+
+	pta = &gps->points[before];
+	ptc = &gps->points[after];
+
+	/* the optimal value is the corresponding to the interpolation of the pressure
+	*  at the distance of point b
+	*/
+	float fac = line_point_factor_v3(&ptb->x, &pta->x, &ptc->x);
+	float optimal = (1.0f - fac) * pta->pressure + fac * ptc->pressure;
+
+	/* Based on influence factor, blend between original and optimal */
+	ptb->pressure = (1.0f - inf) * ptb->pressure + inf * optimal;
+
+	return true;
+}
diff --git a/source/blender/editors/gpencil/gpencil_brush.c b/source/blender/editors/gpencil/gpencil_brush.c
index 1ee0d7bfe1e..88aaab7099e 100644
--- a/source/blender/editors/gpencil/gpencil_brush.c
+++ b/source/blender/editors/gpencil/gpencil_brush.c
@@ -276,13 +276,13 @@ static bool gp_brush_smooth_apply(
 
 	/* perform smoothing */
 	if (gso->settings->flag & GP_BRUSHEDIT_FLAG_APPLY_POSITION) {
-		gp_smooth_stroke(gps, pt_index, inf, affect_pressure);
+		BKE_gp_smooth_stroke(gps, pt_index, inf, affect_pressure);
 	}
 	if (gso->settings->flag & GP_BRUSHEDIT_FLAG_APPLY_STRENGTH) {
-		gp_smooth_stroke_strength(gps, pt_index, inf);
+		BKE_gp_smooth_stroke_strength(gps, pt_index, inf);
 	}
 	if (gso->settings->flag & GP_BRUSHEDIT_FLAG_APPLY_THICKNESS) {
-		gp_smooth_stroke_thickness(gps, pt_index, inf);
+		BKE_gp_smooth_stroke_thickness(gps, pt_index, inf);
 	}
 	
 	return true;
diff --git a/source/blender/editors/gpencil/gpencil_paint.c b/source/blender/editors/gpencil/gpencil_paint.c
index 410c59ed705..b44a88da903 100644
--- a/source/blender/editors/gpencil/gpencil_paint.c
+++ b/source/blender/editors/gpencil/gpencil_paint.c
@@ -1144,7 +1144,7 @@ static void gp_stroke_newfrombuffer(tGPsdata *p)
 			for (int r = 0; r < brush->draw_smoothlvl; ++r) {
 				for (i = 0; i < gps->totpoints; i++) {
 					/* NOTE: No pressure smoothing, or else we get annoying thickness changes while drawing... */
-					gp_smooth_stroke(gps, i, brush->draw_smoothfac - reduce, false);
+					BKE_gp_smooth_stroke(gps, i, brush->draw_smoothfac - reduce, false);
 				}
 				reduce += 0.25f;  // reduce the factor
 			}
diff --git a/source/blender/editors/gpencil/gpencil_utils.c b/source/blender/editors/gpencil/gpencil_utils.c
index 70410c1eaad..b3b33b6b3aa 100644
--- a/source/blender/editors/gpencil/gpencil_utils.c
+++ b/source/blender/editors/gpencil/gpencil_utils.c
@@ -699,164 +699,6 @@ bool gp_point_xy_to_3d(GP_SpaceConversion *gsc, Scene *scene, const float screen
 	}
 }
 
-/**
- * Apply smooth to stroke point
- * \param gps              Stroke to smooth
- * \param i                Point index
- * \param inf              Amount of smoothing to apply
- * \param affect_pressure  Apply smoothing to pressure values too?
- */
-bool gp_smooth_stroke(bGPDstroke *gps, int i, float inf, bool affect_pressure)
-{
-	bGPDspoint *pt = &gps->points[i];
-	float pressure = 0.0f;
-	float sco[3] = {0.0f};
-	
-	/* Do nothing if not enough points to smooth out */
-	if (gps->totpoints <= 2) {
-		return false;
-	}
-	
-	/* Only affect endpoints by a fraction of the normal strength,
-	 * to prevent the stroke from shrinking too much
-	 */
-	if ((i == 0) || (i == gps->totpoints - 1)) {
-	

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list