[Bf-blender-cvs] [bb043db] alembic: Goal springs for cached strand simulation.

Lukas Tönne noreply at git.blender.org
Fri Apr 10 09:17:52 CEST 2015


Commit: bb043dbef3b7b2ea088a5544bf2a3d1a0a8cdf2d
Author: Lukas Tönne
Date:   Fri Apr 10 09:15:11 2015 +0200
Branches: alembic
https://developer.blender.org/rBbb043dbef3b7b2ea088a5544bf2a3d1a0a8cdf2d

Goal springs for cached strand simulation.

In addition to the original particle hair sim settings the parameters
for strand sim have a curve to define the strength of goal springs along
strands. This useful because currently there is no way to override the
weights of strands. This would require a new particle edit mode that can
work with non-particle data in a sane way ...

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

M	release/scripts/startup/bl_ui/properties_object.py
M	source/blender/blenkernel/intern/cache_library.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/blenloader/intern/writefile.c
M	source/blender/makesdna/DNA_cache_library_types.h
M	source/blender/makesrna/intern/rna_cache_library.c
M	source/blender/physics/intern/BPH_mass_spring.cpp

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

diff --git a/release/scripts/startup/bl_ui/properties_object.py b/release/scripts/startup/bl_ui/properties_object.py
index 06534b6..fcff62a 100644
--- a/release/scripts/startup/bl_ui/properties_object.py
+++ b/release/scripts/startup/bl_ui/properties_object.py
@@ -441,6 +441,10 @@ class OBJECT_PT_duplication(ObjectButtonsPanel, Panel):
         row = col.row(align=True)
         row.prop(params, "goal_stiffness")
         row.prop(params, "goal_damping")
+        row.prop(params, "use_goal_stiffness_curve")
+        if params.use_goal_stiffness_curve:
+            sub = col.column()
+            sub.template_curve_mapping(params, "goal_stiffness_curve")
 
         layout.separator()
 
diff --git a/source/blender/blenkernel/intern/cache_library.c b/source/blender/blenkernel/intern/cache_library.c
index fe47bee..4c9446c 100644
--- a/source/blender/blenkernel/intern/cache_library.c
+++ b/source/blender/blenkernel/intern/cache_library.c
@@ -49,6 +49,7 @@
 
 #include "BKE_anim.h"
 #include "BKE_cache_library.h"
+#include "BKE_colortools.h"
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
 #include "BKE_DerivedMesh.h"
@@ -573,6 +574,14 @@ static void hairsim_params_init(HairSimParams *params)
 	params->bend_damping = 1.0f;
 	params->goal_stiffness = 0.0f;
 	params->goal_damping = 1.0f;
+	{
+		CurveMapping *cm = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
+		cm->cm[0].curve[0].x = 0.0f;
+		cm->cm[0].curve[0].y = 1.0f;
+		cm->cm[0].curve[1].x = 1.0f;
+		cm->cm[0].curve[1].y = 0.0f;
+		params->goal_stiffness_mapping = cm;
+	}
 	
 	params->effector_weights = BKE_add_effector_weights(NULL);
 }
