From 7a7eadaf7f6be4008f49a83d76c5a6d5a6294f14 Mon Sep 17 00:00:00 2001 From: OmarSquircleArt Date: Wed, 14 Aug 2019 10:53:19 +0200 Subject: Shading: Add a clamp option to the Map Range node. If the option is enabled, the output is clamped to the target range. The target range is [To Min, To Max]. The option is enabled by default. The clamp option is implemented in EEVEE by linking to the `clamp_value` GLSL function. And it is implemented in Cycles using a graph expand function. Reviewers: brecht, JacquesLucke Differential Revision: https://developer.blender.org/D5477 --- source/blender/editors/space_node/drawnode.c | 8 ++++++++ source/blender/makesrna/intern/rna_nodetree.c | 10 ++++++++++ source/blender/nodes/NOD_static_types.h | 2 +- source/blender/nodes/shader/nodes/node_shader_map_range.c | 12 +++++++++++- 4 files changed, 30 insertions(+), 2 deletions(-) (limited to 'source') diff --git a/source/blender/editors/space_node/drawnode.c b/source/blender/editors/space_node/drawnode.c index b63f0daaa0d..704d878e620 100644 --- a/source/blender/editors/space_node/drawnode.c +++ b/source/blender/editors/space_node/drawnode.c @@ -249,6 +249,11 @@ static void node_buts_texture(uiLayout *layout, bContext *UNUSED(C), PointerRNA } } +static void node_buts_map_range(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) +{ + uiItemR(layout, ptr, "clamp", 0, NULL, ICON_NONE); +} + static void node_buts_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) { uiItemR(layout, ptr, "operation", 0, "", ICON_NONE); @@ -1209,6 +1214,9 @@ static void node_shader_set_butfunc(bNodeType *ntype) case SH_NODE_VALTORGB: ntype->draw_buttons = node_buts_colorramp; break; + case SH_NODE_MAP_RANGE: + ntype->draw_buttons = node_buts_map_range; + break; case SH_NODE_MATH: ntype->draw_buttons = node_buts_math; break; diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index b28403bf28c..347be6b8a72 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -3834,6 +3834,16 @@ static void def_frame(StructRNA *srna) RNA_def_property_update(prop, NC_NODE | ND_DISPLAY, NULL); } +static void def_map_range(StructRNA *srna) +{ + PropertyRNA *prop; + + prop = RNA_def_property(srna, "clamp", PROP_BOOLEAN, PROP_NONE); + 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"); +} + static void def_math(StructRNA *srna) { PropertyRNA *prop; diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h index 2f70e1a39e6..796ad9e5d5b 100644 --- a/source/blender/nodes/NOD_static_types.h +++ b/source/blender/nodes/NOD_static_types.h @@ -50,7 +50,7 @@ DefNode(ShaderNode, SH_NODE_MAPPING, def_sh_mapping, "MAPPIN DefNode(ShaderNode, SH_NODE_CURVE_VEC, def_vector_curve, "CURVE_VEC", VectorCurve, "Vector Curves", "" ) 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, 0, "MAP_RANGE", MapRange, "Map Range", "" ) +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_MATH, def_math, "MATH", Math, "Math", "" ) DefNode(ShaderNode, SH_NODE_VECT_MATH, def_vector_math, "VECT_MATH", VectorMath, "Vector Math", "" ) 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 8bb1b9324e3..7ebf3faf1f3 100644 --- a/source/blender/nodes/shader/nodes/node_shader_map_range.c +++ b/source/blender/nodes/shader/nodes/node_shader_map_range.c @@ -37,13 +37,22 @@ static bNodeSocketTemplate sh_node_map_range_out[] = { {-1, 0, ""}, }; +static void node_shader_init_map_range(bNodeTree *UNUSED(ntree), bNode *node) +{ + node->custom1 = true; +} + static int gpu_shader_map_range(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out) { - return GPU_stack_link(mat, node, "map_range", in, 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); + } + return 1; } void register_node_type_sh_map_range(void) @@ -52,6 +61,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_gpu(&ntype, gpu_shader_map_range); nodeRegisterType(&ntype); -- cgit v1.2.3