2
* ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18
* Contributor(s): Pawel Kowal, Campbell Barton
20
* ***** END GPL LICENSE BLOCK *****
24
/** \file blender/modifiers/intern/MOD_uvwarp.c
30
#include "DNA_meshdata_types.h"
31
#include "DNA_object_types.h"
34
#include "BLI_string.h"
35
#include "BLI_utildefines.h"
37
#include "BKE_action.h" /* BKE_pose_channel_find_name */
38
#include "BKE_cdderivedmesh.h"
39
#include "BKE_deform.h"
40
#include "BKE_modifier.h"
42
#include "depsgraph_private.h"
47
static void uv_warp_from_mat4_pair(float uv_dst[2], const float uv_src[2], float warp_mat[4][4],
48
int axis_u, int axis_v)
50
float tuv[3] = {0.0f};
52
tuv[axis_u] = uv_src[0];
53
tuv[axis_v] = uv_src[1];
55
mul_m4_v3(warp_mat, tuv);
57
uv_dst[0] = tuv[axis_u];
58
uv_dst[1] = tuv[axis_v];
61
static void initData(ModifierData *md)
63
UVWarpModifierData *umd = (UVWarpModifierData *) md;
66
copy_v2_fl(umd->center, 0.5f);
69
static void copyData(ModifierData *md, ModifierData *target)
71
UVWarpModifierData *umd = (UVWarpModifierData *)md;
72
UVWarpModifierData *tumd = (UVWarpModifierData *)target;
74
tumd->axis_u = umd->axis_u;
75
tumd->axis_v = umd->axis_v;
76
copy_v2_v2(tumd->center, umd->center);
77
tumd->object_src = umd->object_src;
78
BLI_strncpy(tumd->bone_src, umd->bone_src, sizeof(tumd->bone_src));
79
tumd->object_dst = umd->object_dst;
80
BLI_strncpy(tumd->bone_dst, umd->bone_dst, sizeof(tumd->bone_dst));
81
BLI_strncpy(tumd->vgroup_name, umd->vgroup_name, sizeof(tumd->vgroup_name));
82
BLI_strncpy(tumd->uvlayer_name, umd->uvlayer_name, sizeof(umd->uvlayer_name));
85
static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
87
UVWarpModifierData *umd = (UVWarpModifierData *)md;
88
CustomDataMask dataMask = 0;
90
/* ask for vertexgroups if we need them */
91
if (umd->vgroup_name[0])
92
dataMask |= CD_MASK_MDEFORMVERT;
97
static void matrix_from_obj_pchan(float mat[4][4], Object *ob, const char *bonename)
99
bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, bonename);
101
mult_m4_m4m4(mat, ob->obmat, pchan->pose_mat);
104
copy_m4_m4(mat, ob->obmat);
108
#define OMP_LIMIT 1000
109
static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
111
ModifierApplyFlag UNUSED(flag))
113
UVWarpModifierData *umd = (UVWarpModifierData *) md;
114
int i, numPolys, numLoops;
120
char uvname[MAX_CUSTOMDATA_LAYER_NAME];
123
float imat_dst[4][4];
124
float warp_mat[4][4];
125
const int axis_u = umd->axis_u;
126
const int axis_v = umd->axis_v;
128
/* make sure there are UV Maps available */
129
if (!CustomData_has_layer(&dm->loopData, CD_MLOOPUV)) {
132
else if (ELEM(NULL, umd->object_src, umd->object_dst)) {
133
modifier_setError(md, "From/To objects must be set");
137
/* make sure anything moving UVs is available */
138
matrix_from_obj_pchan(mat_src, umd->object_src, umd->bone_src);
139
matrix_from_obj_pchan(mat_dst, umd->object_dst, umd->bone_dst);
141
invert_m4_m4(imat_dst, mat_dst);
142
mult_m4_m4m4(warp_mat, imat_dst, mat_src);
145
if (!is_zero_v2(umd->center)) {
146
float mat_cent[4][4];
147
float imat_cent[4][4];
150
mat_cent[3][axis_u] = umd->center[0];
151
mat_cent[3][axis_v] = umd->center[1];
153
invert_m4_m4(imat_cent, mat_cent);
155
mult_m4_m4m4(warp_mat, warp_mat, imat_cent);
156
mult_m4_m4m4(warp_mat, mat_cent, warp_mat);
159
/* make sure we're using an existing layer */
160
CustomData_validate_layer_name(&dm->loopData, CD_MLOOPUV, umd->uvlayer_name, uvname);
162
numPolys = dm->getNumPolys(dm);
163
numLoops = dm->getNumLoops(dm);
165
mpoly = dm->getPolyArray(dm);
166
mloop = dm->getLoopArray(dm);
167
/* make sure we are not modifying the original UV map */
168
mloopuv = CustomData_duplicate_referenced_layer_named(&dm->loopData, CD_MLOOPUV, uvname, numLoops);
169
modifier_get_vgroup(ob, dm, umd->vgroup_name, &dvert, &defgrp_index);
172
#pragma omp parallel for if (numPolys > OMP_LIMIT)
173
for (i = 0; i < numPolys; i++) {
175
MPoly *mp = &mpoly[i];
176
MLoop *ml = &mloop[mp->loopstart];
177
MLoopUV *mluv = &mloopuv[mp->loopstart];
179
for (l = 0; l < mp->totloop; l++, ml++, mluv++) {
180
const float weight = defvert_find_weight(&dvert[ml->v], defgrp_index);
181
uv_warp_from_mat4_pair(uv, mluv->uv, warp_mat, axis_u, axis_v);
182
interp_v2_v2v2(mluv->uv, mluv->uv, uv, weight);
187
#pragma omp parallel for if (numPolys > OMP_LIMIT)
188
for (i = 0; i < numPolys; i++) {
189
MPoly *mp = &mpoly[i];
190
// MLoop *ml = &mloop[mp->loopstart];
191
MLoopUV *mluv = &mloopuv[mp->loopstart];
193
for (l = 0; l < mp->totloop; l++, /* ml++, */ mluv++) {
194
uv_warp_from_mat4_pair(mluv->uv, mluv->uv, warp_mat, axis_u, axis_v);
199
dm->dirty |= DM_DIRTY_TESS_CDLAYERS;
204
static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
205
struct BMEditMesh *UNUSED(editData),
206
DerivedMesh *derivedData)
208
return applyModifier(md, ob, derivedData, MOD_APPLY_USECACHE);
211
static void foreachObjectLink(ModifierData *md, Object *ob, ObjectWalkFunc walk, void *userData)
213
UVWarpModifierData *umd = (UVWarpModifierData *) md;
215
walk(userData, ob, &umd->object_dst);
216
walk(userData, ob, &umd->object_src);
219
static void uv_warp_deps_object_bone(DagForest *forest, DagNode *obNode,
220
Object *obj, const char *bonename)
223
DagNode *curNode = dag_get_node(forest, obj);
226
dag_add_relation(forest, curNode, obNode, DAG_RL_OB_DATA | DAG_RL_DATA_DATA, "UVWarp Modifier");
228
dag_add_relation(forest, curNode, obNode, DAG_RL_OB_DATA, "UVWarp Modifier");
232
static void updateDepgraph(ModifierData *md, DagForest *forest,
233
struct Scene *UNUSED(scene),
237
UVWarpModifierData *umd = (UVWarpModifierData *) md;
239
uv_warp_deps_object_bone(forest, obNode, umd->object_src, umd->bone_src);
240
uv_warp_deps_object_bone(forest, obNode, umd->object_dst, umd->bone_dst);
243
ModifierTypeInfo modifierType_UVWarp = {
245
/* structName */ "UVWarpModifierData",
246
/* structSize */ sizeof(UVWarpModifierData),
247
/* type */ eModifierTypeType_NonGeometrical,
248
/* flags */ eModifierTypeFlag_AcceptsMesh |
249
eModifierTypeFlag_SupportsEditmode |
250
eModifierTypeFlag_EnableInEditmode,
251
/* copyData */ copyData,
252
/* deformVerts */ NULL,
253
/* deformMatrices */ NULL,
254
/* deformVertsEM */ NULL,
255
/* deformMatricesEM */ NULL,
256
/* applyModifier */ applyModifier,
257
/* applyModifierEM */ applyModifierEM,
258
/* initData */ initData,
259
/* requiredDataMask */ requiredDataMask,
261
/* isDisabled */ NULL,
262
/* updateDepgraph */ updateDepgraph,
263
/* dependsOnTime */ NULL,
264
/* dependsOnNormals */ NULL,
265
/* foreachObjectLink */ foreachObjectLink,
266
/* foreachIDLink */ NULL,
267
/* foreachTexLink */ NULL,