1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2005 by the Blender Foundation.
* All rights reserved.
*
* Contributor(s): Daniel Dunbar
* Ton Roosendaal,
* Ben Batt,
* Brecht Van Lommel,
* Campbell Barton
*
* ***** END GPL LICENSE BLOCK *****
*
*/
/** \file blender/modifiers/intern/MOD_displace.c
* \ingroup modifiers
*/
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "BLI_utildefines.h"
#include "BLI_math.h"
#include "BLI_task.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_library.h"
#include "BKE_library_query.h"
#include "BKE_image.h"
#include "BKE_mesh.h"
#include "BKE_modifier.h"
#include "BKE_texture.h"
#include "BKE_deform.h"
#include "BKE_object.h"
#include "MEM_guardedalloc.h"
#include "MOD_util.h"
#include "RE_shader_ext.h"
/* Displace */
static void initData(ModifierData *md)
{
DisplaceModifierData *dmd = (DisplaceModifierData *) md;
dmd->texture = NULL;
dmd->strength = 1;
dmd->direction = MOD_DISP_DIR_NOR;
dmd->midlevel = 0.5;
dmd->space = MOD_DISP_SPACE_LOCAL;
}
static void copyData(ModifierData *md, ModifierData *target)
{
#if 0
DisplaceModifierData *dmd = (DisplaceModifierData *) md;
DisplaceModifierData *tdmd = (DisplaceModifierData *) target;
#endif
modifier_copyData_generic(md, target);
}
static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
{
DisplaceModifierData *dmd = (DisplaceModifierData *)md;
CustomDataMask dataMask = 0;
/* ask for vertexgroups if we need them */
if (dmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
/* ask for UV coordinates if we need them */
if (dmd->texmapping == MOD_DISP_MAP_UV) dataMask |= CD_MASK_MTFACE;
if (dmd->direction == MOD_DISP_DIR_CLNOR) {
dataMask |= CD_MASK_CUSTOMLOOPNORMAL;
}
return dataMask;
}
static bool dependsOnTime(ModifierData *md)
{
DisplaceModifierData *dmd = (DisplaceModifierData *)md;
if (dmd->texture) {
return BKE_texture_dependsOnTime(dmd->texture);
}
else {
return false;
}
}
static bool dependsOnNormals(ModifierData *md)
{
DisplaceModifierData *dmd = (DisplaceModifierData *)md;
return ELEM(dmd->direction, MOD_DISP_DIR_NOR, MOD_DISP_DIR_CLNOR);
}
static void foreachObjectLink(ModifierData *md, Object *ob,
ObjectWalkFunc walk, void *userData)
{
DisplaceModifierData *dmd = (DisplaceModifierData *) md;
walk(userData, ob, &dmd->map_object, IDWALK_CB_NOP);
}
static void foreachIDLink(ModifierData *md, Object *ob,
IDWalkFunc walk, void *userData)
{
DisplaceModifierData *dmd = (DisplaceModifierData *) md;
walk(userData, ob, (ID **)&dmd->texture, IDWALK_CB_USER);
foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
}
static void foreachTexLink(ModifierData *md, Object *ob,
TexWalkFunc walk, void *userData)
{
walk(userData, ob, md, "texture");
}
static bool isDisabled(ModifierData *md, int UNUSED(useRenderParams))
{
DisplaceModifierData *dmd = (DisplaceModifierData *) md;
return ((!dmd->texture && dmd->direction == MOD_DISP_DIR_RGB_XYZ) || dmd->strength == 0.0f);
}
static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
{
DisplaceModifierData *dmd = (DisplaceModifierData *)md;
if (dmd->map_object != NULL && dmd->texmapping == MOD_DISP_MAP_OBJECT) {
DEG_add_object_relation(ctx->node, dmd->map_object, DEG_OB_COMP_TRANSFORM, "Displace Modifier");
}
if (dmd->texmapping == MOD_DISP_MAP_GLOBAL ||
(ELEM(dmd->direction, MOD_DISP_DIR_X, MOD_DISP_DIR_Y, MOD_DISP_DIR_Z, MOD_DISP_DIR_RGB_XYZ) &&
dmd->space == MOD_DISP_SPACE_GLOBAL))
{
DEG_add_object_relation(ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "Displace Modifier");
}
}
typedef struct DisplaceUserdata {
/*const*/ DisplaceModifierData *dmd;
struct ImagePool *pool;
MDeformVert *dvert;
float weight;
int defgrp_index;
int direction;
bool use_global_direction;
float (*tex_co)[3];
float (*vertexCos)[3];
float local_mat[4][4];
MVert *mvert;
float (*vert_clnors)[3];
} DisplaceUserdata;
static void displaceModifier_do_task(
void *__restrict userdata,
const int iter,
const ParallelRangeTLS *__restrict UNUSED(tls))
{
DisplaceUserdata *data = (DisplaceUserdata *)userdata;
DisplaceModifierData *dmd = data->dmd;
MDeformVert *dvert = data->dvert;
float weight = data->weight;
int defgrp_index = data->defgrp_index;
int direction = data->direction;
bool use_global_direction = data->use_global_direction;
float (*tex_co)[3] = data->tex_co;
float (*vertexCos)[3] = data->vertexCos;
MVert *mvert = data->mvert;
float (*vert_clnors)[3] = data->vert_clnors;
const float delta_fixed = 1.0f - dmd->midlevel; /* when no texture is used, we fallback to white */
TexResult texres;
float strength = dmd->strength;
float delta;
float local_vec[3];
if (dvert) {
weight = defvert_find_weight(dvert + iter, defgrp_index);
if (weight == 0.0f) {
return;
}
}
if (dmd->texture) {
texres.nor = NULL;
BKE_texture_get_value_ex(dmd->modifier.scene, dmd->texture, tex_co[iter], &texres, data->pool, false);
delta = texres.tin - dmd->midlevel;
}
else {
delta = delta_fixed; /* (1.0f - dmd->midlevel) */ /* never changes */
}
if (dvert) {
strength *= weight;
}
delta *= strength;
CLAMP(delta, -10000, 10000);
switch (direction) {
case MOD_DISP_DIR_X:
if (use_global_direction) {
vertexCos[iter][0] += delta * data->local_mat[0][0];
vertexCos[iter][1] += delta * data->local_mat[1][0];
vertexCos[iter][2] += delta * data->local_mat[2][0];
}
else {
vertexCos[iter][0] += delta;
}
break;
case MOD_DISP_DIR_Y:
if (use_global_direction) {
vertexCos[iter][0] += delta * data->local_mat[0][1];
vertexCos[iter][1] += delta * data->local_mat[1][1];
vertexCos[iter][2] += delta * data->local_mat[2][1];
}
else {
vertexCos[iter][1] += delta;
}
break;
case MOD_DISP_DIR_Z:
if (use_global_direction) {
vertexCos[iter][0] += delta * data->local_mat[0][2];
vertexCos[iter][1] += delta * data->local_mat[1][2];
vertexCos[iter][2] += delta * data->local_mat[2][2];
}
else {
vertexCos[iter][2] += delta;
}
break;
case MOD_DISP_DIR_RGB_XYZ:
local_vec[0] = texres.tr - dmd->midlevel;
local_vec[1] = texres.tg - dmd->midlevel;
local_vec[2] = texres.tb - dmd->midlevel;
if (use_global_direction) {
mul_transposed_mat3_m4_v3(data->local_mat, local_vec);
}
mul_v3_fl(local_vec, strength);
add_v3_v3(vertexCos[iter], local_vec);
break;
case MOD_DISP_DIR_NOR:
vertexCos[iter][0] += delta * (mvert[iter].no[0] / 32767.0f);
vertexCos[iter][1] += delta * (mvert[iter].no[1] / 32767.0f);
vertexCos[iter][2] += delta * (mvert[iter].no[2] / 32767.0f);
break;
case MOD_DISP_DIR_CLNOR:
madd_v3_v3fl(vertexCos[iter], vert_clnors[iter], delta);
break;
}
}
/* dm must be a CDDerivedMesh */
static void displaceModifier_do(
DisplaceModifierData *dmd, Object *ob,
DerivedMesh *dm, float (*vertexCos)[3], int numVerts)
{
MVert *mvert;
MDeformVert *dvert;
int direction = dmd->direction;
int defgrp_index;
float (*tex_co)[3];
float weight = 1.0f; /* init value unused but some compilers may complain */
float (*vert_clnors)[3] = NULL;
float local_mat[4][4] = {{0}};
const bool use_global_direction = dmd->space == MOD_DISP_SPACE_GLOBAL;
if (!dmd->texture && dmd->direction == MOD_DISP_DIR_RGB_XYZ) return;
if (dmd->strength == 0.0f) return;
mvert = CDDM_get_verts(dm);
modifier_get_vgroup(ob, dm, dmd->defgrp_name, &dvert, &defgrp_index);
if (dmd->texture) {
tex_co = MEM_calloc_arrayN((size_t)numVerts, sizeof(*tex_co),
"displaceModifier_do tex_co");
get_texture_coords((MappingInfoModifierData *)dmd, ob, dm, vertexCos, tex_co, numVerts);
modifier_init_texture(dmd->modifier.scene, dmd->texture);
}
else {
tex_co = NULL;
}
if (direction == MOD_DISP_DIR_CLNOR) {
CustomData *ldata = dm->getLoopDataLayout(dm);
if (CustomData_has_layer(ldata, CD_CUSTOMLOOPNORMAL)) {
float (*clnors)[3] = NULL;
if ((dm->dirty & DM_DIRTY_NORMALS) || !CustomData_has_layer(ldata, CD_NORMAL)) {
dm->calcLoopNormals(dm, true, (float)M_PI);
}
clnors = CustomData_get_layer(ldata, CD_NORMAL);
vert_clnors = MEM_malloc_arrayN(numVerts, sizeof(*vert_clnors), __func__);
BKE_mesh_normals_loop_to_vertex(numVerts, dm->getLoopArray(dm), dm->getNumLoops(dm),
(const float (*)[3])clnors, vert_clnors);
}
else {
direction = MOD_DISP_DIR_NOR;
}
}
else if (ELEM(direction, MOD_DISP_DIR_X, MOD_DISP_DIR_Y, MOD_DISP_DIR_Z, MOD_DISP_DIR_RGB_XYZ) &&
use_global_direction)
{
copy_m4_m4(local_mat, ob->obmat);
}
DisplaceUserdata data = {NULL};
data.dmd = dmd;
data.dvert = dvert;
data.weight = weight;
data.defgrp_index = defgrp_index;
data.direction = direction;
data.use_global_direction = use_global_direction;
data.tex_co = tex_co;
data.vertexCos = vertexCos;
copy_m4_m4(data.local_mat, local_mat);
data.mvert = mvert;
data.vert_clnors = vert_clnors;
if (dmd->texture != NULL) {
data.pool = BKE_image_pool_new();
BKE_texture_fetch_images_for_pool(dmd->texture, data.pool);
}
ParallelRangeSettings settings;
BLI_parallel_range_settings_defaults(&settings);
settings.use_threading = (numVerts > 512);
BLI_task_parallel_range(0, numVerts,
&data,
displaceModifier_do_task,
&settings);
if (data.pool != NULL) {
BKE_image_pool_free(data.pool);
}
if (tex_co) {
MEM_freeN(tex_co);
}
if (vert_clnors) {
MEM_freeN(vert_clnors);
}
}
static void deformVerts(ModifierData *md, const ModifierEvalContext *ctx,
DerivedMesh *derivedData,
float (*vertexCos)[3],
int numVerts)
{
DerivedMesh *dm = get_cddm(ctx->object, NULL, derivedData, vertexCos, dependsOnNormals(md));
displaceModifier_do((DisplaceModifierData *)md, ctx->object, dm,
vertexCos, numVerts);
if (dm != derivedData)
dm->release(dm);
}
static void deformVertsEM(
ModifierData *md, const ModifierEvalContext *ctx, struct BMEditMesh *editData,
DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
{
DerivedMesh *dm = get_cddm(ctx->object, editData, derivedData, vertexCos, dependsOnNormals(md));
displaceModifier_do((DisplaceModifierData *)md, ctx->object, dm,
vertexCos, numVerts);
if (dm != derivedData)
dm->release(dm);
}
ModifierTypeInfo modifierType_Displace = {
/* name */ "Displace",
/* structName */ "DisplaceModifierData",
/* structSize */ sizeof(DisplaceModifierData),
/* type */ eModifierTypeType_OnlyDeform,
/* flags */ eModifierTypeFlag_AcceptsMesh |
eModifierTypeFlag_SupportsEditmode,
/* copyData */ copyData,
/* deformVerts_DM */ deformVerts,
/* deformMatrices_DM */ NULL,
/* deformVertsEM_DM */ deformVertsEM,
/* deformMatricesEM_DM*/NULL,
/* applyModifier_DM */ NULL,
/* applyModifierEM_DM */NULL,
/* deformVerts */ NULL,
/* deformMatrices */ NULL,
/* deformVertsEM */ NULL,
/* deformMatricesEM */ NULL,
/* applyModifier */ NULL,
/* applyModifierEM */ NULL,
/* initData */ initData,
/* requiredDataMask */ requiredDataMask,
/* freeData */ NULL,
/* isDisabled */ isDisabled,
/* updateDepsgraph */ updateDepsgraph,
/* dependsOnTime */ dependsOnTime,
/* dependsOnNormals */ dependsOnNormals,
/* foreachObjectLink */ foreachObjectLink,
/* foreachIDLink */ foreachIDLink,
/* foreachTexLink */ foreachTexLink,
};
|