Blender V5.0
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.h"
12
14
15#include "NOD_multi_function.hh"
16
18#include "UI_resources.hh"
19
21
23{
24 b.is_function_node();
25 b.add_input<decl::Float>("Value").default_value(1.0f);
26 b.add_input<decl::Float>("Min").default_value(0.0f).min(-10000.0f).max(10000.0f);
27 b.add_input<decl::Float>("Max").default_value(1.0f).min(-10000.0f).max(10000.0f);
28 b.add_output<decl::Float>("Result");
29}
30
31static void node_shader_buts_clamp(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
32{
33 layout->prop(ptr, "clamp_type", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
34}
35
36static void node_shader_init_clamp(bNodeTree * /*ntree*/, bNode *node)
37{
38 node->custom1 = NODE_CLAMP_MINMAX; /* clamp type */
39}
40
42 bNode *node,
43 bNodeExecData * /*execdata*/,
46{
47 return (node->custom1 == NODE_CLAMP_MINMAX) ?
48 GPU_stack_link(mat, node, "clamp_minmax", in, out) :
49 GPU_stack_link(mat, node, "clamp_range", in, out);
50}
51
53{
54 static auto minmax_fn = mf::build::SI3_SO<float, float, float, float>(
55 "Clamp (Min Max)",
56 [](float value, float min, float max) { return std::min(std::max(value, min), max); });
57 static auto range_fn = mf::build::SI3_SO<float, float, float, float>(
58 "Clamp (Range)", [](float value, float a, float b) {
59 if (a < b) {
60 return clamp_f(value, a, b);
61 }
62
63 return clamp_f(value, b, a);
64 });
65
66 int clamp_type = builder.node().custom1;
67 if (clamp_type == NODE_CLAMP_MINMAX) {
68 builder.set_matching_fn(minmax_fn);
69 }
70 else {
71 builder.set_matching_fn(range_fn);
72 }
73}
74
76#ifdef WITH_MATERIALX
77{
78 auto type = node_->custom1;
79 NodeItem value = get_input_value("Value", NodeItem::Type::Float);
80 NodeItem min = get_input_value("Min", NodeItem::Type::Float);
81 NodeItem max = get_input_value("Max", NodeItem::Type::Float);
82
83 NodeItem res = empty();
84 if (type == NODE_CLAMP_RANGE) {
85 res = min.if_else(
86 NodeItem::CompareOp::Less, max, value.clamp(min, max), value.clamp(max, min));
87 }
88 else {
89 res = value.clamp(min, max);
90 }
91 return res;
92}
93#endif
95
96} // namespace blender::nodes::node_shader_clamp_cc
97
99{
100 namespace file_ns = blender::nodes::node_shader_clamp_cc;
101
102 static blender::bke::bNodeType ntype;
103
104 common_node_type_base(&ntype, "ShaderNodeClamp", SH_NODE_CLAMP);
105 ntype.ui_name = "Clamp";
106 ntype.ui_description = "Clamp a value between a minimum and a maximum";
107 ntype.enum_name_legacy = "CLAMP";
109 ntype.declare = file_ns::sh_node_clamp_declare;
110 ntype.draw_buttons = file_ns::node_shader_buts_clamp;
111 ntype.initfunc = file_ns::node_shader_init_clamp;
112 ntype.gpu_fn = file_ns::gpu_shader_clamp;
113 ntype.build_multi_function = file_ns::sh_node_clamp_build_multi_function;
114 ntype.materialx_fn = file_ns::node_shader_materialx;
115
117}
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define SH_NODE_CLAMP
MINLINE float clamp_f(float value, float min, float max)
@ NODE_CLAMP_RANGE
@ NODE_CLAMP_MINMAX
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
@ UI_ITEM_R_SPLIT_EMPTY_NAME
void set_matching_fn(const mf::MultiFunction *fn)
#define in
#define out
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
static void sh_node_clamp_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_init_clamp(bNodeTree *, bNode *node)
static void node_shader_buts_clamp(uiLayout *layout, bContext *, PointerRNA *ptr)
static int gpu_shader_clamp(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_clamp_declare(NodeDeclarationBuilder &b)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void register_node_type_sh_clamp()
void common_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:36
int16_t custom1
Defines a node type.
Definition BKE_node.hh:238
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:344
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:342
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
max
Definition text_draw.cc:251
PointerRNA * ptr
Definition wm_files.cc:4238