Blender  V2.93
MOD_gpencilopacity.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2017, Blender Foundation
17  * This is a new part of Blender
18  */
19 
24 #include <stdio.h>
25 
26 #include "BLI_utildefines.h"
27 
28 #include "BLI_blenlib.h"
29 #include "BLI_math_vector.h"
30 
31 #include "BLT_translation.h"
32 
33 #include "DNA_defaults.h"
35 #include "DNA_gpencil_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_screen_types.h"
39 
40 #include "BKE_colortools.h"
41 #include "BKE_context.h"
42 #include "BKE_deform.h"
43 #include "BKE_gpencil_modifier.h"
44 #include "BKE_lib_query.h"
45 #include "BKE_main.h"
46 #include "BKE_modifier.h"
47 #include "BKE_screen.h"
48 
49 #include "DEG_depsgraph.h"
50 
51 #include "UI_interface.h"
52 #include "UI_resources.h"
53 
54 #include "RNA_access.h"
55 
57 #include "MOD_gpencil_ui_common.h"
58 #include "MOD_gpencil_util.h"
59 
60 static void initData(GpencilModifierData *md)
61 {
63 
64  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(gpmd, modifier));
65 
67 
68  gpmd->curve_intensity = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
70 }
71 
72 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
73 {
76 
77  if (tgmd->curve_intensity != NULL) {
79  tgmd->curve_intensity = NULL;
80  }
81 
83 
85 }
86 
87 /* opacity strokes */
90  Object *ob,
91  bGPDlayer *gpl,
92  bGPDframe *UNUSED(gpf),
93  bGPDstroke *gps)
94 {
96  const int def_nr = BKE_object_defgroup_name_index(ob, mmd->vgname);
97  const bool use_curve = (mmd->flag & GP_OPACITY_CUSTOM_CURVE) != 0 && mmd->curve_intensity;
98 
100  mmd->layername,
101  mmd->material,
102  mmd->pass_index,
103  mmd->layer_pass,
104  1,
105  gpl,
106  gps,
111  return;
112  }
113 
114  /* Hardness (at stroke level). */
116  gps->hardeness *= mmd->hardeness;
117  CLAMP(gps->hardeness, 0.0f, 1.0f);
118 
119  return;
120  }
121 
122  for (int i = 0; i < gps->totpoints; i++) {
123  bGPDspoint *pt = &gps->points[i];
124  MDeformVert *dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
125 
126  /* Stroke using strength. */
127  if (mmd->modify_color != GP_MODIFY_COLOR_FILL) {
128  /* verify vertex group */
129  float weight = get_modifier_point_weight(
130  dvert, (mmd->flag & GP_OPACITY_INVERT_VGROUP) != 0, def_nr);
131  if (weight < 0.0f) {
132  continue;
133  }
134  /* Custom curve to modulate value. */
135  float factor_curve = mmd->factor;
136  if (use_curve) {
137  float value = (float)i / (gps->totpoints - 1);
138  factor_curve *= BKE_curvemapping_evaluateF(mmd->curve_intensity, 0, value);
139  }
140 
141  if (def_nr < 0) {
142  if (mmd->flag & GP_OPACITY_NORMALIZE) {
143  pt->strength = factor_curve;
144  }
145  else {
146  pt->strength += factor_curve - 1.0f;
147  }
148  }
149  else {
150  /* High factor values, change weight too. */
151  if ((factor_curve > 1.0f) && (weight < 1.0f)) {
152  weight += factor_curve - 1.0f;
153  CLAMP(weight, 0.0f, 1.0f);
154  }
155  if (mmd->flag & GP_OPACITY_NORMALIZE) {
156  pt->strength = factor_curve;
157  }
158  else {
159  pt->strength += (factor_curve - 1) * weight;
160  }
161  }
162 
163  CLAMP(pt->strength, 0.0f, 1.0f);
164  }
165  }
166 
167  /* Fill using opacity factor. */
168  if (mmd->modify_color != GP_MODIFY_COLOR_STROKE) {
169  gps->fill_opacity_fac = mmd->factor;
170  CLAMP(gps->fill_opacity_fac, 0.0f, 1.0f);
171  }
172 }
173 
174 static void bakeModifier(Main *UNUSED(bmain),
177  Object *ob)
178 {
179  bGPdata *gpd = ob->data;
180 
181  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
182  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
183  LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
184  deformStroke(md, depsgraph, ob, gpl, gpf, gps);
185  }
186  }
187  }
188 }
189 
191 {
193 
194  if (gpmd->curve_intensity) {
196  }
197 }
198 
199 static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
200 {
202 
203  walk(userData, ob, (ID **)&mmd->material, IDWALK_CB_USER);
204 }
205 
206 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
207 {
208  uiLayout *layout = panel->layout;
209 
211 
212  uiLayoutSetPropSep(layout, true);
213 
214  int modify_color = RNA_enum_get(ptr, "modify_color");
215 
216  uiItemR(layout, ptr, "modify_color", 0, NULL, ICON_NONE);
217 
218  if (modify_color == GP_MODIFY_COLOR_HARDNESS) {
219  uiItemR(layout, ptr, "hardness", 0, NULL, ICON_NONE);
220  }
221  else {
222  uiItemR(layout, ptr, "normalize_opacity", 0, NULL, ICON_NONE);
223  const char *text = (RNA_boolean_get(ptr, "normalize_opacity")) ? IFACE_("Strength") :
224  IFACE_("Opacity Factor");
225  uiItemR(layout, ptr, "factor", 0, text, ICON_NONE);
226  }
227 
229 }
230 
231 static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
232 {
234 
235  int modify_color = RNA_enum_get(ptr, "modify_color");
236  bool show_vertex = (modify_color != GP_MODIFY_COLOR_HARDNESS);
237 
238  gpencil_modifier_masking_panel_draw(panel, true, show_vertex);
239 }
240 
241 static void curve_header_draw(const bContext *C, Panel *panel)
242 {
243  uiLayout *layout = panel->layout;
244 
246 
247  int modify_color = RNA_enum_get(ptr, "modify_color");
248  uiLayoutSetActive(layout, modify_color != GP_MODIFY_COLOR_HARDNESS);
249 
251 }
252 
253 static void curve_panel_draw(const bContext *C, Panel *panel)
254 {
255  uiLayout *layout = panel->layout;
256 
258 
259  int modify_color = RNA_enum_get(ptr, "modify_color");
260  uiLayoutSetActive(layout, modify_color != GP_MODIFY_COLOR_HARDNESS);
261 
263 }
264 
265 static void panelRegister(ARegionType *region_type)
266 {
270  region_type, "mask", "Influence", NULL, mask_panel_draw, panel_type);
272  region_type, "curve", "", curve_header_draw, curve_panel_draw, mask_panel_type);
273 }
274 
276  /* name */ "Opacity",
277  /* structName */ "OpacityGpencilModifierData",
278  /* structSize */ sizeof(OpacityGpencilModifierData),
281 
282  /* copyData */ copyData,
283 
284  /* deformStroke */ deformStroke,
285  /* generateStrokes */ NULL,
286  /* bakeModifier */ bakeModifier,
287  /* remapTime */ NULL,
288 
289  /* initData */ initData,
290  /* freeData */ freeData,
291  /* isDisabled */ NULL,
292  /* updateDepsgraph */ NULL,
293  /* dependsOnTime */ NULL,
294  /* foreachIDLink */ foreachIDLink,
295  /* foreachTexLink */ NULL,
296  /* panelRegister */ panelRegister,
297 };
typedef float(TangentPoint)[2]
void BKE_curvemapping_init(struct CurveMapping *cumap)
Definition: colortools.c:1200
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:119
struct CurveMapping * BKE_curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
Definition: colortools.c:88
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:87
void(* IDWalkFunc)(void *userData, struct Object *ob, struct ID **idpoin, int cb_flag)
Definition: BKE_modifier.h:120
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
#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:51
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:44
@ 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)
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 RGB to Bright Vector Camera CLAMP
#define C
Definition: RandGen.cpp:39
void uiLayoutSetActive(uiLayout *layout, bool active)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
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:6261
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6402
Definition: DNA_ID.h:273
Definition: BKE_main.h:116
void * data
struct uiLayout * layout
ListBase frames
bGPDspoint * points
float fill_opacity_fac
struct MDeformVert * dvert
ListBase layers
PointerRNA * ptr
Definition: wm_files.c:3157