37 b.add_input<
decl::Float>(
"Value"_ustr).min(-10000.0f).
max(10000.0f).default_value(1.0f);
39 b.add_input<
decl::Float>(
"From Max"_ustr).min(-10000.0f).
max(10000.0f).default_value(1.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);
44 b.add_input<
decl::Vector>(
"From Min"_ustr,
"From_Min_FLOAT3"_ustr);
46 b.add_input<
decl::Vector>(
"To Min"_ustr,
"To_Min_FLOAT3"_ustr);
84 for (
const auto [
index, socket] :
node->inputs.enumerate()) {
85 new_input_availability[
index] = socket.type == type;
87 for (
const auto [
index, socket] :
node->outputs.enumerate()) {
88 new_output_availability[
index] = socket.type == type;
93 new_input_availability[5] =
false;
96 new_input_availability[11] =
false;
100 for (
const auto [
index, socket] :
node->inputs.enumerate()) {
103 for (
const auto [
index, socket] :
node->outputs.enumerate()) {
114 node->custom1 =
true;
136 switch (socket.
type) {
185 return "vector_map_range_linear";
187 return "vector_map_range_stepped";
189 return "vector_map_range_smoothstep";
191 return "vector_map_range_smootherstep";
197 return "map_range_linear";
199 return "map_range_stepped";
201 return "map_range_smoothstep";
203 return "map_range_smootherstep";
221 if (name !=
nullptr) {
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) {
259 mf::build::exec_presets::SomeSpanOrSingle<0>());
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)
268 float factor =
safe_divide(value - from_min, from_max - from_min);
270 float result = to_min + factor * (to_max - to_min);
271 if constexpr (Clamp) {
276 mf::build::exec_presets::SomeSpanOrSingle<0>());
281 return mf::build::SI5_SO<float3, float3, float3, float3, float3, float3>(
282 Clamp ?
"Vector Map Range (clamped)" :
"Vector Map Range (unclamped)",
290 if constexpr (Clamp) {
295 mf::build::exec_presets::SomeSpanOrSingle<0>());
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)",
311 if constexpr (Clamp) {
316 mf::build::exec_presets::SomeSpanOrSingle<0>());
323 int interpolation_type =
storage.interpolation_type;
327 switch (interpolation_type) {
351 static auto fn = mf::build::SI5_SO<float3, float3, float3, float3, float3, float3>(
352 "Vector Map Range Smoothstep",
360 factor = (
float3(3.0f) - 2.0f * factor) * (factor * factor);
361 return factor * (to_max - to_min) + to_min;
363 mf::build::exec_presets::SomeSpanOrSingle<0>());
368 static auto fn = mf::build::SI5_SO<float3, float3, float3, float3, float3, float3>(
369 "Vector Map Range Smootherstep",
377 factor = factor * factor * factor * (factor * (factor * 6.0f - 15.0f) + 10.0f);
378 return factor * (to_max - to_min) + to_min;
380 mf::build::exec_presets::SomeSpanOrSingle<0>());
389 switch (interpolation_type) {
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)
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);
422 mf::build::exec_presets::SomeSpanOrSingle<0>());
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)
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);
436 mf::build::exec_presets::SomeSpanOrSingle<0>());
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();
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);
467 steps = get_input_value(5, type);
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);
478 steps = get_input_value(11, type);
485 NodeItem res = empty();
488 res = create_node(
"remap",
492 {
"inhigh", from_max},
494 {
"outhigh", to_max}});
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}});
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}});
513 if (map_range->
clamp != 0) {
514 res = res.clamp(to_min, to_max);
531 ntype.
ui_description =
"Remap a value from a range to a target 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;
#define NODE_CLASS_CONVERTER
#define NODE_STORAGE_FUNCS(StorageT)
#define NODE_CLASS_OP_VECTOR
#define SH_NODE_MAP_RANGE
#define BLI_assert_unreachable()
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
const bNodeSocket & other_socket() const
eNodeSocketInOut in_out() const
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
bNode & add_node(UString idname)
void set_matching_fn(const mf::MultiFunction *fn)
void operator()(LinkSearchOpParams ¶ms)
eCustomDataType data_type
#define storage(slot, qualifiers)
constexpr T clamp(T, U, U) RET
void node_register_type(bNodeType &ntype)
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
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))
T safe_divide(const T &a, const T &b)
static int gpu_shader_map_range(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static auto build_vector_linear()
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 auto build_vector_stepped()
static void node_shader_init_map_range(bNodeTree *, bNode *node)
static auto build_float_linear()
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 ¶ms)
static void sh_node_map_range_declare(NodeDeclarationBuilder &b)
static auto build_float_stepped()
@ ITEM_R_SPLIT_EMPTY_NAME
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_SMOOTHSTEP
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)
void node_free_standard_storage(bNode *node)
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
uint8_t interpolation_type
NodeMaterialXFunction materialx_fn
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
std::string ui_description
void(*) updatefunc(bNodeTree *ntree, bNode *node)
NodeGPUExecFunction gpu_fn
NodeMultiFunctionBuildFunction build_multi_function
void(*) initfunc(bNodeTree *ntree, bNode *node)
const char * enum_name_legacy
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
NodeDeclareFunction declare
int(*) ui_class(const bNode *node)
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)