Blender V5.3
node_shader_map_range.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 <algorithm>
10
11#include "node_shader_util.hh"
12#include "node_util.hh"
13
14#include "BLI_math_base_safe.hh"
15#include "BLI_math_vector.hh"
16#include "BLI_math_vector_c.hh"
17
19
20#include "NOD_multi_function.hh"
22
23#include "RNA_access.hh"
24
26#include "UI_resources.hh"
27
28namespace blender {
29
31
33
35{
36 b.is_function_node();
37 b.add_input<decl::Float>("Value"_ustr).min(-10000.0f).max(10000.0f).default_value(1.0f);
38 b.add_input<decl::Float>("From Min"_ustr).min(-10000.0f).max(10000.0f);
39 b.add_input<decl::Float>("From Max"_ustr).min(-10000.0f).max(10000.0f).default_value(1.0f);
40 b.add_input<decl::Float>("To Min"_ustr).min(-10000.0f).max(10000.0f);
41 b.add_input<decl::Float>("To Max"_ustr).min(-10000.0f).max(10000.0f).default_value(1.0f);
42 b.add_input<decl::Float>("Steps"_ustr).min(-10000.0f).max(10000.0f).default_value(4.0f);
43 b.add_input<decl::Vector>("Vector"_ustr).min(0.0f).max(1.0f).hide_value();
44 b.add_input<decl::Vector>("From Min"_ustr, "From_Min_FLOAT3"_ustr);
45 b.add_input<decl::Vector>("From Max"_ustr, "From_Max_FLOAT3"_ustr).default_value(float3(1.0f));
46 b.add_input<decl::Vector>("To Min"_ustr, "To_Min_FLOAT3"_ustr);
47 b.add_input<decl::Vector>("To Max"_ustr, "To_Max_FLOAT3"_ustr).default_value(float3(1.0f));
48 b.add_input<decl::Vector>("Steps"_ustr, "Steps_FLOAT3"_ustr).default_value(float3(4.0f));
49 b.add_output<decl::Float>("Result"_ustr);
50 b.add_output<decl::Vector>("Vector"_ustr);
51}
52
53static void node_shader_buts_map_range(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
54{
55 layout.prop(ptr, "data_type", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
56 layout.prop(ptr, "interpolation_type", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
57 if (!ELEM(RNA_enum_get(ptr, "interpolation_type"),
60 {
61 layout.prop(ptr, "clamp", ui::ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
62 }
63}
64
66{
67 const NodeMapRange &storage = node_storage(*node);
68 const eCustomDataType data_type = eCustomDataType(storage.data_type);
69 if (data_type == CD_PROP_FLOAT3) {
71 }
73}
74
76{
77 const NodeMapRange &storage = node_storage(*node);
78 const eCustomDataType data_type = eCustomDataType(storage.data_type);
79 const int type = (data_type == CD_PROP_FLOAT) ? SOCK_FLOAT : SOCK_VECTOR;
80
81 Array<bool> new_input_availability(node->inputs.count());
82 Array<bool> new_output_availability(node->outputs.count());
83
84 for (const auto [index, socket] : node->inputs.enumerate()) {
85 new_input_availability[index] = socket.type == type;
86 }
87 for (const auto [index, socket] : node->outputs.enumerate()) {
88 new_output_availability[index] = socket.type == type;
89 }
90
91 if (storage.interpolation_type != NODE_MAP_RANGE_STEPPED) {
92 if (type == SOCK_FLOAT) {
93 new_input_availability[5] = false;
94 }
95 else {
96 new_input_availability[11] = false;
97 }
98 }
99
100 for (const auto [index, socket] : node->inputs.enumerate()) {
101 bke::node_set_socket_availability(*ntree, socket, new_input_availability[index]);
102 }
103 for (const auto [index, socket] : node->outputs.enumerate()) {
104 bke::node_set_socket_availability(*ntree, socket, new_output_availability[index]);
105 }
106}
107
109{
110 NodeMapRange *data = MEM_new<NodeMapRange>(__func__);
111 data->clamp = 1;
112 data->data_type = CD_PROP_FLOAT;
113 data->interpolation_type = NODE_MAP_RANGE_LINEAR;
114 node->custom1 = true; /* use_clamp */
115 node->custom2 = NODE_MAP_RANGE_LINEAR; /* interpolation */
116 node->storage = data;
117}
118
120 public:
124
126 {
127 bNode &node = params.add_node("ShaderNodeMapRange"_ustr);
128 node_storage(node).data_type = data_type;
129 node_storage(node).interpolation_type = interpolation_type;
131 }
132};
133
134static std::optional<eCustomDataType> node_type_from_other_socket(const bNodeSocket &socket)
135{
136 switch (socket.type) {
137 case SOCK_FLOAT:
138 case SOCK_BOOLEAN:
139 case SOCK_INT:
140 return CD_PROP_FLOAT;
141 case SOCK_VECTOR:
142 case SOCK_RGBA:
143 return CD_PROP_FLOAT3;
144 default:
145 return {};
146 }
147}
148
150{
151 const std::optional<eCustomDataType> type = node_type_from_other_socket(params.other_socket());
152 if (!type) {
153 return;
154 }
155
156 if (params.in_out() == SOCK_IN) {
157 if (*type == CD_PROP_FLOAT3) {
158 params.add_item(IFACE_("Vector"), SocketSearchOp{"Vector"_ustr, *type}, 0);
159 }
160 else {
161 params.add_item(IFACE_("Value"), SocketSearchOp{"Value"_ustr, *type}, 0);
162 }
163 params.add_item(IFACE_("From Min"), SocketSearchOp{"From Min"_ustr, *type}, -1);
164 params.add_item(IFACE_("From Max"), SocketSearchOp{"From Max"_ustr, *type}, -1);
165 params.add_item(IFACE_("To Min"), SocketSearchOp{"To Min"_ustr, *type}, -2);
166 params.add_item(IFACE_("To Max"), SocketSearchOp{"To Max"_ustr, *type}, -2);
167 params.add_item(
168 IFACE_("Steps"), SocketSearchOp{"Steps"_ustr, *type, NODE_MAP_RANGE_STEPPED}, -3);
169 }
170 else {
171 if (*type == CD_PROP_FLOAT3) {
172 params.add_item(IFACE_("Vector"), SocketSearchOp{"Vector"_ustr, *type});
173 }
174 else {
175 params.add_item(IFACE_("Result"), SocketSearchOp{"Result"_ustr, *type});
176 }
177 }
178}
179
180static const char *gpu_shader_get_name(int mode, bool use_vector)
181{
182 if (use_vector) {
183 switch (mode) {
185 return "vector_map_range_linear";
187 return "vector_map_range_stepped";
189 return "vector_map_range_smoothstep";
191 return "vector_map_range_smootherstep";
192 }
193 }
194 else {
195 switch (mode) {
197 return "map_range_linear";
199 return "map_range_stepped";
201 return "map_range_smoothstep";
203 return "map_range_smootherstep";
204 }
205 }
206
207 return nullptr;
208}
209
211 bNode *node,
212 bNodeExecData * /*execdata*/,
215{
216 const NodeMapRange &storage = node_storage(*node);
217 bool use_vector = (storage.data_type == CD_PROP_FLOAT3);
218 const char *name = gpu_shader_get_name(storage.interpolation_type, use_vector);
219 float clamp = storage.clamp ? 1.0f : 0.0f;
220 int ret = 0;
221 if (name != nullptr) {
222 ret = GPU_stack_link(mat, node, name, in, out, GPU_constant(&clamp));
223 }
224 else {
225 ret = GPU_stack_link(mat, node, "map_range_linear", in, out, GPU_constant(&clamp));
226 }
227 if (ret && storage.clamp && !use_vector &&
229 {
230 GPU_link(mat, "clamp_range", out[0].link, in[3].link, in[4].link, &out[0].link);
231 }
232 return ret;
233}
234
235static inline float clamp_range(const float value, const float min, const float max)
236{
237 return (min > max) ? std::clamp(value, max, min) : std::clamp(value, min, max);
238}
239
240static float3 clamp_range(const float3 value, const float3 min, const float3 max)
241{
242 return float3(clamp_range(value.x, min.x, max.x),
243 clamp_range(value.y, min.y, max.y),
244 clamp_range(value.z, min.z, max.z));
245}
246
247template<bool Clamp> static auto build_float_linear()
248{
249 return mf::build::SI5_SO<float, float, float, float, float, float>(
250 Clamp ? "Map Range (clamped)" : "Map Range (unclamped)",
251 [](float value, float from_min, float from_max, float to_min, float to_max) -> float {
252 const float factor = safe_divide(value - from_min, from_max - from_min);
253 float result = to_min + factor * (to_max - to_min);
254 if constexpr (Clamp) {
255 result = clamp_range(result, to_min, to_max);
256 }
257 return result;
258 },
259 mf::build::exec_presets::SomeSpanOrSingle<0>());
260}
261
262template<bool Clamp> static auto build_float_stepped()
263{
264 return mf::build::SI6_SO<float, float, float, float, float, float, float>(
265 Clamp ? "Map Range Stepped (clamped)" : "Map Range Stepped (unclamped)",
266 [](float value, float from_min, float from_max, float to_min, float to_max, float steps)
267 -> float {
268 float factor = safe_divide(value - from_min, from_max - from_min);
269 factor = safe_divide(floorf(factor * (steps + 1.0f)), steps);
270 float result = to_min + factor * (to_max - to_min);
271 if constexpr (Clamp) {
272 result = clamp_range(result, to_min, to_max);
273 }
274 return result;
275 },
276 mf::build::exec_presets::SomeSpanOrSingle<0>());
277}
278
279template<bool Clamp> static auto build_vector_linear()
280{
281 return mf::build::SI5_SO<float3, float3, float3, float3, float3, float3>(
282 Clamp ? "Vector Map Range (clamped)" : "Vector Map Range (unclamped)",
283 [](const float3 &value,
284 const float3 &from_min,
285 const float3 &from_max,
286 const float3 &to_min,
287 const float3 &to_max) -> float3 {
288 float3 factor = math::safe_divide(value - from_min, from_max - from_min);
289 float3 result = factor * (to_max - to_min) + to_min;
290 if constexpr (Clamp) {
291 result = clamp_range(result, to_min, to_max);
292 }
293 return result;
294 },
295 mf::build::exec_presets::SomeSpanOrSingle<0>());
296}
297
298template<bool Clamp> static auto build_vector_stepped()
299{
300 return mf::build::SI6_SO<float3, float3, float3, float3, float3, float3, float3>(
301 Clamp ? "Vector Map Range Stepped (clamped)" : "Vector Map Range Stepped (unclamped)",
302 [](const float3 &value,
303 const float3 &from_min,
304 const float3 &from_max,
305 const float3 &to_min,
306 const float3 &to_max,
307 const float3 &steps) -> float3 {
308 float3 factor = math::safe_divide(value - from_min, from_max - from_min);
309 factor = math::safe_divide(math::floor(factor * (steps + 1.0f)), steps);
310 float3 result = factor * (to_max - to_min) + to_min;
311 if constexpr (Clamp) {
312 result = clamp_range(result, to_min, to_max);
313 }
314 return result;
315 },
316 mf::build::exec_presets::SomeSpanOrSingle<0>());
317}
318
320{
321 const NodeMapRange &storage = node_storage(builder.node());
322 bool clamp = storage.clamp != 0;
323 int interpolation_type = storage.interpolation_type;
324
325 switch (storage.data_type) {
326 case CD_PROP_FLOAT3:
327 switch (interpolation_type) {
329 if (clamp) {
330 static auto fn = build_vector_linear<true>();
331 builder.set_matching_fn(fn);
332 }
333 else {
334 static auto fn = build_vector_linear<false>();
335 builder.set_matching_fn(fn);
336 }
337 break;
338 }
340 if (clamp) {
341 static auto fn = build_vector_stepped<true>();
342 builder.set_matching_fn(fn);
343 }
344 else {
345 static auto fn = build_vector_stepped<false>();
346 builder.set_matching_fn(fn);
347 }
348 break;
349 }
351 static auto fn = mf::build::SI5_SO<float3, float3, float3, float3, float3, float3>(
352 "Vector Map Range Smoothstep",
353 [](const float3 &value,
354 const float3 &from_min,
355 const float3 &from_max,
356 const float3 &to_min,
357 const float3 &to_max) -> float3 {
358 float3 factor = math::safe_divide(value - from_min, from_max - from_min);
359 clamp_v3(factor, 0.0f, 1.0f);
360 factor = (float3(3.0f) - 2.0f * factor) * (factor * factor);
361 return factor * (to_max - to_min) + to_min;
362 },
363 mf::build::exec_presets::SomeSpanOrSingle<0>());
364 builder.set_matching_fn(fn);
365 break;
366 }
368 static auto fn = mf::build::SI5_SO<float3, float3, float3, float3, float3, float3>(
369 "Vector Map Range Smootherstep",
370 [](const float3 &value,
371 const float3 &from_min,
372 const float3 &from_max,
373 const float3 &to_min,
374 const float3 &to_max) -> float3 {
375 float3 factor = math::safe_divide(value - from_min, from_max - from_min);
376 clamp_v3(factor, 0.0f, 1.0f);
377 factor = factor * factor * factor * (factor * (factor * 6.0f - 15.0f) + 10.0f);
378 return factor * (to_max - to_min) + to_min;
379 },
380 mf::build::exec_presets::SomeSpanOrSingle<0>());
381 builder.set_matching_fn(fn);
382 break;
383 }
384 default:
385 break;
386 }
387 break;
388 case CD_PROP_FLOAT:
389 switch (interpolation_type) {
391 if (clamp) {
392 static auto fn = build_float_linear<true>();
393 builder.set_matching_fn(fn);
394 }
395 else {
396 static auto fn = build_float_linear<false>();
397 builder.set_matching_fn(fn);
398 }
399 break;
400 }
402 if (clamp) {
403 static auto fn = build_float_stepped<true>();
404 builder.set_matching_fn(fn);
405 }
406 else {
407 static auto fn = build_float_stepped<false>();
408 builder.set_matching_fn(fn);
409 }
410 break;
411 }
413 static auto fn = mf::build::SI5_SO<float, float, float, float, float, float>(
414 "Map Range Smoothstep",
415 [](float value, float from_min, float from_max, float to_min, float to_max)
416 -> float {
417 float factor = safe_divide(value - from_min, from_max - from_min);
418 factor = std::clamp(factor, 0.0f, 1.0f);
419 factor = (3.0f - 2.0f * factor) * (factor * factor);
420 return to_min + factor * (to_max - to_min);
421 },
422 mf::build::exec_presets::SomeSpanOrSingle<0>());
423 builder.set_matching_fn(fn);
424 break;
425 }
427 static auto fn = mf::build::SI5_SO<float, float, float, float, float, float>(
428 "Map Range Smoothstep",
429 [](float value, float from_min, float from_max, float to_min, float to_max)
430 -> float {
431 float factor = safe_divide(value - from_min, from_max - from_min);
432 factor = std::clamp(factor, 0.0f, 1.0f);
433 factor = factor * factor * factor * (factor * (factor * 6.0f - 15.0f) + 10.0f);
434 return to_min + factor * (to_max - to_min);
435 },
436 mf::build::exec_presets::SomeSpanOrSingle<0>());
437 builder.set_matching_fn(fn);
438 break;
439 }
440 default:
441 break;
442 }
443 break;
444 }
445}
446
448#ifdef WITH_MATERIALX
449{
450 const NodeMapRange *map_range = static_cast<NodeMapRange *>(node_->storage);
451 NodeItem::Type type;
452 NodeItem value = empty();
453 NodeItem from_min = empty();
454 NodeItem from_max = empty();
455 NodeItem to_min = empty();
456 NodeItem to_max = empty();
457 NodeItem steps = empty();
458 switch (map_range->data_type) {
459 case CD_PROP_FLOAT:
460 type = NodeItem::Type::Float;
461 value = get_input_value("Value", type);
462 from_min = get_input_value(1, type);
463 from_max = get_input_value(2, type);
464 to_min = get_input_value(3, type);
465 to_max = get_input_value(4, type);
466 if (map_range->interpolation_type == NODE_MAP_RANGE_STEPPED) {
467 steps = get_input_value(5, type);
468 }
469 break;
470 case CD_PROP_FLOAT3:
471 type = NodeItem::Type::Vector3;
472 value = get_input_value("Vector", type);
473 from_min = get_input_value(7, type);
474 from_max = get_input_value(8, type);
475 to_min = get_input_value(9, type);
476 to_max = get_input_value(10, type);
477 if (map_range->interpolation_type == NODE_MAP_RANGE_STEPPED) {
478 steps = get_input_value(11, type);
479 }
480 break;
481 default:
483 }
484
485 NodeItem res = empty();
486 switch (map_range->interpolation_type) {
488 res = create_node("remap",
489 type,
490 {{"in", value},
491 {"inlow", from_min},
492 {"inhigh", from_max},
493 {"outlow", to_min},
494 {"outhigh", to_max}});
495 break;
497 NodeItem factor = create_node(
498 "remap", type, {{"in", value}, {"inlow", from_min}, {"inhigh", from_max}});
499 value = (factor * (steps + val(1.0f))).floor() / steps;
500 res = create_node("remap", type, {{"in", value}, {"outlow", to_min}, {"outhigh", to_max}});
501 break;
502 }
505 value = create_node(
506 "smoothstep", type, {{"in", value}, {"low", from_min}, {"high", from_max}});
507 res = create_node("remap", type, {{"in", value}, {"outlow", to_min}, {"outhigh", to_max}});
508 break;
509 default:
511 }
512
513 if (map_range->clamp != 0) {
514 res = res.clamp(to_min, to_max);
515 }
516 return res;
517}
518#endif
520
521} // namespace nodes::node_shader_map_range_cc
522
524{
525 namespace file_ns = nodes::node_shader_map_range_cc;
526
527 static bke::bNodeType ntype;
528
529 common_node_type_base(&ntype, "ShaderNodeMapRange"_ustr, SH_NODE_MAP_RANGE);
530 ntype.ui_name = "Map Range";
531 ntype.ui_description = "Remap a value from a range to a target range";
532 ntype.enum_name_legacy = "MAP_RANGE";
534 ntype.declare = file_ns::sh_node_map_range_declare;
535 ntype.draw_buttons = file_ns::node_shader_buts_map_range;
536 ntype.ui_class = file_ns::node_shader_map_range_ui_class;
537 ntype.initfunc = file_ns::node_shader_init_map_range;
540 ntype.updatefunc = file_ns::node_shader_update_map_range;
541 ntype.gpu_fn = file_ns::gpu_shader_map_range;
542 ntype.build_multi_function = file_ns::sh_node_map_range_build_multi_function;
543 ntype.gather_link_search_ops = file_ns::node_map_range_gather_link_searches;
544 ntype.materialx_fn = file_ns::node_shader_materialx;
546}
547
548} // namespace blender
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:481
#define SH_NODE_MAP_RANGE
#define IFACE_(msgid)
#define BLI_assert_unreachable()
#define ELEM(...)
btScalar max
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void set_matching_fn(const mf::MultiFunction *fn)
#define storage(slot, qualifiers)
#define floor
constexpr T clamp(T, U, U) RET
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:5499
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
T safe_divide(const T &a, const T &b)
T floor(const T &a)
static int gpu_shader_map_range(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_map_range_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_buts_map_range(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_shader_init_map_range(bNodeTree *, bNode *node)
static const char * gpu_shader_get_name(int mode, bool use_vector)
static int node_shader_map_range_ui_class(const bNode *node)
static float clamp_range(const float value, const float min, const float max)
static std::optional< eCustomDataType > node_type_from_other_socket(const bNodeSocket &socket)
static void node_shader_update_map_range(bNodeTree *ntree, bNode *node)
static void node_map_range_gather_link_searches(GatherLinkSearchOpParams &params)
static void sh_node_map_range_declare(NodeDeclarationBuilder &b)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
int RNA_enum_get(PointerRNA *ptr, const char *name)
@ NODE_MAP_RANGE_SMOOTHERSTEP
@ NODE_MAP_RANGE_LINEAR
@ NODE_MAP_RANGE_SMOOTHSTEP
@ NODE_MAP_RANGE_STEPPED
void register_node_type_sh_map_range()
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
BMesh const char void * data
MINLINE void clamp_v3(float vec[3], float min, float max)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
MINLINE float safe_divide(float a, float b)
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define floorf
#define min(a, b)
Definition sort.cc:38
eNodeSocketDatatype type
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
void(*) updatefunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:312
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
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
int(*) ui_class(const bNode *node)
Definition BKE_node.hh:307
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)