Blender  V2.93
MOD_gpencilcolor.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_color.h"
30 #include "BLI_math_vector.h"
31 
32 #include "DNA_defaults.h"
34 #include "DNA_gpencil_types.h"
35 #include "DNA_material_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_screen_types.h"
38 
39 #include "BKE_colortools.h"
40 #include "BKE_context.h"
41 #include "BKE_gpencil_modifier.h"
42 #include "BKE_lib_query.h"
43 #include "BKE_main.h"
44 #include "BKE_material.h"
45 #include "BKE_screen.h"
46 
47 #include "UI_interface.h"
48 #include "UI_resources.h"
49 
50 #include "BKE_modifier.h"
51 
52 #include "DEG_depsgraph.h"
53 
55 #include "MOD_gpencil_ui_common.h"
56 #include "MOD_gpencil_util.h"
57 
58 static void initData(GpencilModifierData *md)
59 {
61 
62  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(gpmd, modifier));
63 
65 
66  gpmd->curve_intensity = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
68 }
69 
70 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
71 {
74 
75  if (tgmd->curve_intensity != NULL) {
77  tgmd->curve_intensity = NULL;
78  }
79 
81 
83 }
84 
85 /* color correction strokes */
88  Object *ob,
89  bGPDlayer *gpl,
90  bGPDframe *UNUSED(gpf),
91  bGPDstroke *gps)
92 {
93 
95  float hsv[3], factor[3];
96  const bool use_curve = (mmd->flag & GP_COLOR_CUSTOM_CURVE) != 0 && mmd->curve_intensity;
97 
99  mmd->layername,
100  mmd->material,
101  mmd->pass_index,
102  mmd->layer_pass,
103  1,
104  gpl,
105  gps,
107  mmd->flag & GP_COLOR_INVERT_PASS,
109  mmd->flag & GP_COLOR_INVERT_MATERIAL)) {
110  return;
111  }
112 
113  copy_v3_v3(factor, mmd->hsv);
115 
116  /* Apply to Vertex Color. */
117  /* Fill */
118  if (mmd->modify_color != GP_MODIFY_COLOR_STROKE) {
119  /* If not using Vertex Color, use the material color. */
120  if ((gp_style != NULL) && (gps->vert_color_fill[3] == 0.0f) &&
121  (gp_style->fill_rgba[3] > 0.0f)) {
122  copy_v4_v4(gps->vert_color_fill, gp_style->fill_rgba);
123  gps->vert_color_fill[3] = 1.0f;
124  }
125 
126  rgb_to_hsv_v(gps->vert_color_fill, hsv);
127  hsv[0] = fractf(hsv[0] + factor[0] + 0.5f);
128  hsv[1] = clamp_f(hsv[1] * factor[1], 0.0f, 1.0f);
129  hsv[2] = hsv[2] * factor[2];
130  hsv_to_rgb_v(hsv, gps->vert_color_fill);
131  }
132 
133  /* Stroke */
134  if (mmd->modify_color != GP_MODIFY_COLOR_FILL) {
135 
136  for (int i = 0; i < gps->totpoints; i++) {
137  bGPDspoint *pt = &gps->points[i];
138  /* If not using Vertex Color, use the material color. */
139  if ((gp_style != NULL) && (pt->vert_color[3] == 0.0f) && (gp_style->stroke_rgba[3] > 0.0f)) {
140  copy_v4_v4(pt->vert_color, gp_style->stroke_rgba);
141  pt->vert_color[3] = 1.0f;
142  }
143 
144  /* Custom curve to modulate value. */
145  float factor_value[3];
146  copy_v3_v3(factor_value, factor);
147  if (use_curve) {
148  float value = (float)i / (gps->totpoints - 1);
149  float mixfac = BKE_curvemapping_evaluateF(mmd->curve_intensity, 0, value);
150  mul_v3_fl(factor_value, mixfac);
151  }
152 
153  rgb_to_hsv_v(pt->vert_color, hsv);
154  hsv[0] = fractf(hsv[0] + factor_value[0] + 0.5f);
155  hsv[1] = clamp_f(hsv[1] * factor_value[1], 0.0f, 1.0f);
156  hsv[2] = hsv[2] * factor_value[2];
157  hsv_to_rgb_v(hsv, pt->vert_color);
158  }
159  }
160 }
161 
162 static void bakeModifier(Main *UNUSED(bmain),
165  Object *ob)
166 {
167  bGPdata *gpd = ob->data;
168 
169  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
170  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
171  LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
172  deformStroke(md, depsgraph, ob, gpl, gpf, gps);
173  }
174  }
175  }
176 }
177 
179 {
181 
182  if (gpmd->curve_intensity) {
184  }
185 }
186 
187 static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
188 {
190 
191  walk(userData, ob, (ID **)&mmd->material, IDWALK_CB_USER);
192 }
193 
194 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
195 {
196  uiLayout *layout = panel->layout;
197 
199 
200  uiLayoutSetPropSep(layout, true);
201 
202  uiItemR(layout, ptr, "modify_color", 0, NULL, ICON_NONE);
203  uiItemR(layout, ptr, "hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
204  uiItemR(layout, ptr, "saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
205  uiItemR(layout, ptr, "value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
206 
208 }
209 
210 static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
211 {
212  gpencil_modifier_masking_panel_draw(panel, true, false);
213 }
214 
215 static void panelRegister(ARegionType *region_type)
216 {
218  region_type, eGpencilModifierType_Color, panel_draw);
220  region_type, "mask", "Influence", NULL, mask_panel_draw, panel_type);
222  "curve",
223  "",
226  mask_panel_type);
227 }
228 
230  /* name */ "Hue/Saturation",
231  /* structName */ "ColorGpencilModifierData",
232  /* structSize */ sizeof(ColorGpencilModifierData),
235 
236  /* copyData */ copyData,
237 
238  /* deformStroke */ deformStroke,
239  /* generateStrokes */ NULL,
240  /* bakeModifier */ bakeModifier,
241  /* remapTime */ NULL,
242 
243  /* initData */ initData,
244  /* freeData */ freeData,
245  /* isDisabled */ NULL,
246  /* updateDepsgraph */ NULL,
247  /* dependsOnTime */ NULL,
248  /* foreachIDLink */ foreachIDLink,
249  /* foreachTexLink */ NULL,
250  /* panelRegister */ panelRegister,
251 };
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
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
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
#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 hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition: math_color.c:68
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
Definition: math_color.c:254
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
#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
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:44
@ GP_COLOR_INVERT_LAYERPASS
@ GP_COLOR_INVERT_MATERIAL
struct ColorGpencilModifierData ColorGpencilModifierData
@ GP_MODIFY_COLOR_STROKE
@ eGpencilModifierType_Color
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)
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)
GpencilModifierTypeInfo modifierType_Gpencil_Color
static void freeData(GpencilModifierData *md)
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 panelRegister(ARegionType *region_type)
static void initData(GpencilModifierData *md)
#define C
Definition: RandGen.cpp:39
@ UI_ITEM_R_SLIDER
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
MINLINE float fractf(float a)
struct CurveMapping * curve_intensity
Definition: DNA_ID.h:273
Definition: BKE_main.h:116
void * data
struct uiLayout * layout
ListBase frames
float vert_color[4]
bGPDspoint * points
float vert_color_fill[4]
ListBase layers
PointerRNA * ptr
Definition: wm_files.c:3157