399ff27eafbd8579eda7d880cabea0f466a07d6c
[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                         if (gps->dvert) {
151                                 dvert = &gps->dvert[i - 1];
152                         }
153                         pt0 = &gps->points[i - 1];
154                         pt1 = &gps->points[i];
155
156                 }
157
158                 /* verify vertex group */
159                 const float weight = get_modifier_point_weight(dvert, (mmd->flag & GP_NOISE_INVERT_VGROUP) != 0, def_nr);
160                 if (weight < 0.0f) {
161                         continue;
162                 }
163
164                 /* initial vector (p0 -> p1) */
165                 sub_v3_v3v3(vec1, &pt1->x, &pt0->x);
166                 vran = len_v3(vec1);
167                 /* vector orthogonal to normal */
168                 cross_v3_v3v3(vec2, vec1, normal);
169                 normalize_v3(vec2);
170                 /* use random noise */
171                 if (mmd->flag & GP_NOISE_USE_RANDOM) {
172                         sc_diff = abs(mmd->scene_frame - sc_frame);
173                         /* only recalc if the gp frame change or the number of scene frames is bigger than step */
174                         if ((!gpl->actframe) || (mmd->gp_frame != gpl->actframe->framenum) ||
175                             (sc_diff >= mmd->step))
176                         {
177                                 vran = mmd->vrand1 = BLI_rng_get_float(mmd->rng);
178                                 vdir = mmd->vrand2 = BLI_rng_get_float(mmd->rng);
179                                 mmd->gp_frame = gpl->actframe->framenum;
180                                 mmd->scene_frame = sc_frame;
181                         }
182                         else {
183                                 vran = mmd->vrand1;
184                                 if (mmd->flag & GP_NOISE_FULL_STROKE) {
185                                         vdir = mmd->vrand2;
186                                 }
187                                 else {
188                                         int f = (mmd->vrand2 * 10.0f) + i;
189                                         vdir = f % 2;
190                                 }
191                         }
192                 }
193                 else {
194                         vran = 1.0f;
195                         if (mmd->flag & GP_NOISE_FULL_STROKE) {
196                                 vdir = gps->totpoints % 2;
197                         }
198                         else {
199                                 vdir = i % 2;
200                         }
201                         mmd->gp_frame = -999999;
202                 }
203
204                 /* apply randomness to location of the point */
205                 if (mmd->flag & GP_NOISE_MOD_LOCATION) {
206                         /* factor is too sensitive, so need divide */
207                         shift = ((vran * mmd->factor) / 1000.0f) * weight;
208                         if (vdir > 0.5f) {
209                                 mul_v3_fl(vec2, shift);
210                         }
211                         else {
212                                 mul_v3_fl(vec2, shift * -1.0f);
213                         }
214                         add_v3_v3(&pt1->x, vec2);
215                 }
216
217                 /* apply randomness to thickness */
218                 if (mmd->flag & GP_NOISE_MOD_THICKNESS) {
219                         if (vdir > 0.5f) {
220                                 pt1->pressure -= pt1->pressure * vran * mmd->factor;
221                         }
222                         else {
223                                 pt1->pressure += pt1->pressure * vran * mmd->factor;
224                         }
225                         CLAMP_MIN(pt1->pressure, GPENCIL_STRENGTH_MIN);
226                 }
227
228                 /* apply randomness to color strength */
229                 if (mmd->flag & GP_NOISE_MOD_STRENGTH) {
230                         if (vdir > 0.5f) {
231                                 pt1->strength -= pt1->strength * vran * mmd->factor;
232                         }
233                         else {
234                                 pt1->strength += pt1->strength * vran * mmd->factor;
235                         }
236                         CLAMP_MIN(pt1->strength, GPENCIL_STRENGTH_MIN);
237                 }
238                 /* apply randomness to uv rotation */
239                 if (mmd->flag & GP_NOISE_MOD_UV) {
240                         if (vdir > 0.5f) {
241                                 pt1->uv_rot -= pt1->uv_rot * vran * mmd->factor;
242                         }
243                         else {
244                                 pt1->uv_rot += pt1->uv_rot * vran * mmd->factor;
245                         }
246                         CLAMP(pt1->uv_rot, -M_PI_2, M_PI_2);
247                 }
248         }
249 }
250
251 static void bakeModifier(
252         struct Main *UNUSED(bmain), Depsgraph *depsgraph,
253         GpencilModifierData *md, Object *ob)
254 {
255         bGPdata *gpd = ob->data;
256
257         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
258                 for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
259                         for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
260                                 deformStroke(md, depsgraph, ob, gpl, gps);
261                         }
262                 }
263         }
264 }
265
266 GpencilModifierTypeInfo modifierType_Gpencil_Noise = {
267         /* name */              "Noise",
268         /* structName */        "NoiseGpencilModifierData",
269         /* structSize */        sizeof(NoiseGpencilModifierData),
270         /* type */              eGpencilModifierTypeType_Gpencil,
271         /* flags */             eGpencilModifierTypeFlag_SupportsEditmode,
272
273         /* copyData */          copyData,
274
275         /* deformStroke */      deformStroke,
276         /* generateStrokes */   NULL,
277         /* bakeModifier */      bakeModifier,
278         /* remapTime */         NULL,
279
280         /* initData */          initData,
281         /* freeData */          freeData,
282         /* isDisabled */        NULL,
283         /* updateDepsgraph */   NULL,
284         /* dependsOnTime */     dependsOnTime,
285         /* foreachObjectLink */ NULL,
286         /* foreachIDLink */     NULL,
287         /* foreachTexLink */    NULL,
288 };