Blender V4.5
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_->name, "Red") ? 0 : STREQ(socket_out_->name, "Green") ? 1 : 2;
89 return convert[index];
90}
91#endif
93
94} // namespace blender::nodes::node_shader_separate_color_cc
95
97{
99
100 static blender::bke::bNodeType ntype;
101
102 sh_node_type_base(&ntype, "ShaderNodeSeparateColor", SH_NODE_SEPARATE_COLOR);
103 ntype.ui_name = "Separate Color";
104 ntype.ui_description = "Split a color into its individual components using multiple models";
105 ntype.enum_name_legacy = "SEPARATE_COLOR";
107 ntype.declare = file_ns::sh_node_sepcolor_declare;
108 ntype.updatefunc = file_ns::node_sepcolor_update;
111 ntype, "NodeCombSepColor", node_free_standard_storage, node_copy_standard_storage);
112 ntype.gpu_fn = file_ns::gpu_shader_sepcolor;
113 ntype.materialx_fn = file_ns::node_shader_materialx;
114
116}
117
118/* **************** COMBINE COLOR ******************** */
119
121
123
125{
126 b.add_input<decl::Float>("Red").default_value(0.0f).min(0.0f).max(1.0f).subtype(PROP_FACTOR);
127 b.add_input<decl::Float>("Green").default_value(0.0f).min(0.0f).max(1.0f).subtype(PROP_FACTOR);
128 b.add_input<decl::Float>("Blue").default_value(0.0f).min(0.0f).max(1.0f).subtype(PROP_FACTOR);
129 b.add_output<decl::Color>("Color");
130}
131
132static void node_combcolor_update(bNodeTree * /*ntree*/, bNode *node)
133{
134 const NodeCombSepColor &storage = node_storage(*node);
136}
137
138static const char *gpu_shader_get_name(int mode)
139{
140 switch (mode) {
142 return "combine_color_rgb";
144 return "combine_color_hsv";
146 return "combine_color_hsl";
147 }
148
149 return nullptr;
150}
151
153 bNode *node,
154 bNodeExecData * /*execdata*/,
157{
158 const NodeCombSepColor &storage = node_storage(*node);
159 const char *name = gpu_shader_get_name(storage.mode);
160 if (name != nullptr) {
161 return GPU_stack_link(mat, node, name, in, out);
162 }
163
164 return 0;
165}
166
168#ifdef WITH_MATERIALX
169{
170 int mode = static_cast<NodeCombSepColor *>(node_->storage)->mode;
171 NodeItem red = get_input_value("Red", NodeItem::Type::Float);
172 NodeItem green = get_input_value("Green", NodeItem::Type::Float);
173 NodeItem blue = get_input_value("Blue", NodeItem::Type::Float);
174
175 NodeItem combine = create_node("combine3", NodeItem::Type::Color3);
176 combine.set_input("in1", red);
177 combine.set_input("in2", green);
178 combine.set_input("in3", blue);
179
180 NodeItem res = empty();
181 switch (mode) {
183 res = combine;
184 break;
187 /* NOTE: HSL is unsupported color model, using HSV instead */
188 res = create_node("hsvtorgb", NodeItem::Type::Color3);
189 res.set_input("in", combine);
190 break;
191 default:
193 }
194 return res;
195}
196#endif
198
199} // namespace blender::nodes::node_shader_combine_color_cc
200
202{
204
205 static blender::bke::bNodeType ntype;
206
207 sh_node_type_base(&ntype, "ShaderNodeCombineColor", SH_NODE_COMBINE_COLOR);
208 ntype.ui_name = "Combine Color";
209 ntype.ui_description = "Create a color from individual components using multiple models";
210 ntype.enum_name_legacy = "COMBINE_COLOR";
212 ntype.declare = file_ns::sh_node_combcolor_declare;
213 ntype.updatefunc = file_ns::node_combcolor_update;
216 ntype, "NodeCombSepColor", node_free_standard_storage, node_copy_standard_storage);
217 ntype.gpu_fn = file_ns::gpu_shader_combcolor;
218 ntype.materialx_fn = file_ns::node_shader_materialx;
219
221}
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:439
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1215
#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:239
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: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
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:240
#define min(a, b)
Definition sort.cc:36
ListBase inputs
void * storage
ListBase outputs
Defines a node type.
Definition BKE_node.hh:226
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:332
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
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355
void(* updatefunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:269
max
Definition text_draw.cc:251