Blender V5.0
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 "BLI_math_color.h"
14
15#include "BKE_colortools.hh"
16#include "BKE_curves.hh"
17#include "BKE_geometry_set.hh"
18#include "BKE_grease_pencil.hh"
19#include "BKE_material.hh"
20#include "BKE_modifier.hh"
21#include "BKE_screen.hh"
22
23#include "BLO_read_write.hh"
24
26
28#include "UI_resources.hh"
29
30#include "BLT_translation.hh"
31
32#include "WM_types.hh"
33
34#include "RNA_access.hh"
35#include "RNA_enum_types.hh"
36#include "RNA_prototypes.hh"
37
39#include "MOD_modifiertypes.hh"
40#include "MOD_ui_common.hh"
41
42namespace blender {
43
45
55
56static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
57{
58 const auto *cmd = reinterpret_cast<const GreasePencilColorModifierData *>(md);
59 auto *tcmd = reinterpret_cast<GreasePencilColorModifierData *>(target);
60
62
64 modifier::greasepencil::copy_influence_data(&cmd->influence, &tcmd->influence, flag);
65}
66
67static void free_data(ModifierData *md)
68{
69 auto *cmd = reinterpret_cast<GreasePencilColorModifierData *>(md);
71}
72
73static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
74{
75 auto *cmd = reinterpret_cast<GreasePencilColorModifierData *>(md);
76 modifier::greasepencil::foreach_influence_ID_link(&cmd->influence, ob, walk, user_data);
77}
78
80 const ColorGeometry4f &material_color,
81 const float3 factor)
82{
83 float3 hsv;
84 /* When input alpha is zero, replace with material color. */
85 if (color.a == 0.0f && material_color.a > 0.0f) {
86 color.a = 1.0f;
87 rgb_to_hsv_v(material_color, hsv);
88 }
89 else {
90 rgb_to_hsv_v(color, hsv);
91 }
92 hsv[0] = fractf(hsv[0] + factor[0] + 0.5f);
93 hsv[1] = clamp_f(hsv[1] * factor[1], 0.0f, 1.0f);
94 hsv[2] = hsv[2] * factor[2];
95 hsv_to_rgb_v(hsv, color);
96}
97
100 bke::CurvesGeometry &curves,
101 const IndexMask &curves_mask,
102 const MutableSpan<ColorGeometry4f> vertex_colors)
103{
104 const bool use_curve = (cmd.influence.flag & GREASE_PENCIL_INFLUENCE_USE_CUSTOM_CURVE);
105 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
106
107 bke::AttributeAccessor attributes = curves.attributes();
108 const VArray<int> stroke_materials = *attributes.lookup_or_default<int>(
109 "material_index", bke::AttrDomain::Curve, 0);
110
111 curves_mask.foreach_index(GrainSize(512), [&](const int64_t curve_i) {
112 const Material *ma = BKE_object_material_get(&ob, stroke_materials[curve_i] + 1);
113 const MaterialGPencilStyle *gp_style = ma ? ma->gp_style : nullptr;
114 const ColorGeometry4f material_color = (gp_style ? ColorGeometry4f(gp_style->stroke_rgba) :
115 ColorGeometry4f(0.0f, 0.0f, 0.0f, 0.0f));
116
117 const IndexRange points = points_by_curve[curve_i];
118 for (const int64_t i : points.index_range()) {
119 const int64_t point_i = points[i];
120 float3 factor = cmd.hsv;
121 if (use_curve) {
122 const float curve_input = points.size() >= 2 ? (float(i) / float(points.size() - 1)) :
123 0.0f;
124 const float curve_factor = BKE_curvemapping_evaluateF(
125 cmd.influence.custom_curve, 0, curve_input);
126 factor *= curve_factor;
127 }
128
129 apply_color_factor(vertex_colors[point_i], material_color, factor);
130 }
131 });
132}
133
134static void modify_fill_color(Object &ob,
136 Drawing &drawing,
137 const IndexMask &curves_mask)
138{
139 const bke::CurvesGeometry &curves = drawing.strokes();
140 const bke::AttributeAccessor attributes = curves.attributes();
141 /* Fill color per stroke. */
143 const VArray<int> stroke_materials = *attributes.lookup_or_default<int>(
144 "material_index", bke::AttrDomain::Curve, 0);
145
146 curves_mask.foreach_index(GrainSize(512), [&](int64_t curve_i) {
147 const Material *ma = BKE_object_material_get(&ob, stroke_materials[curve_i] + 1);
148 const MaterialGPencilStyle *gp_style = ma ? ma->gp_style : nullptr;
149 const ColorGeometry4f material_color = (gp_style ? ColorGeometry4f(gp_style->fill_rgba) :
150 ColorGeometry4f(0.0f, 0.0f, 0.0f, 0.0f));
151
152 apply_color_factor(fill_colors[curve_i], material_color, cmd.hsv);
153 });
154}
155
156static void modify_drawing(ModifierData &md, const ModifierEvalContext &ctx, Drawing &drawing)
157{
158 auto &cmd = reinterpret_cast<GreasePencilColorModifierData &>(md);
159
160 bke::CurvesGeometry &curves = drawing.strokes_for_write();
161 IndexMaskMemory mask_memory;
163 ctx.object, curves, cmd.influence, mask_memory);
164
165 switch (cmd.color_mode) {
168 *ctx.object, cmd, curves, curves_mask, drawing.vertex_colors_for_write());
169 break;
171 modify_fill_color(*ctx.object, cmd, drawing, curves_mask);
172 break;
175 *ctx.object, cmd, curves, curves_mask, drawing.vertex_colors_for_write());
176 modify_fill_color(*ctx.object, cmd, drawing, curves_mask);
177 break;
180 break;
181 }
182}
183
185 const ModifierEvalContext *ctx,
186 bke::GeometrySet *geometry_set)
187{
188 auto *cmd = reinterpret_cast<GreasePencilColorModifierData *>(md);
189
190 if (!geometry_set->has_grease_pencil()) {
191 return;
192 }
193 GreasePencil &grease_pencil = *geometry_set->get_grease_pencil_for_write();
194 const int frame = grease_pencil.runtime->eval_frame;
195
196 IndexMaskMemory mask_memory;
198 grease_pencil, cmd->influence, mask_memory);
200 grease_pencil, layer_mask, frame);
202 [&](Drawing *drawing) { modify_drawing(*md, *ctx, *drawing); });
203}
204
205static void panel_draw(const bContext *C, Panel *panel)
206{
207 uiLayout *layout = panel->layout;
208
209 PointerRNA ob_ptr;
211
212 layout->use_property_split_set(true);
213
214 layout->prop(ptr, "color_mode", UI_ITEM_NONE, std::nullopt, ICON_NONE);
215
216 layout->prop(ptr, "hue", UI_ITEM_R_SLIDER, std::nullopt, ICON_NONE);
217 layout->prop(ptr, "saturation", UI_ITEM_R_SLIDER, std::nullopt, ICON_NONE);
218 layout->prop(ptr, "value", UI_ITEM_R_SLIDER, std::nullopt, ICON_NONE);
219
220 if (uiLayout *influence_panel = layout->panel_prop(
221 C, ptr, "open_influence_panel", IFACE_("Influence")))
222 {
226 }
227
229}
230
235
236static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
237{
238 const auto *cmd = reinterpret_cast<const GreasePencilColorModifierData *>(md);
239
241 modifier::greasepencil::write_influence_data(writer, &cmd->influence);
242}
243
244static void blend_read(BlendDataReader *reader, ModifierData *md)
245{
246 auto *cmd = reinterpret_cast<GreasePencilColorModifierData *>(md);
247
248 modifier::greasepencil::read_influence_data(reader, &cmd->influence);
249}
250
251} // namespace blender
252
254 /*idname*/ "GreasePencilColor",
255 /*name*/ N_("Color"),
256 /*struct_name*/ "GreasePencilColorModifierData",
257 /*struct_size*/ sizeof(GreasePencilColorModifierData),
258 /*srna*/ &RNA_GreasePencilColorModifier,
262 /*icon*/ ICON_MOD_HUE_SATURATION,
263
264 /*copy_data*/ blender::copy_data,
265
266 /*deform_verts*/ nullptr,
267 /*deform_matrices*/ nullptr,
268 /*deform_verts_EM*/ nullptr,
269 /*deform_matrices_EM*/ nullptr,
270 /*modify_mesh*/ nullptr,
271 /*modify_geometry_set*/ blender::modify_geometry_set,
272
273 /*init_data*/ blender::init_data,
274 /*required_data_mask*/ nullptr,
275 /*free_data*/ blender::free_data,
276 /*is_disabled*/ nullptr,
277 /*update_depsgraph*/ nullptr,
278 /*depends_on_time*/ nullptr,
279 /*depends_on_normals*/ nullptr,
280 /*foreach_ID_link*/ blender::foreach_ID_link,
281 /*foreach_tex_link*/ nullptr,
282 /*free_runtime_data*/ nullptr,
283 /*panel_register*/ blender::panel_register,
284 /*blend_write*/ blender::blend_write,
285 /*blend_read*/ blender::blend_read,
286};
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
#define UI_ITEM_NONE
long long int int64_t
ChannelStorageType a
constexpr int64_t size() const
constexpr IndexRange index_range() const
GAttributeReader lookup_or_default(StringRef attribute_id, AttrDomain domain, AttrType 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
nullptr float
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
VecBase< float, 3 > float3
static void blend_read(BlendDataReader *reader, ModifierData *md)
#define fractf
GreasePencilModifierInfluenceData influence
GreasePencilRuntimeHandle * runtime
Definition DNA_ID.h:414
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 use_property_split_set(bool value)
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:4238
uint8_t flag
Definition wm_window.cc:145