From 958d0d4236b1cfa600a7f36f6bdc51fdd0d98a97 Mon Sep 17 00:00:00 2001 From: Charlie Jolly Date: Sat, 7 Dec 2019 12:35:07 +0000 Subject: Shader Nodes: Add Interpolation modes to Map Range node Modes: Linear interpolation (default), stepped linear, smoothstep and smootherstep. This also includes an additional option for the **Clamp node** to switch between **Min Max** (default) and **Range** mode. This was needed to allow clamping when **To Max** is less than **To Min**. Reviewed By: JacquesLucke, brecht Differential Revision: https://developer.blender.org/D5827 --- source/blender/editors/space_node/drawnode.c | 19 +++++- source/blender/gpu/intern/gpu_material_library.h | 2 +- .../material/gpu_shader_material_clamp.glsl | 5 ++ .../material/gpu_shader_material_map_range.glsl | 69 +++++++++++++++++++++- source/blender/makesdna/DNA_node_types.h | 14 +++++ source/blender/makesrna/RNA_enum_types.h | 2 + source/blender/makesrna/intern/rna_nodetree.c | 47 +++++++++++++++ source/blender/nodes/NOD_static_types.h | 2 +- .../blender/nodes/shader/nodes/node_shader_clamp.c | 9 ++- .../nodes/shader/nodes/node_shader_map_range.c | 33 +++++++++-- 10 files changed, 189 insertions(+), 13 deletions(-) (limited to 'source') diff --git a/source/blender/editors/space_node/drawnode.c b/source/blender/editors/space_node/drawnode.c index b60764c410d..a02eeaa1502 100644 --- a/source/blender/editors/space_node/drawnode.c +++ b/source/blender/editors/space_node/drawnode.c @@ -250,9 +250,19 @@ static void node_buts_texture(uiLayout *layout, bContext *UNUSED(C), PointerRNA } } -static void node_buts_map_range(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) +static void node_shader_buts_clamp(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) { - uiItemR(layout, ptr, "clamp", 0, NULL, ICON_NONE); + uiItemR(layout, ptr, "clamp_type", 0, "", ICON_NONE); +} + +static void node_shader_buts_map_range(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) +{ + uiItemR(layout, ptr, "interpolation_type", 0, "", ICON_NONE); + if (!ELEM(RNA_enum_get(ptr, "interpolation_type"), + NODE_MAP_RANGE_SMOOTHSTEP, + NODE_MAP_RANGE_SMOOTHERSTEP)) { + uiItemR(layout, ptr, "clamp", 0, NULL, ICON_NONE); + } } static void node_buts_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) @@ -1172,8 +1182,11 @@ static void node_shader_set_butfunc(bNodeType *ntype) case SH_NODE_VALTORGB: ntype->draw_buttons = node_buts_colorramp; break; + case SH_NODE_CLAMP: + ntype->draw_buttons = node_shader_buts_clamp; + break; case SH_NODE_MAP_RANGE: - ntype->draw_buttons = node_buts_map_range; + ntype->draw_buttons = node_shader_buts_map_range; break; case SH_NODE_MATH: ntype->draw_buttons = node_buts_math; diff --git a/source/blender/gpu/intern/gpu_material_library.h b/source/blender/gpu/intern/gpu_material_library.h index 3a38eb5c600..08c36e24920 100644 --- a/source/blender/gpu/intern/gpu_material_library.h +++ b/source/blender/gpu/intern/gpu_material_library.h @@ -316,7 +316,7 @@ static GPUMaterialLibrary gpu_shader_material_mapping_library = { static GPUMaterialLibrary gpu_shader_material_map_range_library = { .code = datatoc_gpu_shader_material_map_range_glsl, - .dependencies = {NULL}, + .dependencies = {&gpu_shader_material_math_util_library, NULL}, }; static GPUMaterialLibrary gpu_shader_material_math_library = { diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl index b8842064b6f..b196aed690f 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl @@ -2,3 +2,8 @@ void clamp_value(float value, float min, float max, out float result) { result = clamp(value, min, max); } + +void clamp_range(float value, float min, float max, out float result) +{ + result = (max > min) ? clamp(value, min, max) : clamp(value, max, min); +} diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_map_range.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_map_range.glsl index a185774f4b3..7853aae31a1 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_map_range.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_map_range.glsl @@ -1,5 +1,16 @@ -void map_range( - float value, float fromMin, float fromMax, float toMin, float toMax, out float result) +float smootherstep(float edge0, float edge1, float x) +{ + x = clamp(safe_divide((x - edge0), (edge1 - edge0)), 0.0, 1.0); + return x * x * x * (x * (x * 6.0 - 15.0) + 10.0); +} + +void map_range_linear(float value, + float fromMin, + float fromMax, + float toMin, + float toMax, + float steps, + out float result) { if (fromMax != fromMin) { result = toMin + ((value - fromMin) / (fromMax - fromMin)) * (toMax - toMin); @@ -8,3 +19,57 @@ void map_range( result = 0.0; } } + +void map_range_stepped(float value, + float fromMin, + float fromMax, + float toMin, + float toMax, + float steps, + out float result) +{ + if (fromMax != fromMin) { + float factor = (value - fromMin) / (fromMax - fromMin); + factor = (steps > 0.0) ? floor(factor * (steps + 1.0)) / steps : 0.0; + result = toMin + factor * (toMax - toMin); + } + else { + result = 0.0; + } +} + +void map_range_smoothstep(float value, + float fromMin, + float fromMax, + float toMin, + float toMax, + float steps, + out float result) +{ + if (fromMax != fromMin) { + float factor = (fromMin > fromMax) ? 1.0 - smoothstep(fromMax, fromMin, value) : + smoothstep(fromMin, fromMax, value); + result = toMin + factor * (toMax - toMin); + } + else { + result = 0.0; + } +} + +void map_range_smootherstep(float value, + float fromMin, + float fromMax, + float toMin, + float toMax, + float steps, + out float result) +{ + if (fromMax != fromMin) { + float factor = (fromMin > fromMax) ? 1.0 - smootherstep(fromMax, fromMin, value) : + smootherstep(fromMin, fromMax, value); + result = toMin + factor * (toMax - toMin); + } + else { + result = 0.0; + } +} diff --git a/source/blender/makesdna/DNA_node_types.h b/source/blender/makesdna/DNA_node_types.h index 06ddf08b2ce..e3ea3a5b3f8 100644 --- a/source/blender/makesdna/DNA_node_types.h +++ b/source/blender/makesdna/DNA_node_types.h @@ -1260,6 +1260,20 @@ enum { NODE_VECTOR_MATH_MAXIMUM = 19, }; +/* Clamp node types. */ +enum { + NODE_CLAMP_MINMAX = 0, + NODE_CLAMP_RANGE = 1, +}; + +/* Map range node types. */ +enum { + NODE_MAP_RANGE_LINEAR = 0, + NODE_MAP_RANGE_STEPPED = 1, + NODE_MAP_RANGE_SMOOTHSTEP = 2, + NODE_MAP_RANGE_SMOOTHERSTEP = 3, +}; + /* mix rgb node flags */ #define SHD_MIXRGB_USE_ALPHA 1 #define SHD_MIXRGB_CLAMP 2 diff --git a/source/blender/makesrna/RNA_enum_types.h b/source/blender/makesrna/RNA_enum_types.h index 9290b81f1af..318522427d8 100644 --- a/source/blender/makesrna/RNA_enum_types.h +++ b/source/blender/makesrna/RNA_enum_types.h @@ -188,6 +188,8 @@ extern const EnumPropertyItem rna_enum_node_math_items[]; extern const EnumPropertyItem rna_enum_mapping_type_items[]; extern const EnumPropertyItem rna_enum_node_vec_math_items[]; extern const EnumPropertyItem rna_enum_node_filter_items[]; +extern const EnumPropertyItem rna_enum_node_map_range_items[]; +extern const EnumPropertyItem rna_enum_node_clamp_items[]; extern const EnumPropertyItem rna_enum_ramp_blend_items[]; diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index 272a4522152..0a47830b1e3 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -226,6 +226,36 @@ const EnumPropertyItem rna_enum_node_vec_math_items[] = { {0, NULL, 0, NULL, NULL}, }; +const EnumPropertyItem rna_enum_node_map_range_items[] = { + {NODE_MAP_RANGE_LINEAR, + "LINEAR", + 0, + "Linear", + "Linear interpolation between From Min and From Max values"}, + {NODE_MAP_RANGE_STEPPED, + "STEPPED", + 0, + "Stepped Linear", + "Stepped linear interpolation between From Min and From Max values"}, + {NODE_MAP_RANGE_SMOOTHSTEP, + "SMOOTHSTEP", + 0, + "Smoothstep", + "Smooth hermite edge interpolation between From Min and From Max values"}, + {NODE_MAP_RANGE_SMOOTHERSTEP, + "SMOOTHERSTEP", + 0, + "Smootherstep", + "Smoother hermite edge interpolation between From Min and From Max values"}, + {0, NULL, 0, NULL, NULL}, +}; + +const EnumPropertyItem rna_enum_node_clamp_items[] = { + {NODE_CLAMP_MINMAX, "MINMAX", 0, "Min Max", "Clamp values using Min and Max values"}, + {NODE_CLAMP_RANGE, "RANGE", 0, "Range", "Clamp values between Min and Max range"}, + {0, NULL, 0, NULL, NULL}, +}; + static const EnumPropertyItem rna_enum_node_tex_dimensions_items[] = { {1, "1D", 0, "1D", "Use the scalar value W as input"}, {2, "2D", 0, "2D", "Use the 2D vector (x, y) as input. The z component is ignored"}, @@ -3921,6 +3951,17 @@ static void def_frame(StructRNA *srna) RNA_def_property_update(prop, NC_NODE | ND_DISPLAY, NULL); } +static void def_clamp(StructRNA *srna) +{ + PropertyRNA *prop; + + prop = RNA_def_property(srna, "clamp_type", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_sdna(prop, NULL, "custom1"); + RNA_def_property_enum_items(prop, rna_enum_node_clamp_items); + RNA_def_property_ui_text(prop, "Clamp Type", ""); + RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_ShaderNode_socket_update"); +} + static void def_map_range(StructRNA *srna) { PropertyRNA *prop; @@ -3929,6 +3970,12 @@ static void def_map_range(StructRNA *srna) RNA_def_property_boolean_sdna(prop, NULL, "custom1", 1); RNA_def_property_ui_text(prop, "Clamp", "Clamp the result to the target range [To Min, To Max]"); RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update"); + + prop = RNA_def_property(srna, "interpolation_type", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_sdna(prop, NULL, "custom2"); + RNA_def_property_enum_items(prop, rna_enum_node_map_range_items); + RNA_def_property_ui_text(prop, "Interpolation Type", ""); + RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_ShaderNode_socket_update"); } static void def_math(StructRNA *srna) diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h index f3bc5a0cafa..a8163391c13 100644 --- a/source/blender/nodes/NOD_static_types.h +++ b/source/blender/nodes/NOD_static_types.h @@ -51,7 +51,7 @@ DefNode(ShaderNode, SH_NODE_CURVE_VEC, def_vector_curve, "CURVE_ DefNode(ShaderNode, SH_NODE_CURVE_RGB, def_rgb_curve, "CURVE_RGB", RGBCurve, "RGB Curves", "" ) DefNode(ShaderNode, SH_NODE_CAMERA, 0, "CAMERA", CameraData, "Camera Data", "" ) DefNode(ShaderNode, SH_NODE_MAP_RANGE, def_map_range, "MAP_RANGE", MapRange, "Map Range", "" ) -DefNode(ShaderNode, SH_NODE_CLAMP, 0, "CLAMP", Clamp, "Clamp", "" ) +DefNode(ShaderNode, SH_NODE_CLAMP, def_clamp, "CLAMP", Clamp, "Clamp", "" ) DefNode(ShaderNode, SH_NODE_MATH, def_math, "MATH", Math, "Math", "" ) DefNode(ShaderNode, SH_NODE_VECTOR_MATH, def_vector_math, "VECT_MATH", VectorMath, "Vector Math", "" ) DefNode(ShaderNode, SH_NODE_SQUEEZE, 0, "SQUEEZE", Squeeze, "Squeeze Value", "" ) diff --git a/source/blender/nodes/shader/nodes/node_shader_clamp.c b/source/blender/nodes/shader/nodes/node_shader_clamp.c index 8e5b90436ea..329d7492db6 100644 --- a/source/blender/nodes/shader/nodes/node_shader_clamp.c +++ b/source/blender/nodes/shader/nodes/node_shader_clamp.c @@ -35,13 +35,19 @@ static bNodeSocketTemplate sh_node_clamp_out[] = { {-1, 0, ""}, }; +static void node_shader_init_clamp(bNodeTree *UNUSED(ntree), bNode *node) +{ + node->custom1 = NODE_CLAMP_MINMAX; /* clamp type */ +} + static int gpu_shader_clamp(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out) { - return GPU_stack_link(mat, node, "clamp_value", in, out); + return (node->custom1 == NODE_CLAMP_MINMAX) ? GPU_stack_link(mat, node, "clamp_value", in, out) : + GPU_stack_link(mat, node, "clamp_range", in, out); } void register_node_type_sh_clamp(void) @@ -50,6 +56,7 @@ void register_node_type_sh_clamp(void) sh_node_type_base(&ntype, SH_NODE_CLAMP, "Clamp", NODE_CLASS_CONVERTOR, 0); node_type_socket_templates(&ntype, sh_node_clamp_in, sh_node_clamp_out); + node_type_init(&ntype, node_shader_init_clamp); node_type_gpu(&ntype, gpu_shader_clamp); nodeRegisterType(&ntype); diff --git a/source/blender/nodes/shader/nodes/node_shader_map_range.c b/source/blender/nodes/shader/nodes/node_shader_map_range.c index 7ebf3faf1f3..c410dc7a981 100644 --- a/source/blender/nodes/shader/nodes/node_shader_map_range.c +++ b/source/blender/nodes/shader/nodes/node_shader_map_range.c @@ -30,6 +30,7 @@ static bNodeSocketTemplate sh_node_map_range_in[] = { {SOCK_FLOAT, 1, N_("From Max"), 1.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, {SOCK_FLOAT, 1, N_("To Min"), 0.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, {SOCK_FLOAT, 1, N_("To Max"), 1.0f, 1.0f, 1.0f, 1.0f, -10000.0f, 10000.0f, PROP_NONE}, + {SOCK_FLOAT, 1, N_("Steps"), 4.0f, 1.0f, 1.0f, 1.0f, 0.0f, 10000.0f, PROP_NONE}, {-1, 0, ""}, }; static bNodeSocketTemplate sh_node_map_range_out[] = { @@ -37,9 +38,16 @@ static bNodeSocketTemplate sh_node_map_range_out[] = { {-1, 0, ""}, }; +static void node_shader_update_map_range(bNodeTree *UNUSED(ntree), bNode *node) +{ + bNodeSocket *sockSteps = nodeFindSocket(node, SOCK_IN, "Steps"); + nodeSetSocketAvailability(sockSteps, node->custom2 == NODE_MAP_RANGE_STEPPED); +} + static void node_shader_init_map_range(bNodeTree *UNUSED(ntree), bNode *node) { - node->custom1 = true; + node->custom1 = true; /* use_clamp */ + node->custom2 = NODE_MAP_RANGE_LINEAR; /* interpolation */ } static int gpu_shader_map_range(GPUMaterial *mat, @@ -48,11 +56,25 @@ static int gpu_shader_map_range(GPUMaterial *mat, GPUNodeStack *in, GPUNodeStack *out) { - GPU_stack_link(mat, node, "map_range", in, out); - if (node->custom1) { - GPU_link(mat, "clamp_value", out[0].link, in[3].link, in[4].link, &out[0].link); + static const char *names[] = { + [NODE_MAP_RANGE_LINEAR] = "map_range_linear", + [NODE_MAP_RANGE_STEPPED] = "map_range_stepped", + [NODE_MAP_RANGE_SMOOTHSTEP] = "map_range_smoothstep", + [NODE_MAP_RANGE_SMOOTHERSTEP] = "map_range_smootherstep", + }; + + int ret = 0; + if (node->custom2 < ARRAY_SIZE(names) && names[node->custom2]) { + ret = GPU_stack_link(mat, node, names[node->custom2], in, out); + } + else { + ret = GPU_stack_link(mat, node, "map_range_linear", in, out); + } + if (ret && node->custom1 && + !ELEM(node->custom2, NODE_MAP_RANGE_SMOOTHSTEP, NODE_MAP_RANGE_SMOOTHERSTEP)) { + GPU_link(mat, "clamp_range", out[0].link, in[3].link, in[4].link, &out[0].link); } - return 1; + return ret; } void register_node_type_sh_map_range(void) @@ -62,6 +84,7 @@ void register_node_type_sh_map_range(void) sh_node_type_base(&ntype, SH_NODE_MAP_RANGE, "Map Range", NODE_CLASS_CONVERTOR, 0); node_type_socket_templates(&ntype, sh_node_map_range_in, sh_node_map_range_out); node_type_init(&ntype, node_shader_init_map_range); + node_type_update(&ntype, node_shader_update_map_range); node_type_gpu(&ntype, gpu_shader_map_range); nodeRegisterType(&ntype); -- cgit v1.2.3