Blender V5.3
MOD_weightvg_util.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 by Bastien Montagne. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_utildefines.hh"
10
11#include "BLI_math_color_c.hh"
12#include "BLI_rand_c.hh"
13#include "BLI_string.hh"
14
15#include "BLT_translation.hh"
16
17#include "DNA_color_types.h" /* CurveMapping. */
18#include "DNA_mesh_types.h"
19#include "DNA_meshdata_types.h"
20#include "DNA_modifier_types.h"
21#include "DNA_object_types.h"
22#include "DNA_scene_types.h"
23
24#include "BKE_colortools.hh" /* CurveMapping. */
25#include "BKE_context.hh"
26#include "BKE_deform.hh"
27#include "BKE_modifier.hh"
28#include "BKE_texture.h" /* Texture masking. */
29
30#include "UI_interface.hh"
32#include "UI_resources.hh"
33
34#include "RNA_access.hh"
35
37
38#include "MEM_guardedalloc.h"
39#include "MOD_ui_common.hh"
40#include "MOD_util.hh"
41#include "MOD_weightvg_util.hh"
42#include "RE_texture.h" /* Texture masking. */
43
44namespace blender {
45
47 int num, float *new_w, short falloff_type, const bool do_invert, CurveMapping *cmap, RNG *rng)
48{
49 int i;
50
51 /* Return immediately, if we have nothing to do! */
52 /* Also security checks... */
53 if (!do_invert && (((falloff_type == MOD_WVG_MAPPING_CURVE) && (cmap == nullptr)) ||
54 !ELEM(falloff_type,
62 {
63 return;
64 }
65
66 if (cmap && falloff_type == MOD_WVG_MAPPING_CURVE) {
68 }
69
70 /* Map each weight (vertex) to its new value, accordingly to the chosen mode. */
71 for (i = 0; i < num; i++) {
72 float fac = new_w[i];
73
74 /* Code borrowed from the warp modifier. */
75 /* Closely matches PROP_SMOOTH and similar. */
76 switch (falloff_type) {
78 fac = BKE_curvemapping_evaluateF(cmap, 0, fac);
79 break;
81 fac = fac * fac;
82 break;
84 fac = 3.0f * fac * fac - 2.0f * fac * fac * fac;
85 break;
87 fac = sqrtf(fac);
88 break;
90 fac = sqrtf(2 * fac - fac * fac);
91 break;
93 fac = BLI_rng_get_float(rng) * fac;
94 break;
96 fac = (fac >= 0.5f) ? 1.0f : 0.0f;
97 break;
99 BLI_assert(do_invert);
100 break;
101 default:
103 }
104
105 new_w[i] = do_invert ? 1.0f - fac : fac;
106 }
107}
108
110 const int num,
111 const int *indices,
112 float *org_w,
113 const float *new_w,
114 Object *ob,
115 Mesh *mesh,
116 const float fact,
117 const char defgrp_name[MAX_VGROUP_NAME],
118 Scene * /*scene*/,
119 Tex *texture,
120 const int tex_use_channel,
121 const int tex_mapping,
122 Object *tex_map_object,
123 const char *text_map_bone,
124 const char *tex_uvlayer_name,
125 const bool invert_vgroup_mask)
126{
127 int ref_didx;
128 int i;
129
130 /* If influence factor is null, nothing to do! */
131 if (fact == 0.0f) {
132 return;
133 }
134
135 /* If we want to mask vgroup weights from a texture. */
136 if (texture != nullptr) {
137 /* The texture coordinates. */
138 float (*tex_co)[3];
139 /* See mapping note below... */
141 const int verts_num = mesh->verts_num;
142
143 /* Use new generic get_texture_coords, but do not modify our DNA struct for it...
144 * XXX Why use a ModifierData stuff here ? Why not a simple, generic struct for parameters?
145 * What e.g. if a modifier wants to use several textures?
146 * Why use only v_co, and not mesh positions (or both)?
147 */
148 t_map.texture = texture;
149 t_map.map_object = tex_map_object;
150 STRNCPY(t_map.map_bone, text_map_bone);
151 STRNCPY(t_map.uvlayer_name, tex_uvlayer_name);
152 t_map.texmapping = tex_mapping;
153
154 tex_co = MEM_new_array_zeroed<float[3]>(verts_num, __func__);
155 MOD_get_texture_coords(&t_map, ctx, ob, mesh, nullptr, tex_co);
156
157 MOD_init_texture(&t_map, ctx);
158
159 /* For each weight (vertex), make the mix between org and new weights. */
160 for (i = 0; i < num; i++) {
161 int idx = indices ? indices[i] : i;
162 TexResult texres;
163 float hsv[3]; /* For HSV color space. */
164 bool do_color_manage = tex_use_channel != MOD_WVG_MASK_TEX_USE_INT;
165
166 BKE_texture_get_value(texture, tex_co[idx], &texres, do_color_manage);
167 /* Get the good channel value... */
168 switch (tex_use_channel) {
170 org_w[i] = (new_w[i] * texres.tin * fact) + (org_w[i] * (1.0f - (texres.tin * fact)));
171 break;
173 org_w[i] = (new_w[i] * texres.trgba[0] * fact) +
174 (org_w[i] * (1.0f - (texres.trgba[0] * fact)));
175 break;
177 org_w[i] = (new_w[i] * texres.trgba[1] * fact) +
178 (org_w[i] * (1.0f - (texres.trgba[1] * fact)));
179 break;
181 org_w[i] = (new_w[i] * texres.trgba[2] * fact) +
182 (org_w[i] * (1.0f - (texres.trgba[2] * fact)));
183 break;
185 rgb_to_hsv_v(texres.trgba, hsv);
186 org_w[i] = (new_w[i] * hsv[0] * fact) + (org_w[i] * (1.0f - (hsv[0] * fact)));
187 break;
189 rgb_to_hsv_v(texres.trgba, hsv);
190 org_w[i] = (new_w[i] * hsv[1] * fact) + (org_w[i] * (1.0f - (hsv[1] * fact)));
191 break;
193 rgb_to_hsv_v(texres.trgba, hsv);
194 org_w[i] = (new_w[i] * hsv[2] * fact) + (org_w[i] * (1.0f - (hsv[2] * fact)));
195 break;
197 org_w[i] = (new_w[i] * texres.trgba[3] * fact) +
198 (org_w[i] * (1.0f - (texres.trgba[3] * fact)));
199 break;
200 default:
201 org_w[i] = (new_w[i] * texres.tin * fact) + (org_w[i] * (1.0f - (texres.tin * fact)));
202 break;
203 }
204 }
205
206 MEM_delete(tex_co);
207 }
208 else if ((ref_didx = BKE_id_defgroup_name_index(&mesh->id, defgrp_name)) != -1) {
209 /* Check whether we want to set vgroup weights from a constant weight factor or a vertex
210 * group.
211 */
212 /* Get vgroup idx from its name. */
213
214 /* Proceed only if vgroup is valid, else use constant factor. */
215 /* Get actual deform-verts (ie vertex group data). */
216 const MDeformVert *dvert = mesh->deform_verts().data();
217 /* Proceed only if vgroup is valid, else assume factor = O. */
218 if (dvert == nullptr) {
219 return;
220 }
221
222 /* For each weight (vertex), make the mix between org and new weights. */
223 for (i = 0; i < num; i++) {
224 int idx = indices ? indices[i] : i;
225 const float f = (invert_vgroup_mask ?
226 (1.0f - BKE_defvert_find_weight(&dvert[idx], ref_didx)) :
227 BKE_defvert_find_weight(&dvert[idx], ref_didx)) *
228 fact;
229 org_w[i] = (new_w[i] * f) + (org_w[i] * (1.0f - f));
230 /* If that vertex is not in ref vgroup, assume null factor, and hence do nothing! */
231 }
232 }
233 else {
234 /* Default "influence" behavior. */
235 /* For each weight (vertex), make the mix between org and new weights. */
236 const float ifact = 1.0f - fact;
237 for (i = 0; i < num; i++) {
238 org_w[i] = (new_w[i] * fact) + (org_w[i] * ifact);
239 }
240 }
241}
242
244 int defgrp_idx,
245 MDeformWeight **dws,
246 int num,
247 const int *indices,
248 const float *weights,
249 const bool do_add,
250 const float add_thresh,
251 const bool do_rem,
252 const float rem_thresh,
253 const bool do_normalize)
254{
255 int i;
256
257 float min_w = weights[0];
258 float norm_fac = 1.0f;
259 if (do_normalize) {
260 float max_w = weights[0];
261 for (i = 1; i < num; i++) {
262 const float w = weights[i];
263
264 /* No need to clamp here, normalization will ensure we stay within [0.0, 1.0] range. */
265 if (w < min_w) {
266 min_w = w;
267 }
268 else if (w > max_w) {
269 max_w = w;
270 }
271 }
272
273 const float range = max_w - min_w;
274 if (fabsf(range) > FLT_EPSILON) {
275 norm_fac = 1.0f / range;
276 }
277 else {
278 min_w = 0.0f;
279 }
280 }
281
282 for (i = 0; i < num; i++) {
283 float w = weights[i];
284 MDeformVert *dv = &dvert[indices ? indices[i] : i];
285 MDeformWeight *dw = dws ?
286 dws[i] :
287 ((defgrp_idx >= 0) ? BKE_defvert_find_index(dv, defgrp_idx) : nullptr);
288
289 if (do_normalize) {
290 w = (w - min_w) * norm_fac;
291 }
292 /* Never allow weights out of [0.0, 1.0] range. */
293 CLAMP(w, 0.0f, 1.0f);
294
295 /* If the vertex is in this vgroup, remove it if needed, or just update it. */
296 if (dw != nullptr) {
297 if (do_rem && w <= rem_thresh) {
299 }
300 else {
301 dw->weight = w;
302 }
303 }
304 /* Else, add it if needed! */
305 else if (do_add && w >= add_thresh) {
306 BKE_defvert_add_index_notest(dv, defgrp_idx, w);
307 }
308 }
309}
310
311void weightvg_ui_common(const bContext *C, PointerRNA *ob_ptr, PointerRNA *ptr, ui::Layout &layout)
312{
313 PointerRNA mask_texture_ptr = RNA_pointer_get(ptr, "mask_texture");
314 bool has_mask_texture = mask_texture_ptr;
315 bool has_mask_vertex_group = RNA_string_length(ptr, "mask_vertex_group") != 0;
316 int mask_tex_mapping = RNA_enum_get(ptr, "mask_tex_mapping");
317
318 layout.use_property_split_set(true);
319
320 layout.prop(ptr, "mask_constant", ui::ITEM_R_SLIDER, IFACE_("Global Influence:"), ICON_NONE);
321
322 if (!has_mask_texture) {
324 layout, ptr, ob_ptr, "mask_vertex_group", "invert_mask_vertex_group", std::nullopt);
325 }
326
327 if (!has_mask_vertex_group) {
328 template_id(&layout,
329 C,
330 ptr,
331 "mask_texture",
332 "texture.new",
333 nullptr,
334 nullptr,
336 false,
337 IFACE_("Mask Texture"));
338
339 if (has_mask_texture) {
340 layout.prop(ptr, "mask_tex_use_channel", UI_ITEM_NONE, IFACE_("Channel"), ICON_NONE);
341 layout.prop(ptr, "mask_tex_mapping", UI_ITEM_NONE, std::nullopt, ICON_NONE);
342
343 if (mask_tex_mapping == MOD_DISP_MAP_OBJECT) {
344 layout.prop(ptr, "mask_tex_map_object", UI_ITEM_NONE, IFACE_("Object"), ICON_NONE);
345 }
346 else if (mask_tex_mapping == MOD_DISP_MAP_UV && RNA_enum_get(ob_ptr, "type") == OB_MESH) {
347 PointerRNA obj_data_ptr = RNA_pointer_get(ob_ptr, "data");
348 layout.prop_search(
349 ptr, "mask_tex_uv_layer", &obj_data_ptr, "uv_layers", std::nullopt, ICON_NONE);
350 }
351 }
352 }
353}
354
355} // namespace blender
support for deformation groups and hooks.
Random number functions.
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
#define MAX_VGROUP_NAME
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define CLAMP(a, b, c)
#define BLI_assert_unreachable()
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define FLT_EPSILON
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
char * STRNCPY(char(&dst)[N], const char *src)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
void MOD_init_texture(MappingInfoModifierData *dmd, const ModifierEvalContext *ctx)
Definition MOD_util.cc:38
int BKE_id_defgroup_name_index(const ID *id, StringRef name)
Definition deform.cc:557
void BKE_curvemapping_init(CurveMapping *cumap)
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:779
void modifier_vgroup_ui(ui::Layout &layout, PointerRNA *ptr, PointerRNA *ob_ptr, const StringRefNull vgroup_prop, const std::optional< StringRefNull > invert_vgroup_prop, const std::optional< StringRefNull > text)
ATTR_WARN_UNUSED_RESULT const size_t num
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition rand.cc:90
int RNA_string_length(PointerRNA *ptr, const char *name)
void BKE_texture_get_value(struct Tex *texture, const float *tex_co, struct TexResult *texres, bool use_color_management)
Definition texture.cc:640
void weightvg_ui_common(const bContext *C, PointerRNA *ob_ptr, PointerRNA *ptr, ui::Layout &layout)
void BKE_defvert_add_index_notest(MDeformVert *dv, int defgroup, float weight)
Definition deform.cc:861
void weightvg_update_vg(MDeformVert *dvert, int defgrp_idx, MDeformWeight **dws, int num, const int *indices, const float *weights, const bool do_add, const float add_thresh, const bool do_rem, const float rem_thresh, const bool do_normalize)
void weightvg_do_mask(const ModifierEvalContext *ctx, const int num, const int *indices, float *org_w, const float *new_w, Object *ob, Mesh *mesh, const float fact, const char defgrp_name[MAX_VGROUP_NAME], Scene *, Tex *texture, const int tex_use_channel, const int tex_mapping, Object *tex_map_object, const char *text_map_bone, const char *tex_uvlayer_name, const bool invert_vgroup_mask)
void MOD_get_texture_coords(MappingInfoModifierData *dmd, const ModifierEvalContext *, Object *ob, Mesh *mesh, float(*cos)[3], float(*r_texco)[3])
Definition MOD_util.cc:51
void BKE_defvert_remove_group(MDeformVert *dvert, MDeformWeight *dw)
Definition deform.cc:885
void weightvg_do_map(int num, float *new_w, short falloff_type, const bool do_invert, CurveMapping *cmap, RNG *rng)
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
MDeformWeight * BKE_defvert_find_index(const MDeformVert *dv, int defgroup)
Definition deform.cc:811
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
#define fabsf
#define sqrtf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void prop_search(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop, PropertyRNA *item_searchpropname, std::optional< StringRefNull > name, int icon, bool results_are_suggestions)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)