Blender  V3.3
MOD_gpencilopacity.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2017 Blender Foundation. */
3 
8 #include <stdio.h>
9 #include <string.h>
10 
11 #include "BLI_utildefines.h"
12 
13 #include "BLT_translation.h"
14 
15 #include "DNA_defaults.h"
17 #include "DNA_gpencil_types.h"
18 #include "DNA_meshdata_types.h"
19 #include "DNA_object_types.h"
20 #include "DNA_screen_types.h"
21 
22 #include "BKE_colortools.h"
23 #include "BKE_context.h"
24 #include "BKE_deform.h"
25 #include "BKE_gpencil_modifier.h"
26 #include "BKE_lib_query.h"
27 #include "BKE_main.h"
28 #include "BKE_modifier.h"
29 #include "BKE_screen.h"
30 
31 #include "DEG_depsgraph.h"
32 
33 #include "UI_interface.h"
34 #include "UI_resources.h"
35 
36 #include "RNA_access.h"
37 
39 #include "MOD_gpencil_ui_common.h"
40 #include "MOD_gpencil_util.h"
41 
42 static void initData(GpencilModifierData *md)
43 {
45 
46  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(gpmd, modifier));
47 
49 
50  gpmd->curve_intensity = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
52 }
53 
54 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
55 {
58 
59  if (tgmd->curve_intensity != NULL) {
61  tgmd->curve_intensity = NULL;
62  }
63 
65 
67 }
68 
69 /* opacity strokes */
72  Object *ob,
73  bGPDlayer *gpl,
74  bGPDframe *UNUSED(gpf),
75  bGPDstroke *gps)
76 {
78  const int def_nr = BKE_object_defgroup_name_index(ob, mmd->vgname);
79  const bool use_curve = (mmd->flag & GP_OPACITY_CUSTOM_CURVE) != 0 && mmd->curve_intensity;
80  const bool is_normalized = (mmd->flag & GP_OPACITY_NORMALIZE);
81  bool is_inverted = ((mmd->flag & GP_OPACITY_WEIGHT_FACTOR) == 0) &&
82  ((mmd->flag & GP_OPACITY_INVERT_VGROUP) != 0);
83 
85  mmd->layername,
86  mmd->material,
87  mmd->pass_index,
88  mmd->layer_pass,
89  1,
90  gpl,
91  gps,
96  return;
97  }
98 
99  /* Hardness (at stroke level). */
101  gps->hardeness *= mmd->hardeness;
102  CLAMP(gps->hardeness, 0.0f, 1.0f);
103 
104  return;
105  }
106 
107  for (int i = 0; i < gps->totpoints; i++) {
108  bGPDspoint *pt = &gps->points[i];
109  MDeformVert *dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
110 
111  /* Stroke using strength. */
112  if (mmd->modify_color != GP_MODIFY_COLOR_FILL) {
113  /* verify vertex group */
114  float weight = get_modifier_point_weight(dvert, is_inverted, def_nr);
115  if (weight < 0.0f) {
116  continue;
117  }
118 
119  /* Apply weight directly. */
120  if ((mmd->flag & GP_OPACITY_WEIGHT_FACTOR) && (!is_normalized)) {
121  pt->strength *= ((mmd->flag & GP_OPACITY_INVERT_VGROUP) ? 1.0f - weight : weight);
122  continue;
123  }
124 
125  /* Custom curve to modulate value. */
126  float factor_curve = mmd->factor;
127  if (use_curve) {
128  float value = (float)i / (gps->totpoints - 1);
129  factor_curve *= BKE_curvemapping_evaluateF(mmd->curve_intensity, 0, value);
130  }
131 
132  if (def_nr < 0) {
133  if (mmd->flag & GP_OPACITY_NORMALIZE) {
134  pt->strength = factor_curve;
135  }
136  else {
137  pt->strength += factor_curve - 1.0f;
138  }
139  }
140  else {
141  /* High factor values, change weight too. */
142  if ((factor_curve > 1.0f) && (weight < 1.0f)) {
143  weight += factor_curve - 1.0f;
144  CLAMP(weight, 0.0f, 1.0f);
145  }
146  if (mmd->flag & GP_OPACITY_NORMALIZE) {
147  pt->strength = factor_curve;
148  }
149  else {
150  pt->strength += (factor_curve - 1) * weight;
151  }
152  }
153 
154  CLAMP(pt->strength, 0.0f, 1.0f);
155  }
156  }
157 
158  /* Fill using opacity factor. */
159  if (mmd->modify_color != GP_MODIFY_COLOR_STROKE) {
160  float fill_factor = mmd->factor;
161 
162  if ((mmd->flag & GP_OPACITY_WEIGHT_FACTOR) && (!is_normalized)) {
163  /* Use first point for weight. */
164  MDeformVert *dvert = (gps->dvert != NULL) ? &gps->dvert[0] : NULL;
165  float weight = get_modifier_point_weight(
166  dvert, (mmd->flag & GP_OPACITY_INVERT_VGROUP) != 0, def_nr);
167  if (weight >= 0.0f) {
168  fill_factor = ((mmd->flag & GP_OPACITY_INVERT_VGROUP) ? 1.0f - weight : weight);
169  }
170  }
171 
172  gps->fill_opacity_fac = fill_factor;
173  CLAMP(gps->fill_opacity_fac, 0.0f, 1.0f);
174  }
175 }
176 
177 static void bakeModifier(Main *UNUSED(bmain),
180  Object *ob)
181 {
183 }
184 
186 {
188 
189  if (gpmd->curve_intensity) {
191  }
192 }
193 
194 static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
195 {
197 
198  walk(userData, ob, (ID **)&mmd->material, IDWALK_CB_USER);
199 }
200 
201 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
202 {
203  uiLayout *layout = panel->layout;
204 
206 
207  uiLayoutSetPropSep(layout, true);
208 
209  int modify_color = RNA_enum_get(ptr, "modify_color");
210 
211  uiItemR(layout, ptr, "modify_color", 0, NULL, ICON_NONE);
212 
213  if (modify_color == GP_MODIFY_COLOR_HARDNESS) {
214  uiItemR(layout, ptr, "hardness", 0, NULL, ICON_NONE);
215  }
216  else {
217  const bool is_normalized = RNA_boolean_get(ptr, "use_normalized_opacity");
218  const bool is_weighted = RNA_boolean_get(ptr, "use_weight_factor");
219 
220  uiItemR(layout, ptr, "use_normalized_opacity", 0, NULL, ICON_NONE);
221  const char *text = (is_normalized) ? IFACE_("Strength") : IFACE_("Opacity Factor");
222 
223  uiLayout *row = uiLayoutRow(layout, true);
224  uiLayoutSetActive(row, !is_weighted || is_normalized);
225  uiItemR(row, ptr, "factor", 0, text, ICON_NONE);
226  if (!is_normalized) {
227  uiLayout *sub = uiLayoutRow(row, true);
228  uiLayoutSetActive(sub, true);
229  uiItemR(row, ptr, "use_weight_factor", 0, "", ICON_MOD_VERTEX_WEIGHT);
230  }
231  }
232 
234 }
235 
236 static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
237 {
239 
240  int modify_color = RNA_enum_get(ptr, "modify_color");
241  bool show_vertex = (modify_color != GP_MODIFY_COLOR_HARDNESS);
242 
243  gpencil_modifier_masking_panel_draw(panel, true, show_vertex);
244 }
245 
246 static void curve_header_draw(const bContext *C, Panel *panel)
247 {
248  uiLayout *layout = panel->layout;
249 
251 
252  int modify_color = RNA_enum_get(ptr, "modify_color");
253  uiLayoutSetActive(layout, modify_color != GP_MODIFY_COLOR_HARDNESS);
254 
256 }
257 
258 static void curve_panel_draw(const bContext *C, Panel *panel)
259 {
260  uiLayout *layout = panel->layout;
261 
263 
264  int modify_color = RNA_enum_get(ptr, "modify_color");
265  uiLayoutSetActive(layout, modify_color != GP_MODIFY_COLOR_HARDNESS);
266 
268 }
269 
270 static void panelRegister(ARegionType *region_type)
271 {
274 
276  region_type, "mask", "Influence", NULL, mask_panel_draw, panel_type);
278  region_type, "curve", "", curve_header_draw, curve_panel_draw, mask_panel_type);
279 }
280 
282  /* name */ N_("Opacity"),
283  /* structName */ "OpacityGpencilModifierData",
284  /* structSize */ sizeof(OpacityGpencilModifierData),
287 
288  /* copyData */ copyData,
289 
290  /* deformStroke */ deformStroke,
291  /* generateStrokes */ NULL,
292  /* bakeModifier */ bakeModifier,
293  /* remapTime */ NULL,
294 
295  /* initData */ initData,
296  /* freeData */ freeData,
297  /* isDisabled */ NULL,
298  /* updateDepsgraph */ NULL,
299  /* dependsOnTime */ NULL,
300  /* foreachIDLink */ foreachIDLink,
301  /* foreachTexLink */ NULL,
302  /* panelRegister */ panelRegister,
303 };
typedef float(TangentPoint)[2]
void BKE_curvemapping_init(struct CurveMapping *cumap)
Definition: colortools.c:1235
struct CurveMapping * BKE_curvemapping_copy(const struct CurveMapping *cumap)
float BKE_curvemapping_evaluateF(const struct CurveMapping *cumap, int cur, float value)
void BKE_curvemapping_free(struct CurveMapping *cumap)
Definition: colortools.c:103
struct CurveMapping * BKE_curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
Definition: colortools.c:72
support for deformation groups and hooks.
int BKE_object_defgroup_name_index(const struct Object *ob, const char *name)
void BKE_gpencil_modifier_copydata_generic(const struct GpencilModifierData *md_src, struct GpencilModifierData *md_dst)
@ eGpencilModifierTypeFlag_SupportsEditmode
@ eGpencilModifierTypeType_Gpencil
@ IDWALK_CB_USER
Definition: BKE_lib_query.h:73
void(* IDWalkFunc)(void *userData, struct Object *ob, struct ID **idpoin, int cb_flag)
Definition: BKE_modifier.h:107
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define UNUSED(x)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define IFACE_(msgid)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:29
@ GP_OPACITY_WEIGHT_FACTOR
@ GP_OPACITY_INVERT_LAYER
@ GP_OPACITY_INVERT_VGROUP
@ GP_OPACITY_CUSTOM_CURVE
@ GP_OPACITY_INVERT_MATERIAL
@ GP_OPACITY_INVERT_PASS
@ GP_OPACITY_INVERT_LAYERPASS
struct OpacityGpencilModifierData OpacityGpencilModifierData
@ GP_MODIFY_COLOR_STROKE
@ GP_MODIFY_COLOR_HARDNESS
@ eGpencilModifierType_Opacity
Object is a sort of wrapper for general info.
PointerRNA * gpencil_modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
void gpencil_modifier_masking_panel_draw(Panel *panel, bool use_material, bool use_vertex)
void gpencil_modifier_panel_end(uiLayout *layout, PointerRNA *ptr)
PanelType * gpencil_modifier_subpanel_register(ARegionType *region_type, const char *name, const char *label, PanelDrawFn draw_header, PanelDrawFn draw, PanelType *parent)
PanelType * gpencil_modifier_panel_register(ARegionType *region_type, GpencilModifierType type, PanelDrawFn draw)
void gpencil_modifier_curve_header_draw(const bContext *UNUSED(C), Panel *panel)
void gpencil_modifier_curve_panel_draw(const bContext *UNUSED(C), Panel *panel)
float get_modifier_point_weight(MDeformVert *dvert, bool inverse, int def_nr)
void generic_bake_deform_stroke(Depsgraph *depsgraph, GpencilModifierData *md, Object *ob, const bool retime, gpBakeCb bake_cb)
bool is_stroke_affected_by_modifier(Object *ob, char *mlayername, Material *material, const int mpassindex, const int gpl_passindex, const int minpoints, bGPDlayer *gpl, bGPDstroke *gps, const bool inv1, const bool inv2, const bool inv3, const bool inv4)
static void deformStroke(GpencilModifierData *md, Depsgraph *UNUSED(depsgraph), Object *ob, bGPDlayer *gpl, bGPDframe *UNUSED(gpf), bGPDstroke *gps)
static void freeData(GpencilModifierData *md)
GpencilModifierTypeInfo modifierType_Gpencil_Opacity
static void curve_header_draw(const bContext *C, Panel *panel)
static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
static void bakeModifier(Main *UNUSED(bmain), Depsgraph *depsgraph, GpencilModifierData *md, Object *ob)
static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
static void panel_draw(const bContext *UNUSED(C), Panel *panel)
static void curve_panel_draw(const bContext *C, Panel *panel)
static void panelRegister(ARegionType *region_type)
static void initData(GpencilModifierData *md)
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position CLAMP
#define C
Definition: RandGen.cpp:25
void uiLayoutSetActive(uiLayout *layout, bool active)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
uiLayout * uiLayoutRow(uiLayout *layout, bool align)
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
const Depsgraph * depsgraph
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4863
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5004
Definition: DNA_ID.h:368
Definition: BKE_main.h:121
struct uiLayout * layout
bGPDspoint * points
float fill_opacity_fac
struct MDeformVert * dvert
#define N_(msgid)
PointerRNA * ptr
Definition: wm_files.c:3480