Blender  V2.93
MOD_gpenciltint.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_listbase.h"
29 #include "BLI_math.h"
30 
31 #include "DNA_defaults.h"
33 #include "DNA_gpencil_types.h"
34 #include "DNA_material_types.h"
35 #include "DNA_meshdata_types.h"
36 #include "DNA_modifier_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 
41 #include "BKE_colorband.h"
42 #include "BKE_colortools.h"
43 #include "BKE_context.h"
44 #include "BKE_deform.h"
45 #include "BKE_gpencil_modifier.h"
46 #include "BKE_lib_query.h"
47 #include "BKE_main.h"
48 #include "BKE_material.h"
49 #include "BKE_modifier.h"
50 #include "BKE_scene.h"
51 #include "BKE_screen.h"
52 
53 #include "MEM_guardedalloc.h"
54 
55 #include "UI_interface.h"
56 #include "UI_resources.h"
57 
58 #include "RNA_access.h"
59 
61 #include "MOD_gpencil_ui_common.h"
62 #include "MOD_gpencil_util.h"
63 
64 #include "DEG_depsgraph.h"
65 #include "DEG_depsgraph_build.h"
66 #include "DEG_depsgraph_query.h"
67 
68 static void initData(GpencilModifierData *md)
69 {
71 
72  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(gpmd, modifier));
73 
75 
76  /* Add default color ramp. */
77  gpmd->colorband = BKE_colorband_add(false);
78  if (gpmd->colorband) {
79  BKE_colorband_init(gpmd->colorband, true);
80  CBData *ramp = gpmd->colorband->data;
81  ramp[0].r = ramp[0].g = ramp[0].b = ramp[0].a = 1.0f;
82  ramp[0].pos = 0.0f;
83  ramp[1].r = ramp[1].g = ramp[1].b = 0.0f;
84  ramp[1].a = 1.0f;
85  ramp[1].pos = 1.0f;
86 
87  gpmd->colorband->tot = 2;
88  }
89 
90  gpmd->curve_intensity = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
92 }
93 
94 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
95 {
98 
99  MEM_SAFE_FREE(tgmd->colorband);
100 
101  if (tgmd->curve_intensity != NULL) {
103  tgmd->curve_intensity = NULL;
104  }
105 
107 
108  if (gmd->colorband) {
109  tgmd->colorband = MEM_dupallocN(gmd->colorband);
110  }
111 
113 }
114 
115 /* deform stroke */
118  Object *ob,
119  bGPDlayer *gpl,
120  bGPDframe *UNUSED(gpf),
121  bGPDstroke *gps)
122 {
124  if ((mmd->type == GP_TINT_GRADIENT) && (!mmd->object)) {
125  return;
126  }
127 
128  const int def_nr = BKE_object_defgroup_name_index(ob, mmd->vgname);
129  const bool use_curve = (mmd->flag & GP_TINT_CUSTOM_CURVE) != 0 && mmd->curve_intensity;
130 
132  mmd->layername,
133  mmd->material,
134  mmd->pass_index,
135  mmd->layer_pass,
136  1,
137  gpl,
138  gps,
139  mmd->flag & GP_TINT_INVERT_LAYER,
140  mmd->flag & GP_TINT_INVERT_PASS,
142  mmd->flag & GP_TINT_INVERT_MATERIAL)) {
143  return;
144  }
146  const bool is_gradient = (mmd->type == GP_TINT_GRADIENT);
147 
148  /* If factor > 1.0, affect the strength of the stroke. */
149  if (mmd->factor > 1.0f) {
150  for (int i = 0; i < gps->totpoints; i++) {
151  bGPDspoint *pt = &gps->points[i];
152  pt->strength += mmd->factor - 1.0f;
153  CLAMP(pt->strength, 0.0f, 1.0f);
154  }
155  }
156 
157  float coba_res[4];
158  float matrix[4][4];
159  if (is_gradient) {
160  mul_m4_m4m4(matrix, mmd->object->imat, ob->obmat);
161  }
162 
163  /* loop points and apply color. */
164  bool fill_done = false;
165  for (int i = 0; i < gps->totpoints; i++) {
166  bGPDspoint *pt = &gps->points[i];
167  MDeformVert *dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
168 
169  if (!fill_done) {
170  /* Apply to fill. */
171  if (mmd->mode != GPPAINT_MODE_STROKE) {
172 
173  /* If not using Vertex Color, use the material color. */
174  if ((gp_style != NULL) && (gps->vert_color_fill[3] == 0.0f) &&
175  (gp_style->fill_rgba[3] > 0.0f)) {
176  copy_v4_v4(gps->vert_color_fill, gp_style->fill_rgba);
177  gps->vert_color_fill[3] = 1.0f;
178  }
179 
180  if (is_gradient) {
181  float center[3];
183  mul_v3_fl(center, 0.5f);
184  float pt_loc[3];
185  mul_v3_m4v3(pt_loc, matrix, &pt->x);
186  float dist = len_v3(pt_loc);
187  float mix_factor = clamp_f(dist / mmd->radius, 0.0f, 1.0f);
188 
189  BKE_colorband_evaluate(mmd->colorband, mix_factor, coba_res);
190  interp_v3_v3v3(gps->vert_color_fill, gps->vert_color_fill, coba_res, mmd->factor);
191  gps->vert_color_fill[3] = clamp_f(mmd->factor, 0.0f, 1.0f);
192  }
193  else {
195  gps->vert_color_fill,
196  mmd->rgb,
197  clamp_f(mmd->factor, 0.0f, 1.0f));
198  }
199  /* If no stroke, cancel loop. */
200  if (mmd->mode != GPPAINT_MODE_BOTH) {
201  break;
202  }
203  }
204 
205  fill_done = true;
206  }
207 
208  /* Verify vertex group. */
209  if (mmd->mode != GPPAINT_MODE_FILL) {
210  float weight = get_modifier_point_weight(
211  dvert, (mmd->flag & GP_TINT_INVERT_VGROUP) != 0, def_nr);
212  if (weight < 0.0f) {
213  continue;
214  }
215  /* Custom curve to modulate value. */
216  if (use_curve) {
217  float value = (float)i / (gps->totpoints - 1);
218  weight *= BKE_curvemapping_evaluateF(mmd->curve_intensity, 0, value);
219  }
220 
221  /* If not using Vertex Color, use the material color. */
222  if ((gp_style != NULL) && (pt->vert_color[3] == 0.0f) && (gp_style->stroke_rgba[3] > 0.0f)) {
223  copy_v4_v4(pt->vert_color, gp_style->stroke_rgba);
224  pt->vert_color[3] = 1.0f;
225  }
226 
227  if (is_gradient) {
228  /* Calc world position of point. */
229  float pt_loc[3];
230  mul_v3_m4v3(pt_loc, matrix, &pt->x);
231  float dist = len_v3(pt_loc);
232 
233  /* Calc the factor using the distance and get mix color. */
234  float mix_factor = clamp_f(dist / mmd->radius, 0.0f, 1.0f);
235  BKE_colorband_evaluate(mmd->colorband, mix_factor, coba_res);
236 
238  pt->vert_color,
239  coba_res,
240  clamp_f(mmd->factor, 0.0f, 1.0f) * weight * coba_res[3]);
241  }
242  else {
244  pt->vert_color, pt->vert_color, mmd->rgb, clamp_f(mmd->factor * weight, 0.0, 1.0f));
245  }
246  }
247  }
248 }
249 
250 /* FIXME: Ideally we be doing this on a copy of the main depsgraph
251  * (i.e. one where we don't have to worry about restoring state)
252  */
253 static void bakeModifier(Main *UNUSED(bmain),
256  Object *ob)
257 {
260  bGPdata *gpd = ob->data;
261  int oldframe = (int)DEG_get_ctime(depsgraph);
262 
263  if ((mmd->type == GP_TINT_GRADIENT) && (mmd->object == NULL)) {
264  return;
265  }
266 
267  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
268  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
269  /* apply effects on this frame
270  * NOTE: this assumes that we don't want animation on non-keyframed frames
271  */
272  CFRA = gpf->framenum;
274 
275  /* compute effects on this frame */
276  LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
277  deformStroke(md, depsgraph, ob, gpl, gpf, gps);
278  }
279  }
280  }
281 
282  /* return frame state and DB to original state */
283  CFRA = oldframe;
285 }
286 
288 {
290  if (mmd->colorband) {
291  MEM_freeN(mmd->colorband);
292  mmd->colorband = NULL;
293  }
294  if (mmd->curve_intensity) {
296  }
297 }
298 
299 static bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams))
300 {
302  if (mmd->type == GP_TINT_UNIFORM) {
303  return false;
304  }
305 
306  return !mmd->object;
307 }
308 
311  const int UNUSED(mode))
312 {
314  if (lmd->object != NULL) {
315  DEG_add_object_relation(ctx->node, lmd->object, DEG_OB_COMP_GEOMETRY, "Vertexcolor Modifier");
316  DEG_add_object_relation(ctx->node, lmd->object, DEG_OB_COMP_TRANSFORM, "Vertexcolor Modifier");
317  }
318  DEG_add_object_relation(ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "Vertexcolor Modifier");
319 }
320 
321 static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
322 {
324 
325  walk(userData, ob, (ID **)&mmd->material, IDWALK_CB_USER);
326  walk(userData, ob, (ID **)&mmd->object, IDWALK_CB_NOP);
327 }
328 
329 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
330 {
331  uiLayout *col;
332  uiLayout *layout = panel->layout;
333 
335 
336  int tint_type = RNA_enum_get(ptr, "tint_type");
337 
338  uiLayoutSetPropSep(layout, true);
339 
340  uiItemR(layout, ptr, "vertex_mode", 0, NULL, ICON_NONE);
341  uiItemR(layout, ptr, "factor", 0, NULL, ICON_NONE);
342  uiItemR(layout, ptr, "tint_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
343 
344  if (tint_type == GP_TINT_UNIFORM) {
345  uiItemR(layout, ptr, "color", 0, NULL, ICON_NONE);
346  }
347  else {
348  col = uiLayoutColumn(layout, false);
349  uiLayoutSetPropSep(col, false);
350  uiTemplateColorRamp(col, ptr, "colors", true);
351  uiItemS(layout);
352  uiItemR(layout, ptr, "object", 0, NULL, ICON_NONE);
353  uiItemR(layout, ptr, "radius", 0, NULL, ICON_NONE);
354  }
355 
357 }
358 
359 static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
360 {
361  gpencil_modifier_masking_panel_draw(panel, true, true);
362 }
363 
364 static void panelRegister(ARegionType *region_type)
365 {
367  region_type, eGpencilModifierType_Tint, panel_draw);
369  region_type, "mask", "Influence", NULL, mask_panel_draw, panel_type);
371  "curve",
372  "",
375  mask_panel_type);
376 }
377 
379  /* name */ "Tint",
380  /* structName */ "TintGpencilModifierData",
381  /* structSize */ sizeof(TintGpencilModifierData),
384 
385  /* copyData */ copyData,
386 
387  /* deformStroke */ deformStroke,
388  /* generateStrokes */ NULL,
389  /* bakeModifier */ bakeModifier,
390  /* remapTime */ NULL,
391 
392  /* initData */ initData,
393  /* freeData */ freeData,
394  /* isDisabled */ isDisabled,
395  /* updateDepsgraph */ updateDepsgraph,
396  /* dependsOnTime */ NULL,
397  /* foreachIDLink */ foreachIDLink,
398  /* foreachTexLink */ NULL,
399  /* panelRegister */ panelRegister,
400 };
typedef float(TangentPoint)[2]
struct ColorBand * BKE_colorband_add(bool rangetype)
Definition: colorband.c:312
void BKE_colorband_init(struct ColorBand *coba, bool rangetype)
Definition: colorband.c:38
bool BKE_colorband_evaluate(const struct ColorBand *coba, float in, float out[4])
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
@ IDWALK_CB_NOP
Definition: BKE_lib_query.h:47
General operations, lookup, etc. for materials.
struct MaterialGPencilStyle * BKE_gpencil_material_settings(struct Object *ob, short act)
Definition: material.c:713
void(* IDWalkFunc)(void *userData, struct Object *ob, struct ID **idpoin, int cb_flag)
Definition: BKE_modifier.h:120
void BKE_scene_graph_update_for_newframe(struct Depsgraph *depsgraph)
Definition: scene.c:2794
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
MINLINE float clamp_f(float value, float min, float max)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:262
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:742
MINLINE void copy_v4_v4(float r[4], const float a[4])
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], const float t)
Definition: math_vector.c:49
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
#define UNUSED(x)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
void DEG_add_object_relation(struct DepsNodeHandle *node_handle, struct Object *object, eDepsObjectComponentType component, const char *description)
@ DEG_OB_COMP_GEOMETRY
@ DEG_OB_COMP_TRANSFORM
float DEG_get_ctime(const Depsgraph *graph)
struct Scene * DEG_get_evaluated_scene(const struct Depsgraph *graph)
@ GPPAINT_MODE_STROKE
@ GPPAINT_MODE_FILL
@ GPPAINT_MODE_BOTH
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:44
struct TintGpencilModifierData TintGpencilModifierData
@ GP_TINT_INVERT_LAYERPASS
@ GP_TINT_INVERT_MATERIAL
@ eGpencilModifierType_Tint
Object is a sort of wrapper for general info.
#define CFRA
NSNotificationCenter * center
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
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 bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams))
static void freeData(GpencilModifierData *md)
static void updateDepsgraph(GpencilModifierData *md, const ModifierUpdateDepsgraphContext *ctx, const int UNUSED(mode))
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)
GpencilModifierTypeInfo modifierType_Gpencil_Tint
static void panel_draw(const bContext *UNUSED(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
@ UI_ITEM_R_EXPAND
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
void uiItemS(uiLayout *layout)
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
void uiTemplateColorRamp(uiLayout *layout, struct PointerRNA *ptr, const char *propname, bool expand)
Scene scene
const Depsgraph * depsgraph
uint col
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6402
CBData data[32]
Definition: DNA_ID.h:273
Definition: BKE_main.h:116
struct DepsNodeHandle * node
Definition: BKE_modifier.h:147
float imat[4][4]
float obmat[4][4]
void * data
struct uiLayout * layout
struct CurveMapping * curve_intensity
ListBase frames
float vert_color[4]
bGPDspoint * points
float vert_color_fill[4]
float boundbox_max[3]
float boundbox_min[3]
struct MDeformVert * dvert
ListBase layers
PointerRNA * ptr
Definition: wm_files.c:3157