[Bf-blender-cvs] [28936a4] master: Patch T31269: Add sewing seams to cloth simulation

Daniel Genrich noreply at git.blender.org
Thu Feb 6 18:58:46 CET 2014


Commit: 28936a415076dbded4ec55cf94c49e8d0abe4035
Author: Daniel Genrich
Date:   Thu Feb 6 18:44:05 2014 +0100
https://developer.blender.org/rB28936a415076dbded4ec55cf94c49e8d0abe4035

Patch T31269: Add sewing seams to cloth simulation

Description:
--------------------------
Use loose edges marked as seams as sewing springs.

Usage:
-------------------------
All this patch does is set the rest length to 0 and the stiffness to 1 for springs for loose edges marked as seams so that during the cloth simulation they will be brought together.

Example Video:
-------------------------
http://www.youtube.com/watch?v=-Y_bC0gjoM0

Original Patch by thesleepless (+ git patch by codemanx)

Thank you!

===================================================================

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/blenkernel/intern/implicit.c
M	source/blender/makesdna/DNA_cloth_types.h
M	source/blender/makesrna/intern/rna_cloth.c

===================================================================

diff --git a/release/scripts/startup/bl_ui/properties_physics_cloth.py b/release/scripts/startup/bl_ui/properties_physics_cloth.py
index 91b4cc0..4e382f2 100644
--- a/release/scripts/startup/bl_ui/properties_physics_cloth.py
+++ b/release/scripts/startup/bl_ui/properties_physics_cloth.py
@@ -77,6 +77,16 @@ class PHYSICS_PT_cloth(PhysicButtonsPanel, Panel):
         col.prop(cloth, "structural_stiffness", text="Structural")
         col.prop(cloth, "bending_stiffness", text="Bending")
 
+        col.label(text="Sewing:")
+        col.prop(cloth, "use_sewing_springs", text="Use Sewing Springs")
+        col.prop(cloth, "sewing_force_max", text="Sewing Force")
+
+        sub = col.column()
+        col.label(text="Shrinking:")
+        col.prop_search(cloth, "vertex_group_shrink", ob, "vertex_groups", text="")
+        col.prop(cloth, "shrink_min", text="Min")
+        col.prop(cloth, "shrink_max", text="Max")
+
         col = split.column()
 
         col.label(text="Damping:")
diff --git a/source/blender/blenkernel/BKE_cloth.h b/source/blender/blenkernel/BKE_cloth.h
index 1b61d0a..1e7ac28 100644
--- a/source/blender/blenkernel/BKE_cloth.h
+++ b/source/blender/blenkernel/BKE_cloth.h
@@ -106,6 +106,7 @@ typedef struct ClothVertex {
 	float	bend_stiff;
 	float 	shear_stiff;
 	int 	spring_count; /* how many springs attached? */
+	float	shrink_factor; /* how much to shrink this cloth */
 }
 ClothVertex;
 
@@ -149,7 +150,8 @@ typedef enum {
 	CLOTH_SIMSETTINGS_FLAG_TEARING = ( 1 << 4 ),// true if tearing is enabled
 	CLOTH_SIMSETTINGS_FLAG_SCALING = ( 1 << 8 ), /* is advanced scaling active? */
 	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_NO_SPRING_COMPRESS = (1 << 13), /* don't allow spring compression */
+	CLOTH_SIMSETTINGS_FLAG_SEW = (1 << 14), /* pull ends of loose edges together */
 } CLOTH_SIMSETTINGS_FLAGS;
 
 /* COLLISION FLAGS */
@@ -163,7 +165,8 @@ typedef enum {
 	CLOTH_SPRING_TYPE_STRUCTURAL  = (1 << 1),
 	CLOTH_SPRING_TYPE_SHEAR       = (1 << 2),
 	CLOTH_SPRING_TYPE_BENDING     = (1 << 3),
-	CLOTH_SPRING_TYPE_GOAL        = (1 << 4)
+	CLOTH_SPRING_TYPE_GOAL        = (1 << 4),
+	CLOTH_SPRING_TYPE_SEWING      = (1 << 5)
 } CLOTH_SPRING_TYPES;
 
 /* SPRING FLAGS */
diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c
index cab4adf..0f6d414 100644
--- a/source/blender/blenkernel/intern/cloth.c
+++ b/source/blender/blenkernel/intern/cloth.c
@@ -99,6 +99,8 @@ void cloth_init(ClothModifierData *clmd )
 	clmd->sim_parms->preroll = 0;
 	clmd->sim_parms->maxspringlen = 10;
 	clmd->sim_parms->vgroup_mass = 0;
