Blender  V3.3
MOD_gpenciloffset.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2017 Blender Foundation. */
3 
8 #include <stdio.h>
9 
10 #include "BLI_hash.h"
11 #include "BLI_listbase.h"
12 #include "BLI_math.h"
13 #include "BLI_rand.h"
14 #include "BLI_utildefines.h"
15 
16 #include "BLT_translation.h"
17 
18 #include "DNA_defaults.h"
20 #include "DNA_gpencil_types.h"
21 #include "DNA_meshdata_types.h"
22 #include "DNA_object_types.h"
23 #include "DNA_screen_types.h"
24 
25 #include "BKE_context.h"
26 #include "BKE_deform.h"
27 #include "BKE_gpencil_geom.h"
28 #include "BKE_gpencil_modifier.h"
29 #include "BKE_lib_query.h"
30 #include "BKE_modifier.h"
31 #include "BKE_screen.h"
32 
33 #include "DEG_depsgraph.h"
34 
35 #include "UI_interface.h"
36 #include "UI_resources.h"
37 
39 #include "MOD_gpencil_ui_common.h"
40 #include "MOD_gpencil_util.h"
41 
42 static void initData(GpencilModifierData *md)
43 {
45 
46  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(gpmd, modifier));
47 
49 }
50 
51 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
52 {
54 }
55 
56 /* change stroke offsetness */
59  Object *ob,
60  bGPDlayer *gpl,
61  bGPDframe *gpf,
62  bGPDstroke *gps)
63 {
65  const int def_nr = BKE_object_defgroup_name_index(ob, mmd->vgname);
66 
67  float mat[4][4];
68  float loc[3], rot[3], scale[3];
69 
71  mmd->layername,
72  mmd->material,
73  mmd->pass_index,
74  mmd->layer_pass,
75  1,
76  gpl,
77  gps,
82  return;
83  }
84 
85  const bool is_randomized = !(is_zero_v3(mmd->rnd_offset) && is_zero_v3(mmd->rnd_rot) &&
86  is_zero_v3(mmd->rnd_scale));
87 
88  int seed = mmd->seed;
89  /* Make sure different modifiers get different seeds. */
90  seed += BLI_hash_string(ob->id.name + 2);
91  seed += BLI_hash_string(md->name);
92 
93  float rand[3][3];
94  float rand_offset = BLI_hash_int_01(seed);
95 
96  if (is_randomized) {
97  /* Get stroke index for random offset. */
98  int rnd_index = BLI_findindex(&gpf->strokes, gps);
99  for (int j = 0; j < 3; j++) {
100  const uint primes[3] = {2, 3, 7};
101  double offset[3] = {0.0f, 0.0f, 0.0f};
102  double r[3];
103  /* To ensure a nice distribution, we use halton sequence and offset using the seed. */
104  BLI_halton_3d(primes, offset, rnd_index, r);
105 
106  if ((mmd->flag & GP_OFFSET_UNIFORM_RANDOM_SCALE) && j == 2) {
107  float rand_value;
108  rand_value = fmodf(r[0] * 2.0f - 1.0f + rand_offset, 1.0f);
109  rand_value = fmodf(sin(rand_value * 12.9898f + j * 78.233f) * 43758.5453f, 1.0f);
110  copy_v3_fl(rand[j], rand_value);
111  }
112  else {
113  for (int i = 0; i < 3; i++) {
114  rand[j][i] = fmodf(r[i] * 2.0f - 1.0f + rand_offset, 1.0f);
115  rand[j][i] = fmodf(sin(rand[j][i] * 12.9898f + j * 78.233f) * 43758.5453f, 1.0f);
116  }
117  }
118  }
119  }
120 
121  bGPdata *gpd = ob->data;
122 
123  for (int i = 0; i < gps->totpoints; i++) {
124  bGPDspoint *pt = &gps->points[i];
125  MDeformVert *dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
126 
127  /* Verify vertex group. */
128  const float weight = get_modifier_point_weight(
129  dvert, (mmd->flag & GP_OFFSET_INVERT_VGROUP) != 0, def_nr);
130  if (weight < 0.0f) {
131  continue;
132  }
133 
134  /* Calculate Random matrix. */
135  if (is_randomized) {
136  float mat_rnd[4][4];
137  float rnd_loc[3], rnd_rot[3], rnd_scale_weight[3];
138  float rnd_scale[3] = {1.0f, 1.0f, 1.0f};
139 
140  mul_v3_v3fl(rnd_loc, rand[0], weight);
141  mul_v3_v3fl(rnd_rot, rand[1], weight);
142  mul_v3_v3fl(rnd_scale_weight, rand[2], weight);
143 
144  mul_v3_v3v3(rnd_loc, mmd->rnd_offset, rnd_loc);
145  mul_v3_v3v3(rnd_rot, mmd->rnd_rot, rnd_rot);
146  madd_v3_v3v3(rnd_scale, mmd->rnd_scale, rnd_scale_weight);
147 
148  loc_eul_size_to_mat4(mat_rnd, rnd_loc, rnd_rot, rnd_scale);
149  /* Apply randomness matrix. */
150  mul_m4_v3(mat_rnd, &pt->x);
151  }
152 
153  /* Calculate matrix. */
154  mul_v3_v3fl(loc, mmd->loc, weight);
155  mul_v3_v3fl(rot, mmd->rot, weight);
156  mul_v3_v3fl(scale, mmd->scale, weight);
157  add_v3_fl(scale, 1.0);
158  loc_eul_size_to_mat4(mat, loc, rot, scale);
159 
160  /* Apply scale to thickness. */
161  float unit_scale = (fabsf(scale[0]) + fabsf(scale[1]) + fabsf(scale[2])) / 3.0f;
162  pt->pressure *= unit_scale;
163 
164  mul_m4_v3(mat, &pt->x);
165  }
166  /* Calc geometry data. */
168 }
169 
170 static void bakeModifier(struct Main *UNUSED(bmain),
173  Object *ob)
174 {
176 }
177 
178 static void foreachIDLink(GpencilModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
179 {
181 
182  walk(userData, ob, (ID **)&mmd->material, IDWALK_CB_USER);
183 }
184 
185 static void panel_draw(const bContext *UNUSED(C), Panel *panel)
186 {
187  uiLayout *layout = panel->layout;
188 
190 
191  uiLayoutSetPropSep(layout, true);
192 
193  uiItemR(layout, ptr, "location", 0, NULL, ICON_NONE);
194  uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
195  uiItemR(layout, ptr, "scale", 0, NULL, ICON_NONE);
196 
198 }
199 
200 static void random_panel_draw(const bContext *UNUSED(C), Panel *panel)
201 {
202  uiLayout *layout = panel->layout;
203 
205 
206  uiLayoutSetPropSep(layout, true);
207 
208  uiItemR(layout, ptr, "random_offset", 0, IFACE_("Offset"), ICON_NONE);
209  uiItemR(layout, ptr, "random_rotation", 0, IFACE_("Rotation"), ICON_NONE);
210  uiItemR(layout, ptr, "random_scale", 0, IFACE_("Scale"), ICON_NONE);
211  uiItemR(layout, ptr, "use_uniform_random_scale", 0, NULL, ICON_NONE);
212  uiItemR(layout, ptr, "seed", 0, NULL, ICON_NONE);
213 }
214 
215 static void mask_panel_draw(const bContext *UNUSED(C), Panel *panel)
216 {
217  gpencil_modifier_masking_panel_draw(panel, true, true);
218 }
219 
220 static void panelRegister(ARegionType *region_type)
221 {
225  region_type, "randomize", "Randomize", NULL, random_panel_draw, panel_type);
227  region_type, "mask", "Influence", NULL, mask_panel_draw, panel_type);
228 }
229 
231  /* name */ N_("Offset"),
232  /* structName */ "OffsetGpencilModifierData",
233  /* structSize */ sizeof(OffsetGpencilModifierData),
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 */ NULL,
247  /* updateDepsgraph */ NULL,
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)
void BKE_gpencil_stroke_geometry_update(struct bGPdata *gpd, struct bGPDstroke *gps)
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: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
BLI_INLINE float BLI_hash_int_01(unsigned int k)
Definition: BLI_hash.h:94
BLI_INLINE unsigned int BLI_hash_string(const char *str)
Definition: BLI_hash.h:69
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:729
void loc_eul_size_to_mat4(float R[4][4], const float loc[3], const float eul[3], const float size[3])
Definition: math_matrix.c:2547
MINLINE void add_v3_fl(float r[3], float f)
MINLINE void mul_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE bool is_zero_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void madd_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_fl(float r[3], float f)
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
Random number functions.
void BLI_halton_3d(const unsigned int prime[3], double offset[3], int n, double *r)
Definition: rand.cc:311
unsigned int uint
Definition: BLI_sys_types.h:67
#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:35
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:29
struct OffsetGpencilModifierData OffsetGpencilModifierData
@ GP_OFFSET_INVERT_MATERIAL
@ GP_OFFSET_UNIFORM_RANDOM_SCALE
@ GP_OFFSET_INVERT_LAYER
@ GP_OFFSET_INVERT_VGROUP
@ GP_OFFSET_INVERT_LAYERPASS
@ eGpencilModifierType_Offset
Object is a sort of wrapper for general info.
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
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 bakeModifier(struct Main *UNUSED(bmain), Depsgraph *depsgraph, GpencilModifierData *md, Object *ob)
static void random_panel_draw(const bContext *UNUSED(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 copyData(const GpencilModifierData *md, GpencilModifierData *target)
static void panel_draw(const bContext *UNUSED(C), Panel *panel)
static void panelRegister(ARegionType *region_type)
static void deformStroke(GpencilModifierData *md, Depsgraph *UNUSED(depsgraph), Object *ob, bGPDlayer *gpl, bGPDframe *gpf, bGPDstroke *gps)
static void initData(GpencilModifierData *md)
GpencilModifierTypeInfo modifierType_Gpencil_Offset
#define C
Definition: RandGen.cpp:25
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
static unsigned long seed
Definition: btSoftBody.h:39
const Depsgraph * depsgraph
#define rot(x, k)
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
#define fmodf(x, y)
Definition: metal/compat.h:230
#define fabsf(x)
Definition: metal/compat.h:219
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:311
Definition: DNA_ID.h:368
char name[66]
Definition: DNA_ID.h:378
Definition: BKE_main.h:121
void * data
struct uiLayout * layout
ListBase strokes
bGPDspoint * points
struct MDeformVert * dvert
#define N_(msgid)
PointerRNA * ptr
Definition: wm_files.c:3480