Blender  V3.3
MOD_gpencilweight_proximity.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2021 Blender Foundation. */
3 
8 #include <stdio.h>
9 
10 #include "BLI_listbase.h"
11 #include "BLI_math_vector.h"
12 #include "BLI_utildefines.h"
13 
14 #include "BLT_translation.h"
15 
16 #include "DNA_defaults.h"
18 #include "DNA_gpencil_types.h"
19 #include "DNA_meshdata_types.h"
20 #include "DNA_object_types.h"
21 #include "DNA_screen_types.h"
22 
23 #include "BKE_colortools.h"
24 #include "BKE_context.h"
25 #include "BKE_deform.h"
26 #include "BKE_gpencil.h"
27 #include "BKE_gpencil_modifier.h"
28 #include "BKE_lib_query.h"
29 #include "BKE_modifier.h"
30 #include "BKE_screen.h"
31 
32 #include "DEG_depsgraph.h"
33 #include "DEG_depsgraph_build.h"
34 
35 #include "UI_interface.h"
36 #include "UI_resources.h"
37 
38 #include "RNA_access.h"
39 
41 #include "MOD_gpencil_ui_common.h"
42 #include "MOD_gpencil_util.h"
43 
44 static void initData(GpencilModifierData *md)
45 {
47 
48  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(gpmd, modifier));
49 
51 }
52 
53 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
54 {
56 }
57 
58 /* Calc distance between point and target object. */
61  const float dist_max,
62  const float dist_min,
63  bGPDspoint *pt)
64 {
65  float weight;
66  float gvert[3];
67  mul_v3_m4v3(gvert, ob->obmat, &pt->x);
68  float dist = len_v3v3(mmd->object->obmat[3], gvert);
69 
70  if (dist > dist_max) {
71  weight = 1.0f;
72  }
73  else if (dist <= dist_max && dist > dist_min) {
74  weight = 1.0f - ((dist_max - dist) / max_ff((dist_max - dist_min), 0.0001f));
75  }
76  else {
77  weight = 0.0f;
78  }
79 
80  return weight;
81 }
82 
83 /* change stroke thickness */
86  Object *ob,
87  bGPDlayer *gpl,
88  bGPDframe *UNUSED(gpf),
89  bGPDstroke *gps)
90 {
92  const int def_nr = BKE_object_defgroup_name_index(ob, mmd->vgname);
93 
95  mmd->layername,
96  mmd->material,
97  mmd->pass_index,
98  mmd->layer_pass,
99  1,
100  gpl,
101  gps,
106  return;
107  }
108 
109  const float dist_max = MAX2(mmd->dist_start, mmd->dist_end);
110  const float dist_min = MIN2(mmd->dist_start, mmd->dist_end);
111  const int target_def_nr = BKE_object_defgroup_name_index(ob, mmd->target_vgname);
112 
113  if (target_def_nr == -1) {
114  return;
115  }
116 
117  /* Ensure there is a vertex group. */
119 
120  float weight_pt = 1.0f;
121  for (int i = 0; i < gps->totpoints; i++) {
122  MDeformVert *dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
123  /* Verify point is part of vertex group. */
124  float weight = get_modifier_point_weight(
125  dvert, (mmd->flag & GP_WEIGHT_INVERT_VGROUP) != 0, def_nr);
126  if (weight < 0.0f) {
127  continue;
128  }
129 
130  if (mmd->object) {
131  bGPDspoint *pt = &gps->points[i];
132  weight_pt = calc_point_weight_by_distance(ob, mmd, dist_max, dist_min, pt);
133  }
134 
135  /* Invert weight if required. */
136  if (mmd->flag & GP_WEIGHT_INVERT_OUTPUT) {
137  weight_pt = 1.0f - weight_pt;
138  }
139  /* Assign weight. */
140  dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
141  if (dvert != NULL) {
142  MDeformWeight *dw = BKE_defvert_ensure_index(dvert, target_def_nr);
143  if (dw) {
144  dw->weight = (mmd->flag & GP_WEIGHT_MULTIPLY_DATA) ? dw->weight * weight_pt : weight_pt;
145  CLAMP(dw->weight, mmd->min_weight, 1.0f);
146  }
147  }
148  }
149 }
150 
151 static void bakeModifier(struct Main *UNUSED(bmain),
154  Object *ob)
155 {
157 }
158 
159 static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
160 {
162 
163  walk(userData, ob, (ID **)&mmd->material, IDWALK_CB_USER);
164  walk(userData, ob, (ID **)&mmd->object, IDWALK_CB_NOP);
165 }
166 
169  const int UNUSED(mode))
170 {
172  if (mmd->object != NULL) {
174  ctx->node, mmd->object, DEG_OB_COMP_TRANSFORM, "GPencil Weight Modifier");
175  }
177  ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "GPencil Weight Modifier");
178 }
179 
180 static bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams))
181 {
183 
184  return ((mmd->target_vgname[0] == '\0') || (mmd->object == NULL));
185 }
186 
187 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
188 {
189  uiLayout *row, *sub;
190  uiLayout *layout = panel->layout;
191 
192  PointerRNA ob_ptr;
194 
195  uiLayoutSetPropSep(layout, true);
196  row = uiLayoutRow(layout, true);
197  uiItemPointerR(row, ptr, "target_vertex_group", &ob_ptr, "vertex_groups", NULL, ICON_NONE);
198  sub = uiLayoutRow(row, true);
199  bool has_output = RNA_string_length(ptr, "target_vertex_group") != 0;
200  uiLayoutSetPropDecorate(sub, false);
201  uiLayoutSetActive(sub, has_output);
202  uiItemR(sub, ptr, "use_invert_output", 0, "", ICON_ARROW_LEFTRIGHT);
203 
204  uiItemR(layout, ptr, "object", 0, NULL, ICON_NONE);
205 
206  sub = uiLayoutColumn(layout, true);
207  uiItemR(sub, ptr, "distance_start", 0, NULL, ICON_NONE);
208  uiItemR(sub, ptr, "distance_end", 0, NULL, ICON_NONE);
209 
210  uiItemR(layout, ptr, "minimum_weight", 0, NULL, ICON_NONE);
211  uiItemR(layout, ptr, "use_multiply", 0, NULL, ICON_NONE);
212 
214 }
215 
216 static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
217 {
218  gpencil_modifier_masking_panel_draw(panel, true, true);
219 }
220 
221 static void panelRegister(ARegionType *region_type)
222 {
225 
227  region_type, "mask", "Influence", NULL, mask_panel_draw, panel_type);
228 }
229 
231  /* name */ N_("Vertex Weight Proximity"),
232  /* structName */ "WeightProxGpencilModifierData",
233  /* structSize */ sizeof(WeightProxGpencilModifierData),
235  /* flags */ 0,
236 
237  /* copyData */ copyData,
238 
239  /* deformStroke */ deformStroke,
240  /* generateStrokes */ NULL,
241  /* bakeModifier */ bakeModifier,
242  /* remapTime */ NULL,
243 
244  /* initData */ initData,
245  /* freeData */ NULL,
246  /* isDisabled */ isDisabled,
247  /* updateDepsgraph */ updateDepsgraph,
248  /* dependsOnTime */ NULL,
249  /* foreachIDLink */ foreachIDLink,
250  /* foreachTexLink */ NULL,
251  /* panelRegister */ panelRegister,
252 };
support for deformation groups and hooks.
int BKE_object_defgroup_name_index(const struct Object *ob, const char *name)
struct MDeformWeight * BKE_defvert_ensure_index(struct MDeformVert *dv, int defgroup)
Definition: deform.c:748
void BKE_gpencil_dvert_ensure(struct bGPDstroke *gps)
Definition: gpencil.c:1891
void BKE_gpencil_modifier_copydata_generic(const struct GpencilModifierData *md_src, struct GpencilModifierData *md_dst)
@ eGpencilModifierTypeType_Gpencil
@ IDWALK_CB_USER
Definition: BKE_lib_query.h:73
@ IDWALK_CB_NOP
Definition: BKE_lib_query.h:33
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
MINLINE float max_ff(float a, float b)
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:739
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
#define UNUSED(x)
#define MAX2(a, b)
#define MIN2(a, b)
#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:35
void DEG_add_object_relation(struct DepsNodeHandle *node_handle, struct Object *object, eDepsObjectComponentType component, const char *description)
@ DEG_OB_COMP_TRANSFORM
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:29
@ GP_WEIGHT_INVERT_VGROUP
@ GP_WEIGHT_INVERT_OUTPUT
@ GP_WEIGHT_INVERT_MATERIAL
@ GP_WEIGHT_INVERT_LAYERPASS
@ GP_WEIGHT_MULTIPLY_DATA
@ GP_WEIGHT_INVERT_LAYER
@ eGpencilModifierType_WeightProximity
struct WeightProxGpencilModifierData WeightProxGpencilModifierData
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)
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 bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams))
static void updateDepsgraph(GpencilModifierData *md, const ModifierUpdateDepsgraphContext *ctx, const int UNUSED(mode))
static float calc_point_weight_by_distance(Object *ob, WeightProxGpencilModifierData *mmd, const float dist_max, const float dist_min, bGPDspoint *pt)
static void bakeModifier(struct Main *UNUSED(bmain), Depsgraph *depsgraph, GpencilModifierData *md, Object *ob)
static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
GpencilModifierTypeInfo modifierType_Gpencil_WeightProximity
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)
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)
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
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)
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
void uiItemPointerR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, struct PointerRNA *searchptr, const char *searchpropname, const char *name, int icon)
const Depsgraph * depsgraph
int RNA_string_length(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5144
Definition: DNA_ID.h:368
Definition: BKE_main.h:121
struct DepsNodeHandle * node
Definition: BKE_modifier.h:134
float obmat[4][4]
struct uiLayout * layout
bGPDspoint * points
struct MDeformVert * dvert
#define N_(msgid)
PointerRNA * ptr
Definition: wm_files.c:3480