Blender V5.3
node_composite_difference_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_vector.hh"
8
10
11#include "NOD_multi_function.hh"
12
13#include "GPU_material.hh"
14
15#include "COM_result.hh"
16
18
20
22{
23 b.is_function_node();
24 b.add_input<decl::Color>("Image 1"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f});
25 b.add_input<decl::Color>("Image 2"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f});
26 b.add_input<decl::Float>("Tolerance"_ustr)
27 .default_value(0.1f)
29 .min(0.0f)
30 .max(1.0f)
31 .description(
32 "If the average color difference between the two images is less than this threshold, "
33 "it is keyed");
34 b.add_input<decl::Float>("Falloff"_ustr)
35 .default_value(0.1f)
37 .min(0.0f)
38 .max(1.0f)
39 .description(
40 "If the average color difference between the two images is less than this threshold, "
41 "it is partially keyed, otherwise, it is not keyed");
42
43 b.add_output<decl::Color>("Image"_ustr);
44 b.add_output<decl::Float>("Matte"_ustr);
45}
46
47using namespace blender::compositor;
48
49static int node_gpu_material(GPUMaterial *material,
50 bNode *node,
51 bNodeExecData * /*execdata*/,
52 GPUNodeStack *inputs,
53 GPUNodeStack *outputs)
54{
55 return GPU_stack_link(material, node, "node_composite_difference_matte", inputs, outputs);
56}
57
58static void difference_matte(const float4 &color,
59 const float4 &key,
60 const float &tolerance,
61 const float &falloff,
63 float &matte)
64{
65 float difference = math::dot(math::abs(color - key).xyz(), float3(1.0f)) / 3.0f;
66
67 bool is_opaque = difference > tolerance + falloff;
68 float alpha = is_opaque ? color.w :
69 math::safe_divide(math::max(0.0f, difference - tolerance), falloff);
70
71 matte = math::min(alpha, color.w);
72 result = color * matte;
73}
74
76
78{
80 return mf::build::SI4_SO2<Color, Color, float, float, Color, float>(
81 "Difference Key",
82 [=](const Color &color,
83 const Color &key,
84 const float &tolerance,
85 const float &falloff,
86 Color &output_color,
87 float &matte) -> void {
88 float4 out_color;
89 difference_matte(float4(color), float4(key), tolerance, falloff, out_color, matte);
90 output_color = Color(out_color);
91 },
92 mf::build::exec_presets::SomeSpanOrSingle<0, 1>());
93 });
94}
95
96static void node_register()
97{
98 static bke::bNodeType ntype;
99
100 cmp_node_type_base(&ntype, "CompositorNodeDiffMatte"_ustr, CMP_NODE_DIFF_MATTE);
101 ntype.ui_name = "Difference Key";
102 ntype.ui_description =
103 "Produce a matte that isolates foreground content by comparing it with a reference "
104 "background image";
105 ntype.enum_name_legacy = "DIFF_MATTE";
106 ntype.nclass = NODE_CLASS_MATTE;
107 ntype.declare = node_declare;
108 ntype.flag |= NODE_PREVIEW;
111
113}
115
116} // namespace blender::nodes::node_composite_difference_key_cc
#define NODE_CLASS_MATTE
Definition BKE_node.hh:485
#define CMP_NODE_DIFF_MATTE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
T safe_divide(const T &a, const T &b)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T min(const T &a, const T &b)
T max(const T &a, const T &b)
T abs(const T &a)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void difference_matte(const float4 &color, const float4 &key, 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,...)
@ PROP_FACTOR
Definition RNA_types.hh:308
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
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
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393