Blender V5.3
node_shader_bsdf_principled.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <map>
10
11#include "node_shader_util.hh"
12
13#include "BLI_math_base_c.hh"
14
16#include "UI_resources.hh"
17
18#include "BKE_node_runtime.hh"
19
21
22namespace blender {
23
25
27{
28 const bNodeTree *ntree = b.tree_or_null();
29 const bool is_gpu_internal = ntree && (ntree->flag & NTREE_IS_GPU_SHADER_INTERNAL);
30
35
36 b.use_custom_socket_order();
37
38 b.add_output<decl::Shader>("BSDF"_ustr);
39
40 b.add_input<decl::Color>("Base Color"_ustr)
41 .default_value({0.8f, 0.8f, 0.8f, 1.0f})
42 .description(
43 "Color of the material used for diffuse, subsurface, metallic and transmission");
44#define SOCK_BASE_COLOR_ID 0
45 b.add_input<decl::Float>("Metallic"_ustr)
46 .default_value(0.0f)
47 .min(0.0f)
48 .max(1.0f)
50 .description(
51 "Blends between a dielectric and metallic material model. "
52 "At 0.0 the material consists of a diffuse or transmissive base layer, "
53 "with a specular reflection layer on top. A value of 1.0 gives a fully specular "
54 "reflection tinted with the base color, without diffuse reflection or transmission");
55#define SOCK_METALLIC_ID 1
56 b.add_input<decl::Float>("Roughness"_ustr)
57 .default_value(0.5f)
58 .min(0.0f)
59 .max(1.0f)
61 .description(
62 "Specifies microfacet roughness of the surface for specular reflection and transmission"
63 " (0.0 is a perfect mirror reflection, 1.0 is completely rough)");
64#define SOCK_ROUGHNESS_ID 2
65 b.add_input<decl::Float>("IOR"_ustr)
66 .default_value(1.5f)
67 .min(1.0f)
68 .max(1000.0f)
70 "Index of Refraction (IOR) for specular reflection and transmission. "
71 "For most materials, the IOR is between 1.0 (vacuum and air) and 4.0 (germanium). "
72 "The default value of 1.5 is a good approximation for glass");
73#define SOCK_IOR_ID 3
74 b.add_input<decl::Float>("Alpha"_ustr)
75 .default_value(1.0f)
76 .min(0.0f)
77 .max(1.0f)
79 .description("Controls the transparency of the surface, with 1.0 fully opaque");
80#define SOCK_ALPHA_ID 4
81 b.add_input<decl::Bool>("Thin Wall"_ustr)
82 .default_value(false)
84 "A thin structure with the same material on both sides, useful for modeling thin "
85 "objects such as window, papers and leaves");
86#define SOCK_THIN_WALL_ID 5
87 b.add_input<decl::Vector>("Normal"_ustr).hide_value();
88#define SOCK_NORMAL_ID 6
89 b.add_input<decl::Float>("Weight"_ustr).available(is_gpu_internal);
90#define SOCK_WEIGHT_ID 7
91
92 /* Panel for Diffuse settings. */
93 PanelDeclarationBuilder &diffuse = b.add_panel("Diffuse"_ustr).default_closed(true);
94 diffuse.add_input<decl::Float>("Diffuse Roughness"_ustr)
95 .default_value(0.0f)
96 .min(0.0f)
97 .max(1.0f)
99 .description(
100 "Specifies microfacet roughness of the diffuse base"
101 " (0.0 is perfect lambertian reflection, 1.0 is completely rough)");
102#define SOCK_DIFFUSE_ROUGHNESS_ID 8
103
104 /* Panel for Subsurface scattering settings. */
105 PanelDeclarationBuilder &sss = b.add_panel("Subsurface"_ustr).default_closed(true);
106 sss.add_layout([](ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr) {
107 layout.prop(ptr, "subsurface_method", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
108 /* Not used by Thin Wall. Infer the value from Subsurface Radius. */
109 const bNode &node = *ptr->data_as<bNode>();
110 const bNodeSocket &radius_socket = *bke::node_find_socket(
111 node, SOCK_IN, "Subsurface Radius"_ustr);
112 if (radius_socket.is_inactive()) {
113 layout.active_set(false);
114 }
115 });
116 sss.add_input<decl::Float>("Subsurface Weight"_ustr)
117 .default_value(0.0f)
118 .min(0.0f)
119 .max(1.0f)
121 .short_label("Weight"_ustr)
122 .description(
123 "Blend between diffuse surface and subsurface scattering. "
124 "Typically should be zero or one (either fully diffuse or subsurface)");
125#define SOCK_SUBSURFACE_WEIGHT_ID 9
126 sss.add_input<decl::Vector>("Subsurface Radius"_ustr)
127 .default_value({1.0f, 0.2f, 0.1f})
128 .min(0.0f)
129 .max(100.0f)
130 .short_label("Radius"_ustr)
131 .description("Scattering radius per color channel (RGB), multiplied with Scale")
132 .usage_by_bool("Thin Wall"_ustr, false);
133#define SOCK_SUBSURFACE_RADIUS_ID 10
134 sss.add_input<decl::Float>("Subsurface Scale"_ustr)
135 .default_value(0.005f)
136 .min(0.0f)
137 .max(10.0f)
139 .short_label("Scale"_ustr)
140 .description("Scale factor of the subsurface scattering radius")
141 .usage_by_bool("Thin Wall"_ustr, false);
142#define SOCK_SUBSURFACE_SCALE_ID 11
143 sss.add_input<decl::Float>("Subsurface IOR"_ustr)
144 .default_value(1.4f)
145 .min(1.01f)
146 .max(3.8f)
148 .short_label("IOR"_ustr)
149 .description("Index of Refraction (IOR) used for rays that enter the subsurface component")
150 .make_available([](bNode &node) { node.custom2 = SHD_SUBSURFACE_RANDOM_WALK_SKIN; });
151#define SOCK_SUBSURFACE_IOR_ID 12
152 sss.add_input<decl::Float>("Subsurface Anisotropy"_ustr)
153 .default_value(0.0f)
154 .min(-1.0f)
155 .max(1.0f)
157 .short_label("Anisotropy"_ustr)
158 .description(
159 "Directionality of volume scattering within the subsurface medium. "
160 "Zero scatters uniformly in all directions, positive values scatter more in the forward "
161 "direction, and negative values scatter more backwards. "
162 "For example, skin has been measured to have an anisotropy of 0.8")
163 .make_available([](bNode &node) { node.custom2 = SHD_SUBSURFACE_RANDOM_WALK; })
164 .usage_inference(
165 [](const socket_usage_inference::SocketUsageParams &params) -> std::optional<bool> {
166 return params.bool_input_may_be("Thin Wall"_ustr, true) ||
168 });
169#define SOCK_SUBSURFACE_ANISOTROPY_ID 13
170
171 /* Panel for Specular settings. */
172 PanelDeclarationBuilder &spec = b.add_panel("Specular"_ustr).default_closed(true);
173 spec.add_layout([](ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr) {
174 layout.prop(ptr, "distribution", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
175 });
176 spec.add_input<decl::Float>("Specular IOR Level"_ustr)
177 .default_value(0.5f)
178 .min(0.0f)
179 .max(1.0f)
181 .short_label("IOR Level"_ustr)
182 .description(
183 "Adjustment to the Index of Refraction (IOR) to increase or decrease specular intensity "
184 "(0.5 means no adjustment, 0 removes all reflections, 1 doubles them at normal "
185 "incidence)");
186#define SOCK_SPECULAR_ID 14
187 spec.add_input<decl::Color>("Specular Tint"_ustr)
188 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
189 .short_label("Tint"_ustr)
191 "Tint dielectric reflection at normal incidence for artistic control, and metallic "
192 "reflection at near-grazing incidence to simulate complex index of refraction")
193 .translation_context(BLT_I18NCONTEXT_ID_NODETREE);
194#define SOCK_SPECULAR_TINT_ID 15
195 spec.add_input<decl::Float>("Anisotropic"_ustr)
196 .default_value(0.0f)
197 .min(0.0f)
198 .max(1.0f)
200 .short_label("Anisotropy"_ustr)
201 .description(
202 "Amount of anisotropy for specular reflection. "
203 "Higher values give elongated highlights along the tangent direction");
204#define SOCK_ANISOTROPIC_ID 16
205 spec.add_input<decl::Float>("Anisotropic Rotation"_ustr)
206 .default_value(0.0f)
207 .min(0.0f)
208 .max(1.0f)
210 .description("Rotates the direction of anisotropy, with 1.0 going full circle");
211#define SOCK_ANISOTROPIC_ROTATION_ID 17
212 spec.add_input<decl::Vector>("Tangent"_ustr)
213 .hide_value()
214 .description("Controls the tangent direction for anisotropy");
215#define SOCK_TANGENT_ID 18
216
217 /* Panel for Transmission settings. */
218 PanelDeclarationBuilder &transmission = b.add_panel("Transmission"_ustr).default_closed(true);
219 transmission.add_input<decl::Float>("Transmission Weight"_ustr)
220 .default_value(0.0f)
221 .min(0.0f)
222 .max(1.0f)
224 .short_label("Weight"_ustr)
225 .description("Blend between transmission and other base layer components");
226#define SOCK_TRANSMISSION_WEIGHT_ID 19
227 transmission.add_input<decl::Float>("Transmission Dispersion Scale"_ustr)
228 .default_value(0.0f)
229 .min(0.0f)
230 .max(1.0f)
232 .description("Linearly scales the amount of dispersion");
233#define SOCK_TRANSMISSION_DISPERSION_SCALE_ID 20
234 transmission.add_input<decl::Float>("Transmission Dispersion Abbe Number"_ustr)
235 .default_value(20.0f)
236 .min(9.0f)
237 .max(91.0f)
239 "Abbe number of the base dielectric medium. Smaller number gives stronger dispersion");
240#define SOCK_TRANSMISSION_DISPERSION_ABBE_NUMBER_ID 21
241
242 /* Panel for Coat settings. */
243 PanelDeclarationBuilder &coat = b.add_panel("Coat"_ustr).default_closed(true);
244 coat.add_input<decl::Float>("Coat Weight"_ustr)
245 .default_value(0.0f)
246 .min(0.0f)
247 .max(1.0f)
249 .short_label("Weight"_ustr)
250 .description(
251 "Controls the intensity of the coat layer, both the reflection and the tinting. "
252 "Typically should be zero or one for physically-based materials");
253#define SOCK_COAT_WEIGHT_ID 22
254 coat.add_input<decl::Float>("Coat Roughness"_ustr)
255 .default_value(0.03f)
256 .min(0.0f)
257 .max(1.0f)
259 .short_label("Roughness"_ustr)
260 .description("The roughness of the coat layer");
261#define SOCK_COAT_ROUGHNESS_ID 23
262 coat.add_input<decl::Float>("Coat IOR"_ustr)
263 .default_value(1.5f)
264 .min(1.0f)
265 .max(4.0f)
266 .short_label("IOR"_ustr)
267 .description(
268 "The Index of Refraction (IOR) of the coat layer "
269 "(affects its reflectivity as well as the falloff of coat tinting)");
270#define SOCK_COAT_IOR_ID 24
271 coat.add_input<decl::Color>("Coat Tint"_ustr)
272 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
273 .short_label("Tint"_ustr)
275 "Adds a colored tint to the coat layer by modeling absorption in the layer. "
276 "Saturation increases at shallower angles, as the light travels farther "
277 "through the medium (depending on the Coat IOR)")
278 .translation_context(BLT_I18NCONTEXT_ID_NODETREE);
279#define SOCK_COAT_TINT_ID 25
280 coat.add_input<decl::Vector>("Coat Normal"_ustr).short_label("Normal"_ustr).hide_value();
281#define SOCK_COAT_NORMAL_ID 26
282
283 /* Panel for Sheen settings. */
284 PanelDeclarationBuilder &sheen = b.add_panel("Sheen"_ustr).default_closed(true);
285 sheen.add_input<decl::Float>("Sheen Weight"_ustr)
286 .default_value(0.0f)
287 .min(0.0f)
288 .max(1.0f)
290 .short_label("Weight"_ustr)
291 .description(
292 "Intensity of the sheen layer, which simulates very small fibers on the surface");
293#define SOCK_SHEEN_WEIGHT_ID 27
294 sheen.add_input<decl::Float>("Sheen Roughness"_ustr)
295 .default_value(0.5f)
296 .min(0.0f)
297 .max(1.0f)
299 .short_label("Roughness"_ustr)
300 .description(
301 "Roughness of the sheen layer. Low and high roughness values produce fuzzy or dusty "
302 "appearance, respectively");
303#define SOCK_SHEEN_ROUGHNESS_ID 28
304 sheen.add_input<decl::Color>("Sheen Tint"_ustr)
305 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
306 .translation_context(BLT_I18NCONTEXT_ID_NODETREE)
307 .short_label("Tint"_ustr)
308 .description("Color of the sheen reflection");
309#define SOCK_SHEEN_TINT_ID 29
310
311 /* Panel for Emission settings. */
312 PanelDeclarationBuilder &emis = b.add_panel("Emission"_ustr).default_closed(true);
313 emis.add_input<decl::Color>("Emission Color"_ustr)
314 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
315 .short_label("Color"_ustr)
316 .description("Color of light emission from the surface");
317#define SOCK_EMISSION_ID 30
318 emis.add_input<decl::Float>("Emission Strength"_ustr)
319 .default_value(0.0)
320 .min(0.0f)
321 .max(1000000.0f)
322 .short_label("Strength"_ustr)
323 .description(
324 "Strength of the emitted light. A value of 1.0 ensures "
325 "that the object in the image has the exact same color as the Emission Color")
326 .translation_context(BLT_I18NCONTEXT_AMOUNT);
327#define SOCK_EMISSION_STRENGTH_ID 31
328
329 /* Panel for Thin Film settings. */
330 PanelDeclarationBuilder &film = b.add_panel("Thin Film"_ustr).default_closed(true);
331 film.add_input<decl::Float>("Thin Film Thickness"_ustr)
332 .default_value(0.0)
333 .min(0.0f)
334 .max(100000.0f)
336 .description("Thickness of the film in nanometers");
337#define SOCK_THIN_FILM_THICKNESS_ID 32
338 film.add_input<decl::Float>("Thin Film IOR"_ustr)
339 .default_value(1.33f)
340 .min(1.0f)
341 .max(1000.0f)
342 .description("Index of refraction (IOR) of the thin film");
343#define SOCK_THIN_FILM_IOR_ID 33
344}
345
347{
348 node->custom1 = SHD_GLOSSY_MULTI_GGX;
350}
351
352static bool might_have_tinted_specular(const GPUNodeStack &base_color,
353 const GPUNodeStack &metallic,
354 const GPUNodeStack &specular_tint)
355{
356 if (metallic.socket_not_zero()) {
357 /* Metals might have colored specular. */
358 return base_color.might_be_tinted() || specular_tint.might_be_tinted();
359 }
360 /* Dielectrics get colored if tint is used. */
361 return specular_tint.might_be_tinted();
362}
363
365 bNode *node,
366 bNodeExecData * /*execdata*/,
369{
370 /* Normals */
371 if (!in[SOCK_NORMAL_ID].link) {
372 GPU_link(mat, "world_normals_get", GPU_shading_data(), &in[SOCK_NORMAL_ID].link);
373 }
374
375 /* Coat Normals */
376 if (!in[SOCK_COAT_NORMAL_ID].link) {
377 GPU_link(mat, "world_normals_get", GPU_shading_data(), &in[SOCK_COAT_NORMAL_ID].link);
378 }
379
380#if 0 /* Not used at the moment. */
381 /* Tangents */
382 if (!in[SOCK_TANGENT_ID].link) {
383 GPUNodeLink *orco = GPU_attribute(CD_ORCO, "");
384 GPU_link(mat, "tangent_orco_z", orco, &in[SOCK_TANGENT_ID].link);
385 GPU_link(mat, "node_tangent", in[SOCK_TANGENT_ID].link, GPU_kernel_globals(), GPU_shading_data(), &in[SOCK_TANGENT_ID].link);
386 }
387#endif
388
389 bool use_diffuse = in[SOCK_SHEEN_WEIGHT_ID].socket_not_zero() ||
390 (in[SOCK_METALLIC_ID].socket_not_one() &&
391 in[SOCK_TRANSMISSION_WEIGHT_ID].socket_not_one());
392 bool use_subsurf = in[SOCK_SUBSURFACE_WEIGHT_ID].socket_not_zero() && use_diffuse;
393 bool use_refract = in[SOCK_METALLIC_ID].socket_not_one() &&
394 in[SOCK_TRANSMISSION_WEIGHT_ID].socket_not_zero();
395 bool use_transparency = in[SOCK_ALPHA_ID].socket_not_one();
396 bool use_coat = in[SOCK_COAT_WEIGHT_ID].socket_not_zero();
397 bool use_translucent = use_subsurf && in[SOCK_THIN_WALL_ID].socket_not_zero();
398
399 /* EEVEE's subsurface closure still uses the legacy approximation where the radius is scaled by
400 * 1/(4pi) (e.g., SHD_SUBSURFACE_RANDOM_WALK_LEGACY) */
401 GPUNodeLink *subsurface_random_walk_radius_scale = nullptr;
402 float random_walk_scale = 1.0f;
403 if (node->custom2 == SHD_SUBSURFACE_RANDOM_WALK) {
404 random_walk_scale = 4.0f * M_PI;
405 }
406 subsurface_random_walk_radius_scale = GPU_constant(&random_walk_scale);
407
409 if (use_diffuse) {
410 flag |= GPU_MATFLAG_DIFFUSE;
411 }
412 if (use_refract) {
413 flag |= GPU_MATFLAG_REFRACT;
414 }
415 if (use_subsurf && in[SOCK_THIN_WALL_ID].socket_not_one()) {
417 }
418 if (use_transparency) {
420 }
421 if (use_coat) {
422 flag |= GPU_MATFLAG_COAT;
423 }
424 if (use_translucent) {
426 }
427
428 /* Coat tints lower layers. */
429 const bool coat_might_be_tinted = use_coat && in[SOCK_COAT_TINT_ID].might_be_tinted();
430
431 const bool refraction_might_be_tinted = use_refract &&
432 (in[SOCK_BASE_COLOR_ID].might_be_tinted() ||
433 coat_might_be_tinted);
434
435 bool specular_might_be_tinted = might_have_tinted_specular(
437 specular_might_be_tinted |= coat_might_be_tinted;
438 /* Multiscatter GGX can tint the reflection lobe. See `bsdf_lut`. */
439 specular_might_be_tinted |= (refraction_might_be_tinted &&
440 node->custom1 == SHD_GLOSSY_MULTI_GGX);
441
442 if (specular_might_be_tinted) {
443 /* Specular reflection. */
445 }
446 if (refraction_might_be_tinted) {
447 /* Specular refraction. */
449 }
450
451 GPU_material_flag_set(mat, flag);
452
453 /* Make constant link for the cases we optimize. This allows the driver to constant fold.
454 * Note that doing so specialize the final tree topology, and thus the shader becomes less
455 * reusable. So to be used with care.
456 * Also note that we do note override existing links. This is because it would leak the current
457 * nodes otherwise. */
458 const float zero = 0.0f;
459 if (!use_coat && in[SOCK_COAT_WEIGHT_ID].link == nullptr) {
460 in[SOCK_COAT_WEIGHT_ID].link = GPU_constant(&zero);
461 }
462 if (!use_subsurf && in[SOCK_SUBSURFACE_WEIGHT_ID].link == nullptr) {
464 }
465 if (!use_refract && in[SOCK_TRANSMISSION_WEIGHT_ID].link == nullptr) {
467 }
468
469 float use_multi_scatter = (node->custom1 == SHD_GLOSSY_MULTI_GGX) ? 1.0f : 0.0f;
470
471 return GPU_stack_link(mat,
472 node,
473 "node_bsdf_principled",
474 in,
475 out,
476 GPU_constant(&use_multi_scatter),
477 subsurface_random_walk_radius_scale,
480}
481
483{
484 const int sss_method = node->custom2;
485
487 *bke::node_find_socket(*node, SOCK_IN, "Subsurface IOR"_ustr),
488 sss_method == SHD_SUBSURFACE_RANDOM_WALK_SKIN);
489}
490
492#ifdef WITH_MATERIALX
493{
494 using InputsType = std::map<std::string, NodeItem>;
495
496 /* NOTE: commented inputs aren't used for node creation. */
497 auto bsdf_inputs = [&]() -> InputsType {
498 return {
499 {"base_color", get_input_value("Base Color", NodeItem::Type::Color3)},
500 {"diffuse_roughness", get_input_value("Diffuse Roughness", NodeItem::Type::Float)},
501 {"subsurface", get_input_value("Subsurface Weight", NodeItem::Type::Float)},
502 {"subsurface_scale", get_input_value("Subsurface Scale", NodeItem::Type::Float)},
503# if MATERIALX_MAJOR_VERSION <= 1 && MATERIALX_MINOR_VERSION <= 38
504 {"subsurface_radius", get_input_value("Subsurface Radius", NodeItem::Type::Vector3)},
505# else
506 {"subsurface_radius", get_input_value("Subsurface Radius", NodeItem::Type::Color3)},
507# endif
508 //{"subsurface_ior", get_input_value("Subsurface IOR", NodeItem::Type::Vector3)},
509 {"subsurface_anisotropy", get_input_value("Subsurface Anisotropy", NodeItem::Type::Float)},
510 {"metallic", get_input_value("Metallic", NodeItem::Type::Float)},
511 {"specular", get_input_value("Specular IOR Level", NodeItem::Type::Float)},
512 {"specular_tint", get_input_value("Specular Tint", NodeItem::Type::Color3)},
513 {"roughness", get_input_value("Roughness", NodeItem::Type::Float)},
514 {"anisotropic", get_input_value("Anisotropic", NodeItem::Type::Float)},
515 {"anisotropic_rotation", get_input_value("Anisotropic Rotation", NodeItem::Type::Float)},
516 {"sheen", get_input_value("Sheen Weight", NodeItem::Type::Float)},
517 {"sheen_roughness", get_input_value("Sheen Roughness", NodeItem::Type::Float)},
518 {"sheen_tint", get_input_value("Sheen Tint", NodeItem::Type::Color3)},
519 {"coat", get_input_value("Coat Weight", NodeItem::Type::Float)},
520 {"coat_roughness", get_input_value("Coat Roughness", NodeItem::Type::Float)},
521 {"coat_ior", get_input_value("Coat IOR", NodeItem::Type::Float)},
522 {"coat_tint", get_input_value("Coat Tint", NodeItem::Type::Color3)},
523 {"ior", get_input_value("IOR", NodeItem::Type::Float)},
524 {"transmission", get_input_value("Transmission Weight", NodeItem::Type::Float)},
525 {"thin_film_thickness", get_input_value("Thin Film Thickness", NodeItem::Type::Float)},
526 {"thin_film_IOR", get_input_value("Thin Film IOR", NodeItem::Type::Float)},
527 {"alpha", get_input_value("Alpha", NodeItem::Type::Float)},
528 {"normal", get_input_link("Normal", NodeItem::Type::Vector3)},
529 {"coat_normal", get_input_link("Coat Normal", NodeItem::Type::Vector3)},
530 {"tangent", get_input_link("Tangent", NodeItem::Type::Vector3)},
531 {"geometry_thin_walled", get_input_value("Thin Wall", NodeItem::Type::Boolean)},
532 };
533 };
534
535 auto edf_inputs = [&]() -> InputsType {
536 return {
537 {"emission", get_input_value("Emission Strength", NodeItem::Type::Float)},
538 {"emission_color", get_input_value("Emission Color", NodeItem::Type::Color3)},
539 };
540 };
541
542 NodeItem res = empty();
543
544 switch (to_type_) {
545 case NodeItem::Type::BSDF: {
546 auto in = bsdf_inputs();
547
548 NodeItem roughness = in["roughness"];
549 NodeItem diffuse_roughness = in["diffuse_roughness"];
550 NodeItem anisotropy = in["anisotropic"];
551 NodeItem rotation = in["anisotropic_rotation"] * val(360.0f);
552 NodeItem base_color = in["base_color"];
553 NodeItem specular = in["specular"];
554 NodeItem coat = in["coat"];
555 NodeItem ior = in["ior"];
556 NodeItem normal = in["normal"];
557 NodeItem tangent = in["tangent"];
558 NodeItem coat_normal = in["coat_normal"];
559
560 NodeItem n_main_tangent = empty();
561 if (tangent && normal) {
562 NodeItem n_tangent_rotate_normalize = tangent.rotate(rotation, normal).normalize();
563 n_main_tangent = anisotropy.if_else(
564 NodeItem::CompareOp::Greater, val(0.0f), n_tangent_rotate_normalize, tangent);
565 }
566
567 NodeItem n_coat_roughness_vector = create_node(
568 "roughness_anisotropy",
569 NodeItem::Type::Vector2,
570 {{"roughness", in["coat_roughness"]}, {"anisotropy", anisotropy}});
571
572 NodeItem n_coat_bsdf = create_node("dielectric_bsdf",
573 NodeItem::Type::BSDF,
574 {{"weight", coat},
575 {"tint", in["coat_tint"]},
576 {"ior", in["coat_ior"]},
577 {"scatter_mode", val(std::string("R"))},
578 {"roughness", n_coat_roughness_vector},
579 {"normal", coat_normal}});
580
581 if (tangent && coat_normal) {
582 NodeItem n_coat_tangent_rotate_normalize =
583 tangent.rotate(rotation, coat_normal).normalize();
584 NodeItem n_coat_tangent = anisotropy.if_else(
585 NodeItem::CompareOp::Greater, val(0.0f), n_coat_tangent_rotate_normalize, tangent);
586
587 n_coat_bsdf.set_input("tangent", n_coat_tangent);
588 }
589
590 NodeItem thin_film_thickness = in["thin_film_thickness"];
591 NodeItem thin_film_ior = in["thin_film_IOR"];
592 NodeItem n_thin_film_bsdf = create_node(
593 "thin_film_bsdf",
594 NodeItem::Type::BSDF,
595 {{"thickness", thin_film_thickness}, {"ior", thin_film_ior}});
596
597 NodeItem n_artistic_ior = create_node(
598 "artistic_ior",
599 NodeItem::Type::Multioutput,
600 {{"reflectivity", base_color * val(1.0f)}, {"edge_color", base_color * specular}});
601
602 NodeItem n_ior_out = n_artistic_ior.add_output("ior", NodeItem::Type::Color3);
603 NodeItem n_extinction_out = n_artistic_ior.add_output("extinction", NodeItem::Type::Color3);
604
605 NodeItem n_coat_affect_roughness_multiply2 = coat * val(0.0f) * in["coat_roughness"];
606 NodeItem n_coat_affected_roughness = n_coat_affect_roughness_multiply2.mix(roughness,
607 val(1.0f));
608
609 NodeItem n_main_roughness = create_node(
610 "roughness_anisotropy",
611 NodeItem::Type::Vector2,
612 {{"roughness", n_coat_affected_roughness}, {"anisotropy", anisotropy}});
613
614 NodeItem n_metal_bsdf = create_node("conductor_bsdf",
615 NodeItem::Type::BSDF,
616 {{"ior", n_ior_out},
617 {"extinction", n_extinction_out},
618 {"roughness", n_main_roughness},
619 {"normal", normal},
620 {"tangent", n_main_tangent}});
621
622 NodeItem n_specular_bsdf = create_node("dielectric_bsdf",
623 NodeItem::Type::BSDF,
624 {{"weight", specular},
625 {"tint", in["specular_tint"]},
626 {"ior", ior},
627 {"scatter_mode", val(std::string("R"))},
628 {"roughness", n_main_roughness},
629 {"normal", normal},
630 {"tangent", n_main_tangent}});
631
632 NodeItem n_coat_affected_transmission_roughness = n_coat_affect_roughness_multiply2.mix(
633 (roughness + roughness).clamp(), val(1.0f));
634
635 NodeItem n_transmission_roughness = create_node(
636 "roughness_anisotropy",
637 NodeItem::Type::Vector2,
638 {{"roughness", n_coat_affected_transmission_roughness}, {"anisotropy", anisotropy}});
639
640 NodeItem n_transmission_bsdf = create_node("dielectric_bsdf",
641 NodeItem::Type::BSDF,
642 {{"tint", base_color},
643 {"ior", ior},
644 {"roughness", n_transmission_roughness},
645 {"normal", normal},
646 {"tangent", n_main_tangent}});
647
648 NodeItem n_coat_gamma = coat.clamp(0.0f, 1.0f) * val(0.0f) + val(1.0f);
649 NodeItem n_coat_affected_subsurface_color = base_color.max(val(0.0f)) ^ n_coat_gamma;
650
651 /* Subsurface (non-thin-walled) */
652 NodeItem n_subsurface_bsdf = create_node(
653 "subsurface_bsdf",
654 NodeItem::Type::BSDF,
655 {{"color", n_coat_affected_subsurface_color},
656 {"radius", in["subsurface_radius"] * in["subsurface_scale"]},
657 {"anisotropy", in["subsurface_anisotropy"]},
658 {"normal", normal}});
659
660 /* Subsurface (thin-walled) - a 50/50 mix between reflection and transmission sub-graphs */
661 NodeItem n_subsurface_thin_walled_reflection_bsdf = create_node(
662 "oren_nayar_diffuse_bsdf",
663 NodeItem::Type::BSDF,
664 {{"color", n_coat_affected_subsurface_color},
665 {"roughness", diffuse_roughness},
666 {"weight", val(1.0f)},
667 {"normal", normal}});
668 NodeItem n_subsurface_thin_walled_transmission_bsdf = create_node(
669 "translucent_bsdf",
670 NodeItem::Type::BSDF,
671 {{"color", n_coat_affected_subsurface_color}, {"normal", normal}});
672
673 NodeItem n_subsurface_thin_walled_brdf_factor = val(1.0f) - in["subsurface_anisotropy"];
674 NodeItem n_one_plus_subsurface_scatter_anisotropy = val(1.0f) + in["subsurface_anisotropy"];
675
676 NodeItem n_subsurface_thin_walled_reflection = n_subsurface_thin_walled_reflection_bsdf *
677 n_subsurface_thin_walled_brdf_factor;
678 NodeItem n_subsurface_thin_walled_transmission = n_subsurface_thin_walled_transmission_bsdf *
679 n_one_plus_subsurface_scatter_anisotropy;
680 NodeItem n_subsurface_thin_walled_mix = val(0.5f).mix(n_subsurface_thin_walled_reflection,
681 n_subsurface_thin_walled_transmission);
682
683 /* Select the correct subsurface graph based on the thin-walled option. */
684 NodeItem n_subsurface_selector_mix = in["geometry_thin_walled"]
685 .convert(NodeItem::Type::Float)
686 .mix(n_subsurface_bsdf,
687 n_subsurface_thin_walled_mix);
688
689 NodeItem n_sheen_bsdf = create_node("sheen_bsdf",
690 NodeItem::Type::BSDF,
691 {{"weight", in["sheen"]},
692 {"color", in["sheen_tint"]},
693 {"roughness", in["sheen_roughness"]},
694 {"normal", normal}});
695
696 NodeItem n_diffuse_bsdf = create_node("oren_nayar_diffuse_bsdf",
697 NodeItem::Type::BSDF,
698 {{"color", base_color.max(val(0.0f)) ^ n_coat_gamma},
699 {"roughness", diffuse_roughness},
700 {"weight", val(1.0f)},
701 {"normal", normal}});
702
703 NodeItem n_subsurface_mix = in["subsurface"].mix(n_diffuse_bsdf, n_subsurface_selector_mix);
704
705 NodeItem n_sheen_layer = create_node(
706 "layer", NodeItem::Type::BSDF, {{"top", n_sheen_bsdf}, {"base", n_subsurface_mix}});
707
708 NodeItem n_transmission_mix = in["transmission"].mix(n_sheen_layer, n_transmission_bsdf);
709
710 NodeItem n_specular_layer = create_node(
711 "layer", NodeItem::Type::BSDF, {{"top", n_specular_bsdf}, {"base", n_transmission_mix}});
712
713 NodeItem n_metalness_mix = in["metallic"].mix(n_specular_layer, n_metal_bsdf);
714
715 NodeItem n_thin_film_layer = create_node(
716 "layer", NodeItem::Type::BSDF, {{"top", n_thin_film_bsdf}, {"base", n_metalness_mix}});
717
718 NodeItem n_coat_attenuation = coat.mix(val(MaterialX::Color3(1.0f, 1.0f, 1.0f)),
719 in["coat_tint"]);
720
721 res = create_node("layer",
722 NodeItem::Type::BSDF,
723 {{"top", n_coat_bsdf}, {"base", n_thin_film_layer * n_coat_attenuation}});
724 break;
725 }
726
727 case NodeItem::Type::EDF: {
728 auto in = edf_inputs();
729 res = create_node(
730 "uniform_edf", NodeItem::Type::EDF, {{"color", in["emission_color"] * in["emission"]}});
731 break;
732 }
733
734 case NodeItem::Type::SurfaceShader: {
735 auto in = bsdf_inputs();
736 auto e_in = edf_inputs();
737 in.insert(e_in.begin(), e_in.end());
738
739 NodeItem base_color = in["base_color"];
740
741 NodeItem anisotropy = in["anisotropic"];
742 NodeItem tangent = in["tangent"];
743 if (anisotropy) {
744 /* Anisotropy scaled down to approximately match the principled BSDF. */
745 anisotropy = anisotropy * val(0.7f);
746
747 /* Rotation is offset by 90 degrees and inverted to approximately align visually with
748 * principled BSDF direction. */
749 NodeItem rotation = -((in["anisotropic_rotation"] * val(360.0f)) + val(90.0f));
750
751 /* Only create a normal node locally if we need to use it to rotate the tangent vector.
752 * we don't actually pass this to the exported material. */
753 NodeItem normal = in["normal"];
754 if (!normal) {
755 const std::string world = "world";
756 normal =
757 create_node("normal", NodeItem::Type::Vector3, {{"space", val(world)}}).normalize();
758 }
759
760 if (!tangent) {
761 const std::string world = "world";
762 tangent =
763 create_node("tangent", NodeItem::Type::Vector3, {{"space", val(world)}}).normalize();
764 }
765
766 NodeItem n_tangent_rotate_normalize = tangent.rotate(rotation, normal).normalize();
767 tangent = anisotropy.if_else(
768 NodeItem::CompareOp::Greater, val(0.0f), n_tangent_rotate_normalize, tangent);
769 }
770
771 /* Enable OpenPBR thin film only if thickness > 0. */
772 NodeItem thin_film_thickness = in["thin_film_thickness"] * val(0.001f);
773 NodeItem thin_film_weight = thin_film_thickness.if_else(
774 NodeItem::CompareOp::Greater, val(0.0f), val(1.0f), val(0.0f));
775
776 /* "specular" here is "Specular IOR Level" in principled BSDF
777 * 0 = no specular
778 * 0.5 = full weight specular
779 * 1 = double specular weight */
780 NodeItem specular_weight = in["specular"] * val(2.0f);
781
782 res = create_node("open_pbr_surface",
783 NodeItem::Type::SurfaceShader,
784 {{"base_weight", val(1.0f)},
785 {"base_color", base_color},
786 {"base_diffuse_roughness", in["diffuse_roughness"]},
787 {"base_metalness", in["metallic"]},
788 {"specular_weight", specular_weight},
789 {"specular_color", in["specular_tint"]},
790 {"specular_roughness", in["roughness"]},
791 {"specular_ior", in["ior"]},
792 {"specular_roughness_anisotropy", anisotropy},
793 {"transmission_weight", in["transmission"]},
794 {"transmission_color", base_color},
795 {"subsurface_weight", in["subsurface"]},
796 {"subsurface_color", base_color},
797 {"subsurface_radius_scale", in["subsurface_radius"]},
798 {"subsurface_radius", in["subsurface_scale"]},
799 {"subsurface_scatter_anisotropy", in["subsurface_anisotropy"]},
800 {"fuzz_weight", in["sheen"]},
801 {"fuzz_color", in["sheen_tint"]},
802 {"fuzz_roughness", in["sheen_roughness"]},
803 {"coat_weight", in["coat"]},
804 {"coat_color", in["coat_tint"]},
805 {"coat_roughness", in["coat_roughness"]},
806 {"coat_ior", in["coat_ior"]},
807 /* Principled BSDF does not support anisotropy for the coat
808 * {"coat_roughness_anisotropy", anisotropic},
809 * {"geometry_coat_tangent", tangent}, */
810 {"emission_luminance", in["emission"]},
811 {"emission_color", in["emission_color"]},
812 {"thin_film_weight", thin_film_weight},
813 {"thin_film_thickness", thin_film_thickness},
814 {"thin_film_ior", in["thin_film_IOR"]},
815 {"geometry_normal", in["normal"]},
816 {"geometry_coat_normal", in["coat_normal"]},
817 {"geometry_tangent", tangent},
818 {"geometry_opacity", in["alpha"]},
819 {"geometry_thin_walled", in["geometry_thin_walled"]}});
820 break;
821 }
822
823 case NodeItem::Type::SurfaceOpacity: {
824 res = get_input_value("Alpha", NodeItem::Type::Float);
825 break;
826 }
827
828 default:
830 }
831 return res;
832}
833#endif
835
836} // namespace nodes::node_shader_bsdf_principled_cc
837
838/* node type definition */
840{
841 namespace file_ns = nodes::node_shader_bsdf_principled_cc;
842
843 static bke::bNodeType ntype;
844
845 sh_node_type_base(&ntype, "ShaderNodeBsdfPrincipled"_ustr, SH_NODE_BSDF_PRINCIPLED);
846 ntype.ui_name = "Principled BSDF";
847 ntype.ui_description =
848 "Physically-based, easy-to-use shader for rendering surface materials, based on the OpenPBR "
849 "model";
850 ntype.enum_name_legacy = "BSDF_PRINCIPLED";
852 ntype.declare = file_ns::node_declare;
856 ntype.initfunc = file_ns::node_shader_init_principled;
857 ntype.gpu_fn = file_ns::node_shader_gpu_bsdf_principled;
858 ntype.updatefunc = file_ns::node_shader_update_principled;
859 ntype.materialx_fn = file_ns::node_shader_materialx;
860
862}
863
864} // namespace blender
#define NODE_CLASS_SHADER
Definition BKE_node.hh:491
#define SH_NODE_BSDF_PRINCIPLED
#define M_PI
#define BLT_I18NCONTEXT_AMOUNT
#define BLT_I18NCONTEXT_ID_NODETREE
#define BLI_assert_unreachable()
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
void add_layout(std::function< void(ui::Layout &, bContext *, PointerRNA *)> draw)
bool bool_input_may_be(UString identifier, bool bool_value) const
VecBase< float, D > normalize(VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
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_init_principled(bNodeTree *, bNode *node)
static int node_shader_gpu_bsdf_principled(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_declare(NodeDeclarationBuilder &b)
static void node_shader_update_principled(bNodeTree *ntree, bNode *node)
static bool might_have_tinted_specular(const GPUNodeStack &base_color, const GPUNodeStack &metallic, const GPUNodeStack &specular_tint)
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_COAT
@ GPU_MATFLAG_SUBSURFACE
@ GPU_MATFLAG_TRANSLUCENT
@ GPU_MATFLAG_GLOSSY
@ GPU_MATFLAG_REFRACT
@ GPU_MATFLAG_DIFFUSE
@ GPU_MATFLAG_REFLECTION_MAYBE_COLORED
@ GPU_MATFLAG_REFRACTION_MAYBE_COLORED
@ GPU_MATFLAG_TRANSPARENT
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
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)
void register_node_type_sh_bsdf_principled()
GPUNodeLink * GPU_shading_data()
@ SHD_SUBSURFACE_RANDOM_WALK_SKIN
@ SHD_SUBSURFACE_RANDOM_WALK
@ SHD_SUBSURFACE_BURLEY
@ NTREE_IS_GPU_SHADER_INTERNAL
GPUNodeLink * GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name)
GPUNodeLink * GPU_kernel_globals()
@ PROP_FACTOR
Definition RNA_types.hh:308
@ PROP_DISTANCE
Definition RNA_types.hh:313
@ PROP_WAVELENGTH
Definition RNA_types.hh:344
@ SHD_GLOSSY_MULTI_GGX
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define SOCK_BASE_COLOR_ID
#define SOCK_TRANSMISSION_WEIGHT_ID
#define SOCK_SUBSURFACE_WEIGHT_ID
#define SOCK_COAT_WEIGHT_ID
#define SOCK_METALLIC_ID
#define SOCK_COAT_NORMAL_ID
#define SOCK_TANGENT_ID
#define SOCK_NORMAL_ID
#define SOCK_SPECULAR_TINT_ID
#define SOCK_SHEEN_WEIGHT_ID
#define SOCK_THIN_WALL_ID
#define SOCK_ALPHA_ID
#define SOCK_COAT_TINT_ID
#define min(a, b)
Definition sort.cc:38
closure color sheen(normal N, float roughness)
Definition stdcycles.h:96
bool might_be_tinted() const
bool socket_not_zero() const
T * data_as() const
Definition RNA_types.hh:139
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
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)
void active_set(bool active)