Blender V5.0
node_composite_color_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
8
9#include "BLI_math_base.hh"
10#include "BLI_math_color.h"
12
14
15#include "NOD_multi_function.hh"
16
17#include "BKE_node.hh"
18
19#include "UI_resources.hh"
20
21#include "GPU_material.hh"
22
24
25/* ******************* Color Matte ********************************************************** */
26
28
30{
31 b.use_custom_socket_order();
32 b.allow_any_socket_order();
33 b.is_function_node();
34 b.add_input<decl::Color>("Image").default_value({1.0f, 1.0f, 1.0f, 1.0f}).hide_value();
35 b.add_output<decl::Color>("Image").align_with_previous();
36 b.add_output<decl::Float>("Matte");
37
38 b.add_input<decl::Color>("Key Color").default_value({1.0f, 1.0f, 1.0f, 1.0f});
39 b.add_input<decl::Float>("Hue")
40 .default_value(0.01f)
42 .min(0.0f)
43 .max(1.0f)
44 .description(
45 "If the difference in hue between the color and key color is less than this threshold, "
46 "it is keyed");
47 b.add_input<decl::Float>("Saturation")
48 .default_value(0.1f)
50 .min(0.0f)
51 .max(1.0f)
52 .description(
53 "If the difference in saturation between the color and key color is less than this "
54 "threshold, it is keyed");
55 b.add_input<decl::Float>("Value")
56 .default_value(0.1f)
58 .min(0.0f)
59 .max(1.0f)
60 .description(
61 "If the difference in value between the color and key color is less than this "
62 "threshold, it is keyed");
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_color_matte", inputs, outputs);
74}
75
76static void color_matte(const float4 color,
77 const float4 key,
78 const float hue_threshold,
79 const float saturation_epsilon,
80 const float value_epsilon,
82 float &matte)
83{
84 float3 color_hsva;
85 rgb_to_hsv_v(color, color_hsva);
86 float3 key_hsva;
87 rgb_to_hsv_v(key, key_hsva);
88
89 /* Divide by 2 because the hue wraps around. */
90 float hue_epsilon = hue_threshold / 2.0f;
91
92 bool is_within_saturation = math::distance(color_hsva.y, key_hsva.y) < saturation_epsilon;
93 bool is_within_value = math::distance(color_hsva.z, key_hsva.z) < value_epsilon;
94 bool is_within_hue = math::distance(color_hsva.x, key_hsva.x) < hue_epsilon;
95 /* Hue wraps around, so check the distance around the boundary. */
96 float min_hue = math::min(color_hsva.x, key_hsva.x);
97 float max_hue = math::max(color_hsva.x, key_hsva.x);
98 is_within_hue = is_within_hue || ((min_hue + (1.0f - max_hue)) < hue_epsilon);
99
100 matte = (is_within_hue && is_within_saturation && is_within_value) ? 0.0f : color.w;
101 result = color * matte;
102}
103
105{
107 return mf::build::SI5_SO2<float4, float4, float, float, float, float4, float>(
108 "Color Key",
109 [=](const float4 &color,
110 const float4 &key_color,
111 const float &hue,
112 const float &saturation,
113 const float &value,
114 float4 &output_color,
115 float &matte) -> void {
116 color_matte(color, key_color, hue, saturation, value, output_color, matte);
117 },
118 mf::build::exec_presets::SomeSpanOrSingle<0, 1>());
119 });
120}
121
122} // namespace blender::nodes::node_composite_color_matte_cc
123
125{
127
128 static blender::bke::bNodeType ntype;
129
130 cmp_node_type_base(&ntype, "CompositorNodeColorMatte", CMP_NODE_COLOR_MATTE);
131 ntype.ui_name = "Color Key";
132 ntype.ui_description = "Create matte using a given color, for green or blue screen footage";
133 ntype.enum_name_legacy = "COLOR_MATTE";
134 ntype.nclass = NODE_CLASS_MATTE;
135 ntype.declare = file_ns::cmp_node_color_matte_declare;
136 ntype.flag |= NODE_PREVIEW;
137 ntype.gpu_fn = file_ns::node_gpu_material;
138 ntype.build_multi_function = file_ns::node_build_multi_function;
140
142}
#define NODE_CLASS_MATTE
Definition BKE_node.hh:454
constexpr int NODE_DEFAULT_MAX_WIDTH
Definition BKE_node.hh:1250
#define CMP_NODE_COLOR_MATTE
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
@ 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 construct_and_set_matching_fn_cb(Fn &&create_multi_function)
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
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_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void cmp_node_color_matte_declare(NodeDeclarationBuilder &b)
static void color_matte(const float4 color, const float4 key, const float hue_threshold, const float saturation_epsilon, const float value_epsilon, float4 &result, float &matte)
VecBase< float, 4 > float4
VecBase< float, 3 > float3
static void register_node_type_cmp_color_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[]
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
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