[Bf-blender-cvs] [daaa02f8b19] functions: Use weight function in displace modifier

Jacques Lucke noreply at git.blender.org
Thu Feb 21 17:00:51 CET 2019


Commit: daaa02f8b19c5b47d424c4fb8bcbc494e0671b5b
Author: Jacques Lucke
Date:   Thu Feb 21 14:32:59 2019 +0100
Branches: functions
https://developer.blender.org/rBdaaa02f8b19c5b47d424c4fb8bcbc494e0671b5b

Use weight function in displace modifier

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

M	release/scripts/startup/bl_ui/properties_data_modifier.py
M	source/blender/makesdna/DNA_modifier_types.h
M	source/blender/makesrna/intern/rna_modifier.c
M	source/blender/modifiers/intern/MOD_displace.c

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

diff --git a/release/scripts/startup/bl_ui/properties_data_modifier.py b/release/scripts/startup/bl_ui/properties_data_modifier.py
index 8ec1feb14ad..5a18ecc58c1 100644
--- a/release/scripts/startup/bl_ui/properties_data_modifier.py
+++ b/release/scripts/startup/bl_ui/properties_data_modifier.py
@@ -386,6 +386,8 @@ class DATA_PT_modifiers(ModifierButtonsPanel, Panel):
         row.prop(md, "mid_level")
         row.prop(md, "strength")
 
+        layout.prop(md, "function_tree", text="Weight Function")
+
     def DYNAMIC_PAINT(self, layout, ob, md):
         layout.label(text="Settings are inside the Physics tab")
 
diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h
index afc34a6e5b7..02fd3059619 100644
--- a/source/blender/makesdna/DNA_modifier_types.h
+++ b/source/blender/makesdna/DNA_modifier_types.h
@@ -485,6 +485,7 @@ typedef struct DisplaceModifierData {
 	char defgrp_name[64];
 	float midlevel;
 	int space;
+	struct bNodeTree *function_tree;
 } DisplaceModifierData;
 
 /* DisplaceModifierData->direction */
diff --git a/source/blender/makesrna/intern/rna_modifier.c b/source/blender/makesrna/intern/rna_modifier.c
index ad41b6693db..e9d2ffd9d59 100644
--- a/source/blender/makesrna/intern/rna_modifier.c
+++ b/source/blender/makesrna/intern/rna_modifier.c
@@ -2274,6 +2274,11 @@ static void rna_def_modifier_displace(BlenderRNA *brna)
 	RNA_def_property_ui_text(prop, "Space", "");
 	RNA_def_property_update(prop, 0, "rna_Modifier_dependency_update");
 
+	prop = RNA_def_property(srna, "function_tree", PROP_POINTER, PROP_NONE);
+	RNA_def_property_flag(prop, PROP_EDITABLE);
+	RNA_def_property_ui_text(prop, "Function Tree", "Function node tree");
+	RNA_def_property_update(prop, 0, "rna_Modifier_dependency_update");
+
 	rna_def_modifier_generic_map_info(srna);
 }
 
diff --git a/source/blender/modifiers/intern/MOD_displace.c b/source/blender/modifiers/intern/MOD_displace.c
index d1c1f74b68f..855c43f5496 100644
--- a/source/blender/modifiers/intern/MOD_displace.c
+++ b/source/blender/modifiers/intern/MOD_displace.c
@@ -50,6 +50,8 @@
 
 #include "RE_shader_ext.h"
 
+#include "FN_functions.h"
+
 
 /* Displace */
 
@@ -62,6 +64,20 @@ static void initData(ModifierData *md)
 	dmd->direction = MOD_DISP_DIR_NOR;
 	dmd->midlevel = 0.5;
 	dmd->space = MOD_DISP_SPACE_LOCAL;
