Blender V5.3
node_shader_sepcomb_color.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "node_shader_util.hh"
10#include "node_util.hh"
11
12namespace blender {
13
15{
16 NodeCombSepColor *data = MEM_new<NodeCombSepColor>(__func__);
18 node->storage = data;
19}
20
21/* **************** SEPARATE COLOR ******************** */
22
24
26
28{
29 b.add_input<decl::Color>("Color"_ustr).default_value({0.8f, 0.8f, 0.8f, 1.0f});
30 b.add_output<decl::Float>("Red"_ustr).label_fn([](const bNode &node) {
31 switch (node_storage(node).mode) {
33 default:
34 return IFACE_("Red");
37 return IFACE_("Hue");
38 }
39 });
40 b.add_output<decl::Float>("Green"_ustr).label_fn([](const bNode &node) {
41 switch (node_storage(node).mode) {
43 default:
44 return IFACE_("Green");
47 return IFACE_("Saturation");
48 }
49 });
50 b.add_output<decl::Float>("Blue"_ustr).label_fn([](const bNode &node) {
51 switch (node_storage(node).mode) {
53 default:
54 return IFACE_("Blue");
56 return IFACE_("Lightness");
58 return CTX_IFACE_(BLT_I18NCONTEXT_COLOR, "Value");
59 }
60 });
61}
62
63static const char *gpu_shader_get_name(int mode)
64{
65 switch (mode) {
67 return "separate_color_rgb";
69 return "separate_color_hsv";
71 return "separate_color_hsl";
72 }
73
74 return nullptr;
75}
76
78 bNode *node,
79 bNodeExecData * /*execdata*/,
82{
83 const NodeCombSepColor &storage = node_storage(*node);
84 const char *name = gpu_shader_get_name(storage.mode);
85 if (name != nullptr) {
86 return GPU_stack_link(mat, node, name, in, out);
87 }
88
89 return 0;
90}
91
93#ifdef WITH_MATERIALX
94{
95 int mode = static_cast<NodeCombSepColor *>(node_->storage)->mode;
96 NodeItem color = get_input_value("Color", NodeItem::Type::Color3);
97
98 NodeItem convert = empty();
99 switch (mode) {
101 convert = color;
102 break;
105 /* NOTE: HSL is unsupported color model, using HSV instead */
106 convert = create_node("rgbtohsv", NodeItem::Type::Color3, {{"in", color}});
107 break;
108 default:
110 }
111
112 int index = STREQ(socket_out_->identifier, "Red") ? 0 :
113 STREQ(socket_out_->identifier, "Green") ? 1 :
114 2;
115 return convert[index];
116}
117#endif
119
120} // namespace nodes::node_shader_separate_color_cc
121
123{
124 namespace file_ns = nodes::node_shader_separate_color_cc;
125
126 static bke::bNodeType ntype;
127
128 sh_node_type_base(&ntype, "ShaderNodeSeparateColor"_ustr, SH_NODE_SEPARATE_COLOR);
129 ntype.ui_name = "Separate Color";
130 ntype.ui_description = "Split a color into its individual components using multiple models";
131 ntype.enum_name_legacy = "SEPARATE_COLOR";
133 ntype.declare = file_ns::sh_node_sepcolor_declare;
136 ntype, "NodeCombSepColor", node_free_standard_storage, node_copy_standard_storage);
137 ntype.gpu_fn = file_ns::gpu_shader_sepcolor;
138 ntype.materialx_fn = file_ns::node_shader_materialx;
139
141}
142
143/* **************** COMBINE COLOR ******************** */
144
146
148
150{
151 b.add_input<decl::Float>("Red"_ustr)
152 .default_value(0.0f)
153 .min(0.0f)
154 .max(1.0f)
155 .subtype(PROP_FACTOR)
156 .label_fn([](const bNode &node) {
157 switch (node_storage(node).mode) {
159 default:
160 return IFACE_("Red");
163 return IFACE_("Hue");
164 }
165 });
166 b.add_input<decl::Float>("Green"_ustr)
167 .default_value(0.0f)
168 .min(0.0f)
169 .max(1.0f)
170 .subtype(PROP_FACTOR)
171 .label_fn([](const bNode &node) {
172 switch (node_storage(node).mode) {
174 default:
175 return IFACE_("Green");
178 return IFACE_("Saturation");
179 }
180 });
181 b.add_input<decl::Float>("Blue"_ustr)
182 .default_value(0.0f)
183 .min(0.0f)
184 .max(1.0f)
185 .subtype(PROP_FACTOR)
186 .label_fn([](const bNode &node) {
187 switch (node_storage(node).mode) {
189 default:
190 return IFACE_("Blue");
192 return IFACE_("Lightness");
194 return CTX_IFACE_(BLT_I18NCONTEXT_COLOR, "Value");
195 }
196 });
197 b.add_output<decl::Color>("Color"_ustr);
198}
199
200static const char *gpu_shader_get_name(int mode)
201{
202 switch (mode) {
204 return "combine_color_rgb";
206 return "combine_color_hsv";
208 return "combine_color_hsl";
209 }
210
211 return nullptr;
212}
213
215 bNode *node,
216 bNodeExecData * /*execdata*/,
219{
220 const NodeCombSepColor &storage = node_storage(*node);
221 const char *name = gpu_shader_get_name(storage.mode);
222 if (name != nullptr) {
223 return GPU_stack_link(mat, node, name, in, out);
224 }
225
226 return 0;
227}
228
230#ifdef WITH_MATERIALX
231{
232 int mode = static_cast<NodeCombSepColor *>(node_->storage)->mode;
233 NodeItem red = get_input_value("Red", NodeItem::Type::Float);
234 NodeItem green = get_input_value("Green", NodeItem::Type::Float);
235 NodeItem blue = get_input_value("Blue", NodeItem::Type::Float);
236
237 NodeItem combine = create_node("combine3", NodeItem::Type::Color3);
238 combine.set_input("in1", red);
239 combine.set_input("in2", green);
240 combine.set_input("in3", blue);
241
242 NodeItem res = empty();
243 switch (mode) {
245 res = combine;
246 break;
249 /* NOTE: HSL is unsupported color model, using HSV instead */
250 res = create_node("hsvtorgb", NodeItem::Type::Color3);
251 res.set_input("in", combine);
252 break;
253 default:
255 }
256 return res;
257}
258#endif
260
261} // namespace nodes::node_shader_combine_color_cc
262
264{
265 namespace file_ns = nodes::node_shader_combine_color_cc;
266
267 static bke::bNodeType ntype;
268
269 sh_node_type_base(&ntype, "ShaderNodeCombineColor"_ustr, SH_NODE_COMBINE_COLOR);
270 ntype.ui_name = "Combine Color";
271 ntype.ui_description = "Create a color from individual components using multiple models";
272 ntype.enum_name_legacy = "COMBINE_COLOR";
274 ntype.declare = file_ns::sh_node_combcolor_declare;
277 ntype, "NodeCombSepColor", node_free_standard_storage, node_copy_standard_storage);
278 ntype.gpu_fn = file_ns::gpu_shader_combcolor;
279 ntype.materialx_fn = file_ns::node_shader_materialx;
280
282}
283
284} // namespace blender
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define SH_NODE_COMBINE_COLOR
#define SH_NODE_SEPARATE_COLOR
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_COLOR
#define BLI_assert_unreachable()
#define STREQ(a, b)
btScalar max
std::unique_ptr< CustomSocketLabelFn > label_fn
#define storage(slot, qualifiers)
#define index(i)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
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
static void sh_node_combcolor_declare(NodeDeclarationBuilder &b)
static int gpu_shader_combcolor(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static int gpu_shader_sepcolor(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_sepcolor_declare(NodeDeclarationBuilder &b)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
static void node_combsep_color_init(bNodeTree *, bNode *node)
@ NODE_COMBSEP_COLOR_HSL
@ NODE_COMBSEP_COLOR_HSV
@ NODE_COMBSEP_COLOR_RGB
void register_node_type_sh_sepcolor()
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
BMesh const char void * data
void register_node_type_sh_combcolor()
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
@ PROP_FACTOR
Definition RNA_types.hh:308
void convert(DstType &, const SrcType)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define min(a, b)
Definition sort.cc:38
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393