Depsgraph: remove EvaluationContext, pass Depsgraph instead.
[blender.git] / source / blender / modifiers / intern / MOD_dynamicpaint.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  * Contributor(s): Miika Hämäläinen
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  *
22  */
23
24 /** \file blender/modifiers/intern/MOD_dynamicpaint.c
25  *  \ingroup modifiers
26  */
27
28 #include <stddef.h>
29
30 #include "DNA_dynamicpaint_types.h"
31 #include "DNA_object_types.h"
32 #include "DNA_object_force_types.h"
33 #include "DNA_scene_types.h"
34
35 #include "BLI_utildefines.h"
36
37 #include "BKE_cdderivedmesh.h"
38 #include "BKE_dynamicpaint.h"
39 #include "BKE_layer.h"
40 #include "BKE_library.h"
41 #include "BKE_library_query.h"
42 #include "BKE_modifier.h"
43
44 #include "DEG_depsgraph.h"
45
46 #include "DEG_depsgraph_build.h"
47
48 #include "MOD_modifiertypes.h"
49
50 static void initData(ModifierData *md) 
51 {
52         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *) md;
53         
54         pmd->canvas = NULL;
55         pmd->brush = NULL;
56         pmd->type = MOD_DYNAMICPAINT_TYPE_CANVAS;
57 }
58
59 static void copyData(ModifierData *md, ModifierData *target)
60 {
61         DynamicPaintModifierData *pmd  = (DynamicPaintModifierData *)md;
62         DynamicPaintModifierData *tpmd = (DynamicPaintModifierData *)target;
63         
64         dynamicPaint_Modifier_copy(pmd, tpmd);
65
66         if (tpmd->canvas) {
67                 for (DynamicPaintSurface *surface = tpmd->canvas->surfaces.first; surface; surface = surface->next) {
68                         id_us_plus((ID *)surface->init_texture);
69                 }
70         }
71         if (tpmd->brush) {
72                 id_us_plus((ID *)tpmd->brush->mat);
73         }
74 }
75
76 static void freeData(ModifierData *md)
77 {
78         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *) md;
79         dynamicPaint_Modifier_free(pmd);
80 }
81
82 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
83 {
84         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
85         CustomDataMask dataMask = 0;
86
87         if (pmd->canvas) {
88                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
89                 for (; surface; surface = surface->next) {
90                         /* tface */
91                         if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ || 
92                             surface->init_color_type == MOD_DPAINT_INITIAL_TEXTURE)
93                         {
94                                 dataMask |= CD_MASK_MLOOPUV;
95                         }
96                         /* mcol */
97                         if (surface->type == MOD_DPAINT_SURFACE_T_PAINT ||
98                             surface->init_color_type == MOD_DPAINT_INITIAL_VERTEXCOLOR)
99                         {
100                                 dataMask |= CD_MASK_MLOOPCOL;
101                         }
102                         /* CD_MDEFORMVERT */
103                         if (surface->type == MOD_DPAINT_SURFACE_T_WEIGHT) {
104                                 dataMask |= CD_MASK_MDEFORMVERT;
105                         }
106                 }
107         }
108
109         if (pmd->brush) {
110                 if (pmd->brush->flags & MOD_DPAINT_USE_MATERIAL) {
111                         dataMask |= CD_MASK_MLOOPUV;
112                 }
113         }
114         return dataMask;
115 }
116
117 static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *depsgraph,
118                                   Object *ob, DerivedMesh *dm,
119                                   ModifierApplyFlag flag)
120 {
121         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *) md;
122
123         /* dont apply dynamic paint on orco dm stack */
124         if (!(flag & MOD_APPLY_ORCO)) {
125                 return dynamicPaint_Modifier_do(pmd, depsgraph, md->scene, ob, dm);
126         }
127         return dm;
128 }
129
130 static bool is_brush_cb(Object *UNUSED(ob), ModifierData *pmd)
131 {
132         return ((DynamicPaintModifierData *)pmd)->brush != NULL;
133 }
134
135 static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
136 {
137         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
138         /* Add relation from canvases to all brush objects. */
139         if (pmd->canvas != NULL) {
140                 for (DynamicPaintSurface *surface = pmd->canvas->surfaces.first; surface; surface = surface->next) {
141                         if (surface->effect & MOD_DPAINT_EFFECT_DO_DRIP) {
142                                 DEG_add_forcefield_relations(ctx->node, ctx->scene, ctx->object, surface->effector_weights, true, 0, "Dynamic Paint Field");
143                         }
144
145                         /* Actual code uses custom loop over group/scene without layer checks in dynamicPaint_doStep */
146                         DEG_add_collision_relations(ctx->node, ctx->scene, ctx->object, surface->brush_group,  eModifierType_DynamicPaint, is_brush_cb, false, "Dynamic Paint Brush");
147                 }
148         }
149 }
150
151 static bool dependsOnTime(ModifierData *UNUSED(md))
152 {
153         return true;
154 }
155
156 static void foreachIDLink(ModifierData *md, Object *ob,
157                           IDWalkFunc walk, void *userData)
158 {
159         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *) md;
160
161         if (pmd->canvas) {
162                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
163
164                 for (; surface; surface = surface->next) {
165                         walk(userData, ob, (ID **)&surface->brush_group, IDWALK_CB_NOP);
166                         walk(userData, ob, (ID **)&surface->init_texture, IDWALK_CB_USER);
167                         if (surface->effector_weights) {
168                                 walk(userData, ob, (ID **)&surface->effector_weights->group, IDWALK_CB_NOP);
169                         }
170                 }
171         }
172         if (pmd->brush) {
173                 walk(userData, ob, (ID **)&pmd->brush->mat, IDWALK_CB_USER);
174         }
175 }
176
177 static void foreachTexLink(ModifierData *UNUSED(md), Object *UNUSED(ob),
178                            TexWalkFunc UNUSED(walk), void *UNUSED(userData))
179 {
180         //walk(userData, ob, md, ""); /* re-enable when possible */
181 }
182
183 ModifierTypeInfo modifierType_DynamicPaint = {
184         /* name */              "Dynamic Paint",
185         /* structName */        "DynamicPaintModifierData",
186         /* structSize */        sizeof(DynamicPaintModifierData),
187         /* type */              eModifierTypeType_Constructive,
188         /* flags */             eModifierTypeFlag_AcceptsMesh |
189 /*                              eModifierTypeFlag_SupportsMapping |*/
190                                 eModifierTypeFlag_UsesPointCache |
191                                 eModifierTypeFlag_Single |
192                                 eModifierTypeFlag_UsesPreview,
193
194         /* copyData */          copyData,
195         /* deformVerts */       NULL,
196         /* deformMatrices */    NULL,
197         /* deformVertsEM */     NULL,
198         /* deformMatricesEM */  NULL,
199         /* applyModifier */     applyModifier,
200         /* applyModifierEM */   NULL,
201         /* initData */          initData,
202         /* requiredDataMask */  requiredDataMask,
203         /* freeData */          freeData,
204         /* isDisabled */        NULL,
205         /* updateDepsgraph */   updateDepsgraph,
206         /* dependsOnTime */     dependsOnTime,
207         /* dependsOnNormals */  NULL,
208         /* foreachObjectLink */ NULL,
209         /* foreachIDLink */     foreachIDLink,
210         /* foreachTexLink */    foreachTexLink,
211 };