Blender V5.0
node_composite_color_spill.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
10
12
13#include "NOD_multi_function.hh"
14
15#include "GPU_material.hh"
16
18
20
21enum class RGBChannel : uint8_t {
22 R = 0,
23 G = 1,
24 B = 2,
25};
26
28 {int(RGBChannel::R), "R", 0, "R", ""},
29 {int(RGBChannel::G), "G", 0, "G", ""},
30 {int(RGBChannel::B), "B", 0, "B", ""},
31 {0, nullptr, 0, nullptr, nullptr},
32};
33
36 "SINGLE",
37 0,
38 N_("Single"),
39 N_("Limit by a single channel")},
41 "AVERAGE",
42 0,
43 N_("Average"),
44 N_("Limit by the average of the other two channels")},
45 {0, nullptr, 0, nullptr, nullptr},
46};
47
49{
50 b.is_function_node();
51 b.use_custom_socket_order();
52
53 b.add_output<decl::Color>("Image");
54
55 b.add_input<decl::Color>("Image").default_value({1.0f, 1.0f, 1.0f, 1.0f});
56 b.add_input<decl::Float>("Factor", "Fac")
57 .default_value(1.0f)
58 .min(0.0f)
59 .max(1.0f)
61 b.add_input<decl::Menu>("Spill Channel")
62 .default_value(RGBChannel::G)
63 .static_items(rgb_channel_items)
64 .expanded()
66 .optional_label();
67 b.add_input<decl::Menu>("Limit Method")
69 .static_items(limit_method_items)
70 .expanded()
72 b.add_input<decl::Menu>("Limit Channel")
73 .default_value(RGBChannel::R)
74 .static_items(rgb_channel_items)
75 .expanded()
77 .optional_label()
78 .usage_by_menu("Limit Method", CMP_NODE_COLOR_SPILL_LIMIT_ALGORITHM_SINGLE);
79 b.add_input<decl::Float>("Limit Strength")
80 .default_value(1.0f)
82 .min(0.0f)
83 .max(2.0f)
84 .description("Specifies the limiting strength of the limit channel");
85
86 PanelDeclarationBuilder &use_spill_strength_panel =
87 b.add_panel("Spill Strength").default_closed(true);
88 use_spill_strength_panel.add_input<decl::Bool>("Use Spill Strength")
89 .default_value(false)
90 .panel_toggle()
92 "If enabled, the spill strength for each color channel can be specified. If disabled, "
93 "the spill channel will have a unit scale, while other channels will be zero");
94 use_spill_strength_panel.add_input<decl::Color>("Strength", "Spill Strength")
95 .default_value({0.0f, 1.0f, 0.0f, 1.0f})
96 .description("Specifies the spilling strength of each color channel");
97}
98
99static void node_composit_init_color_spill(bNodeTree * /*ntree*/, bNode *node)
100{
101 /* Unused, but allocated for forward compatibility. */
102 node->storage = MEM_callocN<NodeColorspill>(__func__);
103}
104
105using namespace blender::compositor;
106
107static int node_gpu_material(GPUMaterial *material,
108 bNode *node,
109 bNodeExecData * /*execdata*/,
112{
113 return GPU_stack_link(material, node, "node_composite_color_spill", inputs, outputs);
114}
115
116/* Compute the indices of the channels used to compute the limit value. We always assume the limit
117 * algorithm is Average, if it is a single limit channel, store it in both limit channels, because
118 * the average of two identical values is the same value. */
120 const int spill_channel,
121 const int limit_channel)
122{
123 /* If the algorithm is Average, store the indices of the other two channels other than the spill
124 * channel. */
126 return int2((spill_channel + 1) % 3, (spill_channel + 2) % 3);
127 }
128
129 /* If the algorithm is Single, store the index of the limit channel in both channels. */
130 return int2(limit_channel);
131}
132
133static float3 compute_spill_scale(const bool use_spill_strength,
134 const float4 spill_strength,
135 const int spill_channel)
136{
137 if (use_spill_strength) {
138 float3 scale = spill_strength.xyz();
139 scale[spill_channel] *= -1.0f;
140 return scale;
141 }
142
143 float3 scale = float3(0.0f);
144 scale[spill_channel] = -1.0f;
145 return scale;
146}
147
149 const float factor,
150 const int spill_channel,
151 const CMPNodeColorSpillLimitAlgorithm limit_method,
152 const int limit_channel,
153 const float limit_scale,
154 const bool use_spill_strength,
155 const float4 spill_strength)
156{
157 const int2 limit_channels = compute_limit_channels(limit_method, spill_channel, limit_channel);
158 const float average_limit = (color[limit_channels.x] + color[limit_channels.y]) / 2.0f;
159 const float map = factor * color[spill_channel] - limit_scale * average_limit;
160 const float3 spill_scale = compute_spill_scale(
161 use_spill_strength, spill_strength, spill_channel);
162 return float4(map > 0.0f ? color.xyz() + spill_scale * map : color.xyz(), color.w);
163}
164
166{
167 static auto function = mf::build::
168 SI8_SO<float4, float, MenuValue, MenuValue, MenuValue, float, bool, float4, float4>(
169 "Color Spill",
170 [=](const float4 &color,
171 const float &factor,
172 const MenuValue spill_channel,
173 const MenuValue limit_method,
174 const MenuValue limit_channel,
175 const float &limit_scale,
176 const bool &use_spill_strength,
177 const float4 &spill_strength) -> float4 {
178 return color_spill(color,
179 factor,
180 spill_channel.value,
182 limit_channel.value,
183 limit_scale,
184 use_spill_strength,
185 spill_strength);
186 },
187 mf::build::exec_presets::SomeSpanOrSingle<0>());
188 builder.set_matching_fn(function);
189}
190
191} // namespace blender::nodes::node_composite_color_spill_cc
192
194{
196
197 static blender::bke::bNodeType ntype;
198
199 cmp_node_type_base(&ntype, "CompositorNodeColorSpill", CMP_NODE_COLOR_SPILL);
200 ntype.ui_name = "Color Spill";
201 ntype.ui_description =
202 "Remove colors from a blue or green screen, by reducing one RGB channel compared to the "
203 "others";
204 ntype.enum_name_legacy = "COLOR_SPILL";
205 ntype.nclass = NODE_CLASS_MATTE;
206 ntype.declare = file_ns::cmp_node_color_spill_declare;
207 ntype.initfunc = file_ns::node_composit_init_color_spill;
210 ntype.gpu_fn = file_ns::node_gpu_material;
211 ntype.build_multi_function = file_ns::node_build_multi_function;
213
215}
#define NODE_CLASS_MATTE
Definition BKE_node.hh:454
constexpr int NODE_DEFAULT_MAX_WIDTH
Definition BKE_node.hh:1250
#define CMP_NODE_COLOR_SPILL
#define BLT_I18NCONTEXT_COLOR
CMPNodeColorSpillLimitAlgorithm
@ CMP_NODE_COLOR_SPILL_LIMIT_ALGORITHM_AVERAGE
@ CMP_NODE_COLOR_SPILL_LIMIT_ALGORITHM_SINGLE
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_FACTOR
Definition RNA_types.hh:251
DeclType::Builder & add_input(StringRef name, StringRef identifier="")
void set_matching_fn(const mf::MultiFunction *fn)
std::optional< std::string > translation_context
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
#define B
#define R
#define G(x, y, z)
void node_type_size(bNodeType &ntype, int width, int minwidth, int maxwidth)
Definition node.cc:5384
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
static float4 color_spill(const float4 color, const float factor, const int spill_channel, const CMPNodeColorSpillLimitAlgorithm limit_method, const int limit_channel, const float limit_scale, const bool use_spill_strength, const float4 spill_strength)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static int2 compute_limit_channels(const CMPNodeColorSpillLimitAlgorithm limit_method, const int spill_channel, const int limit_channel)
static void node_composit_init_color_spill(bNodeTree *, bNode *node)
static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static float3 compute_spill_scale(const bool use_spill_strength, const float4 spill_strength, const int spill_channel)
static void cmp_node_color_spill_declare(NodeDeclarationBuilder &b)
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 3 > float3
static void register_node_type_cmp_color_spill()
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
void * storage
VecBase< T, 3 > xyz() const
Defines a node type.
Definition BKE_node.hh:238
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
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
#define N_(msgid)