Fix Cycles particle info node not working.
[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 freeData(ModifierData *md)
82 {
83         WaveModifierData *wmd = (WaveModifierData *) md;
84         if (wmd->texture) {
85                 id_us_min(&wmd->texture->id);
86         }
87 }
88
89 static void copyData(ModifierData *md, ModifierData *target)
90 {
91 #if 0
92         WaveModifierData *wmd = (WaveModifierData *) md;
93         WaveModifierData *twmd = (WaveModifierData *) target;
94 #endif
95
96         modifier_copyData_generic(md, target);
97 }
98
99 static bool dependsOnTime(ModifierData *UNUSED(md))
100 {
101         return true;
102 }
103
104 static void foreachObjectLink(
105         ModifierData *md, Object *ob,
106         ObjectWalkFunc walk, void *userData)
107 {
108         WaveModifierData *wmd = (WaveModifierData *) md;
109
110         walk(userData, ob, &wmd->objectcenter, IDWALK_CB_NOP);
111         walk(userData, ob, &wmd->map_object, IDWALK_CB_NOP);
112 }
113
114 static void foreachIDLink(ModifierData *md, Object *ob,
115                           IDWalkFunc walk, void *userData)
116 {
117         WaveModifierData *wmd = (WaveModifierData *) md;
118
119         walk(userData, ob, (ID **)&wmd->texture, IDWALK_CB_USER);
120
121         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
122 }
123
124 static void foreachTexLink(ModifierData *md, Object *ob,
125                            TexWalkFunc walk, void *userData)
126 {
127         walk(userData, ob, md, "texture");
128 }
129
130 static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
131 {
132         WaveModifierData *wmd = (WaveModifierData *)md;
133         if (wmd->objectcenter != NULL) {
134                 DEG_add_object_relation(ctx->node, wmd->objectcenter, DEG_OB_COMP_TRANSFORM, "Wave Modifier");
135         }
136         if (wmd->map_object != NULL) {
137                 DEG_add_object_relation(ctx->node, wmd->map_object, DEG_OB_COMP_TRANSFORM, "Wave Modifier");
138         }
139 }
140
141 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
142 {
143         WaveModifierData *wmd = (WaveModifierData *)md;
144         CustomDataMask dataMask = 0;
145
146
147         /* ask for UV coordinates if we need them */
148         if (wmd->texture && wmd->texmapping == MOD_DISP_MAP_UV)
149                 dataMask |= CD_MASK_MTFACE;
150
151         /* ask for vertexgroups if we need them */
152         if (wmd->defgrp_name[0])
153                 dataMask |= CD_MASK_MDEFORMVERT;
154
155         return dataMask;
156 }
157
158 static void waveModifier_do(WaveModifierData *md,
159                             const EvaluationContext *eval_ctx,
160                             Object *ob, DerivedMesh *dm,
161                             float (*vertexCos)[3], int numVerts)
162 {
163         WaveModifierData *wmd = (WaveModifierData *) md;
164         MVert *mvert = NULL;
165         MDeformVert *dvert;
166         int defgrp_index;
167         float ctime = eval_ctx->ctime;
168         float minfac = (float)(1.0 / exp(wmd->width * wmd->narrow * wmd->width * wmd->narrow));
169         float lifefac = wmd->height;
170         float (*tex_co)[3] = NULL;
171         const int wmd_axis = wmd->flag & (MOD_WAVE_X | MOD_WAVE_Y);
172         const float falloff = wmd->falloff;
173         float falloff_fac = 1.0f; /* when falloff == 0.0f this stays at 1.0f */
174
175         if ((wmd->flag & MOD_WAVE_NORM) && (ob->type == OB_MESH))
176                 mvert = dm->getVertArray(dm);
177
178         if (wmd->objectcenter) {
179                 float mat[4][4];
180                 /* get the control object's location in local coordinates */
181                 invert_m4_m4(ob->imat, ob->obmat);
182                 mul_m4_m4m4(mat, ob->imat, wmd->objectcenter->obmat);
183
184                 wmd->startx = mat[3][0];
185                 wmd->starty = mat[3][1];
186         }
187
188         /* get the index of the deform group */
189         modifier_get_vgroup(ob, dm, wmd->defgrp_name, &dvert, &defgrp_index);
190
191         if (wmd->damp == 0) wmd->damp = 10.0f;
192
193         if (wmd->lifetime != 0.0f) {
194                 float x = ctime - wmd->timeoffs;
195
196                 if (x > wmd->lifetime) {
197                         lifefac = x - wmd->lifetime;
198
199                         if (lifefac > wmd->damp) lifefac = 0.0;
200                         else lifefac = (float)(wmd->height * (1.0f - sqrtf(lifefac / wmd->damp)));
201                 }
202         }
203
204         if (wmd->texture) {
205                 tex_co = MEM_malloc_arrayN(numVerts, sizeof(*tex_co),
206                                      "waveModifier_do tex_co");
207                 get_texture_coords((MappingInfoModifierData *)wmd, ob, dm, vertexCos, tex_co, numVerts);
208
209                 modifier_init_texture(wmd->modifier.scene, wmd->texture);
210         }
211
212         if (lifefac != 0.0f) {
213                 /* avoid divide by zero checks within the loop */
214                 float falloff_inv = falloff ? 1.0f / falloff : 1.0f;
215                 int i;
216
217                 for (i = 0; i < numVerts; i++) {
218                         float *co = vertexCos[i];
219                         float x = co[0] - wmd->startx;
220                         float y = co[1] - wmd->starty;
221                         float amplit = 0.0f;
222                         float def_weight = 1.0f;
223
224                         /* get weights */
225                         if (dvert) {
226                                 def_weight = defvert_find_weight(&dvert[i], defgrp_index);
227
228                                 /* if this vert isn't in the vgroup, don't deform it */
229                                 if (def_weight == 0.0f) {
230                                         continue;
231                                 }
232                         }
233
234                         switch (wmd_axis) {
235                                 case MOD_WAVE_X | MOD_WAVE_Y:
236                                         amplit = sqrtf(x * x + y * y);
237                                         break;
238                                 case MOD_WAVE_X:
239                                         amplit = x;
240                                         break;
241                                 case MOD_WAVE_Y:
242                                         amplit = y;
243                                         break;
244                         }
245
246                         /* this way it makes nice circles */
247                         amplit -= (ctime - wmd->timeoffs) * wmd->speed;
248
249                         if (wmd->flag & MOD_WAVE_CYCL) {
250                                 amplit = (float)fmodf(amplit - wmd->width, 2.0f * wmd->width) +
251                                          wmd->width;
252                         }
253
254                         if (falloff != 0.0f) {
255                                 float dist = 0.0f;
256
257                                 switch (wmd_axis) {
258                                         case MOD_WAVE_X | MOD_WAVE_Y:
259                                                 dist = sqrtf(x * x + y * y);
260                                                 break;
261                                         case MOD_WAVE_X:
262                                                 dist = fabsf(x);
263                                                 break;
264                                         case MOD_WAVE_Y:
265                                                 dist = fabsf(y);
266                                                 break;
267                                 }
268
269                                 falloff_fac = (1.0f - (dist * falloff_inv));
270                                 CLAMP(falloff_fac, 0.0f, 1.0f);
271                         }
272
273                         /* GAUSSIAN */
274                         if ((falloff_fac != 0.0f) && (amplit > -wmd->width) && (amplit < wmd->width)) {
275                                 amplit = amplit * wmd->narrow;
276                                 amplit = (float)(1.0f / expf(amplit * amplit) - minfac);
277
278                                 /*apply texture*/
279                                 if (wmd->texture) {
280                                         TexResult texres;
281                                         texres.nor = NULL;
282                                         BKE_texture_get_value(wmd->modifier.scene, wmd->texture, tex_co[i], &texres, false);
283                                         amplit *= texres.tin;
284                                 }
285
286                                 /*apply weight & falloff */
287                                 amplit *= def_weight * falloff_fac;
288
289                                 if (mvert) {
290                                         /* move along normals */
291                                         if (wmd->flag & MOD_WAVE_NORM_X) {
292                                                 co[0] += (lifefac * amplit) * mvert[i].no[0] / 32767.0f;
293                                         }
294                                         if (wmd->flag & MOD_WAVE_NORM_Y) {
295                                                 co[1] += (lifefac * amplit) * mvert[i].no[1] / 32767.0f;
296                                         }
297                                         if (wmd->flag & MOD_WAVE_NORM_Z) {
298                                                 co[2] += (lifefac * amplit) * mvert[i].no[2] / 32767.0f;
299                                         }
300                                 }
301                                 else {
302                                         /* move along local z axis */
303                                         co[2] += lifefac * amplit;
304                                 }
305                         }
306                 }
307         }
308
309         if (wmd->texture) MEM_freeN(tex_co);
310 }
311
312 static void deformVerts(ModifierData *md, const struct EvaluationContext *eval_ctx,
313                         Object *ob, DerivedMesh *derivedData,
314                         float (*vertexCos)[3],
315                         int numVerts,
316                         ModifierApplyFlag UNUSED(flag))
317 {
318         DerivedMesh *dm = derivedData;
319         WaveModifierData *wmd = (WaveModifierData *)md;
320
321         if (wmd->flag & MOD_WAVE_NORM)
322                 dm = get_cddm(ob, NULL, dm, vertexCos, false);
323         else if (wmd->texture || wmd->defgrp_name[0])
324                 dm = get_dm(ob, NULL, dm, NULL, false, false);
325
326         waveModifier_do(wmd, eval_ctx, ob, dm, vertexCos, numVerts);
327
328         if (dm != derivedData)
329                 dm->release(dm);
330 }
331
332 static void deformVertsEM(
333         ModifierData *md, const struct EvaluationContext *eval_ctx,
334         Object *ob, struct BMEditMesh *editData,
335         DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
336 {
337         DerivedMesh *dm = derivedData;
338         WaveModifierData *wmd = (WaveModifierData *)md;
339
340         if (wmd->flag & MOD_WAVE_NORM)
341                 dm = get_cddm(ob, editData, dm, vertexCos, false);
342         else if (wmd->texture || wmd->defgrp_name[0])
343                 dm = get_dm(ob, editData, dm, NULL, false, false);
344
345         waveModifier_do(wmd, eval_ctx, ob, dm, vertexCos, numVerts);
346
347         if (dm != derivedData)
348                 dm->release(dm);
349 }
350
351
352 ModifierTypeInfo modifierType_Wave = {
353         /* name */              "Wave",
354         /* structName */        "WaveModifierData",
355         /* structSize */        sizeof(WaveModifierData),
356         /* type */              eModifierTypeType_OnlyDeform,
357         /* flags */             eModifierTypeFlag_AcceptsCVs |
358                                 eModifierTypeFlag_AcceptsLattice |
359                                 eModifierTypeFlag_SupportsEditmode,
360         /* copyData */          copyData,
361         /* deformVerts */       deformVerts,
362         /* deformMatrices */    NULL,
363         /* deformVertsEM */     deformVertsEM,
364         /* deformMatricesEM */  NULL,
365         /* applyModifier */     NULL,
366         /* applyModifierEM */   NULL,
367         /* initData */          initData,
368         /* requiredDataMask */  requiredDataMask,
369         /* freeData */          freeData,
370         /* isDisabled */        NULL,
371         /* updateDepsgraph */   updateDepsgraph,
372         /* dependsOnTime */     dependsOnTime,
373         /* dependsOnNormals */  NULL,
374         /* foreachObjectLink */ foreachObjectLink,
375         /* foreachIDLink */     foreachIDLink,
376         /* foreachTexLink */    foreachTexLink,
377 };