GPv3: Smooth modifier

Smooth modifier ported to Grease Pencil 3.0

Exposed `smooth_curve_attribute()` from `grease_pencil_edit.cc`
to achieve the smoothing effect. It will not be the exact same result
as the old algorithm.

Pull Request: https://projects.blender.org/blender/blender/pulls/116975
This commit is contained in:
YimingWu 2024-01-24 04:36:36 +01:00 committed by YimingWu
parent 53a8570aec
commit f54348edc9
9 changed files with 398 additions and 3 deletions

@ -75,7 +75,7 @@ class OBJECT_MT_modifier_add(ModifierAddMenu, Menu):
layout.menu("OBJECT_MT_modifier_add_edit")
if ob_type in {'MESH', 'CURVE', 'FONT', 'SURFACE', 'VOLUME', 'GREASEPENCIL'}:
layout.menu("OBJECT_MT_modifier_add_generate")
if ob_type in {'MESH', 'CURVE', 'FONT', 'SURFACE', 'LATTICE', 'VOLUME'}:
if ob_type in {'MESH', 'CURVE', 'FONT', 'SURFACE', 'LATTICE', 'VOLUME', 'GREASEPENCIL'}:
layout.menu("OBJECT_MT_modifier_add_deform")
if ob_type in {'MESH', 'CURVE', 'FONT', 'SURFACE', 'LATTICE'}:
layout.menu("OBJECT_MT_modifier_add_physics")
@ -187,6 +187,8 @@ class OBJECT_MT_modifier_add_deform(ModifierAddMenu, Menu):
self.operator_modifier_add(layout, 'WAVE')
if ob_type == 'VOLUME':
self.operator_modifier_add(layout, 'VOLUME_DISPLACE')
if ob_type == 'GREASEPENCIL':
self.operator_modifier_add(layout, 'GREASEPENCIL_SMOOTH')
layout.template_modifier_asset_menu_items(catalog_path=self.bl_label)

@ -829,4 +829,11 @@
.color = {1.0f, 1.0f, 1.0f}, \
}
#define _DNA_DEFAULT_GreasePencilSmoothModifierData \
{ \
.flag = MOD_GREASE_PENCIL_SMOOTH_MOD_LOCATION, \
.factor = 1.0f, \
.step = 1, \
}
/* clang-format off */

@ -97,6 +97,7 @@ typedef enum ModifierType {
eModifierType_GreasePencilSubdiv = 62,
eModifierType_GreasePencilColor = 63,
eModifierType_GreasePencilTint = 64,
eModifierType_GreasePencilSmooth = 65,
NUM_MODIFIER_TYPES,
} ModifierType;
@ -2607,3 +2608,27 @@ typedef enum GreasePencilTintModifierFlag {
/* Use vertex group as factors instead of influence. */
MOD_GREASE_PENCIL_TINT_USE_WEIGHT_AS_FACTOR = (1 << 0),
} GreasePencilTintModifierFlag;
typedef struct GreasePencilSmoothModifierData {
ModifierData modifier;
GreasePencilModifierInfluenceData influence;
/** `eGreasePencilSmooth_Flag. */
int flag;
/** Factor of smooth. */
float factor;
/** How many times apply smooth. */
int step;
char _pad[4];
void *_pad1;
} GreasePencilSmoothModifierData;
typedef enum eGreasePencilSmooth_Flag {
MOD_GREASE_PENCIL_SMOOTH_OPEN_INFLUENCE_PANEL = (1 << 0),
MOD_GREASE_PENCIL_SMOOTH_MOD_LOCATION = (1 << 1),
MOD_GREASE_PENCIL_SMOOTH_MOD_STRENGTH = (1 << 2),
MOD_GREASE_PENCIL_SMOOTH_MOD_THICKNESS = (1 << 3),
MOD_GREASE_PENCIL_SMOOTH_MOD_UV = (1 << 4),
MOD_GREASE_PENCIL_SMOOTH_KEEP_SHAPE = (1 << 5),
MOD_GREASE_PENCIL_SMOOTH_SMOOTH_ENDS = (1 << 6),
} eGreasePencilSmooth_Flag;

