Blender V4.5
MOD_grease_pencil_color.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_defaults.h"
10#include "DNA_material_types.h"
11#include "DNA_modifier_types.h"
12
13#include "BKE_colortools.hh"
14#include "BKE_curves.hh"
15#include "BKE_geometry_set.hh"
16#include "BKE_grease_pencil.hh"
17#include "BKE_material.hh"
18#include "BKE_modifier.hh"
19#include "BKE_screen.hh"
20
21#include "BLO_read_write.hh"
22
24
25#include "UI_interface.hh"
26#include "UI_resources.hh"
27
28#include "BLT_translation.hh"
29
30#include "WM_types.hh"
31
32#include "RNA_access.hh"
33#include "RNA_enum_types.hh"
34#include "RNA_prototypes.hh"
35
37#include "MOD_modifiertypes.hh"
38#include "MOD_ui_common.hh"
39
40namespace blender {
41
43
53
54static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
55{
56 const auto *cmd = reinterpret_cast<const GreasePencilColorModifierData *>(md);
57 auto *tcmd = reinterpret_cast<GreasePencilColorModifierData *>(target);
58
60
62 modifier::greasepencil::copy_influence_data(&cmd->influence, &tcmd->influence, flag);
63}
64
65static void free_data(ModifierData *md)
66{
67 auto *cmd = reinterpret_cast<GreasePencilColorModifierData *>(md);
69}
70
71static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
72{
73 auto *cmd = reinterpret_cast<GreasePencilColorModifierData *>(md);
74 modifier::greasepencil::foreach_influence_ID_link(&cmd->influence, ob, walk, user_data);
75}
76
78 const ColorGeometry4f &material_color,
79 const float3 factor)
80{
81 float3 hsv;
82 /* When input alpha is zero, replace with material color. */
83 if (color.a == 0.0f && material_color.a > 0.0f) {
84 color.a = 1.0f;
85 rgb_to_hsv_v(material_color, hsv);
86 }
87 else {
88 rgb_to_hsv_v(color, hsv);
89 }
90 hsv[0] = fractf(hsv[0] + factor[0] + 0.5f);
91 hsv[1] = clamp_f(hsv[1] * factor[1], 0.0f, 1.0f);
92 hsv[2] = hsv[2] * factor[2];
93 hsv_to_rgb_v(hsv, color);
94}
95
98 bke::CurvesGeometry &curves,
99 const IndexMask &curves_mask,
100 const MutableSpan<ColorGeometry4f> vertex_colors)
101{
102 const bool use_curve = (cmd.influence.flag & GREASE_PENCIL_INFLUENCE_USE_CUSTOM_CURVE);
103 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
104
105 bke::AttributeAccessor attributes = curves.attributes();
106 const VArray<int> stroke_materials = *attributes.lookup_or_default<int>(
107 "material_index", bke::AttrDomain::Curve, 0);
108
109 curves_mask.foreach_index(GrainSize(512), [&](const int64_t curve_i) {
110 const Material *ma = BKE_object_material_get(&ob, stroke_materials[curve_i] + 1);
111 const MaterialGPencilStyle *gp_style = ma ? ma->gp_style : nullptr;
112 const ColorGeometry4f material_color = (gp_style ? ColorGeometry4f(gp_style->stroke_rgba) :
113 ColorGeometry4f(0.0f, 0.0f, 0.0f, 0.0f));
114
115 const IndexRange points = points_by_curve[curve_i];
116 for (const int64_t i : points.index_range()) {
117 const int64_t point_i = points[i];
118 float3 factor = cmd.hsv;
119 if (use_curve) {
120 const float curve_input = points.size() >= 2 ? (float(i) / float(points.size() - 1)) :
121 0.0f;
122 const float curve_factor = BKE_curvemapping_evaluateF(
123 cmd.influence.custom_curve, 0, curve_input);
124 factor *= curve_factor;
125 }
126
127 apply_color_factor(vertex_colors[point_i], material_color, factor);
128 }
129 });
130}
131
132static void modify_fill_color(Object &ob,
134 Drawing &drawing,
135 const IndexMask &curves_mask)
136{
137 const bke::CurvesGeometry &curves = drawing.strokes();
138 const bke::AttributeAccessor attributes = curves.attributes();
139 /* Fill color per stroke. */
141 const VArray<int> stroke_materials = *attributes.lookup_or_default<int>(
142 "material_index", bke::AttrDomain::Curve, 0);
143
144 curves_mask.foreach_index(GrainSize(512), [&](int64_t curve_i) {
145 const Material *ma = BKE_object_material_get(&ob, stroke_materials[curve_i] + 1);
146 const MaterialGPencilStyle *gp_style = ma ? ma->gp_style : nullptr;
147 const ColorGeometry4f material_color = (gp_style ? ColorGeometry4f(gp_style->fill_rgba) :
148 ColorGeometry4f(0.0f, 0.0f, 0.0f, 0.0f));
149
150 apply_color_factor(fill_colors[curve_i], material_color, cmd.hsv);
151 });
152}
153
154static void modify_drawing(ModifierData &md, const ModifierEvalContext &ctx, Drawing &drawing)
155{
156 auto &cmd = reinterpret_cast<GreasePencilColorModifierData &>(md);
157
158 bke::CurvesGeometry &curves = drawing.strokes_for_write();
159 IndexMaskMemory mask_memory;
161 ctx.object, curves, cmd.influence, mask_memory);
162
163 switch (cmd.color_mode) {
166 *ctx.object, cmd, curves, curves_mask, drawing.vertex_colors_for_write());
167 break;
169 modify_fill_color(*ctx.object, cmd, drawing, curves_mask);
170 break;
173 *ctx.object, cmd, curves, curves_mask, drawing.vertex_colors_for_write());
174 modify_fill_color(*ctx.object, cmd, drawing, curves_mask);
175 break;
178 break;
179 }
180}
181
183 const ModifierEvalContext *ctx,
184 bke::GeometrySet *geometry_set)
185{
186 auto *cmd = reinterpret_cast<GreasePencilColorModifierData *>(md);
187
188 if (!geometry_set->has_grease_pencil()) {
189 return;
190 }
191 GreasePencil &grease_pencil = *geometry_set->get_grease_pencil_for_write();
192 const int frame = grease_pencil.runtime->eval_frame;
193
194 IndexMaskMemory mask_memory;
196 grease_pencil, cmd->influence, mask_memory);
198 grease_pencil, layer_mask, frame);
200 [&](Drawing *drawing) { modify_drawing(*md, *ctx, *drawing); });
201}
202
203static void panel_draw(const bContext *C, Panel *panel)
204{
205 uiLayout *layout = panel->layout;
206
207 PointerRNA ob_ptr;
209
210 uiLayoutSetPropSep(layout, true);
211
212 layout->prop(ptr, "color_mode", UI_ITEM_NONE, std::nullopt, ICON_NONE);
213
214 layout->prop(ptr, "hue", UI_ITEM_R_SLIDER, std::nullopt, ICON_NONE);
215 layout->prop(ptr, "saturation", UI_ITEM_R_SLIDER, std::nullopt, ICON_NONE);
216 layout->prop(ptr, "value", UI_ITEM_R_SLIDER, std::nullopt, ICON_NONE);
217
218 if (uiLayout *influence_panel = layout->panel_prop(
219 C, ptr, "open_influence_panel", IFACE_("Influence")))
220 {
224 }
225
227}
228
233
234static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
235{
236 const auto *cmd = reinterpret_cast<const GreasePencilColorModifierData *>(md);
237
239 modifier::greasepencil::write_influence_data(writer, &cmd->influence);
240}
241
242static void blend_read(BlendDataReader *reader, ModifierData *md)
243{
244 auto *cmd = reinterpret_cast<GreasePencilColorModifierData *>(md);
245
246 modifier::greasepencil::read_influence_data(reader, &cmd->influence);
247}
248
249} // namespace blender
250
252 /*idname*/ "GreasePencilColor",
253 /*name*/ N_("Color"),
254 /*struct_name*/ "GreasePencilColorModifierData",
255 /*struct_size*/ sizeof(GreasePencilColorModifierData),
256 /*srna*/ &RNA_GreasePencilColorModifier,
260 /*icon*/ ICON_MOD_HUE_SATURATION,
261
262 /*copy_data*/ blender::copy_data,
263
264 /*deform_verts*/ nullptr,
265 /*deform_matrices*/ nullptr,
266 /*deform_verts_EM*/ nullptr,
267 /*deform_matrices_EM*/ nullptr,
268 /*modify_mesh*/ nullptr,
269 /*modify_geometry_set*/ blender::modify_geometry_set,
270
271 /*init_data*/ blender::init_data,
272 /*required_data_mask*/ nullptr,
273 /*free_data*/ blender::free_data,
274 /*is_disabled*/ nullptr,
275 /*update_depsgraph*/ nullptr,
276 /*depends_on_time*/ nullptr,
277 /*depends_on_normals*/ nullptr,
278 /*foreach_ID_link*/ blender::foreach_ID_link,
279 /*foreach_tex_link*/ nullptr,
280 /*free_runtime_data*/ nullptr,
281 /*panel_register*/ blender::panel_register,
282 /*blend_write*/ blender::blend_write,
283 /*blend_read*/ blender::blend_read,
284};
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
Low-level operations for curves.
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
Material * BKE_object_material_get(Object *ob, short act)
void(*)(void *user_data, Object *ob, ID **idpoin, LibraryForeachIDCallbackFlag cb_flag) IDWalkFunc
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
@ eModifierTypeFlag_SupportsMapping
@ eModifierTypeFlag_AcceptsGreasePencil
@ eModifierTypeFlag_EnableInEditmode
@ eModifierTypeFlag_SupportsEditmode
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE float clamp_f(float value, float min, float max)
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:57
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define BLO_write_struct(writer, struct_name, data_ptr)
#define IFACE_(msgid)
#define DNA_struct_default_get(struct_name)
@ GREASE_PENCIL_INFLUENCE_USE_CUSTOM_CURVE
@ MOD_GREASE_PENCIL_COLOR_FILL
@ MOD_GREASE_PENCIL_COLOR_STROKE
@ MOD_GREASE_PENCIL_COLOR_BOTH
@ MOD_GREASE_PENCIL_COLOR_HARDNESS
@ eModifierType_GreasePencilColor
ModifierTypeInfo modifierType_GreasePencilColor
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
void modifier_error_message_draw(uiLayout *layout, PointerRNA *ptr)
#define C
Definition RandGen.cpp:29
@ UI_ITEM_R_SLIDER
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
#define UI_ITEM_NONE
long long int int64_t
ChannelStorageType a
Definition BLI_color.hh:93
constexpr int64_t size() const
constexpr IndexRange index_range() const
GAttributeReader lookup_or_default(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type, const void *default_value=nullptr) const
OffsetIndices< int > points_by_curve() const
AttributeAccessor attributes() const
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
MutableSpan< ColorGeometry4f > fill_colors_for_write()
MutableSpan< ColorGeometry4f > vertex_colors_for_write()
void foreach_index(Fn &&fn) const
MINLINE float fractf(float a)
void read_influence_data(BlendDataReader *reader, GreasePencilModifierInfluenceData *influence_data)
void init_influence_data(GreasePencilModifierInfluenceData *influence_data, const bool has_custom_curve)
static IndexMask get_filtered_stroke_mask(const Object *ob, const bke::CurvesGeometry &curves, const Material *material_filter, const std::optional< int > material_pass_filter, const bool material_filter_invert, const bool material_pass_filter_invert, IndexMaskMemory &memory)
void write_influence_data(BlendWriter *writer, const GreasePencilModifierInfluenceData *influence_data)
static IndexMask get_filtered_layer_mask(const GreasePencil &grease_pencil, const std::optional< StringRef > tree_node_name_filter, const std::optional< int > layer_pass_filter, const bool layer_filter_invert, const bool layer_pass_filter_invert, IndexMaskMemory &memory)
Vector< bke::greasepencil::Drawing * > get_drawings_for_write(GreasePencil &grease_pencil, const IndexMask &layer_mask, const int frame)
void draw_material_filter_settings(const bContext *, uiLayout *layout, PointerRNA *ptr)
void draw_layer_filter_settings(const bContext *, uiLayout *layout, PointerRNA *ptr)
void draw_custom_curve_settings(const bContext *, uiLayout *layout, PointerRNA *ptr)
void free_influence_data(GreasePencilModifierInfluenceData *influence_data)
void foreach_influence_ID_link(GreasePencilModifierInfluenceData *influence_data, Object *ob, IDWalkFunc walk, void *user_data)
void copy_influence_data(const GreasePencilModifierInfluenceData *influence_data_src, GreasePencilModifierInfluenceData *influence_data_dst, const int)
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:56
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
static void apply_color_factor(ColorGeometry4f &color, const ColorGeometry4f &material_color, const float3 factor)
static void blend_write(BlendWriter *writer, const ID *, const ModifierData *md)
static void init_data(ModifierData *md)
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
static void panel_draw(const bContext *C, Panel *panel)
static void modify_geometry_set(ModifierData *md, const ModifierEvalContext *ctx, bke::GeometrySet *geometry_set)
static void modify_fill_color(Object &ob, const GreasePencilColorModifierData &cmd, Drawing &drawing, const IndexMask &curves_mask)
static void free_data(ModifierData *md)
static void panel_register(ARegionType *region_type)
static void modify_drawing(const GreasePencilArrayModifierData &mmd, const ModifierEvalContext &ctx, bke::greasepencil::Drawing &drawing)
static void modify_stroke_color(Object &ob, const GreasePencilColorModifierData &cmd, bke::CurvesGeometry &curves, const IndexMask &curves_mask, const MutableSpan< ColorGeometry4f > vertex_colors)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
Definition BLI_color.hh:342
VecBase< float, 3 > float3
static void blend_read(BlendDataReader *reader, ModifierData *md)
GreasePencilModifierInfluenceData influence
GreasePencilRuntimeHandle * runtime
Definition DNA_ID.h:404
struct MaterialGPencilStyle * gp_style
struct uiLayout * layout
GreasePencil * get_grease_pencil_for_write()
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, blender::StringRefNull open_prop_name)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
i
Definition text_draw.cc:230
#define N_(msgid)
PointerRNA * ptr
Definition wm_files.cc:4226
uint8_t flag
Definition wm_window.cc:139