645c6e43077ce6d7cba926ff69deca556ecf5383
[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 }
72
73 static void freeData(ModifierData *md)
74 {
75         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *) md;
76         dynamicPaint_Modifier_free(pmd);
77 }
78
79 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
80 {
81         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
82         CustomDataMask dataMask = 0;
83
84         if (pmd->canvas) {
85                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
86                 for (; surface; surface = surface->next) {
87                         /* tface */
88                         if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ || 
89                             surface->init_color_type == MOD_DPAINT_INITIAL_TEXTURE)
90                         {
91                                 dataMask |= CD_MASK_MLOOPUV;
92                         }
93                         /* mcol */
94                         if (surface->type == MOD_DPAINT_SURFACE_T_PAINT ||
95                             surface->init_color_type == MOD_DPAINT_INITIAL_VERTEXCOLOR)
96                         {
97                                 dataMask |= CD_MASK_MLOOPCOL;
98                         }
99                         /* CD_MDEFORMVERT */
100                         if (surface->type == MOD_DPAINT_SURFACE_T_WEIGHT) {
101                                 dataMask |= CD_MASK_MDEFORMVERT;
102                         }
103                 }
104         }
105         return dataMask;
106 }
107
108 static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *depsgraph,
109                                   Object *ob, DerivedMesh *dm,
110                                   ModifierApplyFlag flag)
111 {
112         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *) md;
113
114         /* dont apply dynamic paint on orco dm stack */
115         if (!(flag & MOD_APPLY_ORCO)) {
116                 return dynamicPaint_Modifier_do(pmd, depsgraph, md->scene, ob, dm);
117         }
118         return dm;
119 }
120
121 static bool is_brush_cb(Object *UNUSED(ob), ModifierData *pmd)
122 {
123         return ((DynamicPaintModifierData *)pmd)->brush != NULL;
124 }
125
126 static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
127 {
128         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
129         /* Add relation from canvases to all brush objects. */
130         if (pmd->canvas != NULL) {
131                 for (DynamicPaintSurface *surface = pmd->canvas->surfaces.first; surface; surface = surface->next) {
132                         if (surface->effect & MOD_DPAINT_EFFECT_DO_DRIP) {
133                                 DEG_add_forcefield_relations(ctx->node, ctx->scene, ctx->object, surface->effector_weights, true, 0, "Dynamic Paint Field");
134                         }
135
136                         /* Actual code uses custom loop over group/scene without layer checks in dynamicPaint_doStep */
137                         DEG_add_collision_relations(ctx->node, ctx->scene, ctx->object, surface->brush_group,  eModifierType_DynamicPaint, is_brush_cb, false, "Dynamic Paint Brush");
138                 }
139         }
140 }
141
142 static bool dependsOnTime(ModifierData *UNUSED(md))
143 {
144         return true;
145 }
146
147 static void foreachIDLink(ModifierData *md, Object *ob,
148                           IDWalkFunc walk, void *userData)
149 {
150         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *) md;
151
152         if (pmd->canvas) {
153                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
154
155                 for (; surface; surface = surface->next) {
156                         walk(userData, ob, (ID **)&surface->brush_group, IDWALK_CB_NOP);
157                         walk(userData, ob, (ID **)&surface->init_texture, IDWALK_CB_USER);
158                         if (surface->effector_weights) {
159                                 walk(userData, ob, (ID **)&surface->effector_weights->group, IDWALK_CB_NOP);
160                         }
161                 }
162         }
163 }
164
165 static void foreachTexLink(ModifierData *UNUSED(md), Object *UNUSED(ob),
166                            TexWalkFunc UNUSED(walk), void *UNUSED(userData))
167 {
168         //walk(userData, ob, md, ""); /* re-enable when possible */
169 }
170
171 ModifierTypeInfo modifierType_DynamicPaint = {
172         /* name */              "Dynamic Paint",
173         /* structName */        "DynamicPaintModifierData",
174         /* structSize */        sizeof(DynamicPaintModifierData),
175         /* type */              eModifierTypeType_Constructive,
176         /* flags */             eModifierTypeFlag_AcceptsMesh |
177 /*                              eModifierTypeFlag_SupportsMapping |*/
178                                 eModifierTypeFlag_UsesPointCache |
179                                 eModifierTypeFlag_Single |
180                                 eModifierTypeFlag_UsesPreview,
181
182         /* copyData */          copyData,
183
184         /* deformVerts_DM */    NULL,
185         /* deformMatrices_DM */ NULL,
186         /* deformVertsEM_DM */  NULL,
187         /* deformMatricesEM_DM*/NULL,
188         /* applyModifier_DM */  applyModifier,
189         /* applyModifierEM_DM */NULL,
190
191         /* deformVerts */       NULL,
192         /* deformMatrices */    NULL,
193         /* deformVertsEM */     NULL,
194         /* deformMatricesEM */  NULL,
195         /* applyModifier */     NULL,
196         /* applyModifierEM */   NULL,
197
198         /* initData */          initData,
199         /* requiredDataMask */  requiredDataMask,
200         /* freeData */          freeData,
201         /* isDisabled */        NULL,
202         /* updateDepsgraph */   updateDepsgraph,
203         /* dependsOnTime */     dependsOnTime,
204         /* dependsOnNormals */  NULL,
205         /* foreachObjectLink */ NULL,
206         /* foreachIDLink */     foreachIDLink,
207         /* foreachTexLink */    foreachTexLink,
208 };