+	dmd->function_tree = NULL;
+}
+
+static FnFunction getCurrentFunction(DisplaceModifierData *dmd)
+{
+	bNodeTree *tree = (bNodeTree *)DEG_get_original_id((ID *)dmd->function_tree);
+
+	FnType float_ty = FN_type_borrow_float();
+	FnType fvec3_ty = FN_type_borrow_fvec3();
+
+	FnType inputs[] = { fvec3_ty, NULL };
+	FnType outputs[] = { float_ty, NULL };
+
+	return FN_function_get_with_signature(tree, inputs, outputs);
 }
 
 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
@@ -116,6 +132,7 @@ static void foreachIDLink(
 	DisplaceModifierData *dmd = (DisplaceModifierData *) md;
 
 	walk(userData, ob, (ID **)&dmd->texture, IDWALK_CB_USER);
+	walk(userData, ob, (ID **)&dmd->function_tree, IDWALK_CB_USER);
 
 	foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
 }
@@ -149,6 +166,12 @@ static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphConte
 	if (dmd->texture != NULL) {
 		DEG_add_generic_id_relation(ctx->node, &dmd->texture->id, "Displace Modifier");
 	}
+
+	FnFunction fn = getCurrentFunction(dmd);
+	if (fn) {
+		FN_function_update_dependencies(fn, ctx->node);
+		FN_function_free(fn);
+	}
 }
 
 typedef struct DisplaceUserdata {
@@ -166,6 +189,7 @@ typedef struct DisplaceUserdata {
 	float local_mat[4][4];
 	MVert *mvert;
 	float (*vert_clnors)[3];
+	FnFunction calc_weight_func;
 } DisplaceUserdata;
 
 static void displaceModifier_do_task(
@@ -192,13 +216,30 @@ static void displaceModifier_do_task(
 	float delta;
 	float local_vec[3];
 
-	if (dvert) {
-		weight = defvert_find_weight(dvert + iter, defgrp_index);
-		if (weight == 0.0f) {
-			return;
-		}
+	if (data->calc_weight_func) {
+		FnTuple fn_in = FN_tuple_for_input(data->calc_weight_func);
+		FnTuple fn_out = FN_tuple_for_output(data->calc_weight_func);
+		FN_tuple_set_float_vector_3(fn_in, 0, vertexCos[iter]);
+		FnCallable callable = FN_function_get_callable(data->calc_weight_func);
+		FN_function_call(callable, fn_in, fn_out);
+		weight = FN_tuple_get_float(fn_out, 0);
+		FN_tuple_free(fn_in);
+		FN_tuple_free(fn_out);
 	}
 
+	if (weight == 0.0f) {
+		return;
+	}
+
+	// if (dvert) {
+	// 	weight = defvert_find_weight(dvert + iter, defgrp_index);
+	// 	if (weight == 0.0f) {
+	// 		return;
+	// 	}
+	// }
+
+	strength *= weight;
+
 	if (data->tex_target) {
 		texres.nor = NULL;
 		BKE_texture_get_value_ex(data->scene, data->tex_target, tex_co[iter], &texres, data->pool, false);
@@ -208,10 +249,6 @@ static void displaceModifier_do_task(
 		delta = delta_fixed;  /* (1.0f - dmd->midlevel) */  /* never changes */
 	}
 
-	if (dvert) {
-		strength *= weight;
-	}
-
 	delta *= strength;
 	CLAMP(delta, -10000, 10000);
 
@@ -343,6 +380,8 @@ static void displaceModifier_do(
 		data.pool = BKE_image_pool_new();
 		BKE_texture_fetch_images_for_pool(tex_target, data.pool);
 	}
+	data.calc_weight_func = getCurrentFunction(dmd);
+
 	ParallelRangeSettings settings;
 	BLI_parallel_range_settings_defaults(&settings);
 	settings.use_threading = (numVerts > 512);
@@ -355,6 +394,10 @@ static void displaceModifier_do(
 		BKE_image_pool_free(data.pool);
 	}
 
+	if (data.calc_weight_func != NULL) {
+		FN_function_free(data.calc_weight_func);
+	}
+
 	if (tex_co) {
 		MEM_freeN(tex_co);
 	}



More information about the Bf-blender-cvs mailing list