Blender V4.5
node_composite_map_value.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 <algorithm>
10
12
13#include "NOD_multi_function.hh"
14
15#include "BKE_texture.h"
16
17#include "RNA_access.hh"
18
19#include "UI_interface.hh"
20#include "UI_resources.hh"
21
22#include "GPU_material.hh"
23
25
26/* **************** MAP VALUE ******************** */
27
29
31
33{
34 b.add_input<decl::Float>("Value")
35 .default_value(1.0f)
36 .min(0.0f)
37 .max(1.0f)
38 .compositor_domain_priority(0);
39 b.add_output<decl::Float>("Value");
40}
41
46
48{
49 uiLayout *sub, *col;
50
51 col = &layout->column(true);
52 col->prop(ptr, "offset", UI_ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
53 col->prop(ptr, "size", UI_ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
54
55 col = &layout->column(true);
56 col->prop(ptr, "use_min", UI_ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
57 sub = &col->column(false);
58 uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_min"));
59 sub->prop(ptr, "min", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
60
61 col = &layout->column(true);
62 col->prop(ptr, "use_max", UI_ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
63 sub = &col->column(false);
64 uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_max"));
65 sub->prop(ptr, "max", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
66}
67
68using namespace blender::compositor;
69
70static bool get_use_min(const bNode &node)
71{
72 return node_storage(node).flag & TEXMAP_CLIP_MIN;
73}
74
75static bool get_use_max(const bNode &node)
76{
77 return node_storage(node).flag & TEXMAP_CLIP_MAX;
78}
79
80static int node_gpu_material(GPUMaterial *material,
81 bNode *node,
82 bNodeExecData * /*execdata*/,
85{
86 const TexMapping &texture_mapping = node_storage(*node);
87
88 const float use_min = get_use_min(*node);
89 const float use_max = get_use_max(*node);
90
91 return GPU_stack_link(material,
92 node,
93 "node_composite_map_value",
94 inputs,
95 outputs,
96 GPU_uniform(texture_mapping.loc),
97 GPU_uniform(texture_mapping.size),
98 GPU_constant(&use_min),
99 GPU_uniform(texture_mapping.min),
100 GPU_constant(&use_max),
101 GPU_uniform(texture_mapping.max));
102}
103
104template<bool UseMin, bool UseMax>
105static float map_value(
106 const float value, const float offset, const float size, const float min, const float max)
107{
108 float result = (value + offset) * size;
109
110 if constexpr (UseMin) {
111 result = std::max(result, min);
112 }
113
114 if constexpr (UseMax) {
115 result = std::min(result, max);
116 }
117
118 return result;
119}
120
122{
123 const TexMapping &texture_mapping = node_storage(builder.node());
124 const float offset = texture_mapping.loc[0];
125 const float size = texture_mapping.size[0];
126 const float min = texture_mapping.min[0];
127 const float max = texture_mapping.max[0];
128 const bool use_min = get_use_min(builder.node());
129 const bool use_max = get_use_max(builder.node());
130
131 if (use_min) {
132 if (use_max) {
134 return mf::build::SI1_SO<float, float>(
135 "Map Value With Min With Max",
136 [=](const float value) -> float {
137 return map_value<true, true>(value, offset, size, min, max);
138 },
139 mf::build::exec_presets::AllSpanOrSingle());
140 });
141 }
142 else {
144 return mf::build::SI1_SO<float, float>(
145 "Map Value With Min No Max",
146 [=](const float value) -> float {
147 return map_value<true, false>(value, offset, size, min, max);
148 },
149 mf::build::exec_presets::AllSpanOrSingle());
150 });
151 }
152 }
153 else {
154 if (use_max) {
156 return mf::build::SI1_SO<float, float>(
157 "Map Value No Min With Max",
158 [=](const float value) -> float {
159 return map_value<false, true>(value, offset, size, min, max);
160 },
161 mf::build::exec_presets::AllSpanOrSingle());
162 });
163 }
164 else {
166 return mf::build::SI1_SO<float, float>(
167 "Map Value No Min No Max",
168 [=](const float value) -> float {
169 return map_value<false, false>(value, offset, size, min, max);
170 },
171 mf::build::exec_presets::AllSpanOrSingle());
172 });
173 }
174 }
175}
176
177} // namespace blender::nodes::node_composite_map_value_cc
178
180{
182
183 static blender::bke::bNodeType ntype;
184
185 cmp_node_type_base(&ntype, "CompositorNodeMapValue", CMP_NODE_MAP_VALUE);
186 ntype.ui_name = "Map Value";
187 ntype.ui_description = "Scale, offset and clamp values";
188 ntype.enum_name_legacy = "MAP_VALUE";
190 ntype.declare = file_ns::cmp_node_map_value_declare;
191 ntype.draw_buttons = file_ns::node_composit_buts_map_value;
192 ntype.initfunc = file_ns::node_composit_init_map_value;
195 ntype.gpu_fn = file_ns::node_gpu_material;
196 ntype.build_multi_function = file_ns::node_build_multi_function;
197 ntype.gather_link_search_ops = nullptr;
198
200}
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1215
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:436
#define CMP_NODE_MAP_VALUE
struct TexMapping * BKE_texture_mapping_add(int type)
Definition texture.cc:229
@ TEXMAP_TYPE_POINT
@ TEXMAP_CLIP_MIN
@ TEXMAP_CLIP_MAX
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
GPUNodeLink * GPU_uniform(const float *num)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ UI_ITEM_R_SPLIT_EMPTY_NAME
void uiLayoutSetActive(uiLayout *layout, bool active)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
uint col
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
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:5603
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void node_composit_init_map_value(bNodeTree *, bNode *node)
static void node_composit_buts_map_value(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static float map_value(const float value, const float offset, const float size, const float min, const float max)
static void cmp_node_map_value_declare(NodeDeclarationBuilder &b)
static void register_node_type_cmp_map_value()
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[]
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
#define min(a, b)
Definition sort.cc:36
void * storage
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
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)
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:4226