4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2011 by Bastien Montagne.
21 * All rights reserved.
23 * Contributor(s): None yet.
25 * ***** END GPL LICENSE BLOCK *****
30 * XXX I’d like to make modified weights visible in WeightPaint mode,
31 * but couldn’t figure a way to do this…
32 * Maybe this will need changes in mesh_calc_modifiers (DerivedMesh.c)?
33 * Or the WeightPaint mode code itself?
36 #include "BLI_utildefines.h"
38 #include "BLI_string.h"
40 #include "DNA_mesh_types.h"
41 #include "DNA_meshdata_types.h"
42 #include "DNA_modifier_types.h"
43 #include "DNA_object_types.h"
45 #include "BKE_cdderivedmesh.h"
46 #include "BKE_deform.h"
48 #include "BKE_modifier.h"
49 #include "BKE_texture.h" /* Texture masking. */
51 #include "depsgraph_private.h"
52 #include "MEM_guardedalloc.h"
54 #include "MOD_weightvg_util.h"
58 * This mixes the old weight with the new weight factor.
60 static float mix_weight(float weight, float weight2, char mix_mode)
64 * XXX Don’t know why, but the switch version takes many CPU time,
65 * and produces lag in realtime playback…
70 return (weight + weight2);
72 return (weight - weight2);
74 return (weight * weight2);
76 /* Avoid dividing by zero (or really small values). */
77 if (0.0 <= weight2 < MOD_WVG_ZEROFLOOR)
78 weight2 = MOD_WVG_ZEROFLOOR;
79 else if (-MOD_WVG_ZEROFLOOR < weight2)
80 weight2 = -MOD_WVG_ZEROFLOOR;
81 return (weight / weight2);
83 return (weight < weight2 ? weight2 - weight : weight - weight2);
85 return (weight + weight2) / 2.0;
91 if (mix_mode == MOD_WVG_MIX_SET)
93 else if (mix_mode == MOD_WVG_MIX_ADD)
94 return (weight + weight2);
95 else if (mix_mode == MOD_WVG_MIX_SUB)
96 return (weight - weight2);
97 else if (mix_mode == MOD_WVG_MIX_MUL)
98 return (weight * weight2);
99 else if (mix_mode == MOD_WVG_MIX_DIV) {
100 /* Avoid dividing by zero (or really small values). */
101 if (weight2 < 0.0 && weight2 > -MOD_WVG_ZEROFLOOR)
102 weight2 = -MOD_WVG_ZEROFLOOR;
103 else if (weight2 >= 0.0 && weight2 < MOD_WVG_ZEROFLOOR)
104 weight2 = MOD_WVG_ZEROFLOOR;
105 return (weight / weight2);
107 else if (mix_mode == MOD_WVG_MIX_DIF)
108 return (weight < weight2 ? weight2 - weight : weight - weight2);
109 else if (mix_mode == MOD_WVG_MIX_AVG)
110 return (weight + weight2) / 2.0;
114 /**************************************
115 * Modifiers functions. *
116 **************************************/
117 static void initData(ModifierData *md)
119 WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
121 wmd->default_weight = 0.0;
122 wmd->default_weight2 = 0.0;
123 wmd->mix_mode = MOD_WVG_MIX_SET;
124 wmd->mix_set = MOD_WVG_SET_INTER;
126 wmd->mask_constant = 1.0f;
127 wmd->mask_tex_use_channel = MOD_WVG_MASK_TEX_USE_INT; /* Use intensity by default. */
128 wmd->mask_tex_mapping = MOD_DISP_MAP_LOCAL;
131 static void copyData(ModifierData *md, ModifierData *target)
133 WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
134 WeightVGMixModifierData *twmd = (WeightVGMixModifierData*) target;
136 BLI_strncpy(twmd->defgrp_name, wmd->defgrp_name, sizeof(twmd->defgrp_name));
137 BLI_strncpy(twmd->defgrp_name2, wmd->defgrp_name2, sizeof(twmd->defgrp_name2));
138 twmd->default_weight = wmd->default_weight;
139 twmd->default_weight2 = wmd->default_weight2;
140 twmd->mix_mode = wmd->mix_mode;
141 twmd->mix_set = wmd->mix_set;
143 twmd->mask_constant = wmd->mask_constant;
144 BLI_strncpy(twmd->mask_defgrp_name, wmd->mask_defgrp_name, sizeof(twmd->mask_defgrp_name));
145 twmd->mask_texture = wmd->mask_texture;
146 twmd->mask_tex_use_channel = wmd->mask_tex_use_channel;
147 twmd->mask_tex_mapping = wmd->mask_tex_mapping;
148 twmd->mask_tex_map_obj = wmd->mask_tex_map_obj;
149 BLI_strncpy(twmd->mask_tex_uvlayer_name, wmd->mask_tex_uvlayer_name,
150 sizeof(twmd->mask_tex_uvlayer_name));
153 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
155 WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
156 CustomDataMask dataMask = 0;
158 /* We need vertex groups! */
159 dataMask |= CD_MASK_MDEFORMVERT;
161 /* Ask for UV coordinates if we need them. */
162 if(wmd->mask_tex_mapping == MOD_DISP_MAP_UV)
163 dataMask |= CD_MASK_MTFACE;
168 static int dependsOnTime(ModifierData *md)
170 WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
172 if(wmd->mask_texture)
173 return BKE_texture_dependsOnTime(wmd->mask_texture);
177 static void foreachObjectLink(ModifierData *md, Object *ob,
178 void (*walk)(void *userData, Object *ob, Object **obpoin),
181 WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
182 walk(userData, ob, &wmd->mask_tex_map_obj);
185 static void foreachIDLink(ModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
187 WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
189 walk(userData, ob, (ID **)&wmd->mask_texture);
191 foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
194 static void updateDepgraph(ModifierData *md, DagForest *forest, struct Scene *UNUSED(scene),
195 Object *UNUSED(ob), DagNode *obNode)
197 WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
200 if(wmd->mask_tex_map_obj && wmd->mask_tex_mapping == MOD_DISP_MAP_OBJECT) {
201 curNode = dag_get_node(forest, wmd->mask_tex_map_obj);
203 dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA,
204 "WeightVGMix Modifier");
207 if(wmd->mask_tex_mapping == MOD_DISP_MAP_GLOBAL)
208 dag_add_relation(forest, obNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA,
209 "WeightVGMix Modifier");
212 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
214 WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
215 /* If no vertex group, bypass. */
216 return (wmd->defgrp_name == NULL);
219 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData,
220 int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
222 WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
223 DerivedMesh *dm = derivedData, *ret = NULL;
225 MDeformVert *dvert = NULL;
227 int defgrp_idx, defgrp_idx2 = -1;
230 int *tidx, *indices = NULL;
233 char rel_ret = 0; /* Boolean, whether we have to release ret dm or not, when not using it! */
235 /* Get number of verts. */
236 numVerts = dm->getNumVerts(dm);
238 /* Check if we can just return the original mesh.
239 * Must have verts and therefore verts assigned to vgroups to do anything useful!
241 if ((numVerts == 0) || (ob->defbase.first == NULL))
244 /* Get vgroup idx from its name. */
245 defgrp_idx = defgroup_name_index(ob, wmd->defgrp_name);
248 /* Get seconf vgroup idx from its name, if given. */
249 if (wmd->defgrp_name2[0] != (char)0) {
250 defgrp_idx2 = defgroup_name_index(ob, wmd->defgrp_name2);
255 /* XXX All this to avoid copying dm when not needed… However, it nearly doubles compute
256 * time! See scene 5 of the WeighVG test file…
259 /* Get actual dverts (ie vertex group data). */
260 dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
261 /* If no dverts, return unmodified data… */
265 /* Get org mesh, only to test whether affected cdata layer has already been copied
266 * somewhere up in the modifiers stack.
272 /* Create a copy of our dmesh, only if our affected cdata layer is the same as org mesh. */
273 if (dvert == CustomData_get_layer(&ob_m->vdata, CD_MDEFORMVERT)) {
274 /* XXX Seems to create problems with weightpaint mode???
275 * I’m missing something here, I guess…
277 // DM_set_only_copy(dm, CD_MASK_MDEFORMVERT); /* Only copy defgroup layer. */
279 dvert = ret->getVertDataArray(ret, CD_MDEFORMVERT);
291 dvert = ret->getVertDataArray(ret, CD_MDEFORMVERT);
299 /* Find out which vertices to work on. */
300 tidx = MEM_mallocN(sizeof(int) * numVerts, "WeightVGMix Modifier, tidx");
301 switch (wmd->mix_set) {
302 case MOD_WVG_SET_ORG:
303 /* All vertices in first vgroup. */
304 for (i = 0; i < numVerts; i++) {
305 for (j = 0; j < dvert[i].totweight; j++) {
306 if(dvert[i].dw[j].def_nr == defgrp_idx) {
313 case MOD_WVG_SET_NEW:
314 /* All vertices in second vgroup. */
315 for (i = 0; i < numVerts; i++) {
316 for (j = 0; j < dvert[i].totweight; j++) {
317 if(dvert[i].dw[j].def_nr == defgrp_idx2) {
324 case MOD_WVG_SET_UNION:
325 /* All vertices in one vgroup or the other. */
326 for (i = 0; i < numVerts; i++) {
327 for (j = 0; j < dvert[i].totweight; j++) {
328 if(dvert[i].dw[j].def_nr == defgrp_idx || dvert[i].dw[j].def_nr == defgrp_idx2) {
335 case MOD_WVG_SET_INTER:
336 /* All vertices in both vgroups. */
337 for (i = 0; i < numVerts; i++) {
340 for (j = 0; j < dvert[i].totweight; j++) {
341 if(dvert[i].dw[j].def_nr == defgrp_idx) {
349 else if(dvert[i].dw[j].def_nr == defgrp_idx2) {
360 case MOD_WVG_SET_ALL:
362 /* Use all vertices, no need to do anything here. */
366 indices = MEM_mallocN(sizeof(int) * numIdx, "WeightVGMix Modifier, indices");
367 memcpy(indices, tidx, sizeof(int) * numIdx);
373 org_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGMix Modifier, org_w");
374 new_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGMix Modifier, new_w");
377 for (i = 0; i < numIdx; i++) {
381 int idx = indices ? indices[i] : i;
382 for (j = 0; j < dvert[idx].totweight; j++) {
383 if(dvert[idx].dw[j].def_nr == defgrp_idx) {
384 org_w[i] = dvert[idx].dw[j].weight;
389 else if(dvert[idx].dw[j].def_nr == defgrp_idx2) {
390 weight2 = dvert[idx].dw[j].weight;
397 org_w[i] = wmd->default_weight;
399 weight2 = wmd->default_weight2;
400 new_w[i] = mix_weight(org_w[i], weight2, wmd->mix_mode);
404 weightvg_do_mask(numIdx, indices, org_w, new_w, ob, ret, wmd->mask_constant,
405 wmd->mask_defgrp_name, wmd->mask_texture, wmd->mask_tex_use_channel,
406 wmd->mask_tex_mapping, wmd->mask_tex_map_obj, wmd->mask_tex_uvlayer_name);
408 /* Update (add to) vgroup.
409 * XXX Depending on the MOD_WVG_SET_xxx option chosen, we might have to add vertices to vgroup.
411 weightvg_update_vg(dvert, defgrp_idx, numIdx, indices, org_w, 1, -FLT_MAX, 0, 0.0f);
421 /* Return the vgroup-modified mesh. */
425 static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
426 struct EditMesh *UNUSED(editData),
427 DerivedMesh *derivedData)
429 return applyModifier(md, ob, derivedData, 0, 1);
433 ModifierTypeInfo modifierType_WeightVGMix = {
434 /* name */ "WeightVGMix",
435 /* structName */ "WeightVGMixModifierData",
436 /* structSize */ sizeof(WeightVGMixModifierData),
437 /* type */ eModifierTypeType_Nonconstructive,
438 /* flags */ eModifierTypeFlag_AcceptsMesh
439 |eModifierTypeFlag_SupportsMapping
440 |eModifierTypeFlag_SupportsEditmode,
442 /* copyData */ copyData,
443 /* deformVerts */ NULL,
444 /* deformMatrices */ NULL,
445 /* deformVertsEM */ NULL,
446 /* deformMatricesEM */ NULL,
447 /* applyModifier */ applyModifier,
448 /* applyModifierEM */ applyModifierEM,
449 /* initData */ initData,
450 /* requiredDataMask */ requiredDataMask,
452 /* isDisabled */ isDisabled,
453 /* updateDepgraph */ updateDepgraph,
454 /* dependsOnTime */ dependsOnTime,
455 /* dependsOnNormals */ NULL,
456 /* foreachObjectLink */ foreachObjectLink,
457 /* foreachIDLink */ foreachIDLink,