Blender V5.3
node_composite_distance_key.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
5#include "BLI_math_base.hh"
6#include "BLI_math_color_c.hh"
7#include "BLI_math_vector.hh"
9
11
12#include "NOD_multi_function.hh"
13
14#include "GPU_material.hh"
15
16#include "COM_result.hh"
17
19
21
23 {CMP_NODE_DISTANCE_MATTE_COLOR_SPACE_RGBA, "RGB", 0, N_("RGB"), N_("RGB color space")},
24 {CMP_NODE_DISTANCE_MATTE_COLOR_SPACE_YCCA, "YCC", 0, N_("YCC"), N_("YCbCr color space")},
25 {0, nullptr, 0, nullptr, nullptr},
26};
27
29{
30 b.use_custom_socket_order();
31 b.allow_any_socket_order();
32 b.is_function_node();
33 b.add_input<decl::Color>("Image"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f}).hide_value();
34 b.add_output<decl::Color>("Image"_ustr).align_with_previous();
35 b.add_output<decl::Float>("Matte"_ustr);
36
37 b.add_input<decl::Color>("Key Color"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f});
38 b.add_input<decl::Menu>("Color Space"_ustr)
40 .static_items(color_space_items)
41 .expanded()
43 b.add_input<decl::Float>("Tolerance"_ustr)
44 .default_value(0.1f)
46 .min(0.0f)
47 .max(1.0f)
48 .description(
49 "If the distance between the color and the key color in the given color space is less "
50 "than this threshold, it is keyed");
51 b.add_input<decl::Float>("Falloff"_ustr)
52 .default_value(0.1f)
54 .min(0.0f)
55 .max(1.0f)
56 .description(
57 "If the distance between the color and the key color in the given color space is less "
58 "than this threshold, it is partially keyed, otherwise, it is not keyed");
59}
60
61static void node_init(bNodeTree * /*ntree*/, bNode *node)
62{
63 /* Unused, but allocated for forward compatibility. */
64 node->storage = MEM_new<NodeChroma>(__func__);
65}
66
67using namespace blender::compositor;
68
69static int node_gpu_material(GPUMaterial *material,
70 bNode *node,
71 bNodeExecData * /*execdata*/,
72 GPUNodeStack *inputs,
73 GPUNodeStack *outputs)
74{
75 return GPU_stack_link(material, node, "node_composite_distance_matte", inputs, outputs);
76}
77
78static void distance_key(const float4 color,
79 const float4 key,
80 const CMPNodeDistanceMatteColorSpace color_space,
81 const float tolerance,
82 const float falloff,
84 float &matte)
85{
86 float4 color_vector = color;
87 float4 key_vector = key;
88 switch (color_space) {
90 color_vector = color;
91 key_vector = key;
92 break;
95 color.y,
96 color.z,
97 &color_vector.x,
98 &color_vector.y,
99 &color_vector.z,
101 color_vector /= 255.0f;
103 key.x, key.y, key.z, &key_vector.x, &key_vector.y, &key_vector.z, BLI_YCC_ITU_BT709);
104 key_vector /= 255.0f;
105 break;
106 }
107
108 float difference = math::distance(color_vector.xyz(), key_vector.xyz());
109 bool is_opaque = difference > tolerance + falloff;
110 float alpha = is_opaque ? color.w :
111 math::safe_divide(math::max(0.0f, difference - tolerance), falloff);
112 matte = math::min(alpha, color.w);
113 result = color * matte;
114}
115
117
119{
120 static auto function = mf::build::SI5_SO2<Color, Color, MenuValue, float, float, Color, float>(
121 "Distance Key",
122 [=](const Color &color,
123 const Color &key_color,
124 const MenuValue &color_space,
125 const float &tolerance,
126 const float &falloff,
127 Color &output_color,
128 float &matte) -> void {
129 float4 out_color;
131 float4(key_color),
133 tolerance,
134 falloff,
135 out_color,
136 matte);
137 output_color = Color(out_color);
138 },
139 mf::build::exec_presets::SomeSpanOrSingle<0, 1>());
140 builder.set_matching_fn(function);
141}
142
143static void node_register()
144{
145 static bke::bNodeType ntype;
146
147 cmp_node_type_base(&ntype, "CompositorNodeDistanceMatte"_ustr, CMP_NODE_DIST_MATTE);
148 ntype.ui_name = "Distance Key";
149 ntype.ui_description = "Create matte based on 3D distance between colors";
150 ntype.enum_name_legacy = "DISTANCE_MATTE";
151 ntype.nclass = NODE_CLASS_MATTE;
152 ntype.declare = node_declare;
153 ntype.flag |= NODE_PREVIEW;
154 ntype.initfunc = node_init;
160
162}
164
165} // namespace blender::nodes::node_composite_distance_key_cc
#define NODE_CLASS_MATTE
Definition BKE_node.hh:485
#define CMP_NODE_DIST_MATTE
#define BLI_YCC_ITU_BT709
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void set_matching_fn(const mf::MultiFunction *fn)
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
T safe_divide(const T &a, const T &b)
T distance(const T &a, const T &b)
T min(const T &a, const T &b)
T max(const T &a, const T &b)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void node_declare(NodeDeclarationBuilder &b)
static void distance_key(const float4 color, const float4 key, const CMPNodeDistanceMatteColorSpace color_space, const float tolerance, const float falloff, float4 &result, float &matte)
static void node_build_multi_function(nodes::NodeMultiFunctionBuilder &builder)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
CMPNodeDistanceMatteColorSpace
@ CMP_NODE_DISTANCE_MATTE_COLOR_SPACE_YCCA
@ CMP_NODE_DISTANCE_MATTE_COLOR_SPACE_RGBA
void rgb_to_ycc(float r, float g, float b, float *r_y, float *r_cb, float *r_cr, int colorspace)
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)