Blender V5.0
node_composite_distance_matte.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.h"
7#include "BLI_math_vector.hh"
9
11
12#include "NOD_multi_function.hh"
13
14#include "GPU_material.hh"
15
17
19
21 {CMP_NODE_DISTANCE_MATTE_COLOR_SPACE_RGBA, "RGB", 0, N_("RGB"), N_("RGB color space")},
22 {CMP_NODE_DISTANCE_MATTE_COLOR_SPACE_YCCA, "YCC", 0, N_("YCC"), N_("YCbCr color space")},
23 {0, nullptr, 0, nullptr, nullptr},
24};
25
27{
28 b.use_custom_socket_order();
29 b.allow_any_socket_order();
30 b.is_function_node();
31 b.add_input<decl::Color>("Image").default_value({1.0f, 1.0f, 1.0f, 1.0f}).hide_value();
32 b.add_output<decl::Color>("Image").align_with_previous();
33 b.add_output<decl::Float>("Matte");
34
35 b.add_input<decl::Color>("Key Color").default_value({1.0f, 1.0f, 1.0f, 1.0f});
36 b.add_input<decl::Menu>("Color Space")
38 .static_items(color_space_items)
39 .expanded()
41 b.add_input<decl::Float>("Tolerance")
42 .default_value(0.1f)
44 .min(0.0f)
45 .max(1.0f)
46 .description(
47 "If the distance between the color and the key color in the given color space is less "
48 "than this threshold, it is keyed");
49 b.add_input<decl::Float>("Falloff")
50 .default_value(0.1f)
52 .min(0.0f)
53 .max(1.0f)
54 .description(
55 "If the distance between the color and the key color in the given color space is less "
56 "than this threshold, it is partially keyed, otherwise, it is not keyed");
57}
58
59static void node_composit_init_distance_matte(bNodeTree * /*ntree*/, bNode *node)
60{
61 /* Unused, but allocated for forward compatibility. */
62 node->storage = MEM_callocN<NodeChroma>(__func__);
63}
64
65using namespace blender::compositor;
66
67static int node_gpu_material(GPUMaterial *material,
68 bNode *node,
69 bNodeExecData * /*execdata*/,
72{
73 return GPU_stack_link(material, node, "node_composite_distance_matte", inputs, outputs);
74}
75
76static void distance_key(const float4 color,
77 const float4 key,
78 const CMPNodeDistanceMatteColorSpace color_space,
79 const float tolerance,
80 const float falloff,
82 float &matte)
83{
84 float4 color_vector = color;
85 float4 key_vector = key;
86 switch (color_space) {
88 color_vector = color;
89 key_vector = key;
90 break;
93 color.y,
94 color.z,
95 &color_vector.x,
96 &color_vector.y,
97 &color_vector.z,
99 color_vector /= 255.0f;
101 key.x, key.y, key.z, &key_vector.x, &key_vector.y, &key_vector.z, BLI_YCC_ITU_BT709);
102 key_vector /= 255.0f;
103 break;
104 }
105
106 float difference = math::distance(color_vector.xyz(), key_vector.xyz());
107 bool is_opaque = difference > tolerance + falloff;
108 float alpha = is_opaque ? color.w :
109 math::safe_divide(math::max(0.0f, difference - tolerance), falloff);
110 matte = math::min(alpha, color.w);
111 result = color * matte;
112}
113
115{
116 static auto function =
117 mf::build::SI5_SO2<float4, float4, MenuValue, float, float, float4, float>(
118 "Distance Key",
119 [=](const float4 &color,
120 const float4 &key_color,
121 const MenuValue &color_space,
122 const float &tolerance,
123 const float &falloff,
124 float4 &output_color,
125 float &matte) -> void {
127 key_color,
129 tolerance,
130 falloff,
131 output_color,
132 matte);
133 },
134 mf::build::exec_presets::SomeSpanOrSingle<0, 1>());
135 builder.set_matching_fn(function);
136}
137
138} // namespace blender::nodes::node_composite_distance_matte_cc
139
141{
143
144 static blender::bke::bNodeType ntype;
145
146 cmp_node_type_base(&ntype, "CompositorNodeDistanceMatte", CMP_NODE_DIST_MATTE);
147 ntype.ui_name = "Distance Key";
148 ntype.ui_description = "Create matte based on 3D distance between colors";
149 ntype.enum_name_legacy = "DISTANCE_MATTE";
150 ntype.nclass = NODE_CLASS_MATTE;
151 ntype.declare = file_ns::cmp_node_distance_matte_declare;
152 ntype.flag |= NODE_PREVIEW;
153 ntype.initfunc = file_ns::node_composit_init_distance_matte;
156 ntype.gpu_fn = file_ns::node_gpu_material;
157 ntype.build_multi_function = file_ns::node_build_multi_function;
159
161}
#define NODE_CLASS_MATTE
Definition BKE_node.hh:454
constexpr int NODE_DEFAULT_MAX_WIDTH
Definition BKE_node.hh:1250
#define CMP_NODE_DIST_MATTE
void rgb_to_ycc(float r, float g, float b, float *r_y, float *r_cb, float *r_cr, int colorspace)
#define BLI_YCC_ITU_BT709
CMPNodeDistanceMatteColorSpace
@ CMP_NODE_DISTANCE_MATTE_COLOR_SPACE_YCCA
@ CMP_NODE_DISTANCE_MATTE_COLOR_SPACE_RGBA
@ NODE_PREVIEW
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
void set_matching_fn(const mf::MultiFunction *fn)
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
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
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 void node_composit_init_distance_matte(bNodeTree *, bNode *node)
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(blender::nodes::NodeMultiFunctionBuilder &builder)
static void cmp_node_distance_matte_declare(NodeDeclarationBuilder &b)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
VecBase< float, 4 > float4
static void register_node_type_cmp_distance_matte()
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)