Blender V4.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
9#include "node_shader_util.hh"
10
12
13#include "NOD_multi_function.hh"
14
15#include "UI_interface.hh"
16#include "UI_resources.hh"
17
19
21{
22 b.is_function_node();
23 b.add_input<decl::Float>("Value").default_value(1.0f);
24 b.add_input<decl::Float>("Min").default_value(0.0f).min(-10000.0f).max(10000.0f);
25 b.add_input<decl::Float>("Max").default_value(1.0f).min(-10000.0f).max(10000.0f);
26 b.add_output<decl::Float>("Result");
27}
28
29static void node_shader_buts_clamp(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
30{
31 uiItemR(layout, ptr, "clamp_type", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
32}
33
34static void node_shader_init_clamp(bNodeTree * /*ntree*/, bNode *node)
35{
36 node->custom1 = NODE_CLAMP_MINMAX; /* clamp type */
37}
38
40 bNode *node,
41 bNodeExecData * /*execdata*/,
42 GPUNodeStack *in,
43 GPUNodeStack *out)
44{
45 return (node->custom1 == NODE_CLAMP_MINMAX) ?
46 GPU_stack_link(mat, node, "clamp_minmax", in, out) :
47 GPU_stack_link(mat, node, "clamp_range", in, out);
48}
49
51{
52 static auto minmax_fn = mf::build::SI3_SO<float, float, float, float>(
53 "Clamp (Min Max)",
54 [](float value, float min, float max) { return std::min(std::max(value, min), max); });
55 static auto range_fn = mf::build::SI3_SO<float, float, float, float>(
56 "Clamp (Range)", [](float value, float a, float b) {
57 if (a < b) {
58 return clamp_f(value, a, b);
59 }
60
61 return clamp_f(value, b, a);
62 });
63
64 int clamp_type = builder.node().custom1;
65 if (clamp_type == NODE_CLAMP_MINMAX) {
66 builder.set_matching_fn(minmax_fn);
67 }
68 else {
69 builder.set_matching_fn(range_fn);
70 }
71}
72
74#ifdef WITH_MATERIALX
75{
76 auto type = node_->custom1;
77 NodeItem value = get_input_value("Value", NodeItem::Type::Float);
78 NodeItem min = get_input_value("Min", NodeItem::Type::Float);
79 NodeItem max = get_input_value("Max", NodeItem::Type::Float);
80
81 NodeItem res = empty();
82 if (type == NODE_CLAMP_RANGE) {
83 res = min.if_else(
84 NodeItem::CompareOp::Less, max, value.clamp(min, max), value.clamp(max, min));
85 }
86 else {
87 res = value.clamp(min, max);
88 }
89 return res;
90}
91#endif
93
94} // namespace blender::nodes::node_shader_clamp_cc
95
97{
98 namespace file_ns = blender::nodes::node_shader_clamp_cc;
99
100 static blender::bke::bNodeType ntype;
101
103 ntype.declare = file_ns::sh_node_clamp_declare;
104 ntype.draw_buttons = file_ns::node_shader_buts_clamp;
105 ntype.initfunc = file_ns::node_shader_init_clamp;
106 ntype.gpu_fn = file_ns::gpu_shader_clamp;
107 ntype.build_multi_function = file_ns::sh_node_clamp_build_multi_function;
108 ntype.materialx_fn = file_ns::node_shader_materialx;
109
111}
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:410
#define SH_NODE_CLAMP
Definition BKE_node.hh:986
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,...)
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
@ UI_ITEM_R_SPLIT_EMPTY_NAME
void set_matching_fn(const mf::MultiFunction *fn)
local_group_size(16, 16) .push_constant(Type b
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
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 sh_fn_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
#define min(a, b)
Definition sort.c:32
int16_t custom1
Defines a node type.
Definition BKE_node.hh:218
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:320
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:318
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:336
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeDeclareFunction declare
Definition BKE_node.hh:347
PointerRNA * ptr
Definition wm_files.cc:4126