Blender V5.3
node_shader_bsdf_hair_principled.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_string.hh"
10
11#include "node_shader_util.hh"
12#include "node_util.hh"
13
15#include "UI_resources.hh"
16
17namespace blender {
18
20
21/* Color, melanin and absorption coefficient default to approximately same brownish hair. */
23{
24 auto set_parametrization_reflectance = [](bNode &node) {
25 static_cast<NodeShaderHairPrincipled *>(node.storage)->parametrization =
27 };
28 auto set_parametrization_pigment_concentration = [](bNode &node) {
29 static_cast<NodeShaderHairPrincipled *>(node.storage)->parametrization =
31 };
32 auto set_parametrization_direct_absorption = [](bNode &node) {
33 static_cast<NodeShaderHairPrincipled *>(node.storage)->parametrization =
35 };
36 auto set_model_huang = [](bNode &node) {
37 static_cast<NodeShaderHairPrincipled *>(node.storage)->model = SHD_PRINCIPLED_HAIR_HUANG;
38 };
39 auto set_model_chiang = [](bNode &node) {
40 static_cast<NodeShaderHairPrincipled *>(node.storage)->model = SHD_PRINCIPLED_HAIR_CHIANG;
41 };
42
43 const bNodeTree *ntree = b.tree_or_null();
44 const bool is_gpu_internal = ntree && (ntree->flag & NTREE_IS_GPU_SHADER_INTERNAL);
45
46 b.add_input<decl::Color>("Color"_ustr)
47 .default_value({0.017513f, 0.005763f, 0.002059f, 1.0f})
48 .description("The RGB color of the strand. Only used in Direct Coloring")
49 .make_available(set_parametrization_reflectance);
50 b.add_input<decl::Float>("Melanin"_ustr)
51 .default_value(0.8f)
52 .min(0.0f)
53 .max(1.0f)
55 .description("Hair pigment. Specify its absolute quantity between 0 and 1")
56 .make_available(set_parametrization_pigment_concentration);
57 b.add_input<decl::Float>("Melanin Redness"_ustr)
58 .default_value(1.0f)
59 .min(0.0f)
60 .max(1.0f)
62 .description(
63 "Fraction of pheomelanin in melanin, gives yellowish to reddish color, as opposed to "
64 "the brownish to black color of eumelanin")
65 .make_available(set_parametrization_pigment_concentration);
66 b.add_input<decl::Color>("Tint"_ustr)
67 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
68 .description("Additional color used for dyeing the hair")
69 .make_available(set_parametrization_pigment_concentration);
70 b.add_input<decl::Vector>("Absorption Coefficient"_ustr)
71 .default_value({0.245531f, 0.52f, 1.365f})
72 .min(0.0f)
73 .max(1000.0f)
74 .description(
75 "Specifies energy absorption per unit length as light passes through the hair. A higher "
76 "value leads to a darker color")
77 .make_available(set_parametrization_direct_absorption);
78 b.add_input<decl::Float>("Aspect Ratio"_ustr)
79 .default_value(0.85f)
80 .min(0.0f)
81 .max(1.0f)
83 .description(
84 "The ratio of the minor axis to the major axis of an elliptical cross-section. "
85 "Recommended values are 0.8~1 for Asian hair, 0.65~0.9 for Caucasian hair, 0.5~0.65 for "
86 "African hair. The major axis is aligned with the curve normal, which is not supported "
87 "in particle hair")
88 .make_available(set_model_huang);
89 b.add_input<decl::Float>("Roughness"_ustr)
90 .default_value(0.3f)
91 .min(0.0f)
92 .max(1.0f)
94 .description("Hair roughness. A low value leads to a metallic look");
95 b.add_input<decl::Float>("Radial Roughness"_ustr)
96 .default_value(0.3f)
97 .min(0.0f)
98 .max(1.0f)
100 .make_available(set_model_chiang);
101 b.add_input<decl::Float>("Coat"_ustr)
102 .default_value(0.0f)
103 .min(0.0f)
104 .max(1.0f)
106 .description(
107 "Simulate a shiny coat by reducing the roughness to the given factor only for the first "
108 "light bounce (diffuse). Range [0, 1] is equivalent to a reduction of [0%, 100%] of the "
109 "original roughness")
110 .make_available(set_model_chiang);
111 b.add_input<decl::Float>("IOR"_ustr)
112 .default_value(1.55f)
113 .min(0.0f)
114 .max(1000.0f)
116 "Index of refraction determines how much the ray is bent. At 1.0 rays pass straight "
117 "through "
118 "like in a transparent material; higher values cause larger deflection in angle. "
119 "Default "
120 "value is 1.55 (the IOR of keratin)");
121 b.add_input<decl::Float>("Offset"_ustr)
122 .default_value(2.0f * float(M_PI) / 180.0f)
123 .min(-M_PI_2)
124 .max(M_PI_2)
126 .description(
127 "The tilt angle of the cuticle scales (the outermost part of the hair). They are always "
128 "tilted towards the hair root. The value is usually between 2 and 4 for human hair");
129 b.add_input<decl::Float>("Random Color"_ustr)
130 .default_value(0.0f)
131 .min(0.0f)
132 .max(1.0f)
134 .description("Vary the melanin concentration for each strand")
135 .make_available(set_parametrization_pigment_concentration);
136 b.add_input<decl::Float>("Random Roughness"_ustr)
137 .default_value(0.0f)
138 .min(0.0f)
139 .max(1.0f)
141 .description("Vary roughness values for each strand");
142 b.add_input<decl::Float>("Random"_ustr).hide_value();
143 b.add_input<decl::Float>("Weight"_ustr).available(is_gpu_internal);
144 b.add_input<decl::Float>("Reflection"_ustr, "R lobe"_ustr)
145 .default_value(1.0f)
146 .min(0.0f)
147 .max(1.0f)
149 .description(
150 "Optional factor for modulating the first light bounce off the hair surface. The color "
151 "of this component is always white. Keep this 1.0 for physical correctness")
152 .make_available(set_model_huang);
153 b.add_input<decl::Float>("Transmission"_ustr, "TT lobe"_ustr)
154 .default_value(1.0f)
155 .min(0.0f)
156 .max(1.0f)
158 .description(
159 "Optional factor for modulating the transmission component. Picks up the color of the "
160 "pigment inside the hair. Keep this 1.0 for physical correctness")
161 .make_available(set_model_huang);
162 b.add_input<decl::Float>("Secondary Reflection"_ustr, "TRT lobe"_ustr)
163 .default_value(1.0f)
164 .min(0.0f)
165 .max(1.0f)
167 .description(
168 "Optional factor for modulating the component which is transmitted into the hair, "
169 "reflected off the backside of the hair and then transmitted out of the hair. This "
170 "component is oriented approximately around the incoming direction, and picks up the "
171 "color of the pigment inside the hair. Keep this 1.0 for physical correctness")
172 .make_available(set_model_huang);
173 b.add_output<decl::Shader>("BSDF"_ustr);
174}
175
177{
178 layout.prop(ptr, "model", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
179 layout.prop(ptr, "parametrization", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
180}
181
182/* Initialize custom properties. */
184{
185 NodeShaderHairPrincipled *data = MEM_new<NodeShaderHairPrincipled>(__func__);
186
188 data->parametrization = SHD_PRINCIPLED_HAIR_REFLECTANCE;
189
190 node->storage = data;
191}
192
193/* Triggers (in)visibility of some sockets when changing the parametrization or the model. */
195{
197
198 int parametrization = data->parametrization;
199 int model = data->model;
200
201 for (bNodeSocket &sock : node->inputs) {
202 if (STREQ(sock.name, "Color")) {
204 *ntree, sock, parametrization == SHD_PRINCIPLED_HAIR_REFLECTANCE);
205 }
206 else if (STREQ(sock.name, "Melanin")) {
208 *ntree, sock, parametrization == SHD_PRINCIPLED_HAIR_PIGMENT_CONCENTRATION);
209 }
210 else if (STREQ(sock.name, "Melanin Redness")) {
212 *ntree, sock, parametrization == SHD_PRINCIPLED_HAIR_PIGMENT_CONCENTRATION);
213 }
214 else if (STREQ(sock.name, "Tint")) {
216 *ntree, sock, parametrization == SHD_PRINCIPLED_HAIR_PIGMENT_CONCENTRATION);
217 }
218 else if (STREQ(sock.name, "Absorption Coefficient")) {
220 *ntree, sock, parametrization == SHD_PRINCIPLED_HAIR_DIRECT_ABSORPTION);
221 }
222 else if (STREQ(sock.name, "Random Color")) {
224 *ntree, sock, parametrization == SHD_PRINCIPLED_HAIR_PIGMENT_CONCENTRATION);
225 }
226 else if (STREQ(sock.name, "Radial Roughness")) {
228 }
229 else if (STREQ(sock.name, "Coat")) {
231 }
232 else if (STREQ(sock.name, "Aspect Ratio")) {
234 }
235 else if (STR_ELEM(sock.name, "Reflection", "Transmission", "Secondary Reflection")) {
237 }
238 }
239}
240
242 bNode *node,
243 bNodeExecData * /*execdata*/,
246{
248
249 return GPU_stack_link(
250 mat, node, "node_bsdf_hair_principled", in, out, GPU_kernel_globals(), GPU_shading_data());
251}
252
253} // namespace nodes::node_shader_bsdf_hair_principled_cc
254
255/* node type definition */
257{
259
260 static bke::bNodeType ntype;
261
262 sh_node_type_base(&ntype, "ShaderNodeBsdfHairPrincipled"_ustr, SH_NODE_BSDF_HAIR_PRINCIPLED);
263 ntype.ui_name = "Principled Hair BSDF";
264 ntype.ui_description = "Physically-based, easy-to-use shader for rendering hair and fur";
265 ntype.enum_name_legacy = "BSDF_HAIR_PRINCIPLED";
267 ntype.declare = file_ns::node_declare;
270 ntype.draw_buttons = file_ns::node_shader_buts_principled_hair;
272 ntype.initfunc = file_ns::node_shader_init_hair_principled;
273 ntype.updatefunc = file_ns::node_shader_update_hair_principled;
274 ntype.gpu_fn = file_ns::node_shader_gpu_hair_principled;
276 ntype, "NodeShaderHairPrincipled", node_free_standard_storage, node_copy_standard_storage);
277
279}
280
281} // namespace blender
#define NODE_CLASS_SHADER
Definition BKE_node.hh:491
#define SH_NODE_BSDF_HAIR_PRINCIPLED
#define M_PI_2
#define M_PI
#define STR_ELEM(...)
#define STREQ(a, b)
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
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
static int node_shader_gpu_hair_principled(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_update_hair_principled(bNodeTree *ntree, bNode *node)
static void node_shader_buts_principled_hair(ui::Layout &layout, bContext *, PointerRNA *ptr)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
@ SHD_PRINCIPLED_HAIR_CHIANG
@ SHD_PRINCIPLED_HAIR_HUANG
bool object_cycles_shader_nodes_poll(const bContext *C)
void register_node_type_sh_bsdf_hair_principled()
@ GPU_MATFLAG_DIFFUSE
@ SHD_PRINCIPLED_HAIR_PIGMENT_CONCENTRATION
@ SHD_PRINCIPLED_HAIR_REFLECTANCE
@ SHD_PRINCIPLED_HAIR_DIRECT_ABSORPTION
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
BMesh const char void * data
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 node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
@ PROP_FACTOR
Definition RNA_types.hh:308
#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
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)