@@ -586,12 +595,16 @@ static void hairsim_copy(HairSimCacheModifier *hsmd, HairSimCacheModifier *thsmd
 {
 	if (hsmd->sim_params.effector_weights)
 		thsmd->sim_params.effector_weights = MEM_dupallocN(hsmd->sim_params.effector_weights);
+	if (hsmd->sim_params.goal_stiffness_mapping)
+		thsmd->sim_params.goal_stiffness_mapping = curvemapping_copy(hsmd->sim_params.goal_stiffness_mapping);
 }
 
 static void hairsim_free(HairSimCacheModifier *hsmd)
 {
 	if (hsmd->sim_params.effector_weights)
 		MEM_freeN(hsmd->sim_params.effector_weights);
+	if (hsmd->sim_params.goal_stiffness_mapping)
+		curvemapping_free(hsmd->sim_params.goal_stiffness_mapping);
 }
 
 static void hairsim_process(HairSimCacheModifier *hsmd, CacheProcessContext *ctx, CacheProcessData *data, int frame, int frame_prev)
@@ -602,6 +615,9 @@ static void hairsim_process(HairSimCacheModifier *hsmd, CacheProcessContext *ctx
 	if (frame <= frame_prev)
 		return;
 	
+	if (hsmd->sim_params.flag & eHairSimParams_Flag_UseGoalStiffnessCurve)
+		curvemapping_changed_all(hsmd->sim_params.goal_stiffness_mapping);
+	
 	iter = BKE_dupli_cache_iter_new(data->dupcache);
 	for (; BKE_dupli_cache_iter_valid(iter); BKE_dupli_cache_iter_next(iter)) {
 		DupliObjectData *dobdata = BKE_dupli_cache_iter_get(iter);
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 013dc6c..0c1a8c9 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -2026,6 +2026,9 @@ static void direct_link_cache_modifiers(FileData *fd, ListBase *modifiers)
 			case eCacheModifierType_HairSimulation: {
 				HairSimCacheModifier *hsmd = (HairSimCacheModifier *)md;
 				hsmd->sim_params.effector_weights = newdataadr(fd, hsmd->sim_params.effector_weights);
+				hsmd->sim_params.goal_stiffness_mapping = newdataadr(fd, hsmd->sim_params.goal_stiffness_mapping);
+				if (hsmd->sim_params.goal_stiffness_mapping)
+					direct_link_curvemapping(fd, hsmd->sim_params.goal_stiffness_mapping);
 				break;
 			}
 		}
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index c8670d0..6a071b8 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -3500,6 +3500,8 @@ static void write_cache_modifiers(WriteData *wd, CacheLibrary *cachelib)
 			case eCacheModifierType_HairSimulation: {
 				HairSimCacheModifier *hsmd = (HairSimCacheModifier *)md;
 				writestruct(wd, DATA, "EffectorWeights", 1, hsmd->sim_params.effector_weights);
+				if (hsmd->sim_params.goal_stiffness_mapping)
+					write_curvemapping(wd, hsmd->sim_params.goal_stiffness_mapping);
 				break;
 			}
 		}
diff --git a/source/blender/makesdna/DNA_cache_library_types.h b/source/blender/makesdna/DNA_cache_library_types.h
index dee7032..37366b3 100644
--- a/source/blender/makesdna/DNA_cache_library_types.h
+++ b/source/blender/makesdna/DNA_cache_library_types.h
@@ -114,18 +114,25 @@ typedef enum eCacheModifier_Type {
 } eCacheModifier_Type;
 
 typedef struct HairSimParams {
+	int flag;
 	float timescale;
 	int substeps;
+	int pad;
 	
 	struct EffectorWeights *effector_weights;
 	
 	float mass;
 	float drag;
 	float goal_stiffness, goal_damping;
+	struct CurveMapping *goal_stiffness_mapping;
 	float stretch_stiffness, stretch_damping;
 	float bend_stiffness, bend_damping;
 } HairSimParams;
 
+typedef enum eHairSimParams_Flag {
+	eHairSimParams_Flag_UseGoalStiffnessCurve        = (1 << 0),
+} eHairSimParams_Flag;
+
 typedef struct HairSimCacheModifier {
 	CacheModifier modifier;
 	
diff --git a/source/blender/makesrna/intern/rna_cache_library.c b/source/blender/makesrna/intern/rna_cache_library.c
index f9a7bb2..f2a3561 100644
--- a/source/blender/makesrna/intern/rna_cache_library.c
+++ b/source/blender/makesrna/intern/rna_cache_library.c
@@ -217,6 +217,18 @@ static void rna_def_hair_sim_params(BlenderRNA *brna)
 	RNA_def_property_ui_text(prop, "Goal Damping", "Damping factor of goal springs");
 	RNA_def_property_update(prop, 0, "rna_CacheModifier_update");
 	
+	prop = RNA_def_property(srna, "use_goal_stiffness_curve", PROP_BOOLEAN, PROP_NONE);
+	RNA_def_property_boolean_sdna(prop, NULL, "flag", eHairSimParams_Flag_UseGoalStiffnessCurve);
+	RNA_def_property_ui_text(prop, "Use Goal Stiffness Curve", "Use a curve to define goal stiffness along the strand");
+	RNA_def_property_update(prop, 0, "rna_CacheModifier_update");
+	
+	prop = RNA_def_property(srna, "goal_stiffness_curve", PROP_POINTER, PROP_NONE);
+	RNA_def_property_pointer_sdna(prop, NULL, "goal_stiffness_mapping");
+	RNA_def_property_struct_type(prop, "CurveMapping");
+	RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+	RNA_def_property_ui_text(prop, "Goal Stiffness Curve", "Stiffness of goal springs along the strand curves");
+	RNA_def_property_update(prop, 0, "rna_CacheModifier_update");
+	
 	prop = RNA_def_property(srna, "stretch_stiffness", PROP_FLOAT, PROP_NONE);
 	RNA_def_property_range(prop, 0.0f, FLT_MAX);
 	RNA_def_property_ui_range(prop, 0.0f, 10000.0f, 0.1f, 3);
diff --git a/source/blender/physics/intern/BPH_mass_spring.cpp b/source/blender/physics/intern/BPH_mass_spring.cpp
index 9e9e22a..84ab4c6 100644
--- a/source/blender/physics/intern/BPH_mass_spring.cpp
+++ b/source/blender/physics/intern/BPH_mass_spring.cpp
@@ -46,6 +46,7 @@ extern "C" {
 
 #include "BKE_cloth.h"
 #include "BKE_collision.h"
+#include "BKE_colortools.h"
 #include "BKE_effect.h"
 #include "BKE_strands.h"
 }
@@ -1351,11 +1352,56 @@ static void strands_calc_curve_bending_forces(Strands *strands, float space[4][4
 	} while (BKE_strand_bend_iter_valid(&it_bend));
 }
 
+static float strands_goal_stiffness(Strands *UNUSED(strands), HairSimParams *params, StrandsVertex *vert, float t)
+{
+	/* XXX There is no possibility of tweaking them in linked data currently,
+	 * so the original workflow of painting weights in particle edit mode is virtually useless.
+	 */
+	float weight;
+	
+	if (params->flag & eHairSimParams_Flag_UseGoalStiffnessCurve)
+		weight = curvemapping_evaluateF(params->goal_stiffness_mapping, 0, t);
+	else
+		weight = vert->weight;
+	CLAMP(weight, 0.0f, 1.0f);
+	
+	return params->goal_stiffness * weight;
+}
+
+/* goal forces pull vertices toward their rest position */
+static void strands_calc_vertex_goal_forces(Strands *strands, float space[4][4], HairSimParams *params, Implicit_Data *data, StrandIterator *it_strand)
+{
+	StrandEdgeIterator it_edge;
+	
+	float rootvel[3];
+	BPH_mass_spring_get_velocity(data, BKE_strand_iter_vertex_offset(strands, it_strand), rootvel);
+	
+	float length = 0.0f;
+	for (BKE_strand_edge_iter_init(&it_edge, it_strand); BKE_strand_edge_iter_valid(&it_edge); BKE_strand_edge_iter_next(&it_edge))
+		length += len_v3v3(it_edge.vertex1->co, it_edge.vertex0->co);
+	float length_inv = length > 0.0f ? 1.0f / length : 0.0f;
+	
+	float t = 0.0f;
+	for (BKE_strand_edge_iter_init(&it_edge, it_strand); BKE_strand_edge_iter_valid(&it_edge); BKE_strand_edge_iter_next(&it_edge)) {
+		int vj = BKE_strand_edge_iter_vertex1_offset(strands, &it_edge);
+		t += len_v3v3(it_edge.vertex1->co, it_edge.vertex0->co);
+		
+		float stiffness = strands_goal_stiffness(strands, params, it_edge.vertex1, t * length_inv);
+		float damping = stiffness * params->goal_damping;
+		
+		float goal[3];
+		mul_v3_m4v3(goal, space, it_edge.vertex1->co);
+		
+		BPH_mass_spring_force_spring_goal(data, vj, goal, rootvel, stiffness, damping, NULL, NULL, NULL);
+	}
+}
+
 /* calculates internal forces for a single strand curve */
 static void strands_calc_curve_forces(Strands *strands, float space[4][4], HairSimParams *params, Implicit_Data *data, StrandIterator *it_strand)
 {
 	strands_calc_curve_stretch_forces(strands, space, params, data, it_strand);
 	strands_calc_curve_bending_forces(strands, space, params, data, it_strand);
+	strands_calc_vertex_goal_forces(strands, space, params, data, it_strand);
 }
 
 /* Collect forces and derivatives:  F, dFdX, dFdV */




More information about the Bf-blender-cvs mailing list