Blender V5.3
node_shader_clamp.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "node_shader_util.hh"
10
11#include "BLI_math_base_c.hh"
12
14
15#include "NOD_multi_function.hh"
16
18#include "UI_resources.hh"
19
20namespace blender {
21
23
25{
26 b.is_function_node();
27 b.add_input<decl::Float>("Value"_ustr).default_value(1.0f);
28 b.add_input<decl::Float>("Min"_ustr).default_value(0.0f).min(-10000.0f).max(10000.0f);
29 b.add_input<decl::Float>("Max"_ustr).default_value(1.0f).min(-10000.0f).max(10000.0f);
30 b.add_output<decl::Float>("Result"_ustr);
31}
32
33static void node_shader_buts_clamp(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
34{
35 layout.prop(ptr, "clamp_type", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
36}
37
38static void node_shader_init_clamp(bNodeTree * /*ntree*/, bNode *node)
39{
40 node->custom1 = NODE_CLAMP_MINMAX; /* clamp type */
41}
42
44 bNode *node,
45 bNodeExecData * /*execdata*/,
48{
49 return (node->custom1 == NODE_CLAMP_MINMAX) ?
50 GPU_stack_link(mat, node, "clamp_minmax", in, out) :
51 GPU_stack_link(mat, node, "clamp_range", in, out);
52}
53
55{
56 static auto minmax_fn = mf::build::SI3_SO<float, float, float, float>(
57 "Clamp (Min Max)",
58 [](float value, float min, float max) { return std::min(std::max(value, min), max); });
59 static auto range_fn = mf::build::SI3_SO<float, float, float, float>(
60 "Clamp (Range)", [](float value, float a, float b) {
61 if (a < b) {
62 return clamp_f(value, a, b);
63 }
64
65 return clamp_f(value, b, a);
66 });
67
68 int clamp_type = builder.node().custom1;
69 if (clamp_type == NODE_CLAMP_MINMAX) {
70 builder.set_matching_fn(minmax_fn);
71 }
72 else {
73 builder.set_matching_fn(range_fn);
74 }
75}
76
78#ifdef WITH_MATERIALX
79{
80 auto type = node_->custom1;
81 NodeItem value = get_input_value("Value", NodeItem::Type::Float);
82 NodeItem min = get_input_value("Min", NodeItem::Type::Float);
83 NodeItem max = get_input_value("Max", NodeItem::Type::Float);
84
85 NodeItem res = empty();
86 if (type == NODE_CLAMP_RANGE) {
87 res = min.if_else(
88 NodeItem::CompareOp::Less, max, value.clamp(min, max), value.clamp(max, min));
89 }
90 else {
91 res = value.clamp(min, max);
92 }
93 return res;
94}
95#endif
97
98} // namespace nodes::node_shader_clamp_cc
99
101{
102 namespace file_ns = nodes::node_shader_clamp_cc;
103
104 static bke::bNodeType ntype;
105
106 common_node_type_base(&ntype, "ShaderNodeClamp"_ustr, SH_NODE_CLAMP);
107 ntype.ui_name = "Clamp";
108 ntype.ui_description = "Clamp a value between a minimum and a maximum";
109 ntype.enum_name_legacy = "CLAMP";
111 ntype.declare = file_ns::sh_node_clamp_declare;
112 ntype.draw_buttons = file_ns::node_shader_buts_clamp;
113 ntype.initfunc = file_ns::node_shader_init_clamp;
114 ntype.gpu_fn = file_ns::gpu_shader_clamp;
115 ntype.build_multi_function = file_ns::sh_node_clamp_build_multi_function;
116 ntype.materialx_fn = file_ns::node_shader_materialx;
117
119}
120
121} // namespace blender
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define SH_NODE_CLAMP
btScalar max
void set_matching_fn(const mf::MultiFunction *fn)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void sh_node_clamp_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_init_clamp(bNodeTree *, bNode *node)
static int gpu_shader_clamp(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_buts_clamp(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void sh_node_clamp_declare(NodeDeclarationBuilder &b)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
void register_node_type_sh_clamp()
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
MINLINE float clamp_f(float value, float min, float max)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define min(a, b)
Definition sort.cc:38
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)