Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCody Winchester <CodyWinch>2020-05-26 21:53:38 +0300
committerBastien Montagne <bastien@blender.org>2020-05-26 21:56:27 +0300
commitde257b6366455ba6a3604c0830a92245df11f7bc (patch)
tree239c022c52cbb655ce2116fddb151c3f55549ed6 /source/blender/modifiers/intern/MOD_weightvg_util.c
parent00674c12cc0f08254e5643ad18120b2e9e710a94 (diff)
Modifiers: Add normalize weights option to vertex weight modifiers
Original patch by Cody Winchester (@CodyWinch), several fixes and cleanup by Bastien Montagne (@mont29). Differential revision: https://developer.blender.org/D7656
Diffstat (limited to 'source/blender/modifiers/intern/MOD_weightvg_util.c')
-rw-r--r--source/blender/modifiers/intern/MOD_weightvg_util.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/source/blender/modifiers/intern/MOD_weightvg_util.c b/source/blender/modifiers/intern/MOD_weightvg_util.c
index 5dae6bb8505..1a38787777f 100644
--- a/source/blender/modifiers/intern/MOD_weightvg_util.c
+++ b/source/blender/modifiers/intern/MOD_weightvg_util.c
@@ -272,16 +272,45 @@ void weightvg_update_vg(MDeformVert *dvert,
const bool do_add,
const float add_thresh,
const bool do_rem,
- const float rem_thresh)
+ const float rem_thresh,
+ const bool do_normalize)
{
int i;
+ float min_w = weights[0];
+ float norm_fac = 1.0f;
+ if (do_normalize) {
+ float max_w = weights[0];
+ for (i = 1; i < num; i++) {
+ const float w = weights[i];
+
+ /* No need to clamp here, normalization will ensure we stay within [0.0, 1.0] range. */
+ if (w < min_w) {
+ min_w = w;
+ }
+ else if (w > max_w) {
+ max_w = w;
+ }
+ }
+
+ const float range = max_w - min_w;
+ if (fabsf(range) > FLT_EPSILON) {
+ norm_fac = 1.0f / range;
+ }
+ else {
+ min_w = 0.0f;
+ }
+ }
+
for (i = 0; i < num; i++) {
float w = weights[i];
MDeformVert *dv = &dvert[indices ? indices[i] : i];
MDeformWeight *dw = dws ? dws[i] :
((defgrp_idx >= 0) ? BKE_defvert_find_index(dv, defgrp_idx) : NULL);
+ if (do_normalize) {
+ w = (w - min_w) * norm_fac;
+ }
/* Never allow weights out of [0.0, 1.0] range. */
CLAMP(w, 0.0f, 1.0f);