[Bf-blender-cvs] [6edd60d959] cloth-improvements: Implement instability compensation
Luca Rood
noreply at git.blender.org
Sat Mar 18 23:47:04 CET 2017
Commit: 6edd60d95938aa41cdd0ce8ebef24f0231084411
Author: Luca Rood
Date: Fri Mar 17 22:33:34 2017 -0300
Branches: cloth-improvements
https://developer.blender.org/rB6edd60d95938aa41cdd0ce8ebef24f0231084411
Implement instability compensation
===================================================================
M release/scripts/startup/bl_ui/properties_physics_cloth.py
M source/blender/blenkernel/BKE_cloth.h
M source/blender/blenkernel/intern/cloth.c
M source/blender/makesrna/intern/rna_cloth.c
M source/blender/physics/intern/BPH_mass_spring.cpp
===================================================================
diff --git a/release/scripts/startup/bl_ui/properties_physics_cloth.py b/release/scripts/startup/bl_ui/properties_physics_cloth.py
index 8661aa41be..c4464a79cf 100644
--- a/release/scripts/startup/bl_ui/properties_physics_cloth.py
+++ b/release/scripts/startup/bl_ui/properties_physics_cloth.py
@@ -376,6 +376,8 @@ class PHYSICS_PT_cloth_adaptive_subframes(PhysicButtonsPanel, Panel):
col.prop(cloth, "max_impulse")
col.prop(cloth, "impulse_adjustment_factor")
+ layout.prop(cloth, "compensate_instability")
+
class PHYSICS_PT_cloth_field_weights(PhysicButtonsPanel, Panel):
bl_label = "Cloth Field Weights"
diff --git a/source/blender/blenkernel/BKE_cloth.h b/source/blender/blenkernel/BKE_cloth.h
index de738d13e9..0668f54eb4 100644
--- a/source/blender/blenkernel/BKE_cloth.h
+++ b/source/blender/blenkernel/BKE_cloth.h
@@ -96,7 +96,8 @@ typedef struct Cloth {
struct BVHTree *bvhselftree; /* collision tree for this cloth object */
struct MVertTri *tri;
struct Implicit_Data *implicit; /* our implicit solver connects to this pointer */
- int last_frame, pad4;
+ int last_frame;
+ float adapt_fact; /* Stability dt compensation factor */
} Cloth;
/**
@@ -176,6 +177,7 @@ typedef enum {
CLOTH_SIMSETTINGS_FLAG_INIT_VEL = ( 1 << 3 ), /* initialize cloth velocity from animation */
CLOTH_SIMSETTINGS_FLAG_TEARING = ( 1 << 4 ),// true if tearing is enabled
CLOTH_SIMSETTINGS_FLAG_COMB_GOAL = (1 << 5), // combined weights for goal
+ CLOTH_SIMSETTINGS_FLAG_COMPENSATE_INSTABILITY = (1 << 6), /* Compensate instability by increasing subframes */
CLOTH_SIMSETTINGS_FLAG_CCACHE_EDIT = (1 << 12), /* edit cache in editmode */
CLOTH_SIMSETTINGS_FLAG_NO_SPRING_COMPRESS = (1 << 13), /* don't allow spring compression */
CLOTH_SIMSETTINGS_FLAG_SEW = (1 << 14), /* pull ends of loose edges together */
diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c
index 738e1df1ac..ef89a3c86e 100644
--- a/source/blender/blenkernel/intern/cloth.c
+++ b/source/blender/blenkernel/intern/cloth.c
@@ -298,6 +298,7 @@ static int do_init_cloth(Object *ob, ClothModifierData *clmd, DerivedMesh *resul
BKE_cloth_solver_set_positions(clmd);
clmd->clothObject->last_frame= MINFRAME-1;
+ clmd->clothObject->adapt_fact = 1.0f;
clmd->sim_parms->dt = 1.0f / clmd->sim_parms->stepsPerFrame;
}
diff --git a/source/blender/makesrna/intern/rna_cloth.c b/source/blender/makesrna/intern/rna_cloth.c
index 125969ac19..85fc5627a2 100644
--- a/source/blender/makesrna/intern/rna_cloth.c
+++ b/source/blender/makesrna/intern/rna_cloth.c
@@ -906,6 +906,12 @@ static void rna_def_cloth_sim_settings(BlenderRNA *brna)
RNA_def_property_update(prop, 0, "rna_cloth_update");
RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+ prop = RNA_def_property(srna, "compensate_instability", PROP_BOOLEAN, PROP_NONE);
+ RNA_def_property_boolean_sdna(prop, NULL, "flags", CLOTH_SIMSETTINGS_FLAG_COMPENSATE_INSTABILITY);
+ RNA_def_property_ui_text(prop, "Compensate Instability", "Compensate instability by increasing subframes");
+ RNA_def_property_update(prop, 0, "rna_cloth_update");
+ RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+
/* unused */
/* unused still */
diff --git a/source/blender/physics/intern/BPH_mass_spring.cpp b/source/blender/physics/intern/BPH_mass_spring.cpp
index 72c93ec1a3..f030e58dcf 100644
--- a/source/blender/physics/intern/BPH_mass_spring.cpp
+++ b/source/blender/physics/intern/BPH_mass_spring.cpp
@@ -1038,7 +1038,7 @@ int BPH_cloth_solve(Object *ob, float frame, ClothModifierData *clmd, ListBase *
Implicit_Data *id = cloth->implicit;
ColliderContacts *contacts = NULL;
int totcolliders = 0;
- float max_vel;
+ float max_vel, max_elong;
float vel;
float max_impulse = 0.0f;
float tmp_vec[3];
@@ -1067,14 +1067,20 @@ int BPH_cloth_solve(Object *ob, float frame, ClothModifierData *clmd, ListBase *
/* divide by time_scale to prevent constrained velocities from being multiplied */
mul_v3_fl(v, 1.0f / clmd->sim_parms->time_scale);
BPH_mass_spring_set_velocity(id, i, v);
+
+ if (init_vel) {
+ copy_v3_v3(verts[i].tvold, v);
+ }
}
}
}
while (step < tf) {
ImplicitSolverResult result;
- float dt = clmd->sim_parms->dt * clmd->sim_parms->timescale;
+ float dt = max_ff((clmd->sim_parms->dt * clmd->sim_parms->timescale * cloth->adapt_fact),
+ (1.0f / clmd->sim_parms->max_subframes));
max_vel = 0.0f;
+ max_elong = 0.0f;
adapt_fact = FLT_MAX;
if (step + dt > tf) {
@@ -1133,6 +1139,22 @@ int BPH_cloth_solve(Object *ob, float frame, ClothModifierData *clmd, ListBase *
max_vel = max_ff(max_vel, vel);
}
+ for (LinkNode *link = cloth->springs; link; link = link->next) {
+ ClothSpring *spring = (ClothSpring *)link->link;
+ if (!(spring->flags & CLOTH_SPRING_FLAG_DEACTIVATE) && (spring->type & CLOTH_SPRING_TYPE_STRUCTURAL)) {
+ float tmp1[3], tmp2[3], len_old, len_new;
+
+ len_old = len_v3v3(verts[spring->ij].txold, verts[spring->kl].txold);
+
+ BPH_mass_spring_get_new_position(id, spring->ij, tmp1);
+ BPH_mass_spring_get_new_position(id, spring->kl, tmp2);
+
+ len_new = len_v3v3(tmp1, tmp2);
+
+ max_elong = max_ff(max_elong, (len_new / len_old));
+ }
+ }
+
/* Adaptive step calculation */
if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_ADAPTIVE_SUBFRAMES_VEL) {
if (max_vel < FLT_EPSILON) {
@@ -1158,7 +1180,17 @@ int BPH_cloth_solve(Object *ob, float frame, ClothModifierData *clmd, ListBase *
}
}
- if (clmd->sim_parms->flags & (CLOTH_SIMSETTINGS_FLAG_ADAPTIVE_SUBFRAMES_VEL | CLOTH_SIMSETTINGS_FLAG_ADAPTIVE_SUBFRAMES_IMP)) {
+ if ((clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_COMPENSATE_INSTABILITY) && (max_elong > 2.0f)) {
+ cloth->adapt_fact *= 0.5f;
+ }
+ else {
+ cloth->adapt_fact *= 1.1;
+ cloth->adapt_fact = min_ff(cloth->adapt_fact, 1.0f);
+ }
+
+ if ((clmd->sim_parms->flags & (CLOTH_SIMSETTINGS_FLAG_ADAPTIVE_SUBFRAMES_VEL |
+ CLOTH_SIMSETTINGS_FLAG_ADAPTIVE_SUBFRAMES_IMP |
+ CLOTH_SIMSETTINGS_FLAG_COMPENSATE_INSTABILITY))) {
clmd->sim_parms->dt *= adapt_fact;
clmd->sim_parms->dt = min_ff(1.0f / clmd->sim_parms->stepsPerFrame, clmd->sim_parms->dt);
@@ -1167,7 +1199,8 @@ int BPH_cloth_solve(Object *ob, float frame, ClothModifierData *clmd, ListBase *
}
else {
if (((max_vel > clmd->sim_parms->max_vel) && (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_ADAPTIVE_SUBFRAMES_VEL)) ||
- ((max_impulse > clmd->sim_parms->max_imp) && (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_ADAPTIVE_SUBFRAMES_IMP)))
+ ((max_impulse > clmd->sim_parms->max_imp) && (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_ADAPTIVE_SUBFRAMES_IMP)) ||
+ ((max_elong > 2.0f) && (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_COMPENSATE_INSTABILITY)))
{
for (i = 0; i < mvert_num; i++) {
BPH_mass_spring_set_motion_state(id, i, verts[i].txold, verts[i].tvold);
More information about the Bf-blender-cvs
mailing list