Blender V5.0
node_composite_diff_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_vector.hh"
12
14
15#include "NOD_multi_function.hh"
16
17#include "UI_resources.hh"
18
19#include "GPU_material.hh"
20
22
23/* ******************* channel Difference Matte ********************************* */
24
26
28{
29 b.is_function_node();
30 b.add_input<decl::Color>("Image 1").default_value({1.0f, 1.0f, 1.0f, 1.0f});
31 b.add_input<decl::Color>("Image 2").default_value({1.0f, 1.0f, 1.0f, 1.0f});
32 b.add_input<decl::Float>("Tolerance")
33 .default_value(0.1f)
35 .min(0.0f)
36 .max(1.0f)
37 .description(
38 "If the average color difference between the two images is less than this threshold, "
39 "it is keyed");
40 b.add_input<decl::Float>("Falloff")
41 .default_value(0.1f)
43 .min(0.0f)
44 .max(1.0f)
45 .description(
46 "If the average color difference between the two images is less than this threshold, "
47 "it is partially keyed, otherwise, it is not keyed");
48
49 b.add_output<decl::Color>("Image");
50 b.add_output<decl::Float>("Matte");
51}
52
53using namespace blender::compositor;
54
55static int node_gpu_material(GPUMaterial *material,
56 bNode *node,
57 bNodeExecData * /*execdata*/,
60{
61 return GPU_stack_link(material, node, "node_composite_difference_matte", inputs, outputs);
62}
63
65{
67 return mf::build::SI4_SO2<float4, float4, float, float, float4, float>(
68 "Difference Key",
69 [=](const float4 &color,
70 const float4 &key,
71 const float &tolerance,
72 const float &falloff,
74 float &matte) -> void {
75 float difference = math::dot(math::abs(color - key).xyz(), float3(1.0f)) / 3.0f;
76
77 bool is_opaque = difference > tolerance + falloff;
78 float alpha = is_opaque ?
79 color.w :
80 math::safe_divide(math::max(0.0f, difference - tolerance), falloff);
81
82 matte = math::min(alpha, color.w);
83 result = color * matte;
84 },
85 mf::build::exec_presets::SomeSpanOrSingle<0, 1>());
86 });
87}
88
89} // namespace blender::nodes::node_composite_diff_matte_cc
90
92{
94
95 static blender::bke::bNodeType ntype;
96
97 cmp_node_type_base(&ntype, "CompositorNodeDiffMatte", CMP_NODE_DIFF_MATTE);
98 ntype.ui_name = "Difference Key";
99 ntype.ui_description =
100 "Produce a matte that isolates foreground content by comparing it with a reference "
101 "background image";
102 ntype.enum_name_legacy = "DIFF_MATTE";
103 ntype.nclass = NODE_CLASS_MATTE;
104 ntype.declare = file_ns::cmp_node_diff_matte_declare;
105 ntype.flag |= NODE_PREVIEW;
106 ntype.gpu_fn = file_ns::node_gpu_material;
107 ntype.build_multi_function = file_ns::node_build_multi_function;
108
110}
#define NODE_CLASS_MATTE
Definition BKE_node.hh:454
#define CMP_NODE_DIFF_MATTE
@ 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_register_type(bNodeType &ntype)
Definition node.cc:2416
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 void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static void cmp_node_diff_matte_declare(NodeDeclarationBuilder &b)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
VecBase< float, 4 > float4
VecBase< float, 3 > float3
static void register_node_type_cmp_diff_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