Blender V5.0
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
12static void node_combsep_color_init(bNodeTree * /*tree*/, bNode *node)
13{
16 node->storage = data;
17}
18
19/* **************** SEPARATE COLOR ******************** */
20
22
24
26{
27 b.add_input<decl::Color>("Color").default_value({0.8f, 0.8f, 0.8f, 1.0f});
28 b.add_output<decl::Float>("Red");
29 b.add_output<decl::Float>("Green");
30 b.add_output<decl::Float>("Blue");
31}
32
33static void node_sepcolor_update(bNodeTree * /*ntree*/, bNode *node)
34{
35 const NodeCombSepColor &storage = node_storage(*node);
37}
38
39static const char *gpu_shader_get_name(int mode)
40{
41 switch (mode) {
43 return "separate_color_rgb";
45 return "separate_color_hsv";
47 return "separate_color_hsl";
48 }
49
50 return nullptr;
51}
52
54 bNode *node,
55 bNodeExecData * /*execdata*/,
58{
59 const NodeCombSepColor &storage = node_storage(*node);
60 const char *name = gpu_shader_get_name(storage.mode);
61 if (name != nullptr) {
62 return GPU_stack_link(mat, node, name, in, out);
63 }
64
65 return 0;
66}
67
69#ifdef WITH_MATERIALX
70{
71 int mode = static_cast<NodeCombSepColor *>(node_->storage)->mode;
72 NodeItem color = get_input_value("Color", NodeItem::Type::Color3);
73
74 NodeItem convert = empty();
75 switch (mode) {
77 convert = color;
78 break;
81 /* NOTE: HSL is unsupported color model, using HSV instead */
82 convert = create_node("rgbtohsv", NodeItem::Type::Color3, {{"in", color}});
83 break;
84 default:
86 }
87
88 int index = STREQ(socket_out_->identifier, "Red") ? 0 :
89 STREQ(socket_out_->identifier, "Green") ? 1 :
90 2;
91 return convert[index];
92}
93#endif
95
96} // namespace blender::nodes::node_shader_separate_color_cc
97
99{
101
102 static blender::bke::bNodeType ntype;
103
104 sh_node_type_base(&ntype, "ShaderNodeSeparateColor", SH_NODE_SEPARATE_COLOR);
105 ntype.ui_name = "Separate Color";
106 ntype.ui_description = "Split a color into its individual components using multiple models";
107 ntype.enum_name_legacy = "SEPARATE_COLOR";
109 ntype.declare = file_ns::sh_node_sepcolor_declare;
110 ntype.updatefunc = file_ns::node_sepcolor_update;
113 ntype, "NodeCombSepColor", node_free_standard_storage, node_copy_standard_storage);
114 ntype.gpu_fn = file_ns::gpu_shader_sepcolor;
115 ntype.materialx_fn = file_ns::node_shader_materialx;
116
118}
119
120/* **************** COMBINE COLOR ******************** */
121
123
125
127{
128 b.add_input<decl::Float>("Red").default_value(0.0f).min(0.0f).max(1.0f).subtype(PROP_FACTOR);
129 b.add_input<decl::Float>("Green").default_value(0.0f).min(0.0f).max(1.0f).subtype(PROP_FACTOR);
130 b.add_input<decl::Float>("Blue").default_value(0.0f).min(0.0f).max(1.0f).subtype(PROP_FACTOR);
131 b.add_output<decl::Color>("Color");
132}
133
134static void node_combcolor_update(bNodeTree * /*ntree*/, bNode *node)
135{
136 const NodeCombSepColor &storage = node_storage(*node);
138}
139
140static const char *gpu_shader_get_name(int mode)
141{
142 switch (mode) {
144 return "combine_color_rgb";
146 return "combine_color_hsv";
148 return "combine_color_hsl";
149 }
150
151 return nullptr;
152}
153
155 bNode *node,
156 bNodeExecData * /*execdata*/,
159{
160 const NodeCombSepColor &storage = node_storage(*node);
161 const char *name = gpu_shader_get_name(storage.mode);
162 if (name != nullptr) {
163 return GPU_stack_link(mat, node, name, in, out);
164 }
165
166 return 0;
167}
168
170#ifdef WITH_MATERIALX
171{
172 int mode = static_cast<NodeCombSepColor *>(node_->storage)->mode;
173 NodeItem red = get_input_value("Red", NodeItem::Type::Float);
174 NodeItem green = get_input_value("Green", NodeItem::Type::Float);
175 NodeItem blue = get_input_value("Blue", NodeItem::Type::Float);
176
177 NodeItem combine = create_node("combine3", NodeItem::Type::Color3);
178 combine.set_input("in1", red);
179 combine.set_input("in2", green);
180 combine.set_input("in3", blue);
181
182 NodeItem res = empty();
183 switch (mode) {
185 res = combine;
186 break;
189 /* NOTE: HSL is unsupported color model, using HSV instead */
190 res = create_node("hsvtorgb", NodeItem::Type::Color3);
191 res.set_input("in", combine);
192 break;
193 default:
195 }
196 return res;
197}
198#endif
200
201} // namespace blender::nodes::node_shader_combine_color_cc
202
204{
206
207 static blender::bke::bNodeType ntype;
208
209 sh_node_type_base(&ntype, "ShaderNodeCombineColor", SH_NODE_COMBINE_COLOR);
210 ntype.ui_name = "Combine Color";
211 ntype.ui_description = "Create a color from individual components using multiple models";
212 ntype.enum_name_legacy = "COMBINE_COLOR";
214 ntype.declare = file_ns::sh_node_combcolor_declare;
215 ntype.updatefunc = file_ns::node_combcolor_update;
218 ntype, "NodeCombSepColor", node_free_standard_storage, node_copy_standard_storage);
219 ntype.gpu_fn = file_ns::gpu_shader_combcolor;
220 ntype.materialx_fn = file_ns::node_shader_materialx;
221
223}
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1240
#define SH_NODE_COMBINE_COLOR
#define SH_NODE_SEPARATE_COLOR
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define STREQ(a, b)
NodeCombSepColorMode
@ NODE_COMBSEP_COLOR_RGB
@ NODE_COMBSEP_COLOR_HSV
@ NODE_COMBSEP_COLOR_HSL
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
@ PROP_FACTOR
Definition RNA_types.hh:251
BMesh const char void * data
#define in
#define out
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
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:5414
void convert(SignedNormalized< StorageType > &dst, const F32 &src)
static void node_combcolor_update(bNodeTree *, bNode *node)
static void sh_node_combcolor_declare(NodeDeclarationBuilder &b)
static int gpu_shader_combcolor(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_sepcolor_update(bNodeTree *, bNode *node)
static int gpu_shader_sepcolor(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_sepcolor_declare(NodeDeclarationBuilder &b)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void register_node_type_sh_sepcolor()
static void node_combsep_color_init(bNodeTree *, bNode *node)
void register_node_type_sh_combcolor()
void sh_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
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
void node_combsep_color_label(const ListBase *sockets, NodeCombSepColorMode mode)
Definition node_util.cc:227
const char * name
#define min(a, b)
Definition sort.cc:36
ListBase inputs
void * storage
ListBase outputs
Defines a node type.
Definition BKE_node.hh:238
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:344
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:342
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
void(* updatefunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:281
max
Definition text_draw.cc:251