Blender V5.3
node_shader_subsurface_scattering.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 "node_shader_util.hh"
10
11#include "BLI_string.hh"
12
14#include "UI_resources.hh"
15
16#include "BKE_node_runtime.hh"
17
18namespace blender {
19
21
23{
24 const bNodeTree *ntree = b.tree_or_null();
25 const bool is_gpu_internal = ntree && (ntree->flag & NTREE_IS_GPU_SHADER_INTERNAL);
26
27 b.add_input<decl::Color>("Color"_ustr).default_value({0.8f, 0.8f, 0.8f, 1.0f});
28 b.add_input<decl::Float>("Scale"_ustr)
29 .default_value(0.005f)
30 .min(0.0f)
31 .max(1000.0f)
32 .description("Scale factor of the subsurface scattering radius");
33 b.add_input<decl::Vector>("Radius"_ustr)
34 .default_value({1.0f, 0.2f, 0.1f})
35 .min(0.0f)
36 .max(100.0f)
37 .description("Scattering radius per color channel (RGB), multiplied with Scale");
38 b.add_input<decl::Float>("IOR"_ustr)
39 .default_value(1.4f)
40 .min(1.01f)
41 .max(3.8f)
43 b.add_input<decl::Float>("Roughness"_ustr)
44 .default_value(1.0f)
45 .min(0.0f)
46 .max(1.0f)
48 b.add_input<decl::Float>("Anisotropy"_ustr)
49 .default_value(0.0f)
50 .min(-1.0f)
51 .max(1.0f)
53 .description(
54 "Directionality of volume scattering within the subsurface medium. "
55 "Zero scatters uniformly in all directions, positive values scatter more in the forward "
56 "direction, and negative values scatter more backwards. "
57 "For example, skin has been measured to have an anisotropy of 0.8");
58 b.add_input<decl::Vector>("Normal"_ustr).hide_value();
59 b.add_input<decl::Float>("Weight"_ustr).available(is_gpu_internal);
60 b.add_output<decl::Shader>("BSSRDF"_ustr);
61}
62
63static void node_shader_buts_subsurface(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
64{
65 layout.prop(ptr, "falloff", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
66}
67
69{
71 node->custom2 = true;
72}
73
75 bNode *node,
76 bNodeExecData * /*execdata*/,
79{
80 if (!in[6].link) {
81 GPU_link(mat, "world_normals_get", GPU_shading_data(), &in[6].link);
82 }
83
85
86 /* EEVEE's subsurface closure still uses the legacy approximation where the radius is scaled by
87 * 1/(4pi) (e.g., SHD_SUBSURFACE_RANDOM_WALK_LEGACY) */
88 GPUNodeLink *random_walk_radius_scale = nullptr;
89 float random_walk_scale = 1.0f;
90 if (node->custom1 == SHD_SUBSURFACE_RANDOM_WALK) {
91 random_walk_scale = 4.0f * M_PI;
92 }
93 random_walk_radius_scale = GPU_constant(&random_walk_scale);
94
95 return GPU_stack_link(mat,
96 node,
97 "node_subsurface_scattering",
98 in,
99 out,
100 random_walk_radius_scale,
103}
104
106{
107 const int sss_method = node->custom1;
108
109 for (bNodeSocket &sock : node->inputs) {
110 if (STR_ELEM(sock.name, "IOR", "Anisotropy")) {
111 bke::node_set_socket_availability(*ntree, sock, sss_method != SHD_SUBSURFACE_BURLEY);
112 }
113 if (STR_ELEM(sock.name, "Roughness")) {
114 const bool is_random_walk = sss_method == SHD_SUBSURFACE_RANDOM_WALK ||
116 bke::node_set_socket_availability(*ntree, sock, is_random_walk);
117 }
118 }
119}
120
122#ifdef WITH_MATERIALX
123{
124 /* NOTE: IOR and Subsurface Method isn't supported for this node in MaterialX. */
125 if (to_type_ != NodeItem::Type::BSDF) {
126 return empty();
127 }
128
129 NodeItem color = get_input_value("Color", NodeItem::Type::Color3);
130 NodeItem scale = get_input_value("Scale", NodeItem::Type::Float);
131# if MATERIALX_MAJOR_VERSION <= 1 && MATERIALX_MINOR_VERSION <= 38
132 NodeItem radius = get_input_value("Radius", NodeItem::Type::Vector3);
133# else
134 NodeItem radius = get_input_value("Radius", NodeItem::Type::Color3);
135# endif
136 NodeItem anisotropy = get_input_value("Anisotropy", NodeItem::Type::Float);
137 NodeItem normal = get_input_link("Normal", NodeItem::Type::Vector3);
138
139 return create_node("subsurface_bsdf",
140 NodeItem::Type::BSDF,
141 {{"weight", val(1.0f)},
142 {"color", color},
143 {"radius", radius * scale},
144 {"anisotropy", anisotropy},
145 {"normal", normal}});
146}
147#endif
149
150} // namespace nodes::node_shader_subsurface_scattering_cc
151
152/* node type definition */
154{
156
157 static bke::bNodeType ntype;
158
159 sh_node_type_base(&ntype, "ShaderNodeSubsurfaceScattering"_ustr, SH_NODE_SUBSURFACE_SCATTERING);
160 ntype.ui_name = "Subsurface Scattering";
161 ntype.ui_description =
162 "Subsurface multiple scattering shader to simulate light entering the surface and bouncing "
163 "internally.\nTypically used for materials such as skin, wax, marble or milk";
164 ntype.enum_name_legacy = "SUBSURFACE_SCATTERING";
166 ntype.declare = file_ns::node_declare;
169 ntype.draw_buttons = file_ns::node_shader_buts_subsurface;
171 ntype.initfunc = file_ns::node_shader_init_subsurface_scattering;
172 ntype.gpu_fn = file_ns::node_shader_gpu_subsurface_scattering;
173 ntype.updatefunc = file_ns::node_shader_update_subsurface_scattering;
174 ntype.materialx_fn = file_ns::node_shader_materialx;
175
177}
178
179} // namespace blender
#define NODE_CLASS_SHADER
Definition BKE_node.hh:491
#define SH_NODE_SUBSURFACE_SCATTERING
#define M_PI
#define STR_ELEM(...)
float3x3 scale(float3x3 mat, float2 scale)
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
static void node_shader_update_subsurface_scattering(bNodeTree *ntree, bNode *node)
static int node_shader_gpu_subsurface_scattering(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_buts_subsurface(ui::Layout &layout, bContext *, PointerRNA *ptr)
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_SUBSURFACE
@ GPU_MATFLAG_DIFFUSE
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)
GPUNodeLink * GPU_shading_data()
@ SHD_SUBSURFACE_RANDOM_WALK
@ SHD_SUBSURFACE_BURLEY
@ SHD_SUBSURFACE_RANDOM_WALK_LEGACY
@ NTREE_IS_GPU_SHADER_INTERNAL
GPUNodeLink * GPU_kernel_globals()
@ PROP_FACTOR
Definition RNA_types.hh:308
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define min(a, b)
Definition sort.cc:38
eNodeTree_Flag flag
static constexpr int _160
Definition BKE_node.hh:246
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)