Blender V4.5
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 "BKE_node.hh"
10#include "BLI_math_base.hh"
11#include "BLI_math_color.h"
13
15
16#include "NOD_multi_function.hh"
17
18#include "UI_interface.hh"
19#include "UI_resources.hh"
20
21#include "GPU_material.hh"
22
24
25/* ******************* Color Matte ********************************************************** */
26
28
30{
31 b.add_input<decl::Color>("Image").default_value({1.0f, 1.0f, 1.0f, 1.0f});
32 b.add_input<decl::Color>("Key Color").default_value({1.0f, 1.0f, 1.0f, 1.0f});
33 b.add_input<decl::Float>("Hue")
34 .default_value(0.01f)
36 .min(0.0f)
37 .max(1.0f)
38 .description(
39 "If the difference in hue between the color and key color is less than this threshold, "
40 "it is keyed");
41 b.add_input<decl::Float>("Saturation")
42 .default_value(0.1f)
44 .min(0.0f)
45 .max(1.0f)
46 .description(
47 "If the difference in saturation between the color and key color is less than this "
48 "threshold, it is keyed");
49 b.add_input<decl::Float>("Value")
50 .default_value(0.1f)
52 .min(0.0f)
53 .max(1.0f)
54 .description(
55 "If the difference in value between the color and key color is less than this "
56 "threshold, it is keyed");
57
58 b.add_output<decl::Color>("Image");
59 b.add_output<decl::Float>("Matte");
60}
61
62using namespace blender::compositor;
63
64static int node_gpu_material(GPUMaterial *material,
65 bNode *node,
66 bNodeExecData * /*execdata*/,
69{
70 return GPU_stack_link(material, node, "node_composite_color_matte", inputs, outputs);
71}
72
73static void color_matte(const float4 color,
74 const float4 key,
75 const float hue_threshold,
76 const float saturation_epsilon,
77 const float value_epsilon,
79 float &matte)
80{
81 float3 color_hsva;
82 rgb_to_hsv_v(color, color_hsva);
83 float3 key_hsva;
84 rgb_to_hsv_v(key, key_hsva);
85
86 /* Divide by 2 because the hue wraps around. */
87 float hue_epsilon = hue_threshold / 2.0f;
88
89 bool is_within_saturation = math::distance(color_hsva.y, key_hsva.y) < saturation_epsilon;
90 bool is_within_value = math::distance(color_hsva.z, key_hsva.z) < value_epsilon;
91 bool is_within_hue = math::distance(color_hsva.x, key_hsva.x) < hue_epsilon;
92 /* Hue wraps around, so check the distance around the boundary. */
93 float min_hue = math::min(color_hsva.x, key_hsva.x);
94 float max_hue = math::max(color_hsva.x, key_hsva.x);
95 is_within_hue = is_within_hue || ((min_hue + (1.0f - max_hue)) < hue_epsilon);
96
97 matte = (is_within_hue && is_within_saturation && is_within_value) ? 0.0f : color.w;
98 result = color * matte;
99}
100
102{
104 return mf::build::SI5_SO2<float4, float4, float, float, float, float4, float>(
105 "Color Key",
106 [=](const float4 &color,
107 const float4 &key_color,
108 const float &hue,
109 const float &saturation,
110 const float &value,
111 float4 &output_color,
112 float &matte) -> void {
113 color_matte(color, key_color, hue, saturation, value, output_color, matte);
114 },
115 mf::build::exec_presets::SomeSpanOrSingle<0, 1>());
116 });
117}
118
119} // namespace blender::nodes::node_composite_color_matte_cc
120
122{
124
125 static blender::bke::bNodeType ntype;
126
127 cmp_node_type_base(&ntype, "CompositorNodeColorMatte", CMP_NODE_COLOR_MATTE);
128 ntype.ui_name = "Color Key";
129 ntype.ui_description = "Create matte using a given color, for green or blue screen footage";
130 ntype.enum_name_legacy = "COLOR_MATTE";
131 ntype.nclass = NODE_CLASS_MATTE;
132 ntype.declare = file_ns::cmp_node_color_matte_declare;
133 ntype.flag |= NODE_PREVIEW;
134 ntype.gpu_fn = file_ns::node_gpu_material;
135 ntype.build_multi_function = file_ns::node_build_multi_function;
136
138}
#define NODE_CLASS_MATTE
Definition BKE_node.hh:440
#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:239
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
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:226
std::string ui_description
Definition BKE_node.hh:232
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:330
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:344
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355