+	clmd->sim_parms->vgroup_shrink = 0;
+	clmd->sim_parms->shrink_min = 0.0f; /* min amount the fabric will shrink by 0.0 = no shrinking, 1.0 = shrink to nothing*/
 	clmd->sim_parms->avg_spring_len = 0.0;
 	clmd->sim_parms->presets = 2; /* cotton as start setting */
 	clmd->sim_parms->timescale = 1.0f; /* speed factor, describes how fast cloth moves */
@@ -737,6 +739,7 @@ int cloth_uses_vgroup(ClothModifierData *clmd)
 		((clmd->sim_parms->vgroup_mass>0) || 
 		(clmd->sim_parms->vgroup_struct>0)||
 		(clmd->sim_parms->vgroup_bend>0)  ||
+		(clmd->sim_parms->vgroup_shrink>0) ||
 		(clmd->coll_parms->vgroup_selfcol>0)));
 }
 
@@ -809,6 +812,18 @@ static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm )
 								verts->flags |= CLOTH_VERT_FLAG_NOSELFCOLL;
 							}
 						}
+
+					if (clmd->sim_parms->vgroup_shrink > 0 )
+					{
+						if ( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_shrink-1))
+						{
+							verts->shrink_factor = clmd->sim_parms->shrink_min*(1.0f-dvert->dw[j].weight)+clmd->sim_parms->shrink_max*dvert->dw [j].weight; // linear interpolation between min and max shrink factor based on weight
+						}
+					}
+					else {
+						verts->shrink_factor = clmd->sim_parms->shrink_min;
+					}
+
 					}
 				}
 			}
@@ -816,6 +831,7 @@ static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm )
 	}
 }
 
+
 static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float UNUSED(framenr), int first)
 {
 	int i = 0;
@@ -1103,6 +1119,7 @@ static int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm )
 	unsigned int numverts = (unsigned int)dm->getNumVerts (dm);
 	unsigned int numedges = (unsigned int)dm->getNumEdges (dm);
 	unsigned int numfaces = (unsigned int)dm->getNumTessFaces (dm);
+	float shrink_factor;
 	MEdge *medge = dm->getEdgeArray (dm);
 	MFace *mface = dm->getTessFaceArray (dm);
 	int index2 = 0; // our second vertex index
@@ -1133,15 +1150,26 @@ static int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm )
 
 		if ( spring ) {
 			spring_verts_ordered_set(spring, medge[i].v1, medge[i].v2);
-			spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest);
+			if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_SEW && medge[i].flag & ME_LOOSEEDGE) {
+				// handle sewing (loose edges will be pulled together)
+				spring->restlen = 0.0f;
+				spring->stiffness = 1.0f;
+				spring->type = CLOTH_SPRING_TYPE_SEWING;
+			} else {
+				if(clmd->sim_parms->vgroup_shrink > 0)
+					shrink_factor = 1.0f - ((cloth->verts[spring->ij].shrink_factor + cloth->verts[spring->kl].shrink_factor) / 2.0f);
+				else
+					shrink_factor = 1.0f - clmd->sim_parms->shrink_min;
+				spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest) * shrink_factor;
+				spring->stiffness = (cloth->verts[spring->kl].struct_stiff + cloth->verts[spring->ij].struct_stiff) / 2.0f;
+				spring->type = CLOTH_SPRING_TYPE_STRUCTURAL;
+			}
 			clmd->sim_parms->avg_spring_len += spring->restlen;
 			cloth->verts[spring->ij].avg_spring_len += spring->restlen;
 			cloth->verts[spring->kl].avg_spring_len += spring->restlen;
 			cloth->verts[spring->ij].spring_count++;
 			cloth->verts[spring->kl].spring_count++;
-			spring->type = CLOTH_SPRING_TYPE_STRUCTURAL;
 			spring->flags = 0;
-			spring->stiffness = (cloth->verts[spring->kl].struct_stiff + cloth->verts[spring->ij].struct_stiff) / 2.0f;
 			struct_springs++;
 			
 			BLI_linklist_prepend ( &cloth->springs, spring );
@@ -1173,7 +1201,11 @@ static int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm )
 		}
 
 		spring_verts_ordered_set(spring, mface[i].v1, mface[i].v3);
