Blender V5.3
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
6
8
10
11#include "GPU_material.hh"
12
13#include "COM_result.hh"
14
16
18
19enum class RGBChannel : uint8_t {
20 R = 0,
21 G = 1,
22 B = 2,
23};
24
26 {int(RGBChannel::R), "R", 0, "R", ""},
27 {int(RGBChannel::G), "G", 0, "G", ""},
28 {int(RGBChannel::B), "B", 0, "B", ""},
29 {0, nullptr, 0, nullptr, nullptr},
30};
31
34 "SINGLE",
35 0,
36 N_("Single"),
37 N_("Limit by a single channel")},
39 "AVERAGE",
40 0,
41 N_("Average"),
42 N_("Limit by the average of the other two channels")},
43 {0, nullptr, 0, nullptr, nullptr},
44};
45
47{
48 b.is_function_node();
49 b.use_custom_socket_order();
50
51 b.add_output<decl::Color>("Image"_ustr);
52
53 b.add_input<decl::Color>("Image"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f});
54 b.add_input<decl::Float>("Factor"_ustr, "Fac"_ustr)
55 .default_value(1.0f)
56 .min(0.0f)
57 .max(1.0f)
59 b.add_input<decl::Menu>("Spill Channel"_ustr)
60 .default_value(RGBChannel::G)
61 .static_items(rgb_channel_items)
62 .expanded()
64 .optional_label();
65 b.add_input<decl::Menu>("Limit Method"_ustr)
67 .static_items(limit_method_items)
68 .expanded()
70 b.add_input<decl::Menu>("Limit Channel"_ustr)
71 .default_value(RGBChannel::R)
72 .static_items(rgb_channel_items)
73 .expanded()
75 .optional_label()
76 .usage_by_menu("Limit Method"_ustr, CMP_NODE_COLOR_SPILL_LIMIT_ALGORITHM_SINGLE);
77 b.add_input<decl::Float>("Limit Strength"_ustr)
78 .default_value(1.0f)
80 .min(0.0f)
81 .max(2.0f)
82 .description("Specifies the limiting strength of the limit channel");
83
84 PanelDeclarationBuilder &use_spill_strength_panel =
85 b.add_panel("Spill Strength"_ustr).default_closed(true);
86 use_spill_strength_panel.add_input<decl::Bool>("Use Spill Strength"_ustr)
87 .default_value(false)
88 .panel_toggle()
90 "If enabled, the spill strength for each color channel can be specified. If disabled, "
91 "the spill channel will have a unit scale, while other channels will be zero");
92 use_spill_strength_panel.add_input<decl::Color>("Strength"_ustr, "Spill Strength"_ustr)
93 .default_value({0.0f, 1.0f, 0.0f, 1.0f})
94 .description("Specifies the spilling strength of each color channel");
95}
96
97static void node_init(bNodeTree * /*ntree*/, bNode *node)
98{
99 /* Unused, but allocated for forward compatibility. */
100 node->storage = MEM_new<NodeColorspill>(__func__);
101}
102
103using namespace blender::compositor;
104
105static int node_gpu_material(GPUMaterial *material,
106 bNode *node,
107 bNodeExecData * /*execdata*/,
108 GPUNodeStack *inputs,
109 GPUNodeStack *outputs)
110{
111 return GPU_stack_link(material, node, "node_composite_color_spill", inputs, outputs);
112}
113
114/* Compute the indices of the channels used to compute the limit value. We always assume the limit
115 * algorithm is Average, if it is a single limit channel, store it in both limit channels, because
116 * the average of two identical values is the same value. */
118 const int spill_channel,
119 const int limit_channel)
120{
121 /* If the algorithm is Average, store the indices of the other two channels other than the spill
122 * channel. */
124 return int2((spill_channel + 1) % 3, (spill_channel + 2) % 3);
125 }
126
127 /* If the algorithm is Single, store the index of the limit channel in both channels. */
128 return int2(limit_channel);
129}
130
131static float3 compute_spill_scale(const bool use_spill_strength,
132 const float4 spill_strength,
133 const int spill_channel)
134{
135 if (use_spill_strength) {
136 float3 scale = spill_strength.xyz();
137 scale[spill_channel] *= -1.0f;
138 return scale;
139 }
140
141 float3 scale = float3(0.0f);
142 scale[spill_channel] = -1.0f;
143 return scale;
144}
145
147 const float factor,
148 const int spill_channel,
149 const CMPNodeColorSpillLimitAlgorithm limit_method,
150 const int limit_channel,
151 const float limit_scale,
152 const bool use_spill_strength,
153 const float4 spill_strength)
154{
155 const int2 limit_channels = compute_limit_channels(limit_method, spill_channel, limit_channel);
156 const float average_limit = (color[limit_channels.x] + color[limit_channels.y]) / 2.0f;
157 const float map = factor * color[spill_channel] - limit_scale * average_limit;
158 const float3 spill_scale = compute_spill_scale(
159 use_spill_strength, spill_strength, spill_channel);
160 return float4(map > 0.0f ? color.xyz() + spill_scale * map : color.xyz(), color.w);
161}
162
164
166{
167 static auto function =
168 mf::build::SI8_SO<Color, float, MenuValue, MenuValue, MenuValue, float, bool, Color, Color>(
169 "Color Spill",
170 [=](const Color &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 Color &spill_strength) -> Color {
179 factor,
180 spill_channel.value,
182 limit_channel.value,
183 limit_scale,
184 use_spill_strength,
185 float4(spill_strength)));
186 },
187 mf::build::exec_presets::SomeSpanOrSingle<0>());
188 builder.set_matching_fn(function);
189}
190
191static void node_register()
192{
193 static bke::bNodeType ntype;
194
195 cmp_node_type_base(&ntype, "CompositorNodeColorSpill"_ustr, CMP_NODE_COLOR_SPILL);
196 ntype.ui_name = "Color Spill";
197 ntype.ui_description =
198 "Remove colors from a blue or green screen, by reducing one RGB channel compared to the "
199 "others";
200 ntype.enum_name_legacy = "COLOR_SPILL";
201 ntype.nclass = NODE_CLASS_MATTE;
202 ntype.declare = node_declare;
203 ntype.initfunc = node_init;
209
211}
213
214} // namespace blender::nodes::node_composite_color_spill_cc
#define NODE_CLASS_MATTE
Definition BKE_node.hh:485
#define CMP_NODE_COLOR_SPILL
#define BLT_I18NCONTEXT_COLOR
#define NOD_REGISTER_NODE(REGISTER_FUNC)
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
void set_matching_fn(const mf::MultiFunction *fn)
std::optional< std::string > translation_context
float3x3 scale(float3x3 mat, float2 scale)
#define B
#define R
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
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
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_build_multi_function(nodes::NodeMultiFunctionBuilder &builder)
static float3 compute_spill_scale(const bool use_spill_strength, const float4 spill_strength, const int spill_channel)
static void node_declare(NodeDeclarationBuilder &b)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
CMPNodeColorSpillLimitAlgorithm
@ CMP_NODE_COLOR_SPILL_LIMIT_ALGORITHM_AVERAGE
@ CMP_NODE_COLOR_SPILL_LIMIT_ALGORITHM_SINGLE
Global G
Definition blender.cc:57
unsigned char uint8_t
Definition BKE_text.h:79
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 cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
static constexpr int _160
Definition BKE_node.hh:246
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
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
#define N_(msgid)