Blender V4.5
node_composite_map_range.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_math_base.hh"
10
12
13#include "NOD_multi_function.hh"
14
15#include "UI_interface.hh"
16#include "UI_resources.hh"
17
18#include "GPU_material.hh"
19
21
22/* **************** Map Range ******************** */
23
25
27{
28 b.add_input<decl::Float>("Value")
29 .default_value(1.0f)
30 .min(0.0f)
31 .max(1.0f)
33 b.add_input<decl::Float>("From Min")
34 .default_value(0.0f)
35 .min(-10000.0f)
36 .max(10000.0f)
38 b.add_input<decl::Float>("From Max")
39 .default_value(1.0f)
40 .min(-10000.0f)
41 .max(10000.0f)
43 b.add_input<decl::Float>("To Min")
44 .default_value(0.0f)
45 .min(-10000.0f)
46 .max(10000.0f)
48 b.add_input<decl::Float>("To Max")
49 .default_value(1.0f)
50 .min(-10000.0f)
51 .max(10000.0f)
53 b.add_output<decl::Float>("Value");
54}
55
57{
59
60 col = &layout->column(true);
61 col->prop(ptr, "use_clamp", UI_ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
62}
63
64using namespace blender::compositor;
65
66static bool get_should_clamp(const bNode &node)
67{
68 return node.custom1;
69}
70
71static int node_gpu_material(GPUMaterial *material,
72 bNode *node,
73 bNodeExecData * /*execdata*/,
76{
77 const float should_clamp = get_should_clamp(*node);
78 return GPU_stack_link(
79 material, node, "node_composite_map_range", inputs, outputs, GPU_constant(&should_clamp));
80}
81
82/* An arbitrary value determined by Blender. */
83#define BLENDER_ZMAX 10000.0f
84
85template<bool ShouldClamp>
86static float map_range(const float value,
87 const float from_min,
88 const float from_max,
89 const float to_min,
90 const float to_max)
91{
92 if (math::abs(from_max - from_min) < 1e-6f) {
93 return 0.0f;
94 }
95
96 float result = 0.0f;
97 if (value >= -BLENDER_ZMAX && value <= BLENDER_ZMAX) {
98 result = (value - from_min) / (from_max - from_min);
99 result = to_min + result * (to_max - to_min);
100 }
101 else if (value > BLENDER_ZMAX) {
102 result = to_max;
103 }
104 else {
105 result = to_min;
106 }
107
108 if constexpr (ShouldClamp) {
109 if (to_max > to_min) {
110 result = math::clamp(result, to_min, to_max);
111 }
112 else {
113 result = math::clamp(result, to_max, to_min);
114 }
115 }
116
117 return result;
118}
119
120#undef BLENDER_ZMAX
121
123{
124 static auto no_clamp_function = mf::build::SI5_SO<float, float, float, float, float, float>(
125 "Map Range No Clamp",
126 [](const float value,
127 const float from_min,
128 const float from_max,
129 const float to_min,
130 const float to_max) -> float {
131 return map_range<false>(value, from_min, from_max, to_min, to_max);
132 },
133 mf::build::exec_presets::SomeSpanOrSingle<0>());
134 static auto clamp_function = mf::build::SI5_SO<float, float, float, float, float, float>(
135 "Map Range Clamp",
136 [](const float value,
137 const float from_min,
138 const float from_max,
139 const float to_min,
140 const float to_max) -> float {
141 return map_range<true>(value, from_min, from_max, to_min, to_max);
142 },
143 mf::build::exec_presets::SomeSpanOrSingle<0>());
144
145 const bool should_clamp = get_should_clamp(builder.node());
146 if (should_clamp) {
147 builder.set_matching_fn(clamp_function);
148 }
149 else {
150 builder.set_matching_fn(no_clamp_function);
151 }
152}
153
154} // namespace blender::nodes::node_composite_map_range_cc
155
157{
159
160 static blender::bke::bNodeType ntype;
161
162 cmp_node_type_base(&ntype, "CompositorNodeMapRange", CMP_NODE_MAP_RANGE);
163 ntype.ui_name = "Map Range";
164 ntype.ui_description = "Map an input value range into a destination range";
165 ntype.enum_name_legacy = "MAP_RANGE";
167 ntype.declare = file_ns::cmp_node_map_range_declare;
168 ntype.draw_buttons = file_ns::node_composit_buts_map_range;
169 ntype.gpu_fn = file_ns::node_gpu_material;
170 ntype.build_multi_function = file_ns::node_build_multi_function;
171 ntype.gather_link_search_ops = nullptr;
172
174}
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:436
#define CMP_NODE_MAP_RANGE
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ UI_ITEM_R_SPLIT_EMPTY_NAME
void set_matching_fn(const mf::MultiFunction *fn)
uint col
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
T clamp(const T &a, const T &min, const T &max)
T abs(const T &a)
static void cmp_node_map_range_declare(NodeDeclarationBuilder &b)
static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static float map_range(const float value, const float from_min, const float from_max, const float to_min, const float to_max)
static void node_composit_buts_map_range(uiLayout *layout, bContext *, PointerRNA *ptr)
#define BLENDER_ZMAX
static void register_node_type_cmp_map_range()
void cmp_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
static blender::bke::bNodeSocketTemplate outputs[]
static blender::bke::bNodeSocketTemplate inputs[]
int16_t custom1
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:330
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:344
const char * enum_name_legacy
Definition BKE_node.hh:235
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:371
NodeDeclareFunction declare
Definition BKE_node.hh:355
uiLayout & column(bool align)
PointerRNA * ptr
Definition wm_files.cc:4227