-		spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest);
+		if(clmd->sim_parms->vgroup_shrink > 0)
+			shrink_factor = 1.0f - ((cloth->verts[spring->ij].shrink_factor + cloth->verts[spring->kl].shrink_factor) / 2.0f);
+		else
+			shrink_factor = 1.0f - clmd->sim_parms->shrink_min;
+		spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest) * shrink_factor;
 		spring->type = CLOTH_SPRING_TYPE_SHEAR;
 		spring->stiffness = (cloth->verts[spring->kl].shear_stiff + cloth->verts[spring->ij].shear_stiff) / 2.0f;
 
@@ -1193,7 +1225,11 @@ static int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm )
 		}
 
 		spring_verts_ordered_set(spring, mface[i].v2, mface[i].v4);
-		spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest);
+		if(clmd->sim_parms->vgroup_shrink > 0)
+			shrink_factor = 1.0f - ((cloth->verts[spring->ij].shrink_factor + cloth->verts[spring->kl].shrink_factor) / 2.0f);
+		else
+			shrink_factor = 1.0f - clmd->sim_parms->shrink_min;
+		spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest) * shrink_factor;
 		spring->type = CLOTH_SPRING_TYPE_SHEAR;
 		spring->stiffness = (cloth->verts[spring->kl].shear_stiff + cloth->verts[spring->ij].shear_stiff) / 2.0f;
 
diff --git a/source/blender/blenkernel/intern/implicit.c b/source/blender/blenkernel/intern/implicit.c
index af057b6..d328c90 100644
--- a/source/blender/blenkernel/intern/implicit.c
+++ b/source/blender/blenkernel/intern/implicit.c
@@ -1260,7 +1260,7 @@ DO_INLINE void cloth_calc_spring_force(ClothModifierData *clmd, ClothSpring *s,
 	}
 	
 	// calculate force of structural + shear springs
-	if ((s->type & CLOTH_SPRING_TYPE_STRUCTURAL) || (s->type & CLOTH_SPRING_TYPE_SHEAR)) {
+	if ((s->type & CLOTH_SPRING_TYPE_STRUCTURAL) || (s->type & CLOTH_SPRING_TYPE_SHEAR) || (s->type & CLOTH_SPRING_TYPE_SEWING) ) {
 		if (length > L || no_compress) {
 			s->flags |= CLOTH_SPRING_FLAG_NEEDED;
 			
@@ -1271,7 +1271,16 @@ DO_INLINE void cloth_calc_spring_force(ClothModifierData *clmd, ClothSpring *s,
 			k = scaling / (clmd->sim_parms->avg_spring_len + FLT_EPSILON);
 
 			// TODO: verify, half verified (couldn't see error)
-			mul_fvector_S(stretch_force, dir, k*(length-L));
+ 			if(s->type & CLOTH_SPRING_TYPE_SEWING) {
+ 				// sewing springs usually have a large distance at first so clamp the force so we don't get tunnelling through colission objects
+ 				float force = k*(length-L);
+ 				if(force > clmd->sim_parms->max_sewing) {
+ 					force = clmd->sim_parms->max_sewing;
+ 				}
+ 				mul_fvector_S(stretch_force, dir, force);
+ 			} else {
+  				mul_fvector_S(stretch_force, dir, k*(length-L));
+ 			}
 
 			VECADD(s->f, s->f, stretch_force);
 
diff --git a/source/blender/makesdna/DNA_cloth_types.h b/source/blender/makesdna/DNA_cloth_types.h
index df1cba6..6c7d500 100644
--- a/source/blender/makesdna/DNA_cloth_types.h
+++ b/source/blender/makesdna/DNA_cloth_types.h
@@ -58,6 +58,7 @@ typedef struct ClothSimSettings {
 	float	max_bend; 	/* max bending scaling value, min is "bending" */
 	float	max_struct; 	/* max structural scaling value, min is "structural" */
 	float	max_shear; 	/* max shear scaling value, UNUSED */
+	float	max_sewing; 	/* max sewing force */
 	float 	avg_spring_len; /* used for normalized springs */
 	float 	timescale; /* parameter how fast cloth runs */
 	float	maxgoal; 	/* see SB */
@@ -70,6 +71,8 @@ typedef struct ClothSimSettings {
 	float	velocity_smooth; /* smoothing of velocities for hair */
 	float	collider_friction; /* friction with colliders */
 	float	vel_damping; /* damp the velocity to speed up getting to the resting position */
+	float	shrink_min;  /* min amount to

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list