@ -294,6 +294,9 @@ SDNA_DEFAULT_DECL_STRUCT(WeldModifierData);
SDNA_DEFAULT_DECL_STRUCT(WireframeModifierData);
SDNA_DEFAULT_DECL_STRUCT(GreasePencilSubdivModifierData);
/* Grease Pencil 3.0 modifiers. */
SDNA_DEFAULT_DECL_STRUCT(GreasePencilSmoothModifierData);
/* DNA_gpencil_modifier_defaults.h */
SDNA_DEFAULT_DECL_STRUCT(ArmatureGpencilModifierData);
SDNA_DEFAULT_DECL_STRUCT(ArrayGpencilModifierData);
@ -542,6 +545,9 @@ const void *DNA_default_table[SDNA_TYPE_MAX] = {
SDNA_DEFAULT_DECL(WireframeModifierData),
SDNA_DEFAULT_DECL(GreasePencilSubdivModifierData),
/* Grease Pencil 3.0 defaults. */
SDNA_DEFAULT_DECL(GreasePencilSmoothModifierData),
/* DNA_gpencil_modifier_defaults.h */
SDNA_DEFAULT_DECL(ArmatureGpencilModifierData),
SDNA_DEFAULT_DECL(ArrayGpencilModifierData),

@ -296,6 +296,11 @@ const EnumPropertyItem rna_enum_object_modifier_type_items[] = {
ICON_VOLUME_DATA,
"Volume Displace",
"Deform volume based on noise or other vector fields"}, /* TODO: Use correct icon. */
{eModifierType_GreasePencilSmooth,
"GREASEPENCIL_SMOOTH",
ICON_SMOOTHCURVE,
"Smooth",
"Smooth grease pencil strokes"},
RNA_ENUM_ITEM_HEADING(N_("Physics"), nullptr),
{eModifierType_Cloth, "CLOTH", ICON_MOD_CLOTH, "Cloth", ""},
@ -1817,10 +1822,12 @@ RNA_MOD_GREASE_PENCIL_MATERIAL_FILTER_SET(GreasePencilColor);
RNA_MOD_GREASE_PENCIL_MATERIAL_FILTER_SET(GreasePencilOpacity);
RNA_MOD_GREASE_PENCIL_MATERIAL_FILTER_SET(GreasePencilSubdiv);
RNA_MOD_GREASE_PENCIL_MATERIAL_FILTER_SET(GreasePencilTint);
RNA_MOD_GREASE_PENCIL_MATERIAL_FILTER_SET(GreasePencilSmooth);
RNA_MOD_GREASE_PENCIL_VERTEX_GROUP_SET(GreasePencilOpacity);
RNA_MOD_GREASE_PENCIL_VERTEX_GROUP_SET(GreasePencilSubdiv);
RNA_MOD_GREASE_PENCIL_VERTEX_GROUP_SET(GreasePencilTint);
RNA_MOD_GREASE_PENCIL_VERTEX_GROUP_SET(GreasePencilSmooth);
static void rna_GreasePencilOpacityModifier_opacity_factor_range(
PointerRNA *ptr, float *min, float *max, float *softmin, float *softmax)
@ -7779,7 +7786,6 @@ static void rna_def_modifier_grease_pencil_subdiv(BlenderRNA *brna)
srna, "rna_GreasePencilSubdivModifier_material_filter_set");
rna_def_modifier_grease_pencil_vertex_group(
srna, "rna_GreasePencilSubdivModifier_vertex_group_name_set");
rna_def_modifier_grease_pencil_custom_curve(srna);
rna_def_modifier_panel_open_prop(srna, "open_influence_panel", 0);
@ -7801,7 +7807,7 @@ static void rna_def_modifier_grease_pencil_color(BlenderRNA *brna)
{
StructRNA *srna;
PropertyRNA *prop;
static const EnumPropertyItem color_mode_items[] = {
{MOD_GREASE_PENCIL_COLOR_BOTH, "BOTH", 0, "Stroke & Fill", "Modify fill and stroke colors"},
{MOD_GREASE_PENCIL_COLOR_STROKE, "STROKE", 0, "Stroke", "Modify stroke color only"},
@ -7935,6 +7941,78 @@ static void rna_def_modifier_grease_pencil_tint(BlenderRNA *brna)
RNA_def_property_update(prop, 0, "rna_Modifier_update");
}
static void rna_def_modifier_grease_pencil_smooth(BlenderRNA *brna)
{
StructRNA *srna;
PropertyRNA *prop;
srna = RNA_def_struct(brna, "GreasePencilSmoothModifier", "Modifier");
RNA_def_struct_ui_text(srna, "Smooth Modifier", "Smooth effect modifier");
RNA_def_struct_sdna(srna, "GreasePencilSmoothModifierData");
RNA_def_struct_ui_icon(srna, ICON_MOD_SMOOTH);
rna_def_modifier_grease_pencil_layer_filter(srna);
rna_def_modifier_grease_pencil_material_filter(
srna, "rna_GreasePencilSmoothModifier_material_filter_set");
rna_def_modifier_grease_pencil_vertex_group(
srna, "rna_GreasePencilSmoothModifier_vertex_group_name_set");
rna_def_modifier_grease_pencil_custom_curve(srna);
rna_def_modifier_panel_open_prop(srna, "open_influence_panel", 0);
RNA_define_lib_overridable(true);
prop = RNA_def_property(srna, "factor", PROP_FLOAT, PROP_FACTOR);
RNA_def_property_float_sdna(prop, nullptr, "factor");
RNA_def_property_range(prop, 0, 1);
RNA_def_property_ui_text(prop, "Factor", "Amount of smooth to apply");
RNA_def_property_update(prop, 0, "rna_Modifier_update");
prop = RNA_def_property(srna, "use_edit_position", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, nullptr, "flag", MOD_GREASE_PENCIL_SMOOTH_MOD_LOCATION);
RNA_def_property_ui_text(
prop, "Affect Position", "The modifier affects the position of the point");
RNA_def_property_update(prop, 0, "rna_Modifier_update");
prop = RNA_def_property(srna, "use_edit_strength", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, nullptr, "flag", MOD_GREASE_PENCIL_SMOOTH_MOD_STRENGTH);
RNA_def_property_ui_text(
prop, "Affect Strength", "The modifier affects the color strength of the point");
RNA_def_property_update(prop, 0, "rna_Modifier_update");
prop = RNA_def_property(srna, "use_edit_thickness", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, nullptr, "flag", MOD_GREASE_PENCIL_SMOOTH_MOD_THICKNESS);
RNA_def_property_ui_text(
prop, "Affect Thickness", "The modifier affects the thickness of the point");
RNA_def_property_update(prop, 0, "rna_Modifier_update");
prop = RNA_def_property(srna, "use_edit_uv", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, nullptr, "flag", MOD_GREASE_PENCIL_SMOOTH_MOD_UV);
RNA_def_property_ui_text(
prop, "Affect UV", "The modifier affects the UV rotation factor of the point");
RNA_def_property_update(prop, 0, "rna_Modifier_update");
prop = RNA_def_property(srna, "step", PROP_INT, PROP_NONE);
RNA_def_property_int_sdna(prop, nullptr, "step");
RNA_def_property_range(prop, 1, 1000);
RNA_def_property_ui_text(
prop, "Steps", "Number of times to apply smooth (high numbers can reduce fps)");
RNA_def_property_update(prop, 0, "rna_Modifier_update");
prop = RNA_def_property(srna, "use_keep_shape", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, nullptr, "flag", MOD_GREASE_PENCIL_SMOOTH_KEEP_SHAPE);
RNA_def_property_ui_text(prop, "Keep Shape", "Smooth the details, but keep the overall shape");
RNA_def_property_update(prop, 0, "rna_Modifier_update");
prop = RNA_def_property(srna, "use_smooth_ends", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, nullptr, "flag", MOD_GREASE_PENCIL_SMOOTH_SMOOTH_ENDS);
RNA_def_property_ui_text(prop, "Smooth Ends", "Smooth ends of strokes");
RNA_def_property_update(prop, 0, "rna_Modifier_update");
RNA_define_lib_overridable(false);
}
void RNA_def_modifier(BlenderRNA *brna)
{
StructRNA *srna;
@ -8099,6 +8177,7 @@ void RNA_def_modifier(BlenderRNA *brna)
rna_def_modifier_grease_pencil_subdiv(brna);
rna_def_modifier_grease_pencil_color(brna);
rna_def_modifier_grease_pencil_tint(brna);
rna_def_modifier_grease_pencil_smooth(brna);
}
#endif

@ -46,6 +46,7 @@ set(SRC
intern/MOD_fluid.cc
intern/MOD_grease_pencil_color.cc
intern/MOD_grease_pencil_opacity.cc
intern/MOD_grease_pencil_smooth.cc
intern/MOD_grease_pencil_subdiv.cc
intern/MOD_grease_pencil_tint.cc
intern/MOD_grease_pencil_util.cc

@ -77,6 +77,7 @@ extern ModifierTypeInfo modifierType_GreasePencilOpacity;
extern ModifierTypeInfo modifierType_GreasePencilSubdiv;
extern ModifierTypeInfo modifierType_GreasePencilColor;
extern ModifierTypeInfo modifierType_GreasePencilTint;
extern ModifierTypeInfo modifierType_GreasePencilSmooth;
/* MOD_util.cc */

@ -0,0 +1,273 @@
/* SPDX-FileCopyrightText: 2005 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup modifiers
*/
#include "BLI_index_mask.hh"
#include "BLT_translation.h"
#include "BLO_read_write.hh"
#include "DNA_defaults.h"
#include "DNA_modifier_types.h"
#include "DNA_screen_types.h"
#include "RNA_access.hh"
#include "BKE_curves.hh"
#include "BKE_geometry_set.hh"
#include "BKE_grease_pencil.hh"
#include "BKE_lib_query.hh"
#include "BKE_modifier.hh"
#include "UI_interface.hh"
#include "UI_resources.hh"
#include "GEO_smooth_curves.hh"
#include "MOD_grease_pencil_util.hh"
#include "MOD_modifiertypes.hh"
#include "MOD_ui_common.hh"
#include "RNA_prototypes.h"
namespace blender {
static void init_data(ModifierData *md)
{
GreasePencilSmoothModifierData *gpmd = reinterpret_cast<GreasePencilSmoothModifierData *>(md);
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(gpmd, modifier));
MEMCPY_STRUCT_AFTER(gpmd, DNA_struct_default_get(GreasePencilSmoothModifierData), modifier);
modifier::greasepencil::init_influence_data(&gpmd->influence, false);
}
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
{
const GreasePencilSmoothModifierData *gmd =
reinterpret_cast<const GreasePencilSmoothModifierData *>(md);
GreasePencilSmoothModifierData *tgmd = reinterpret_cast<GreasePencilSmoothModifierData *>(
target);
BKE_modifier_copydata_generic(md, target, flag);
modifier::greasepencil::copy_influence_data(&gmd->influence, &tgmd->influence, flag);
}
static void free_data(ModifierData *md)
{
GreasePencilSmoothModifierData *mmd = reinterpret_cast<GreasePencilSmoothModifierData *>(md);
modifier::greasepencil::free_influence_data(&mmd->influence);
}
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
{
GreasePencilSmoothModifierData *mmd = reinterpret_cast<GreasePencilSmoothModifierData *>(md);
modifier::greasepencil::foreach_influence_ID_link(&mmd->influence, ob, walk, user_data);
}
static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
{
const GreasePencilSmoothModifierData *mmd =
reinterpret_cast<const GreasePencilSmoothModifierData *>(md);
BLO_write_struct(writer, GreasePencilSmoothModifierData, mmd);
modifier::greasepencil::write_influence_data(writer, &mmd->influence);
}
static void blend_read(BlendDataReader *reader, ModifierData *md)
{
GreasePencilSmoothModifierData *mmd = reinterpret_cast<GreasePencilSmoothModifierData *>(md);
modifier::greasepencil::read_influence_data(reader, &mmd->influence);
}
static void deform_drawing(const ModifierData &md,
const Object &ob,
bke::greasepencil::Drawing &drawing)
{
auto &mmd = reinterpret_cast<const GreasePencilSmoothModifierData &>(md);
const int iterations = mmd.step;
const float influence = mmd.factor;
const bool keep_shape = (mmd.flag & MOD_GREASE_PENCIL_SMOOTH_KEEP_SHAPE);
const bool smooth_ends = (mmd.flag & MOD_GREASE_PENCIL_SMOOTH_SMOOTH_ENDS);
const bool smooth_position = (mmd.flag & MOD_GREASE_PENCIL_SMOOTH_MOD_LOCATION);
const bool smooth_radius = (mmd.flag & MOD_GREASE_PENCIL_SMOOTH_MOD_THICKNESS);
const bool smooth_opacity = (mmd.flag & MOD_GREASE_PENCIL_SMOOTH_MOD_STRENGTH);
if (iterations <= 0 || influence <= 0.0f) {
return;
}
if (!(smooth_position || smooth_radius || smooth_opacity)) {
return;
}
bke::CurvesGeometry &curves = drawing.strokes_for_write();
if (curves.points_num() == 0) {
return;
}
IndexMaskMemory memory;
const IndexMask strokes = modifier::greasepencil::get_filtered_stroke_mask(
&ob, curves, mmd.influence, memory);
if (strokes.is_empty()) {
return;
}
bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
const OffsetIndices points_by_curve = curves.points_by_curve();
const VArray<bool> cyclic = curves.cyclic();
const VArray<bool> point_selection = VArray<bool>::ForSingle(true, curves.points_num());
if (smooth_position) {
bke::GSpanAttributeWriter positions = attributes.lookup_for_write_span("position");
geometry::smooth_curve_attribute(strokes,
points_by_curve,
point_selection,
cyclic,
iterations,
influence,
smooth_ends,
keep_shape,
positions.span);
positions.finish();
drawing.tag_positions_changed();
}
if (smooth_opacity && drawing.opacities().is_span()) {
bke::GSpanAttributeWriter opacities = attributes.lookup_for_write_span("opacity");
geometry::smooth_curve_attribute(strokes,
points_by_curve,
point_selection,
cyclic,
iterations,
influence,
smooth_ends,
false,
opacities.span);
opacities.finish();
}
if (smooth_radius && drawing.radii().is_span()) {
bke::GSpanAttributeWriter radii = attributes.lookup_for_write_span("radius");
geometry::smooth_curve_attribute(strokes,
points_by_curve,
point_selection,
cyclic,
iterations,
influence,
smooth_ends,
false,
radii.span);
radii.finish();
}
}
static void modify_geometry_set(ModifierData *md,
const ModifierEvalContext *ctx,
bke::GeometrySet *geometry_set)
{
GreasePencilSmoothModifierData *mmd = reinterpret_cast<GreasePencilSmoothModifierData *>(md);
if (!geometry_set->has_grease_pencil()) {
return;
}
GreasePencil &grease_pencil = *geometry_set->get_grease_pencil_for_write();
const int current_frame = grease_pencil.runtime->eval_frame;
IndexMaskMemory mask_memory;
const IndexMask layer_mask = modifier::greasepencil::get_filtered_layer_mask(
grease_pencil, mmd->influence, mask_memory);
const Vector<bke::greasepencil::Drawing *> drawings =
modifier::greasepencil::get_drawings_for_write(grease_pencil, layer_mask, current_frame);
threading::parallel_for_each(drawings, [&](bke::greasepencil::Drawing *drawing) {
deform_drawing(*md, *ctx->object, *drawing);
});
}
static void panel_draw(const bContext *C, Panel *panel)
{
uiLayout *row, *col;
uiLayout *layout = panel->layout;
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, nullptr);
row = uiLayoutRow(layout, true);
uiItemR(row, ptr, "use_edit_position", UI_ITEM_R_TOGGLE, IFACE_("Position"), ICON_NONE);
uiItemR(row, ptr, "use_edit_strength", UI_ITEM_R_TOGGLE, IFACE_("Strength"), ICON_NONE);
uiItemR(row, ptr, "use_edit_thickness", UI_ITEM_R_TOGGLE, IFACE_("Thickness"), ICON_NONE);
/* TODO: UV not implemented yet in GPv3. */
// uiItemR(row, ptr, "use_edit_uv", UI_ITEM_R_TOGGLE, IFACE_("UV"), ICON_NONE);
uiLayoutSetPropSep(layout, true);
uiItemR(layout, ptr, "factor", UI_ITEM_NONE, nullptr, ICON_NONE);
uiItemR(layout, ptr, "step", UI_ITEM_NONE, IFACE_("Repeat"), ICON_NONE);
col = uiLayoutColumn(layout, false);
uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_edit_position"));
uiItemR(col, ptr, "use_keep_shape", UI_ITEM_NONE, nullptr, ICON_NONE);
uiItemR(col, ptr, "use_smooth_ends", UI_ITEM_NONE, nullptr, ICON_NONE);
if (uiLayout *influence_panel = uiLayoutPanel(
C, layout, "Influence", ptr, "open_influence_panel"))
{
modifier::greasepencil::draw_layer_filter_settings(C, influence_panel, ptr);
modifier::greasepencil::draw_material_filter_settings(C, influence_panel, ptr);
modifier::greasepencil::draw_vertex_group_settings(C, influence_panel, ptr);
}
modifier_panel_end(layout, ptr);
}
static void panel_register(ARegionType *region_type)
{
modifier_panel_register(region_type, eModifierType_GreasePencilSmooth, panel_draw);
}
} // namespace blender
ModifierTypeInfo modifierType_GreasePencilSmooth = {
/*idname*/ "GreasePencilSmoothModifier",
/*name*/ N_("Smooth"),
/*struct_name*/ "GreasePencilSmoothModifierData",
/*struct_size*/ sizeof(GreasePencilSmoothModifierData),
/*srna*/ &RNA_GreasePencilSmoothModifier,
/*type*/ ModifierTypeType::OnlyDeform,
/*flags*/
eModifierTypeFlag_AcceptsGreasePencil | eModifierTypeFlag_SupportsEditmode |
eModifierTypeFlag_EnableInEditmode | eModifierTypeFlag_SupportsMapping,
/*icon*/ ICON_SMOOTHCURVE,
/*copy_data*/ blender::copy_data,
/*deform_verts*/ nullptr,
/*deform_matrices*/ nullptr,
/*deform_verts_EM*/ nullptr,
/*deform_matrices_EM*/ nullptr,
/*modify_mesh*/ nullptr,
/*modify_geometry_set*/ blender::modify_geometry_set,
/*init_data*/ blender::init_data,
/*required_data_mask*/ nullptr,
/*free_data*/ blender::free_data,
/*is_disabled*/ nullptr,
/*update_depsgraph*/ nullptr,
/*depends_on_time*/ nullptr,
/*depends_on_normals*/ nullptr,
/*foreach_ID_link*/ blender::foreach_ID_link,
/*foreach_tex_link*/ nullptr,
/*free_runtime_data*/ nullptr,
/*panel_register*/ blender::panel_register,
/*blend_write*/ blender::blend_write,
/*blend_read*/ blender::blend_read,
};

@ -274,5 +274,6 @@ void modifier_type_init(ModifierTypeInfo *types[])
INIT_TYPE(GreasePencilSubdiv);
INIT_TYPE(GreasePencilColor);
INIT_TYPE(GreasePencilTint);
INIT_TYPE(GreasePencilSmooth);
#undef INIT_TYPE
}