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