Blender V5.0
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
5#include "node_shader_util.hh"
6
8#include "UI_resources.hh"
9
11
13{
14 b.use_custom_socket_order();
15
16 b.add_output<decl::Shader>("BSDF");
17 b.add_default_layout();
18
19 b.add_input<decl::Color>("Base Color")
20 .default_value({0.617f, 0.577f, 0.540f, 1.0f})
21 .description("Color of the material");
22 b.add_input<decl::Color>("Edge Tint")
23 .default_value({0.695f, 0.726f, 0.770f, 1.0f})
24 .description(
25 "Tint reflection at near-grazing incidence to simulate complex index of refraction");
26 b.add_input<decl::Vector>("IOR")
27 .default_value({2.757f, 2.513f, 2.231f})
28 .min(0.0f)
29 .max(100.0f)
30 .description("Real part of the conductor's refractive index, often called n");
31 b.add_input<decl::Vector>("Extinction")
32 .default_value({3.867f, 3.404f, 3.009f})
33 .min(0.0f)
34 .max(100.0f)
35 .description("Imaginary part of the conductor's refractive index, often called k");
36 b.add_input<decl::Float>("Roughness")
37 .default_value(0.5f)
38 .min(0.0f)
39 .max(1.0f)
41 .description(
42 "Microfacet roughness of the surface (0.0 is a perfect mirror reflection, 1.0 is "
43 "completely rough)");
44 b.add_input<decl::Float>("Anisotropy")
45 .default_value(0.0f)
46 .min(0.0f)
47 .max(1.0f)
49 .description(
50 "Amount of anisotropy for reflection. Higher values give elongated highlights along the "
51 "tangent direction");
52 b.add_input<decl::Float>("Rotation")
53 .default_value(0.0f)
54 .min(0.0f)
55 .max(1.0f)
57 .description("Rotates the direction of anisotropy, with 1.0 going full circle");
58 b.add_input<decl::Vector>("Normal").hide_value();
59 b.add_input<decl::Vector>("Tangent").hide_value();
60 b.add_input<decl::Float>("Weight").available(false);
61
62 PanelDeclarationBuilder &film = b.add_panel("Thin Film").default_closed(true);
63 film.add_input<decl::Float>("Thin Film Thickness")
64 .default_value(0.0)
65 .min(0.0f)
66 .max(100000.0f)
68 .description("Thickness of the film in nanometers");
69 film.add_input<decl::Float>("Thin Film IOR")
70 .default_value(1.33f)
71 .min(1.0f)
72 .max(1000.0f)
73 .description("Index of refraction (IOR) of the thin film");
74}
75
77{
78 layout->prop(ptr, "distribution", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
79 layout->prop(ptr, "fresnel_type", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
80}
81
82static void node_shader_init_metallic(bNodeTree * /*ntree*/, bNode *node)
83{
86}
87
89 bNode *node,
90 bNodeExecData * /*execdata*/,
93{
94 float use_multi_scatter = (node->custom1 == SHD_GLOSSY_MULTI_GGX) ? 1.0f : 0.0f;
95 float use_complex_ior = (node->custom2 == SHD_PHYSICAL_CONDUCTOR) ? 1.0f : 0.0f;
96
97 if (!in[7].link) {
98 GPU_link(mat, "world_normals_get", &in[7].link);
99 }
100
102 if (use_complex_ior == 0.0f) {
103 if (in[0].might_be_tinted() || in[1].might_be_tinted()) {
105 }
106 }
107 else {
108 if (in[2].might_be_tinted() || in[3].might_be_tinted()) {
110 }
111 }
112
113 return GPU_stack_link(mat,
114 node,
115 "node_bsdf_metallic",
116 in,
117 out,
118 GPU_constant(&use_multi_scatter),
119 GPU_constant(&use_complex_ior));
120}
121
123{
124 const bool is_physical = (node->custom2 == SHD_PHYSICAL_CONDUCTOR);
125
127 *ntree, *bke::node_find_socket(*node, SOCK_IN, "Base Color"), !is_physical);
129 *ntree, *bke::node_find_socket(*node, SOCK_IN, "Edge Tint"), !is_physical);
131 *ntree, *bke::node_find_socket(*node, SOCK_IN, "IOR"), is_physical);
133 *ntree, *bke::node_find_socket(*node, SOCK_IN, "Extinction"), is_physical);
134}
135
137#ifdef WITH_MATERIALX
138{
139 if (to_type_ != NodeItem::Type::BSDF) {
140 return empty();
141 }
142
143 NodeItem color = get_input_value("Base Color", NodeItem::Type::Color3);
144 NodeItem edge_tint = get_input_value("Edge Tint", NodeItem::Type::Color3);
145 NodeItem roughness = get_input_value("Roughness", NodeItem::Type::Vector2);
146 NodeItem anisotropy = get_input_value("Anisotropy", NodeItem::Type::Color3);
147 NodeItem normal = get_input_link("Normal", NodeItem::Type::Vector3);
148 NodeItem tangent = get_input_link("Tangent", NodeItem::Type::Vector3);
149 NodeItem thin_film_thickness = get_input_value("Thin Film Thickness", NodeItem::Type::Float);
150 NodeItem thin_film_ior = get_input_value("Thin Film IOR", NodeItem::Type::Float);
151
152 NodeItem ior_out, extinction_out;
153 if (node_->custom2 == SHD_PHYSICAL_CONDUCTOR) {
154 ior_out = get_input_value("IOR", NodeItem::Type::Color3);
155 extinction_out = get_input_value("Extinction", NodeItem::Type::Color3);
156 }
157 else {
158 NodeItem artistic_ior = create_node("artistic_ior",
159 NodeItem::Type::Multioutput,
160 {{"reflectivity", color}, {"edge_color", edge_tint}});
161 ior_out = artistic_ior.add_output("ior", NodeItem::Type::Color3);
162 extinction_out = artistic_ior.add_output("extinction", NodeItem::Type::Color3);
163 }
164
165 return create_node("conductor_bsdf",
166 NodeItem::Type::BSDF,
167 {{"normal", normal},
168 {"tangent", tangent},
169 {"ior", ior_out},
170 {"extinction", extinction_out},
171 {"roughness", roughness},
172 {"thinfilm_thickness", thin_film_thickness},
173 {"thinfilm_ior", thin_film_ior}});
174}
175#endif
177
178} // namespace blender::nodes::node_shader_bsdf_metallic_cc
179
180/* node type definition */
182{
184
185 static blender::bke::bNodeType ntype;
186
187 sh_node_type_base(&ntype, "ShaderNodeBsdfMetallic", SH_NODE_BSDF_METALLIC);
188 ntype.ui_name = "Metallic BSDF";
189 ntype.ui_description = "Metallic reflection with microfacet distribution, and metallic fresnel";
190 ntype.enum_name_legacy = "BSDF_METALLIC";
192 ntype.declare = file_ns::node_declare;
194 ntype.draw_buttons = file_ns::node_shader_buts_metallic;
196 ntype.initfunc = file_ns::node_shader_init_metallic;
197 ntype.gpu_fn = file_ns::node_shader_gpu_bsdf_metallic;
198 ntype.updatefunc = file_ns::node_shader_update_metallic;
199 ntype.materialx_fn = file_ns::node_shader_materialx;
200
202}
#define NODE_CLASS_SHADER
Definition BKE_node.hh:460
#define SH_NODE_BSDF_METALLIC
@ SHD_GLOSSY_MULTI_GGX
@ SOCK_IN
@ SHD_CONDUCTOR_F82
@ SHD_PHYSICAL_CONDUCTOR
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
@ GPU_MATFLAG_REFLECTION_MAYBE_COLORED
@ GPU_MATFLAG_GLOSSY
void GPU_material_flag_set(GPUMaterial *mat, eGPUMaterialFlag flag)
bool GPU_link(GPUMaterial *mat, const char *name,...)
@ PROP_FACTOR
Definition RNA_types.hh:251
@ PROP_WAVELENGTH
Definition RNA_types.hh:287
@ UI_ITEM_R_SPLIT_EMPTY_NAME
DeclType::Builder & add_input(StringRef name, StringRef identifier="")
#define in
#define out
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, StringRef identifier)
Definition node.cc:2532
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:4739
void node_type_size_preset(bNodeType &ntype, eNodeSizePreset size)
Definition node.cc:5396
static void node_shader_update_metallic(bNodeTree *ntree, bNode *node)
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)
static void node_shader_buts_metallic(uiLayout *layout, bContext *, PointerRNA *ptr)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void register_node_type_sh_bsdf_metallic()
void sh_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
bool object_shader_nodes_poll(const bContext *C)
#define min(a, b)
Definition sort.cc:36
int16_t custom1
int16_t custom2
Defines a node type.
Definition BKE_node.hh:238
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:344
std::string ui_description
Definition BKE_node.hh:244
bool(* add_ui_poll)(const bContext *C)
Definition BKE_node.hh:310
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:342
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
void(* updatefunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:281
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238