Fix T59519: Grease Pencil stroke with noise modifier crashes blender
[blender.git] / source / blender / gpencil_modifiers / intern / MOD_gpencilnoise.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2017, Blender Foundation
19  * This is a new part of Blender
20  *
21  * Contributor(s): Antonio Vazquez
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  *
25  */
26
27 /** \file blender/gpencil_modifiers/intern/MOD_gpencilnoise.c
28  *  \ingroup modifiers
29  */
30
31 #include <stdio.h>
32
33 #include "BLI_blenlib.h"
34 #include "BLI_utildefines.h"
35 #include "BLI_math_vector.h"
36 #include "BLI_rand.h"
37
38 #include "PIL_time.h"
39
40 #include "DNA_meshdata_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_gpencil_types.h"
44 #include "DNA_gpencil_modifier_types.h"
45
46 #include "BKE_context.h"
47 #include "BKE_global.h"
48 #include "BKE_deform.h"
49 #include "BKE_gpencil.h"
50 #include "BKE_gpencil_modifier.h"
51 #include "BKE_object.h"
52
53 #include "DEG_depsgraph.h"
54 #include "DEG_depsgraph_query.h"
55
56 #include "MOD_gpencil_util.h"
57 #include "MOD_gpencil_modifiertypes.h"
58
59 static void initData(GpencilModifierData *md)
60 {
61         NoiseGpencilModifierData *gpmd = (NoiseGpencilModifierData *)md;
62         gpmd->pass_index = 0;
63         gpmd->flag |= GP_NOISE_MOD_LOCATION;
64         gpmd->flag |= GP_NOISE_FULL_STROKE;
65         gpmd->flag |= GP_NOISE_USE_RANDOM;
66         gpmd->factor = 0.5f;
67         gpmd->layername[0] = '\0';
68         gpmd->vgname[0] = '\0';
69         gpmd->step = 1;
70         gpmd->scene_frame = -999999;
71         gpmd->gp_frame = -999999;
72
73         gpmd->vrand1 = 1.0;
74         gpmd->vrand2 = 1.0;
75 }
76
77 static void freeData(GpencilModifierData *md)
78 {
79         NoiseGpencilModifierData *mmd = (NoiseGpencilModifierData *)md;
80
81         if (mmd->rng != NULL) {
82                 BLI_rng_free(mmd->rng);
83         }
84 }
85
86 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
87 {
88         BKE_gpencil_modifier_copyData_generic(md, target);
89 }
90
91 static bool dependsOnTime(GpencilModifierData *md)
92 {
93         NoiseGpencilModifierData *mmd = (NoiseGpencilModifierData *)md;
94         return (mmd->flag & GP_NOISE_USE_RANDOM) != 0;
95 }
96
97 /* aply noise effect based on stroke direction */
98 static void deformStroke(
99         GpencilModifierData *md, Depsgraph *depsgraph,
100         Object *ob, bGPDlayer *gpl, bGPDstroke *gps)
101 {
102         NoiseGpencilModifierData *mmd = (NoiseGpencilModifierData *)md;
103         bGPDspoint *pt0, *pt1;
104         MDeformVert *dvert = NULL;
105         float shift, vran, vdir;
106         float normal[3];
107         float vec1[3], vec2[3];
108         int sc_frame = 0;
109         int sc_diff = 0;
110         const int def_nr = defgroup_name_index(ob, mmd->vgname);
111
112         /* Random generator, only init once. */
113         if (mmd->rng == NULL) {
114                 uint rng_seed = (uint)(PIL_check_seconds_timer_i() & UINT_MAX);
115                 rng_seed ^= POINTER_AS_UINT(mmd);
116                 mmd->rng = BLI_rng_new(rng_seed);
117         }
118
119         if (!is_stroke_affected_by_modifier(
120                     ob,
121                     mmd->layername, mmd->pass_index, mmd->layer_pass, 3, gpl, gps,
122                     mmd->flag & GP_NOISE_INVERT_LAYER, mmd->flag & GP_NOISE_INVERT_PASS,
123                     mmd->flag & GP_NOISE_INVERT_LAYERPASS))
124         {
125                 return;
126         }
127
128         sc_frame = (int)DEG_get_ctime(depsgraph);
129
130         zero_v3(vec2);
131
132         /* calculate stroke normal*/
133         BKE_gpencil_stroke_normal(gps, normal);
134
135         /* move points */
136         for (int i = 0; i < gps->totpoints; i++) {
137                 if (((i == 0) || (i == gps->totpoints - 1)) && ((mmd->flag & GP_NOISE_MOVE_EXTREME) == 0)) {
138                         continue;
139                 }
140
141                 /* last point is special */
142                 if (i == gps->totpoints) {
143                         if (gps->dvert) {
144                                 dvert = &gps->dvert[i - 2];
145                         }
146                         pt0 = &gps->points[i - 2];
147                         pt1 = &gps->points[i - 1];
148                 }
149                 else {
150                         int prev_idx = i - 1;
151                         CLAMP_MIN(prev_idx, 0);
152                         if (gps->dvert) {
153                                 dvert = &gps->dvert[prev_idx];
154                         }
155                         pt0 = &gps->points[prev_idx];
156                         pt1 = &gps->points[i];
157
158                 }
159
160                 /* verify vertex group */
161                 const float weight = get_modifier_point_weight(dvert, (mmd->flag & GP_NOISE_INVERT_VGROUP) != 0, def_nr);
162                 if (weight < 0.0f) {
163                         continue;
164                 }
165
166                 /* initial vector (p0 -> p1) */
167                 sub_v3_v3v3(vec1, &pt1->x, &pt0->x);
168                 vran = len_v3(vec1);
169                 /* vector orthogonal to normal */
170                 cross_v3_v3v3(vec2, vec1, normal);
171                 normalize_v3(vec2);
172                 /* use random noise */
173                 if (mmd->flag & GP_NOISE_USE_RANDOM) {
174                         sc_diff = abs(mmd->scene_frame - sc_frame);
175                         /* only recalc if the gp frame change or the number of scene frames is bigger than step */
176                         if ((!gpl->actframe) || (mmd->gp_frame != gpl->actframe->framenum) ||
177                             (sc_diff >= mmd->step))
178                         {
179                                 vran = mmd->vrand1 = BLI_rng_get_float(mmd->rng);
180                                 vdir = mmd->vrand2 = BLI_rng_get_float(mmd->rng);
181                                 mmd->gp_frame = gpl->actframe->framenum;
182                                 mmd->scene_frame = sc_frame;
183                         }
184                         else {
185                                 vran = mmd->vrand1;
186                                 if (mmd->flag & GP_NOISE_FULL_STROKE) {
187                                         vdir = mmd->vrand2;
188                                 }
189                                 else {
190                                         int f = (mmd->vrand2 * 10.0f) + i;
191                                         vdir = f % 2;
192                                 }
193                         }
194                 }
195                 else {
196                         vran = 1.0f;
197                         if (mmd->flag & GP_NOISE_FULL_STROKE) {
198                                 vdir = gps->totpoints % 2;
199                         }
200                         else {
201                                 vdir = i % 2;
202                         }
203                         mmd->gp_frame = -999999;
204                 }
205
206                 /* apply randomness to location of the point */
207                 if (mmd->flag & GP_NOISE_MOD_LOCATION) {
208                         /* factor is too sensitive, so need divide */
209                         shift = ((vran * mmd->factor) / 1000.0f) * weight;
210                         if (vdir > 0.5f) {
211                                 mul_v3_fl(vec2, shift);
212                         }
213                         else {
214                                 mul_v3_fl(vec2, shift * -1.0f);
215                         }
216                         add_v3_v3(&pt1->x, vec2);
217                 }
218
219                 /* apply randomness to thickness */
220                 if (mmd->flag & GP_NOISE_MOD_THICKNESS) {
221                         if (vdir > 0.5f) {
222                                 pt1->pressure -= pt1->pressure * vran * mmd->factor;
223                         }
224                         else {
225                                 pt1->pressure += pt1->pressure * vran * mmd->factor;
226                         }
227                         CLAMP_MIN(pt1->pressure, GPENCIL_STRENGTH_MIN);
228                 }
229
230                 /* apply randomness to color strength */
231                 if (mmd->flag & GP_NOISE_MOD_STRENGTH) {
232                         if (vdir > 0.5f) {
233                                 pt1->strength -= pt1->strength * vran * mmd->factor;
234                         }
235                         else {
236                                 pt1->strength += pt1->strength * vran * mmd->factor;
237                         }
238                         CLAMP_MIN(pt1->strength, GPENCIL_STRENGTH_MIN);
239                 }
240                 /* apply randomness to uv rotation */
241                 if (mmd->flag & GP_NOISE_MOD_UV) {
242                         if (vdir > 0.5f) {
243                                 pt1->uv_rot -= pt1->uv_rot * vran * mmd->factor;
244                         }
245                         else {
246                                 pt1->uv_rot += pt1->uv_rot * vran * mmd->factor;
247                         }
248                         CLAMP(pt1->uv_rot, -M_PI_2, M_PI_2);
249                 }
250         }
251 }
252
253 static void bakeModifier(
254         struct Main *UNUSED(bmain), Depsgraph *depsgraph,
255         GpencilModifierData *md, Object *ob)
256 {
257         bGPdata *gpd = ob->data;
258
259         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
260                 for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
261                         for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
262                                 deformStroke(md, depsgraph, ob, gpl, gps);
263                         }
264                 }
265         }
266 }
267
268 GpencilModifierTypeInfo modifierType_Gpencil_Noise = {
269         /* name */              "Noise",
270         /* structName */        "NoiseGpencilModifierData",
271         /* structSize */        sizeof(NoiseGpencilModifierData),
272         /* type */              eGpencilModifierTypeType_Gpencil,
273         /* flags */             eGpencilModifierTypeFlag_SupportsEditmode,
274
275         /* copyData */          copyData,
276
277         /* deformStroke */      deformStroke,
278         /* generateStrokes */   NULL,
279         /* bakeModifier */      bakeModifier,
280         /* remapTime */         NULL,
281
282         /* initData */          initData,
283         /* freeData */          freeData,
284         /* isDisabled */        NULL,
285         /* updateDepsgraph */   NULL,
286         /* dependsOnTime */     dependsOnTime,
287         /* foreachObjectLink */ NULL,
288         /* foreachIDLink */     NULL,
289         /* foreachTexLink */    NULL,
290         /* getDuplicationFactor */ NULL,
291 };