Depsgrapgh: Use more distinctive opcode for texture and image evaluation
[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_mesh_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42
43 #include "BLI_utildefines.h"
44
45
46 #include "BKE_deform.h"
47 #include "BKE_editmesh.h"
48 #include "BKE_library.h"
49 #include "BKE_library_query.h"
50 #include "BKE_mesh.h"
51 #include "BKE_scene.h"
52 #include "BKE_texture.h"
53
54 #include "MEM_guardedalloc.h"
55 #include "RE_shader_ext.h"
56
57 #include "MOD_modifiertypes.h"
58 #include "MOD_util.h"
59
60 #include "DEG_depsgraph.h"
61 #include "DEG_depsgraph_query.h"
62
63 static void initData(ModifierData *md)
64 {
65         WaveModifierData *wmd = (WaveModifierData *) md; // whadya know, moved here from Iraq
66
67         wmd->flag |= (MOD_WAVE_X | MOD_WAVE_Y | MOD_WAVE_CYCL |
68                       MOD_WAVE_NORM_X | MOD_WAVE_NORM_Y | MOD_WAVE_NORM_Z);
69
70         wmd->objectcenter = NULL;
71         wmd->texture = NULL;
72         wmd->map_object = NULL;
73         wmd->height = 0.5f;
74         wmd->width = 1.5f;
75         wmd->speed = 0.25f;
76         wmd->narrow = 1.5f;
77         wmd->lifetime = 0.0f;
78         wmd->damp = 10.0f;
79         wmd->falloff = 0.0f;
80         wmd->texmapping = MOD_DISP_MAP_LOCAL;
81         wmd->defgrp_name[0] = 0;
82 }
83
84 static bool dependsOnTime(ModifierData *UNUSED(md))
85 {
86         return true;
87 }
88
89 static void foreachObjectLink(
90         ModifierData *md, Object *ob,
91         ObjectWalkFunc walk, void *userData)
92 {
93         WaveModifierData *wmd = (WaveModifierData *) md;
94
95         walk(userData, ob, &wmd->objectcenter, IDWALK_CB_NOP);
96         walk(userData, ob, &wmd->map_object, IDWALK_CB_NOP);
97 }
98
99 static void foreachIDLink(
100         ModifierData *md, Object *ob,
101         IDWalkFunc walk, void *userData)
102 {
103         WaveModifierData *wmd = (WaveModifierData *) md;
104
105         walk(userData, ob, (ID **)&wmd->texture, IDWALK_CB_USER);
106
107         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
108 }
109
110 static void foreachTexLink(
111         ModifierData *md, Object *ob,
112         TexWalkFunc walk, void *userData)
113 {
114         walk(userData, ob, md, "texture");
115 }
116
117 static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
118 {
119         WaveModifierData *wmd = (WaveModifierData *)md;
120         if (wmd->objectcenter != NULL) {
121                 DEG_add_object_relation(ctx->node, wmd->objectcenter, DEG_OB_COMP_TRANSFORM, "Wave Modifier");
122         }
123         if (wmd->map_object != NULL) {
124                 DEG_add_object_relation(ctx->node, wmd->map_object, DEG_OB_COMP_TRANSFORM, "Wave Modifier");
125         }
126         if (wmd->objectcenter != NULL || wmd->map_object != NULL) {
127                 DEG_add_object_relation(ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "Wave Modifier");
128         }
129 }
130
131 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
132 {
133         WaveModifierData *wmd = (WaveModifierData *)md;
134         CustomDataMask dataMask = 0;
135
136
137         /* ask for UV coordinates if we need them */
138         if (wmd->texture && wmd->texmapping == MOD_DISP_MAP_UV)
139                 dataMask |= CD_MASK_MTFACE;
140
141         /* ask for vertexgroups if we need them */
142         if (wmd->defgrp_name[0])
143                 dataMask |= CD_MASK_MDEFORMVERT;
144
145         return dataMask;
146 }
147
148 static bool dependsOnNormals(ModifierData *md)
149 {
150         WaveModifierData *wmd = (WaveModifierData *)md;
151
152         return (wmd->flag & MOD_WAVE_NORM) != 0;
153 }
154
155 static void waveModifier_do(
156         WaveModifierData *md,
157         Depsgraph *depsgraph,
158         Object *ob, Mesh *mesh,
159         float (*vertexCos)[3], int numVerts)
160 {
161         WaveModifierData *wmd = (WaveModifierData *) md;
162         MVert *mvert = NULL;
163         MDeformVert *dvert;
164         int defgrp_index;
165         float ctime = DEG_get_ctime(depsgraph);
166         float minfac = (float)(1.0 / exp(wmd->width * wmd->narrow * wmd->width * wmd->narrow));
167         float lifefac = wmd->height;
168         float (*tex_co)[3] = NULL;
169         const int wmd_axis = wmd->flag & (MOD_WAVE_X | MOD_WAVE_Y);
170         const float falloff = wmd->falloff;
171         float falloff_fac = 1.0f; /* when falloff == 0.0f this stays at 1.0f */
172
173         if ((wmd->flag & MOD_WAVE_NORM) && (mesh != NULL)) {
174                 mvert = mesh->mvert;
175         }
176
177         if (wmd->objectcenter) {
178                 float mat[4][4];
179                 /* get the control object's location in local coordinates */
180                 invert_m4_m4(ob->imat, ob->obmat);
181                 mul_m4_m4m4(mat, ob->imat, wmd->objectcenter->obmat);
182
183                 wmd->startx = mat[3][0];
184                 wmd->starty = mat[3][1];
185         }
186
187         /* get the index of the deform group */
188         MOD_get_vgroup(ob, mesh, wmd->defgrp_name, &dvert, &defgrp_index);
189
190         if (wmd->damp == 0.0f) {
191                 wmd->damp = 10.0f;
192         }
193
194         if (wmd->lifetime != 0.0f) {
195                 float x = ctime - wmd->timeoffs;
196
197                 if (x > wmd->lifetime) {
198                         lifefac = x - wmd->lifetime;
199
200                         if (lifefac > wmd->damp) lifefac = 0.0;
201                         else lifefac = (float)(wmd->height * (1.0f - sqrtf(lifefac / wmd->damp)));
202                 }
203         }
204
205         if (mesh != NULL && wmd->texture) {
206                 tex_co = MEM_malloc_arrayN(numVerts, sizeof(*tex_co), "waveModifier_do tex_co");
207                 MOD_get_texture_coords((MappingInfoModifierData *)wmd, ob, mesh, vertexCos, tex_co);
208
209                 MOD_init_texture(depsgraph, wmd->texture);
210         }
211
212         if (lifefac != 0.0f) {
213                 /* avoid divide by zero checks within the loop */
214                 float falloff_inv = falloff != 0.0f ? 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                                         Scene *scene = DEG_get_evaluated_scene(depsgraph);
281                                         TexResult texres;
282                                         texres.nor = NULL;
283                                         BKE_texture_get_value(scene, wmd->texture, tex_co[i], &texres, false);
284                                         amplit *= texres.tin;
285                                 }
286
287                                 /*apply weight & falloff */
288                                 amplit *= def_weight * falloff_fac;
289
290                                 if (mvert) {
291                                         /* move along normals */
292                                         if (wmd->flag & MOD_WAVE_NORM_X) {
293                                                 co[0] += (lifefac * amplit) * mvert[i].no[0] / 32767.0f;
294                                         }
295                                         if (wmd->flag & MOD_WAVE_NORM_Y) {
296                                                 co[1] += (lifefac * amplit) * mvert[i].no[1] / 32767.0f;
297                                         }
298                                         if (wmd->flag & MOD_WAVE_NORM_Z) {
299                                                 co[2] += (lifefac * amplit) * mvert[i].no[2] / 32767.0f;
300                                         }
301                                 }
302                                 else {
303                                         /* move along local z axis */
304                                         co[2] += lifefac * amplit;
305                                 }
306                         }
307                 }
308         }
309
310         MEM_SAFE_FREE(tex_co);
311 }
312
313 static void deformVerts(
314         ModifierData *md, const ModifierEvalContext *ctx,
315         Mesh *mesh,
316         float (*vertexCos)[3],
317         int numVerts)
318 {
319         WaveModifierData *wmd = (WaveModifierData *)md;
320         Mesh *mesh_src = NULL;
321
322         if (wmd->flag & MOD_WAVE_NORM) {
323                 mesh_src = MOD_deform_mesh_eval_get(ctx->object, NULL, mesh, vertexCos, numVerts, true, false);
324         }
325         else if (wmd->texture != NULL || wmd->defgrp_name[0] != '\0') {
326                 mesh_src = MOD_deform_mesh_eval_get(ctx->object, NULL, mesh, NULL, numVerts, false, false);
327         }
328
329         waveModifier_do(wmd, ctx->depsgraph, ctx->object, mesh_src, vertexCos, numVerts);
330
331         if (!ELEM(mesh_src, NULL, mesh)) {
332                 BKE_id_free(NULL, mesh_src);
333         }
334 }
335
336 static void deformVertsEM(
337         ModifierData *md, const ModifierEvalContext *ctx,
338         struct BMEditMesh *editData,
339         Mesh *mesh, float (*vertexCos)[3], int numVerts)
340 {
341         WaveModifierData *wmd = (WaveModifierData *)md;
342         Mesh *mesh_src = NULL;
343
344         if (wmd->flag & MOD_WAVE_NORM) {
345                 mesh_src = MOD_deform_mesh_eval_get(ctx->object, editData, mesh, vertexCos, numVerts, true, false);
346         }
347         else if (wmd->texture != NULL || wmd->defgrp_name[0] != '\0') {
348                 mesh_src = MOD_deform_mesh_eval_get(ctx->object, editData, mesh, NULL, numVerts, false, false);
349         }
350
351         waveModifier_do(wmd, ctx->depsgraph, ctx->object, mesh_src, vertexCos, numVerts);
352
353         if (!ELEM(mesh_src, NULL, mesh)) {
354                 BKE_id_free(NULL, mesh_src);
355         }
356 }
357
358
359 ModifierTypeInfo modifierType_Wave = {
360         /* name */              "Wave",
361         /* structName */        "WaveModifierData",
362         /* structSize */        sizeof(WaveModifierData),
363         /* type */              eModifierTypeType_OnlyDeform,
364         /* flags */             eModifierTypeFlag_AcceptsCVs |
365                                 eModifierTypeFlag_AcceptsLattice |
366                                 eModifierTypeFlag_SupportsEditmode,
367
368         /* copyData */          modifier_copyData_generic,
369
370         /* deformVerts_DM */    NULL,
371         /* deformMatrices_DM */ NULL,
372         /* deformVertsEM_DM */  NULL,
373         /* deformMatricesEM_DM*/NULL,
374         /* applyModifier_DM */  NULL,
375
376         /* deformVerts */       deformVerts,
377         /* deformMatrices */    NULL,
378         /* deformVertsEM */     deformVertsEM,
379         /* deformMatricesEM */  NULL,
380         /* applyModifier */     NULL,
381
382         /* initData */          initData,
383         /* requiredDataMask */  requiredDataMask,
384         /* freeData */          NULL,
385         /* isDisabled */        NULL,
386         /* updateDepsgraph */   updateDepsgraph,
387         /* dependsOnTime */     dependsOnTime,
388         /* dependsOnNormals */  dependsOnNormals,
389         /* foreachObjectLink */ foreachObjectLink,
390         /* foreachIDLink */     foreachIDLink,
391         /* foreachTexLink */    foreachTexLink,
392 };