Blender  V3.3
MOD_gpencilweight_angle.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_context.h"
24 #include "BKE_deform.h"
25 #include "BKE_gpencil.h"
26 #include "BKE_gpencil_modifier.h"
27 #include "BKE_lib_query.h"
28 #include "BKE_modifier.h"
29 #include "BKE_screen.h"
30 
31 #include "DEG_depsgraph.h"
32 #include "DEG_depsgraph_build.h"
33 
34 #include "UI_interface.h"
35 #include "UI_resources.h"
36 
37 #include "RNA_access.h"
38 
40 #include "MOD_gpencil_ui_common.h"
41 #include "MOD_gpencil_util.h"
42 
43 static void initData(GpencilModifierData *md)
44 {
46 
47  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(gpmd, modifier));
48 
50 }
51 
52 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
53 {
55 }
56 
57 /* change stroke thickness */
60  Object *ob,
61  bGPDlayer *gpl,
62  bGPDframe *UNUSED(gpf),
63  bGPDstroke *gps)
64 {
66  const int def_nr = BKE_object_defgroup_name_index(ob, mmd->vgname);
67 
69  mmd->layername,
70  mmd->material,
71  mmd->pass_index,
72  mmd->layer_pass,
73  1,
74  gpl,
75  gps,
80  return;
81  }
82 
83  const int target_def_nr = BKE_object_defgroup_name_index(ob, mmd->target_vgname);
84 
85  if (target_def_nr == -1) {
86  return;
87  }
88 
89  /* Use default Z up. */
90  float vec_axis[3] = {0.0f, 0.0f, 1.0f};
91  float axis[3] = {0.0f, 0.0f, 0.0f};
92  axis[mmd->axis] = 1.0f;
93  float vec_ref[3];
94  /* Apply modifier rotation (sub 90 degrees for Y axis due Z-Up vector). */
95  float rot_angle = mmd->angle - ((mmd->axis == 1) ? M_PI_2 : 0.0f);
96  rotate_normalized_v3_v3v3fl(vec_ref, vec_axis, axis, rot_angle);
97 
98  /* Apply the rotation of the object. */
99  if (mmd->space == GP_SPACE_LOCAL) {
100  mul_mat3_m4_v3(ob->obmat, vec_ref);
101  }
102 
103  /* Ensure there is a vertex group. */
105 
106  float weight_pt = 1.0f;
107  for (int i = 0; i < gps->totpoints; i++) {
108  MDeformVert *dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
109  /* Verify point is part of vertex group. */
110  float weight = get_modifier_point_weight(
111  dvert, (mmd->flag & GP_WEIGHT_INVERT_VGROUP) != 0, def_nr);
112  if (weight < 0.0f) {
113  continue;
114  }
115 
116  /* Special case for single points. */
117  if (gps->totpoints == 1) {
118  weight_pt = 1.0f;
119  break;
120  }
121 
122  bGPDspoint *pt1 = (i > 0) ? &gps->points[i] : &gps->points[i + 1];
123  bGPDspoint *pt2 = (i > 0) ? &gps->points[i - 1] : &gps->points[i];
124  float fpt1[3], fpt2[3];
125  mul_v3_m4v3(fpt1, ob->obmat, &pt1->x);
126  mul_v3_m4v3(fpt2, ob->obmat, &pt2->x);
127 
128  float vec[3];
129  sub_v3_v3v3(vec, fpt1, fpt2);
130  float angle = angle_on_axis_v3v3_v3(vec_ref, vec, axis);
131  /* Use sin to get a value between 0 and 1. */
132  weight_pt = 1.0f - sin(angle);
133 
134  /* Invert weight if required. */
135  if (mmd->flag & GP_WEIGHT_INVERT_OUTPUT) {
136  weight_pt = 1.0f - weight_pt;
137  }
138  /* Assign weight. */
139  dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
140  if (dvert != NULL) {
141  MDeformWeight *dw = BKE_defvert_ensure_index(dvert, target_def_nr);
142  if (dw) {
143  dw->weight = (mmd->flag & GP_WEIGHT_MULTIPLY_DATA) ? dw->weight * weight_pt : weight_pt;
144  CLAMP(dw->weight, mmd->min_weight, 1.0f);
145  }
146  }
147  }
148 }
149 
150 static void bakeModifier(struct Main *UNUSED(bmain),
153  Object *ob)
154 {
156 }
157 
158 static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
159 {
161 
162  walk(userData, ob, (ID **)&mmd->material, IDWALK_CB_USER);
163 }
164 
165 static bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams))
166 {
168 
169  return (mmd->target_vgname[0] == '\0');
170 }
171 
172 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
173 {
174  uiLayout *row, *sub;
175  uiLayout *layout = panel->layout;
176 
177  PointerRNA ob_ptr;
179 
180  uiLayoutSetPropSep(layout, true);
181  row = uiLayoutRow(layout, true);
182  uiItemPointerR(row, ptr, "target_vertex_group", &ob_ptr, "vertex_groups", NULL, ICON_NONE);
183  sub = uiLayoutRow(row, true);
184  bool has_output = RNA_string_length(ptr, "target_vertex_group") != 0;
185  uiLayoutSetPropDecorate(sub, false);
186  uiLayoutSetActive(sub, has_output);
187  uiItemR(sub, ptr, "use_invert_output", 0, "", ICON_ARROW_LEFTRIGHT);
188 
189  uiItemR(layout, ptr, "angle", 0, NULL, ICON_NONE);
190  uiItemR(layout, ptr, "axis", 0, NULL, ICON_NONE);
191  uiItemR(layout, ptr, "space", 0, NULL, ICON_NONE);
192 
193  uiItemR(layout, ptr, "minimum_weight", 0, NULL, ICON_NONE);
194  uiItemR(layout, ptr, "use_multiply", 0, NULL, ICON_NONE);
195 
197 }
198 
199 static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
200 {
201  gpencil_modifier_masking_panel_draw(panel, true, true);
202 }
203 
204 static void panelRegister(ARegionType *region_type)
205 {
208 
210  region_type, "mask", "Influence", NULL, mask_panel_draw, panel_type);
211 }
212 
214  /* name */ N_("Vertex Weight Angle"),
215  /* structName */ "WeightAngleGpencilModifierData",
216  /* structSize */ sizeof(WeightAngleGpencilModifierData),
218  /* flags */ 0,
219 
220  /* copyData */ copyData,
221 
222  /* deformStroke */ deformStroke,
223  /* generateStrokes */ NULL,
224  /* bakeModifier */ bakeModifier,
225  /* remapTime */ NULL,
226 
227  /* initData */ initData,
228  /* freeData */ NULL,
229  /* isDisabled */ isDisabled,
230  /* updateDepsgraph */ NULL,
231  /* dependsOnTime */ NULL,
232  /* foreachIDLink */ foreachIDLink,
233  /* foreachTexLink */ NULL,
234  /* panelRegister */ panelRegister,
235 };
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
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 M_PI_2
Definition: BLI_math_base.h:23
void mul_mat3_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:790
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:739
float angle_on_axis_v3v3_v3(const float v1[3], const float v2[3], const float axis[3]) ATTR_WARN_UNUSED_RESULT
Definition: math_vector.c:477
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void rotate_normalized_v3_v3v3fl(float out[3], const float p[3], const float axis[3], float angle)
Definition: math_vector.c:776
#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:35
#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
struct WeightAngleGpencilModifierData WeightAngleGpencilModifierData
@ eGpencilModifierType_WeightAngle
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))
GpencilModifierTypeInfo modifierType_Gpencil_WeightAngle
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)
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)
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)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
const Depsgraph * depsgraph
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:311
int RNA_string_length(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5144
Definition: DNA_ID.h:368
Definition: BKE_main.h:121
float obmat[4][4]
struct uiLayout * layout
bGPDspoint * points
struct MDeformVert * dvert
#define N_(msgid)
PointerRNA * ptr
Definition: wm_files.c:3480