Merge branch 'master' into blender2.8
[blender.git] / source / blender / modifiers / intern / MOD_wave.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) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Daniel Dunbar
22  *                 Ton Roosendaal,
23  *                 Ben Batt,
24  *                 Brecht Van Lommel,
25  *                 Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/modifiers/intern/MOD_wave.c
32  *  \ingroup modifiers
33  */
34
35
36 #include "BLI_math.h"
37
38 #include "DNA_meshdata_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_object_types.h"
41
42 #include "BLI_utildefines.h"
43
44
45 #include "BKE_deform.h"
46 #include "BKE_DerivedMesh.h"
47 #include "BKE_library.h"
48 #include "BKE_library_query.h"
49 #include "BKE_scene.h"
50 #include "BKE_texture.h"
51
52 #include "MEM_guardedalloc.h"
53 #include "RE_shader_ext.h"
54
55 #include "MOD_modifiertypes.h"
56 #include "MOD_util.h"
57
58 #include "DEG_depsgraph.h"
59
60 static void initData(ModifierData *md)
61 {
62         WaveModifierData *wmd = (WaveModifierData *) md; // whadya know, moved here from Iraq
63
64         wmd->flag |= (MOD_WAVE_X | MOD_WAVE_Y | MOD_WAVE_CYCL |
65                       MOD_WAVE_NORM_X | MOD_WAVE_NORM_Y | MOD_WAVE_NORM_Z);
66
67         wmd->objectcenter = NULL;
68         wmd->texture = NULL;
69         wmd->map_object = NULL;
70         wmd->height = 0.5f;
71         wmd->width = 1.5f;
72         wmd->speed = 0.25f;
73         wmd->narrow = 1.5f;
74         wmd->lifetime = 0.0f;
75         wmd->damp = 10.0f;
76         wmd->falloff = 0.0f;
77         wmd->texmapping = MOD_DISP_MAP_LOCAL;
78         wmd->defgrp_name[0] = 0;
79 }
80
81 static void copyData(ModifierData *md, ModifierData *target)
82 {
83 #if 0
84         WaveModifierData *wmd = (WaveModifierData *) md;
85         WaveModifierData *twmd = (WaveModifierData *) target;
86 #endif
87
88         modifier_copyData_generic(md, target);
89 }
90
91 static bool dependsOnTime(ModifierData *UNUSED(md))
92 {
93         return true;
94 }
95
96 static void foreachObjectLink(
97         ModifierData *md, Object *ob,
98         ObjectWalkFunc walk, void *userData)
99 {
100         WaveModifierData *wmd = (WaveModifierData *) md;
101
102         walk(userData, ob, &wmd->objectcenter, IDWALK_CB_NOP);
103         walk(userData, ob, &wmd->map_object, IDWALK_CB_NOP);
104 }
105
106 static void foreachIDLink(ModifierData *md, Object *ob,
107                           IDWalkFunc walk, void *userData)
108 {
109         WaveModifierData *wmd = (WaveModifierData *) md;
110
111         walk(userData, ob, (ID **)&wmd->texture, IDWALK_CB_USER);
112
113         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
114 }
115
116 static void foreachTexLink(ModifierData *md, Object *ob,
117                            TexWalkFunc walk, void *userData)
118 {
119         walk(userData, ob, md, "texture");
120 }
121
122 static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
123 {
124         WaveModifierData *wmd = (WaveModifierData *)md;
125         if (wmd->objectcenter != NULL) {
126                 DEG_add_object_relation(ctx->node, wmd->objectcenter, DEG_OB_COMP_TRANSFORM, "Wave Modifier");
127         }
128         if (wmd->map_object != NULL) {
129                 DEG_add_object_relation(ctx->node, wmd->map_object, DEG_OB_COMP_TRANSFORM, "Wave Modifier");
130         }
131 }
132
133 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
134 {
135         WaveModifierData *wmd = (WaveModifierData *)md;
136         CustomDataMask dataMask = 0;
137
138
139         /* ask for UV coordinates if we need them */
140         if (wmd->texture && wmd->texmapping == MOD_DISP_MAP_UV)
141                 dataMask |= CD_MASK_MTFACE;
142
143         /* ask for vertexgroups if we need them */
144         if (wmd->defgrp_name[0])
145                 dataMask |= CD_MASK_MDEFORMVERT;
146
147         return dataMask;
148 }
149
150 static void waveModifier_do(WaveModifierData *md,
151                             const EvaluationContext *eval_ctx,
152                             Object *ob, DerivedMesh *dm,
153                             float (*vertexCos)[3], int numVerts)
154 {
155         WaveModifierData *wmd = (WaveModifierData *) md;
156         MVert *mvert = NULL;
157         MDeformVert *dvert;
158         int defgrp_index;
159         float ctime = eval_ctx->ctime;
160         float minfac = (float)(1.0 / exp(wmd->width * wmd->narrow * wmd->width * wmd->narrow));
161         float lifefac = wmd->height;
162         float (*tex_co)[3] = NULL;
163         const int wmd_axis = wmd->flag & (MOD_WAVE_X | MOD_WAVE_Y);
164         const float falloff = wmd->falloff;
165         float falloff_fac = 1.0f; /* when falloff == 0.0f this stays at 1.0f */
166
167         if ((wmd->flag & MOD_WAVE_NORM) && (ob->type == OB_MESH))
168                 mvert = dm->getVertArray(dm);
169
170         if (wmd->objectcenter) {
171                 float mat[4][4];
172                 /* get the control object's location in local coordinates */
173                 invert_m4_m4(ob->imat, ob->obmat);
174                 mul_m4_m4m4(mat, ob->imat, wmd->objectcenter->obmat);
175
176                 wmd->startx = mat[3][0];
177                 wmd->starty = mat[3][1];
178         }
179
180         /* get the index of the deform group */
181         modifier_get_vgroup(ob, dm, wmd->defgrp_name, &dvert, &defgrp_index);
182
183         if (wmd->damp == 0) wmd->damp = 10.0f;
184
185         if (wmd->lifetime != 0.0f) {
186                 float x = ctime - wmd->timeoffs;
187
188                 if (x > wmd->lifetime) {
189                         lifefac = x - wmd->lifetime;
190
191                         if (lifefac > wmd->damp) lifefac = 0.0;
192                         else lifefac = (float)(wmd->height * (1.0f - sqrtf(lifefac / wmd->damp)));
193                 }
194         }
195
196         if (wmd->texture) {
197                 tex_co = MEM_malloc_arrayN(numVerts, sizeof(*tex_co),
198                                      "waveModifier_do tex_co");
199                 get_texture_coords((MappingInfoModifierData *)wmd, ob, dm, vertexCos, tex_co, numVerts);
200
201                 modifier_init_texture(wmd->modifier.scene, wmd->texture);
202         }
203
204         if (lifefac != 0.0f) {
205                 /* avoid divide by zero checks within the loop */
206                 float falloff_inv = falloff ? 1.0f / falloff : 1.0f;
207                 int i;
208
209                 for (i = 0; i < numVerts; i++) {
210                         float *co = vertexCos[i];
211                         float x = co[0] - wmd->startx;
212                         float y = co[1] - wmd->starty;
213                         float amplit = 0.0f;
214                         float def_weight = 1.0f;
215
216                         /* get weights */
217                         if (dvert) {
218                                 def_weight = defvert_find_weight(&dvert[i], defgrp_index);
219
220                                 /* if this vert isn't in the vgroup, don't deform it */
221                                 if (def_weight == 0.0f) {
222                                         continue;
223                                 }
224                         }
225
226                         switch (wmd_axis) {
227                                 case MOD_WAVE_X | MOD_WAVE_Y:
228                                         amplit = sqrtf(x * x + y * y);
229                                         break;
230                                 case MOD_WAVE_X:
231                                         amplit = x;
232                                         break;
233                                 case MOD_WAVE_Y:
234                                         amplit = y;
235                                         break;
236                         }
237
238                         /* this way it makes nice circles */
239                         amplit -= (ctime - wmd->timeoffs) * wmd->speed;
240
241                         if (wmd->flag & MOD_WAVE_CYCL) {
242                                 amplit = (float)fmodf(amplit - wmd->width, 2.0f * wmd->width) +
243                                          wmd->width;
244                         }
245
246                         if (falloff != 0.0f) {
247                                 float dist = 0.0f;
248
249                                 switch (wmd_axis) {
250                                         case MOD_WAVE_X | MOD_WAVE_Y:
251                                                 dist = sqrtf(x * x + y * y);
252                                                 break;
253                                         case MOD_WAVE_X:
254                                                 dist = fabsf(x);
255                                                 break;
256                                         case MOD_WAVE_Y:
257                                                 dist = fabsf(y);
258                                                 break;
259                                 }
260
261                                 falloff_fac = (1.0f - (dist * falloff_inv));
262                                 CLAMP(falloff_fac, 0.0f, 1.0f);
263                         }
264
265                         /* GAUSSIAN */
266                         if ((falloff_fac != 0.0f) && (amplit > -wmd->width) && (amplit < wmd->width)) {
267                                 amplit = amplit * wmd->narrow;
268                                 amplit = (float)(1.0f / expf(amplit * amplit) - minfac);
269
270                                 /*apply texture*/
271                                 if (wmd->texture) {
272                                         TexResult texres;
273                                         texres.nor = NULL;
274                                         BKE_texture_get_value(wmd->modifier.scene, wmd->texture, tex_co[i], &texres, false);
275                                         amplit *= texres.tin;
276                                 }
277
278                                 /*apply weight & falloff */
279                                 amplit *= def_weight * falloff_fac;
280
281                                 if (mvert) {
282                                         /* move along normals */
283                                         if (wmd->flag & MOD_WAVE_NORM_X) {
284                                                 co[0] += (lifefac * amplit) * mvert[i].no[0] / 32767.0f;
285                                         }
286                                         if (wmd->flag & MOD_WAVE_NORM_Y) {
287                                                 co[1] += (lifefac * amplit) * mvert[i].no[1] / 32767.0f;
288                                         }
289                                         if (wmd->flag & MOD_WAVE_NORM_Z) {
290                                                 co[2] += (lifefac * amplit) * mvert[i].no[2] / 32767.0f;
291                                         }
292                                 }
293                                 else {
294                                         /* move along local z axis */
295                                         co[2] += lifefac * amplit;
296                                 }
297                         }
298                 }
299         }
300
301         if (wmd->texture) MEM_freeN(tex_co);
302 }
303
304 static void deformVerts(ModifierData *md, const struct EvaluationContext *eval_ctx,
305                         Object *ob, DerivedMesh *derivedData,
306                         float (*vertexCos)[3],
307                         int numVerts,
308                         ModifierApplyFlag UNUSED(flag))
309 {
310         DerivedMesh *dm = derivedData;
311         WaveModifierData *wmd = (WaveModifierData *)md;
312
313         if (wmd->flag & MOD_WAVE_NORM)
314                 dm = get_cddm(ob, NULL, dm, vertexCos, false);
315         else if (wmd->texture || wmd->defgrp_name[0])
316                 dm = get_dm(ob, NULL, dm, NULL, false, false);
317
318         waveModifier_do(wmd, eval_ctx, ob, dm, vertexCos, numVerts);
319
320         if (dm != derivedData)
321                 dm->release(dm);
322 }
323
324 static void deformVertsEM(
325         ModifierData *md, const struct EvaluationContext *eval_ctx,
326         Object *ob, struct BMEditMesh *editData,
327         DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
328 {
329         DerivedMesh *dm = derivedData;
330         WaveModifierData *wmd = (WaveModifierData *)md;
331
332         if (wmd->flag & MOD_WAVE_NORM)
333                 dm = get_cddm(ob, editData, dm, vertexCos, false);
334         else if (wmd->texture || wmd->defgrp_name[0])
335                 dm = get_dm(ob, editData, dm, NULL, false, false);
336
337         waveModifier_do(wmd, eval_ctx, ob, dm, vertexCos, numVerts);
338
339         if (dm != derivedData)
340                 dm->release(dm);
341 }
342
343
344 ModifierTypeInfo modifierType_Wave = {
345         /* name */              "Wave",
346         /* structName */        "WaveModifierData",
347         /* structSize */        sizeof(WaveModifierData),
348         /* type */              eModifierTypeType_OnlyDeform,
349         /* flags */             eModifierTypeFlag_AcceptsCVs |
350                                 eModifierTypeFlag_AcceptsLattice |
351                                 eModifierTypeFlag_SupportsEditmode,
352         /* copyData */          copyData,
353         /* deformVerts */       deformVerts,
354         /* deformMatrices */    NULL,
355         /* deformVertsEM */     deformVertsEM,
356         /* deformMatricesEM */  NULL,
357         /* applyModifier */     NULL,
358         /* applyModifierEM */   NULL,
359         /* initData */          initData,
360         /* requiredDataMask */  requiredDataMask,
361         /* freeData */          NULL,
362         /* isDisabled */        NULL,
363         /* updateDepsgraph */   updateDepsgraph,
364         /* dependsOnTime */     dependsOnTime,
365         /* dependsOnNormals */  NULL,
366         /* foreachObjectLink */ foreachObjectLink,
367         /* foreachIDLink */     foreachIDLink,
368         /* foreachTexLink */    foreachTexLink,
369 };