Blender V5.3
node_shader_bsdf_metallic.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "node_shader_util.hh"
10
12#include "UI_resources.hh"
13
14namespace blender {
15
17
19{
20 const bNodeTree *ntree = b.tree_or_null();
21 const bool is_gpu_internal = ntree && (ntree->flag & NTREE_IS_GPU_SHADER_INTERNAL);
22
23 b.use_custom_socket_order();
24
25 b.add_output<decl::Shader>("BSDF"_ustr);
26 b.add_default_layout();
27
28 b.add_input<decl::Color>("Base Color"_ustr)
29 .default_value({0.617f, 0.577f, 0.540f, 1.0f})
30 .description("Color of the material")
31 .make_available([](bNode &node) { node.custom2 = SHD_CONDUCTOR_F82; });
32 b.add_input<decl::Color>("Edge Tint"_ustr)
33 .default_value({0.695f, 0.726f, 0.770f, 1.0f})
34 .description(
35 "Tint reflection at near-grazing incidence to simulate complex index of refraction")
36 .make_available([](bNode &node) { node.custom2 = SHD_CONDUCTOR_F82; });
37 b.add_input<decl::Vector>("IOR"_ustr)
38 .default_value({2.757f, 2.513f, 2.231f})
39 .min(0.0f)
40 .max(100.0f)
41 .description("Real part of the conductor's refractive index, often called n")
42 .make_available([](bNode &node) { node.custom2 = SHD_PHYSICAL_CONDUCTOR; });
43 b.add_input<decl::Vector>("Extinction"_ustr)
44 .default_value({3.867f, 3.404f, 3.009f})
45 .min(0.0f)
46 .max(100.0f)
47 .description("Imaginary part of the conductor's refractive index, often called k")
48 .make_available([](bNode &node) { node.custom2 = SHD_PHYSICAL_CONDUCTOR; });
49 b.add_input<decl::Float>("Roughness"_ustr)
50 .default_value(0.5f)
51 .min(0.0f)
52 .max(1.0f)
54 .description(
55 "Microfacet roughness of the surface (0.0 is a perfect mirror reflection, 1.0 is "
56 "completely rough)");
57 b.add_input<decl::Float>("Anisotropy"_ustr)
58 .default_value(0.0f)
59 .min(0.0f)
60 .max(1.0f)
62 .description(
63 "Amount of anisotropy for reflection. Higher values give elongated highlights along the "
64 "tangent direction");
65 b.add_input<decl::Float>("Rotation"_ustr)
66 .default_value(0.0f)
67 .min(0.0f)
68 .max(1.0f)
70 .description("Rotates the direction of anisotropy, with 1.0 going full circle");
71 b.add_input<decl::Vector>("Normal"_ustr).hide_value();
72 b.add_input<decl::Vector>("Tangent"_ustr).hide_value();
73 b.add_input<decl::Float>("Weight"_ustr).available(is_gpu_internal);
74
75 PanelDeclarationBuilder &film = b.add_panel("Thin Film"_ustr).default_closed(true);
76 film.add_input<decl::Float>("Thin Film Thickness"_ustr)
77 .default_value(0.0)
78 .min(0.0f)
79 .max(100000.0f)
81 .description("Thickness of the film in nanometers");
82 film.add_input<decl::Float>("Thin Film IOR"_ustr)
83 .default_value(1.33f)
84 .min(1.0f)
85 .max(1000.0f)
86 .description("Index of refraction (IOR) of the thin film");
87}
88
89static void node_shader_buts_metallic(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
90{
91 layout.prop(ptr, "distribution", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
92 layout.prop(ptr, "fresnel_type", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
93}
94
96{
97 node->custom1 = SHD_GLOSSY_MULTI_GGX;
98 node->custom2 = SHD_CONDUCTOR_F82;
99}
100
102 bNode *node,
103 bNodeExecData * /*execdata*/,
106{
107 float use_multi_scatter = (node->custom1 == SHD_GLOSSY_MULTI_GGX) ? 1.0f : 0.0f;
108 float use_complex_ior = (node->custom2 == SHD_PHYSICAL_CONDUCTOR) ? 1.0f : 0.0f;
109
110 if (!in[7].link) {
111 GPU_link(mat, "world_normals_get", GPU_shading_data(), &in[7].link);
112 }
113
115 if (use_complex_ior == 0.0f) {
116 if (in[0].might_be_tinted() || in[1].might_be_tinted()) {
118 }
119 }
120 else {
121 if (in[2].might_be_tinted() || in[3].might_be_tinted()) {
123 }
124 }
125
126 return GPU_stack_link(mat,
127 node,
128 "node_bsdf_metallic",
129 in,
130 out,
131 GPU_constant(&use_multi_scatter),
132 GPU_constant(&use_complex_ior),
135}
136
138{
139 const bool is_physical = (node->custom2 == SHD_PHYSICAL_CONDUCTOR);
140
142 *ntree, *bke::node_find_socket(*node, SOCK_IN, "Base Color"_ustr), !is_physical);
144 *ntree, *bke::node_find_socket(*node, SOCK_IN, "Edge Tint"_ustr), !is_physical);
146 *ntree, *bke::node_find_socket(*node, SOCK_IN, "IOR"_ustr), is_physical);
148 *ntree, *bke::node_find_socket(*node, SOCK_IN, "Extinction"_ustr), is_physical);
149}
150
152#ifdef WITH_MATERIALX
153{
154 if (to_type_ != NodeItem::Type::BSDF) {
155 return empty();
156 }
157
158 NodeItem color = get_input_value("Base Color", NodeItem::Type::Color3);
159 NodeItem edge_tint = get_input_value("Edge Tint", NodeItem::Type::Color3);
160 NodeItem roughness = get_input_value("Roughness", NodeItem::Type::Vector2);
161 NodeItem anisotropy = get_input_value("Anisotropy", NodeItem::Type::Color3);
162 NodeItem normal = get_input_link("Normal", NodeItem::Type::Vector3);
163 NodeItem tangent = get_input_link("Tangent", NodeItem::Type::Vector3);
164 NodeItem thin_film_thickness = get_input_value("Thin Film Thickness", NodeItem::Type::Float);
165 NodeItem thin_film_ior = get_input_value("Thin Film IOR", NodeItem::Type::Float);
166
167 NodeItem ior_out, extinction_out;
168 if (node_->custom2 == SHD_PHYSICAL_CONDUCTOR) {
169 ior_out = get_input_value("IOR", NodeItem::Type::Color3);
170 extinction_out = get_input_value("Extinction", NodeItem::Type::Color3);
171 }
172 else {
173 NodeItem artistic_ior = create_node("artistic_ior",
174 NodeItem::Type::Multioutput,
175 {{"reflectivity", color}, {"edge_color", edge_tint}});
176 ior_out = artistic_ior.add_output("ior", NodeItem::Type::Color3);
177 extinction_out = artistic_ior.add_output("extinction", NodeItem::Type::Color3);
178 }
179
180 return create_node("conductor_bsdf",
181 NodeItem::Type::BSDF,
182 {{"normal", normal},
183 {"tangent", tangent},
184 {"ior", ior_out},
185 {"extinction", extinction_out},
186 {"roughness", roughness},
187 {"thinfilm_thickness", thin_film_thickness},
188 {"thinfilm_ior", thin_film_ior}});
189}
190#endif
192
193} // namespace nodes::node_shader_bsdf_metallic_cc
194
195/* node type definition */
197{
198 namespace file_ns = nodes::node_shader_bsdf_metallic_cc;
199
200 static bke::bNodeType ntype;
201
202 sh_node_type_base(&ntype, "ShaderNodeBsdfMetallic"_ustr, SH_NODE_BSDF_METALLIC);
203 ntype.ui_name = "Metallic BSDF";
204 ntype.ui_description = "Metallic reflection with microfacet distribution, and metallic fresnel";
205 ntype.enum_name_legacy = "BSDF_METALLIC";
207 ntype.declare = file_ns::node_declare;
210 ntype.draw_buttons = file_ns::node_shader_buts_metallic;
212 ntype.initfunc = file_ns::node_shader_init_metallic;
213 ntype.gpu_fn = file_ns::node_shader_gpu_bsdf_metallic;
214 ntype.updatefunc = file_ns::node_shader_update_metallic;
215 ntype.materialx_fn = file_ns::node_shader_materialx;
216
218}
219
220} // namespace blender
#define NODE_CLASS_SHADER
Definition BKE_node.hh:491
#define SH_NODE_BSDF_METALLIC
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
void make_available(bNode &node) const
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:5499
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
static void node_shader_update_metallic(bNodeTree *ntree, bNode *node)
static void node_shader_buts_metallic(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_shader_init_metallic(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static int node_shader_gpu_bsdf_metallic(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
bool object_shader_nodes_poll(const bContext *C)
@ GPU_MATFLAG_GLOSSY
@ GPU_MATFLAG_REFLECTION_MAYBE_COLORED
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
@ SHD_PHYSICAL_CONDUCTOR
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
void search_link_ops_for_shader_bsdf_node(nodes::GatherLinkSearchOpParams &params)
void GPU_material_flag_set(GPUMaterial *mat, eGPUMaterialFlag flag)
GPUNodeLink * GPU_shading_data()
@ NTREE_IS_GPU_SHADER_INTERNAL
GPUNodeLink * GPU_kernel_globals()
void register_node_type_sh_bsdf_metallic()
@ PROP_FACTOR
Definition RNA_types.hh:308
@ PROP_WAVELENGTH
Definition RNA_types.hh:344
@ SHD_GLOSSY_MULTI_GGX
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define min(a, b)
Definition sort.cc:38
eNodeTree_Flag flag
static constexpr int _240
Definition BKE_node.hh:250
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
void(*) updatefunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:312
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
bool(*) add_ui_poll(const bContext *C)
Definition BKE_node.hh:341
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)