From c2a7185efac3fbd8d32fcd2bac5654b7b18a4a0b Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Tue, 19 Sep 2023 12:04:07 +0300 Subject: [PATCH 01/10] Initial --- .../nodes/node_shader_bsdf_principled.cc | 213 +++++++++++++++++- 1 file changed, 210 insertions(+), 3 deletions(-) diff --git a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc index d15b7cf27837..d50d076543d3 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc @@ -291,11 +291,11 @@ NODE_SHADER_MATERIALX_BEGIN /* TODO: implement for BSDF and EDF */ return empty(); } - + NodeItem base_color = get_input_value("Base Color", NodeItem::Type::Color3); NodeItem subsurface = get_input_value("Subsurface", NodeItem::Type::Float); - NodeItem subsurface_radius = get_input_value("Subsurface Radius", NodeItem::Type::Color3); + NodeItem subsurface_radius = get_input_value("Subsurface Radius", NodeItem::Type::Vector3); NodeItem subsurface_scale = get_input_value("Subsurface Scale", NodeItem::Type::Float); NodeItem metallic = get_input_value("Metallic", NodeItem::Type::Float); @@ -329,7 +329,7 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem tangent = get_input_link("Tangent", NodeItem::Type::Vector3); subsurface_radius = subsurface_radius * subsurface_scale; - +# if 0 /* Creating standard_surface */ NodeItem res = create_node("standard_surface", NodeItem::Type::SurfaceShader); res.set_input("base", val(1.0f)); @@ -377,6 +377,213 @@ NODE_SHADER_MATERIALX_BEGIN res.set_input("emission_color", emission); return res; + #else + + NodeItem res = create_node("surface", NodeItem::Type::SurfaceShader); + + NodeItem n_opacity_luminance = create_node("luminance", NodeItem::Type::Color3); + n_opacity_luminance.set_input("in", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); + res.set_input("opacity", n_opacity_luminance.extract(0)); + + NodeItem n_emission_edf = create_node("uniform_edf", NodeItem::Type::EDF); + n_emission_edf.set_input("color", emission * emission_strength); + res.set_input("edf", n_emission_edf); + + NodeItem n_coat_layer = create_node("layer", NodeItem::Type::BSDF); + NodeItem n_coat_attenuation = create_node("mix", NodeItem::Type::Color3); + n_coat_attenuation.set_input("fg", base_color); + n_coat_attenuation.set_input("bg", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); + n_coat_attenuation.set_input("mix", coat); + + NodeItem n_metalness_mix = create_node("mix", NodeItem::Type::BSDF); + n_metalness_mix.set_input("mix", metallic); + + NodeItem n_specular_layer = create_node("layer", NodeItem::Type::BSDF); + NodeItem n_transmission_mix = create_node("mix", NodeItem::Type::BSDF); + n_transmission_mix.set_input("mix", transmission); + + NodeItem n_sheen_layer = create_node("layer", NodeItem::Type::BSDF); + NodeItem n_subsurface_mix = create_node("mix", NodeItem::Type::BSDF); + n_subsurface_mix.set_input("mix", subsurface); + + NodeItem n_diffuse_bsdf = create_node("oren_nayar_diffuse_bsdf", NodeItem::Type::BSDF); + n_diffuse_bsdf.set_input("roughness", roughness); + n_diffuse_bsdf.set_input("weight", val(1.0f)); + if (normal) { + n_diffuse_bsdf.set_input("normal", normal); + } + + NodeItem n_coat_gamma = coat.clamp(0.0f, 1.0f) * val(0.0f) + val(1.0f); + n_diffuse_bsdf.set_input("color", base_color.max(val(0.0f)) ^ n_coat_gamma); + + n_subsurface_mix.set_input("bg", n_diffuse_bsdf); + + NodeItem n_selected_subsurface_bsdf = create_node("mix", NodeItem::Type::BSDF); + n_selected_subsurface_bsdf.set_input("mix", val(0.0f)); + + NodeItem n_subsurface_bsdf = create_node("subsurface_bsdf", NodeItem::Type::BSDF); + n_subsurface_bsdf.set_input("radius", subsurface_radius); + n_subsurface_bsdf.set_input("anisotropy", anisotropic); + if (normal) { + n_subsurface_bsdf.set_input("normal", normal); + } + + NodeItem n_coat_affected_subsurface_color = base_color.max(val(0.0f)) ^ n_coat_gamma; + n_subsurface_bsdf.set_input("color", n_coat_affected_subsurface_color); + + n_selected_subsurface_bsdf.set_input("bg", n_subsurface_bsdf); + + NodeItem n_translucent_bsdf = create_node("translucent_bsdf", NodeItem::Type::BSDF); + n_translucent_bsdf.set_input("color", n_coat_affected_subsurface_color); + if (normal) { + n_translucent_bsdf.set_input("normal", normal); + } + + n_selected_subsurface_bsdf.set_input("fg", n_translucent_bsdf); + + n_subsurface_mix.set_input("fg", n_selected_subsurface_bsdf); + + n_sheen_layer.set_input("base", n_subsurface_mix); + + NodeItem n_sheen_bsdf = create_node("sheen_bsdf", NodeItem::Type::BSDF); + n_sheen_bsdf.set_input("weight", sheen); + n_sheen_bsdf.set_input("color", base_color); + n_sheen_bsdf.set_input("roughness", roughness); + if (normal) { + n_translucent_bsdf.set_input("normal", normal); + } + + n_sheen_layer.set_input("top", n_sheen_bsdf); + + n_transmission_mix.set_input("bg", n_sheen_layer); + + NodeItem n_transmission_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); + n_transmission_bsdf.set_input("tint", base_color); + n_transmission_bsdf.set_input("ior", ior); + if (normal) { + n_transmission_bsdf.set_input("normal", normal); + } + + NodeItem n_transmission_roughness = create_node("roughness_anisotropy", NodeItem::Type::Vector2); + NodeItem n_coat_affected_transmission_roughness = create_node("mix", + NodeItem::Type::Float); + n_coat_affected_transmission_roughness.set_input("fg", val(1.0f)); + n_coat_affected_transmission_roughness.set_input("bg", (roughness + roughness).clamp(0.0f, 1.0f)); + + NodeItem n_coat_affect_roughness_multiply2 = coat * val(0.0f) * roughness; + + n_coat_affected_transmission_roughness.set_input("mix", n_coat_affect_roughness_multiply2); + n_transmission_roughness.set_input("roughness", n_coat_affected_transmission_roughness); + n_transmission_roughness.set_input("anisotropy", anisotropic); + n_transmission_bsdf.set_input("roughness", n_transmission_roughness); + + NodeItem n_main_tangent = empty(); + + if (tangent) { + NodeItem n_tangent_rotate = create_node("rotate3d", NodeItem::Type::Vector3); + n_tangent_rotate.set_input("in", tangent); + n_tangent_rotate.set_input("amount", anisotropic_rotation * val(360.0f)); + n_tangent_rotate.set_input("axis", normal); + + NodeItem n_tangent_rotate_normalize = create_node("normalize", NodeItem::Type::Vector3); + n_tangent_rotate.set_input("in", n_tangent_rotate); + + n_main_tangent = anisotropic.if_else( + NodeItem::CompareOp::Greater, val(0.0f), n_tangent_rotate_normalize, tangent); + } + if (n_main_tangent) { + n_transmission_bsdf.set_input("tangent", n_main_tangent); + } + + + n_transmission_mix.set_input("fg", n_transmission_bsdf); + n_specular_layer.set_input("base", n_transmission_mix); + + NodeItem n_specular_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); + n_specular_bsdf.set_input("weight", specular); + n_specular_bsdf.set_input("tint", base_color); + n_specular_bsdf.set_input("ior", ior); + n_specular_bsdf.set_input("scatter_mode", val(std::string("R"))); + if (n_main_tangent) { + n_specular_bsdf.set_input("tangent", n_main_tangent); + } + NodeItem n_main_roughness = create_node("roughness_anisotropy", NodeItem::Type::Vector2); + NodeItem n_coat_affected_roughness = create_node("mix", NodeItem::Type::Float); + n_coat_affected_roughness.set_input("fg", val(1.0f)); + n_coat_affected_roughness.set_input("bg", roughness); + n_coat_affected_roughness.set_input("mix", n_coat_affect_roughness_multiply2); + + n_main_roughness.set_input("roughness", n_coat_affected_roughness); + n_main_roughness.set_input("anisotropy", anisotropic); + + n_specular_bsdf.set_input("roughness", n_main_roughness); + + n_specular_layer.set_input("top", n_specular_bsdf); + + n_metalness_mix.set_input("bg", n_specular_layer); + + NodeItem n_metal_bsdf = create_node("conductor_bsdf", NodeItem::Type::BSDF); + if (normal) { + n_metal_bsdf.set_input("normal", normal); + } + if (n_main_tangent) { + n_metal_bsdf.set_input("tangent", n_main_tangent); + } + NodeItem n_artistic_ior = create_node("artistic_ior", NodeItem::Type::Multioutput); + n_artistic_ior.set_input("reflectivity", base_color * val(1.0f)); + n_artistic_ior.set_input("edge_color", base_color * specular); + NodeItem n_ior_out = n_artistic_ior.add_output("ior", NodeItem::Type::Color3); + NodeItem n_extinction_out = n_artistic_ior.add_output("extinction", NodeItem::Type::Color3); + n_metal_bsdf.set_input("ior", n_ior_out); + n_metal_bsdf.set_input("extinction", n_extinction_out); + + n_metalness_mix.set_input("fg", n_metal_bsdf); + + NodeItem n_thin_film_layer = create_node("layer", NodeItem::Type::BSDF); + n_thin_film_layer.set_input("base", n_metalness_mix); + + NodeItem n_thin_film_bsdf = create_node("thin_film_bsdf", NodeItem::Type::BSDF); + n_thin_film_bsdf.set_input("thickness", val(0.0f)); + n_thin_film_bsdf.set_input("ior", val(1.5f)); + + n_thin_film_layer.set_input("top", n_thin_film_bsdf); + + n_coat_layer.set_input("base", n_thin_film_layer * n_coat_attenuation); + + NodeItem n_coat_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); + if (normal) { + n_coat_bsdf.set_input("normal", normal); + } + n_coat_bsdf.set_input("weight", coat); + n_coat_bsdf.set_input("tint", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); + n_coat_bsdf.set_input("ior", ior); + n_coat_bsdf.set_input("scatter_mode", val(std::string("R"))); + + NodeItem n_coat_roughness_vector = create_node("roughness_anisotropy", NodeItem::Type::Vector2); + n_coat_roughness_vector.set_input("roughness", roughness); + n_coat_roughness_vector.set_input("anisotropy", anisotropic); + + n_coat_bsdf.set_input("roughness", n_coat_roughness_vector); + + if (tangent) { + NodeItem n_coat_tangent_rotate = create_node("rotate3d", NodeItem::Type::Vector3); + n_coat_tangent_rotate.set_input("in", tangent); + n_coat_tangent_rotate.set_input("amount", anisotropic_rotation * val(360.0f)); + n_coat_tangent_rotate.set_input("axis", normal); + + NodeItem n_coat_tangent_rotate_normalize = create_node("normalize", NodeItem::Type::Vector3); + n_coat_tangent_rotate_normalize.set_input("in", n_coat_tangent_rotate); + + NodeItem n_coat_tangent = anisotropic.if_else( + NodeItem::CompareOp::Greater, val(0.0f), n_coat_tangent_rotate_normalize, tangent); + + n_coat_bsdf.set_input("tangent", n_coat_tangent); + } + n_coat_layer.set_input("top", n_coat_bsdf); + + res.set_input("bsdf", n_coat_layer); +# endif + return res; } #endif NODE_SHADER_MATERIALX_END -- 2.30.2 From 872702b808c7d3480e9cc2740c3bc54abfda0d0d Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Tue, 19 Sep 2023 17:34:39 +0300 Subject: [PATCH 02/10] MaterialX: PrincipledBSDF splitted into nodes --- .../nodes/node_shader_bsdf_principled.cc | 377 ++++++++---------- 1 file changed, 158 insertions(+), 219 deletions(-) diff --git a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc index d50d076543d3..c607284b63ff 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc @@ -291,7 +291,7 @@ NODE_SHADER_MATERIALX_BEGIN /* TODO: implement for BSDF and EDF */ return empty(); } - + NodeItem base_color = get_input_value("Base Color", NodeItem::Type::Color3); NodeItem subsurface = get_input_value("Subsurface", NodeItem::Type::Float); @@ -329,156 +329,8 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem tangent = get_input_link("Tangent", NodeItem::Type::Vector3); subsurface_radius = subsurface_radius * subsurface_scale; -# if 0 - /* Creating standard_surface */ - NodeItem res = create_node("standard_surface", NodeItem::Type::SurfaceShader); - res.set_input("base", val(1.0f)); - res.set_input("base_color", base_color); - res.set_input("diffuse_roughness", roughness); - if (normal) { - res.set_input("normal", normal); - } - if (tangent) { - res.set_input("tangent", tangent); - } - res.set_input("metalness", metallic); - - res.set_input("specular", specular); - res.set_input("specular_color", base_color); - res.set_input("specular_roughness", roughness); - res.set_input("specular_IOR", ior); - res.set_input("specular_anisotropy", anisotropic); - res.set_input("specular_rotation", anisotropic_rotation); - - res.set_input("transmission", transmission); - res.set_input("transmission_color", base_color); - res.set_input("transmission_extra_roughness", roughness); - - res.set_input("subsurface", subsurface); - res.set_input("subsurface_color", base_color); - res.set_input("subsurface_radius", subsurface_radius); - res.set_input("subsurface_anisotropy", anisotropic); - - res.set_input("sheen", sheen); - res.set_input("sheen_color", base_color); - res.set_input("sheen_roughness", roughness); - - res.set_input("coat", coat); - res.set_input("coat_color", base_color); - res.set_input("coat_roughness", coat_roughness); - res.set_input("coat_IOR", ior); - res.set_input("coat_anisotropy", anisotropic); - res.set_input("coat_rotation", anisotropic_rotation); - if (coat_normal) { - res.set_input("coat_normal", coat_normal); - } - - res.set_input("emission", emission_strength); - res.set_input("emission_color", emission); - - return res; - #else - - NodeItem res = create_node("surface", NodeItem::Type::SurfaceShader); - - NodeItem n_opacity_luminance = create_node("luminance", NodeItem::Type::Color3); - n_opacity_luminance.set_input("in", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); - res.set_input("opacity", n_opacity_luminance.extract(0)); - - NodeItem n_emission_edf = create_node("uniform_edf", NodeItem::Type::EDF); - n_emission_edf.set_input("color", emission * emission_strength); - res.set_input("edf", n_emission_edf); - - NodeItem n_coat_layer = create_node("layer", NodeItem::Type::BSDF); - NodeItem n_coat_attenuation = create_node("mix", NodeItem::Type::Color3); - n_coat_attenuation.set_input("fg", base_color); - n_coat_attenuation.set_input("bg", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); - n_coat_attenuation.set_input("mix", coat); - - NodeItem n_metalness_mix = create_node("mix", NodeItem::Type::BSDF); - n_metalness_mix.set_input("mix", metallic); - - NodeItem n_specular_layer = create_node("layer", NodeItem::Type::BSDF); - NodeItem n_transmission_mix = create_node("mix", NodeItem::Type::BSDF); - n_transmission_mix.set_input("mix", transmission); - - NodeItem n_sheen_layer = create_node("layer", NodeItem::Type::BSDF); - NodeItem n_subsurface_mix = create_node("mix", NodeItem::Type::BSDF); - n_subsurface_mix.set_input("mix", subsurface); - - NodeItem n_diffuse_bsdf = create_node("oren_nayar_diffuse_bsdf", NodeItem::Type::BSDF); - n_diffuse_bsdf.set_input("roughness", roughness); - n_diffuse_bsdf.set_input("weight", val(1.0f)); - if (normal) { - n_diffuse_bsdf.set_input("normal", normal); - } - - NodeItem n_coat_gamma = coat.clamp(0.0f, 1.0f) * val(0.0f) + val(1.0f); - n_diffuse_bsdf.set_input("color", base_color.max(val(0.0f)) ^ n_coat_gamma); - - n_subsurface_mix.set_input("bg", n_diffuse_bsdf); - - NodeItem n_selected_subsurface_bsdf = create_node("mix", NodeItem::Type::BSDF); - n_selected_subsurface_bsdf.set_input("mix", val(0.0f)); - - NodeItem n_subsurface_bsdf = create_node("subsurface_bsdf", NodeItem::Type::BSDF); - n_subsurface_bsdf.set_input("radius", subsurface_radius); - n_subsurface_bsdf.set_input("anisotropy", anisotropic); - if (normal) { - n_subsurface_bsdf.set_input("normal", normal); - } - - NodeItem n_coat_affected_subsurface_color = base_color.max(val(0.0f)) ^ n_coat_gamma; - n_subsurface_bsdf.set_input("color", n_coat_affected_subsurface_color); - - n_selected_subsurface_bsdf.set_input("bg", n_subsurface_bsdf); - - NodeItem n_translucent_bsdf = create_node("translucent_bsdf", NodeItem::Type::BSDF); - n_translucent_bsdf.set_input("color", n_coat_affected_subsurface_color); - if (normal) { - n_translucent_bsdf.set_input("normal", normal); - } - - n_selected_subsurface_bsdf.set_input("fg", n_translucent_bsdf); - - n_subsurface_mix.set_input("fg", n_selected_subsurface_bsdf); - - n_sheen_layer.set_input("base", n_subsurface_mix); - - NodeItem n_sheen_bsdf = create_node("sheen_bsdf", NodeItem::Type::BSDF); - n_sheen_bsdf.set_input("weight", sheen); - n_sheen_bsdf.set_input("color", base_color); - n_sheen_bsdf.set_input("roughness", roughness); - if (normal) { - n_translucent_bsdf.set_input("normal", normal); - } - - n_sheen_layer.set_input("top", n_sheen_bsdf); - - n_transmission_mix.set_input("bg", n_sheen_layer); - - NodeItem n_transmission_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); - n_transmission_bsdf.set_input("tint", base_color); - n_transmission_bsdf.set_input("ior", ior); - if (normal) { - n_transmission_bsdf.set_input("normal", normal); - } - - NodeItem n_transmission_roughness = create_node("roughness_anisotropy", NodeItem::Type::Vector2); - NodeItem n_coat_affected_transmission_roughness = create_node("mix", - NodeItem::Type::Float); - n_coat_affected_transmission_roughness.set_input("fg", val(1.0f)); - n_coat_affected_transmission_roughness.set_input("bg", (roughness + roughness).clamp(0.0f, 1.0f)); - - NodeItem n_coat_affect_roughness_multiply2 = coat * val(0.0f) * roughness; - - n_coat_affected_transmission_roughness.set_input("mix", n_coat_affect_roughness_multiply2); - n_transmission_roughness.set_input("roughness", n_coat_affected_transmission_roughness); - n_transmission_roughness.set_input("anisotropy", anisotropic); - n_transmission_bsdf.set_input("roughness", n_transmission_roughness); NodeItem n_main_tangent = empty(); - if (tangent) { NodeItem n_tangent_rotate = create_node("rotate3d", NodeItem::Type::Vector3); n_tangent_rotate.set_input("in", tangent); @@ -486,84 +338,25 @@ NODE_SHADER_MATERIALX_BEGIN n_tangent_rotate.set_input("axis", normal); NodeItem n_tangent_rotate_normalize = create_node("normalize", NodeItem::Type::Vector3); - n_tangent_rotate.set_input("in", n_tangent_rotate); + n_tangent_rotate_normalize.set_input("in", n_tangent_rotate); n_main_tangent = anisotropic.if_else( NodeItem::CompareOp::Greater, val(0.0f), n_tangent_rotate_normalize, tangent); } - if (n_main_tangent) { - n_transmission_bsdf.set_input("tangent", n_main_tangent); - } - - - n_transmission_mix.set_input("fg", n_transmission_bsdf); - n_specular_layer.set_input("base", n_transmission_mix); - - NodeItem n_specular_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); - n_specular_bsdf.set_input("weight", specular); - n_specular_bsdf.set_input("tint", base_color); - n_specular_bsdf.set_input("ior", ior); - n_specular_bsdf.set_input("scatter_mode", val(std::string("R"))); - if (n_main_tangent) { - n_specular_bsdf.set_input("tangent", n_main_tangent); - } - NodeItem n_main_roughness = create_node("roughness_anisotropy", NodeItem::Type::Vector2); - NodeItem n_coat_affected_roughness = create_node("mix", NodeItem::Type::Float); - n_coat_affected_roughness.set_input("fg", val(1.0f)); - n_coat_affected_roughness.set_input("bg", roughness); - n_coat_affected_roughness.set_input("mix", n_coat_affect_roughness_multiply2); - - n_main_roughness.set_input("roughness", n_coat_affected_roughness); - n_main_roughness.set_input("anisotropy", anisotropic); - - n_specular_bsdf.set_input("roughness", n_main_roughness); - - n_specular_layer.set_input("top", n_specular_bsdf); - - n_metalness_mix.set_input("bg", n_specular_layer); - - NodeItem n_metal_bsdf = create_node("conductor_bsdf", NodeItem::Type::BSDF); - if (normal) { - n_metal_bsdf.set_input("normal", normal); - } - if (n_main_tangent) { - n_metal_bsdf.set_input("tangent", n_main_tangent); - } - NodeItem n_artistic_ior = create_node("artistic_ior", NodeItem::Type::Multioutput); - n_artistic_ior.set_input("reflectivity", base_color * val(1.0f)); - n_artistic_ior.set_input("edge_color", base_color * specular); - NodeItem n_ior_out = n_artistic_ior.add_output("ior", NodeItem::Type::Color3); - NodeItem n_extinction_out = n_artistic_ior.add_output("extinction", NodeItem::Type::Color3); - n_metal_bsdf.set_input("ior", n_ior_out); - n_metal_bsdf.set_input("extinction", n_extinction_out); - - n_metalness_mix.set_input("fg", n_metal_bsdf); - - NodeItem n_thin_film_layer = create_node("layer", NodeItem::Type::BSDF); - n_thin_film_layer.set_input("base", n_metalness_mix); - - NodeItem n_thin_film_bsdf = create_node("thin_film_bsdf", NodeItem::Type::BSDF); - n_thin_film_bsdf.set_input("thickness", val(0.0f)); - n_thin_film_bsdf.set_input("ior", val(1.5f)); - - n_thin_film_layer.set_input("top", n_thin_film_bsdf); - - n_coat_layer.set_input("base", n_thin_film_layer * n_coat_attenuation); - - NodeItem n_coat_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); - if (normal) { - n_coat_bsdf.set_input("normal", normal); - } - n_coat_bsdf.set_input("weight", coat); - n_coat_bsdf.set_input("tint", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); - n_coat_bsdf.set_input("ior", ior); - n_coat_bsdf.set_input("scatter_mode", val(std::string("R"))); NodeItem n_coat_roughness_vector = create_node("roughness_anisotropy", NodeItem::Type::Vector2); n_coat_roughness_vector.set_input("roughness", roughness); n_coat_roughness_vector.set_input("anisotropy", anisotropic); + NodeItem n_coat_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); + n_coat_bsdf.set_input("weight", coat); + n_coat_bsdf.set_input("tint", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); + n_coat_bsdf.set_input("ior", ior); + n_coat_bsdf.set_input("scatter_mode", val(std::string("R"))); n_coat_bsdf.set_input("roughness", n_coat_roughness_vector); + if (normal) { + n_coat_bsdf.set_input("normal", normal); + } if (tangent) { NodeItem n_coat_tangent_rotate = create_node("rotate3d", NodeItem::Type::Vector3); @@ -579,10 +372,156 @@ NODE_SHADER_MATERIALX_BEGIN n_coat_bsdf.set_input("tangent", n_coat_tangent); } - n_coat_layer.set_input("top", n_coat_bsdf); + NodeItem n_thin_film_bsdf = create_node("thin_film_bsdf", NodeItem::Type::BSDF); + n_thin_film_bsdf.set_input("thickness", val(0.0f)); + n_thin_film_bsdf.set_input("ior", val(1.5f)); + + NodeItem n_artistic_ior = create_node("artistic_ior", NodeItem::Type::Multioutput); + n_artistic_ior.set_input("reflectivity", base_color * val(1.0f)); + n_artistic_ior.set_input("edge_color", base_color * specular); + NodeItem n_ior_out = n_artistic_ior.add_output("ior", NodeItem::Type::Color3); + NodeItem n_extinction_out = n_artistic_ior.add_output("extinction", NodeItem::Type::Color3); + + NodeItem n_coat_affect_roughness_multiply2 = coat * val(0.0f) * roughness; + NodeItem n_coat_affected_roughness = create_node("mix", NodeItem::Type::Float); + n_coat_affected_roughness.set_input("fg", val(1.0f)); + n_coat_affected_roughness.set_input("bg", roughness); + n_coat_affected_roughness.set_input("mix", n_coat_affect_roughness_multiply2); + + NodeItem n_main_roughness = create_node("roughness_anisotropy", NodeItem::Type::Vector2); + n_main_roughness.set_input("roughness", n_coat_affected_roughness); + n_main_roughness.set_input("anisotropy", anisotropic); + + NodeItem n_metal_bsdf = create_node("conductor_bsdf", NodeItem::Type::BSDF); + n_metal_bsdf.set_input("ior", n_ior_out); + n_metal_bsdf.set_input("extinction", n_extinction_out); + n_metal_bsdf.set_input("roughness", n_main_roughness); + if (normal) { + n_metal_bsdf.set_input("normal", normal); + } + if (n_main_tangent) { + n_metal_bsdf.set_input("tangent", n_main_tangent); + } + + NodeItem n_specular_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); + n_specular_bsdf.set_input("weight", specular); + n_specular_bsdf.set_input("tint", base_color); + n_specular_bsdf.set_input("ior", ior); + n_specular_bsdf.set_input("scatter_mode", val(std::string("R"))); + n_specular_bsdf.set_input("roughness", n_main_roughness); + if (normal) { + n_specular_bsdf.set_input("normal", normal); + } + if (n_main_tangent) { + n_specular_bsdf.set_input("tangent", n_main_tangent); + } + + NodeItem n_coat_affected_transmission_roughness = create_node("mix", NodeItem::Type::Float); + n_coat_affected_transmission_roughness.set_input("fg", val(1.0f)); + n_coat_affected_transmission_roughness.set_input("bg", + (roughness + roughness).clamp(0.0f, 1.0f)); + n_coat_affected_transmission_roughness.set_input("mix", n_coat_affect_roughness_multiply2); + + NodeItem n_transmission_roughness = create_node("roughness_anisotropy", NodeItem::Type::Vector2); + n_transmission_roughness.set_input("roughness", n_coat_affected_transmission_roughness); + n_transmission_roughness.set_input("anisotropy", anisotropic); + + NodeItem n_transmission_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); + n_transmission_bsdf.set_input("tint", base_color); + n_transmission_bsdf.set_input("ior", ior); + n_transmission_bsdf.set_input("roughness", n_transmission_roughness); + if (normal) { + n_transmission_bsdf.set_input("normal", normal); + } + if (n_main_tangent) { + n_transmission_bsdf.set_input("tangent", n_main_tangent); + } + + NodeItem n_coat_gamma = coat.clamp(0.0f, 1.0f) * val(0.0f) + val(1.0f); + NodeItem n_coat_affected_subsurface_color = base_color.max(val(0.0f)) ^ n_coat_gamma; + NodeItem n_translucent_bsdf = create_node("translucent_bsdf", NodeItem::Type::BSDF); + n_translucent_bsdf.set_input("color", n_coat_affected_subsurface_color); + if (normal) { + n_translucent_bsdf.set_input("normal", normal); + } + + NodeItem n_subsurface_bsdf = create_node("subsurface_bsdf", NodeItem::Type::BSDF); + n_subsurface_bsdf.set_input("color", n_coat_affected_subsurface_color); + n_subsurface_bsdf.set_input("radius", subsurface_radius); + n_subsurface_bsdf.set_input("anisotropy", anisotropic); + if (normal) { + n_subsurface_bsdf.set_input("normal", normal); + } + + NodeItem n_selected_subsurface_bsdf = create_node("mix", NodeItem::Type::BSDF); + n_selected_subsurface_bsdf.set_input("fg", n_translucent_bsdf); + n_selected_subsurface_bsdf.set_input("bg", n_subsurface_bsdf); + n_selected_subsurface_bsdf.set_input("mix", val(0.0f)); + + NodeItem n_sheen_bsdf = create_node("sheen_bsdf", NodeItem::Type::BSDF); + n_sheen_bsdf.set_input("weight", sheen); + n_sheen_bsdf.set_input("color", base_color); + n_sheen_bsdf.set_input("roughness", roughness); + if (normal) { + n_sheen_bsdf.set_input("normal", normal); + } + + NodeItem n_diffuse_bsdf = create_node("oren_nayar_diffuse_bsdf", NodeItem::Type::BSDF); + n_diffuse_bsdf.set_input("color", base_color.max(val(0.0f)) ^ n_coat_gamma); + n_diffuse_bsdf.set_input("roughness", roughness); + n_diffuse_bsdf.set_input("weight", val(1.0f)); + if (normal) { + n_diffuse_bsdf.set_input("normal", normal); + } + + NodeItem n_subsurface_mix = create_node("mix", NodeItem::Type::BSDF); + n_subsurface_mix.set_input("fg", n_selected_subsurface_bsdf); + n_subsurface_mix.set_input("bg", n_diffuse_bsdf); + n_subsurface_mix.set_input("mix", subsurface); + + NodeItem n_sheen_layer = create_node("layer", NodeItem::Type::BSDF); + n_sheen_layer.set_input("top", n_sheen_bsdf); + n_sheen_layer.set_input("base", n_subsurface_mix); + + NodeItem n_transmission_mix = create_node("mix", NodeItem::Type::BSDF); + n_transmission_mix.set_input("fg", n_transmission_bsdf); + n_transmission_mix.set_input("bg", n_sheen_layer); + n_transmission_mix.set_input("mix", transmission); + + NodeItem n_specular_layer = create_node("layer", NodeItem::Type::BSDF); + n_specular_layer.set_input("top", n_specular_bsdf); + n_specular_layer.set_input("base", n_transmission_mix); + + NodeItem n_metalness_mix = create_node("mix", NodeItem::Type::BSDF); + n_metalness_mix.set_input("fg", n_metal_bsdf); + n_metalness_mix.set_input("bg", n_specular_layer); + n_metalness_mix.set_input("mix", metallic); + + NodeItem n_thin_film_layer = create_node("layer", NodeItem::Type::BSDF); + n_thin_film_layer.set_input("top", n_thin_film_bsdf); + n_thin_film_layer.set_input("base", n_metalness_mix); + + NodeItem n_opacity_luminance = create_node("luminance", NodeItem::Type::Color3); + n_opacity_luminance.set_input("in", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); + + NodeItem n_emission_edf = create_node("uniform_edf", NodeItem::Type::EDF); + n_emission_edf.set_input("color", emission * emission_strength); + + NodeItem n_coat_attenuation = create_node("mix", NodeItem::Type::Color3); + n_coat_attenuation.set_input("fg", base_color); + n_coat_attenuation.set_input("bg", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); + n_coat_attenuation.set_input("mix", coat); + + NodeItem n_coat_layer = create_node("layer", NodeItem::Type::BSDF); + n_coat_layer.set_input("top", n_coat_bsdf); + n_coat_layer.set_input("base", n_thin_film_layer * n_coat_attenuation); + + NodeItem res = create_node("surface", NodeItem::Type::SurfaceShader); res.set_input("bsdf", n_coat_layer); -# endif + res.set_input("edf", n_emission_edf); + res.set_input("opacity", n_opacity_luminance.extract(0)); + return res; } #endif -- 2.30.2 From 4a2633b85dd0ea9c829277efd41586cade84ae22 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Wed, 20 Sep 2023 13:48:56 +0300 Subject: [PATCH 03/10] MaterialX: fix Mix node and PrincipleBSDF node --- .../nodes/node_shader_bsdf_principled.cc | 418 ++++++++++-------- .../shader/nodes/node_shader_mix_shader.cc | 4 +- 2 files changed, 233 insertions(+), 189 deletions(-) diff --git a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc index c607284b63ff..1726ba7b8c1b 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc @@ -287,11 +287,6 @@ static void node_shader_update_principled(bNodeTree *ntree, bNode *node) NODE_SHADER_MATERIALX_BEGIN #ifdef WITH_MATERIALX { - if (to_type_ != NodeItem::Type::SurfaceShader) { - /* TODO: implement for BSDF and EDF */ - return empty(); - } - NodeItem base_color = get_input_value("Base Color", NodeItem::Type::Color3); NodeItem subsurface = get_input_value("Subsurface", NodeItem::Type::Float); @@ -330,198 +325,247 @@ NODE_SHADER_MATERIALX_BEGIN subsurface_radius = subsurface_radius * subsurface_scale; - NodeItem n_main_tangent = empty(); - if (tangent) { - NodeItem n_tangent_rotate = create_node("rotate3d", NodeItem::Type::Vector3); - n_tangent_rotate.set_input("in", tangent); - n_tangent_rotate.set_input("amount", anisotropic_rotation * val(360.0f)); - n_tangent_rotate.set_input("axis", normal); + NodeItem res = empty(); + if (to_type_ == NodeItem::Type::BSDF) { + NodeItem n_main_tangent = empty(); + if (tangent) { + NodeItem n_tangent_rotate = create_node("rotate3d", NodeItem::Type::Vector3); + n_tangent_rotate.set_input("in", tangent); + n_tangent_rotate.set_input("amount", anisotropic_rotation * val(360.0f)); + n_tangent_rotate.set_input("axis", normal); - NodeItem n_tangent_rotate_normalize = create_node("normalize", NodeItem::Type::Vector3); - n_tangent_rotate_normalize.set_input("in", n_tangent_rotate); + NodeItem n_tangent_rotate_normalize = create_node("normalize", NodeItem::Type::Vector3); + n_tangent_rotate_normalize.set_input("in", n_tangent_rotate); - n_main_tangent = anisotropic.if_else( - NodeItem::CompareOp::Greater, val(0.0f), n_tangent_rotate_normalize, tangent); + n_main_tangent = anisotropic.if_else( + NodeItem::CompareOp::Greater, val(0.0f), n_tangent_rotate_normalize, tangent); + } + + NodeItem n_coat_roughness_vector = create_node("roughness_anisotropy", + NodeItem::Type::Vector2); + n_coat_roughness_vector.set_input("roughness", roughness); + n_coat_roughness_vector.set_input("anisotropy", anisotropic); + + NodeItem n_coat_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); + n_coat_bsdf.set_input("weight", coat); + n_coat_bsdf.set_input("tint", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); + n_coat_bsdf.set_input("ior", ior); + n_coat_bsdf.set_input("scatter_mode", val(std::string("R"))); + n_coat_bsdf.set_input("roughness", n_coat_roughness_vector); + if (normal) { + n_coat_bsdf.set_input("normal", normal); + } + + if (tangent) { + NodeItem n_coat_tangent_rotate = create_node("rotate3d", NodeItem::Type::Vector3); + n_coat_tangent_rotate.set_input("in", tangent); + n_coat_tangent_rotate.set_input("amount", anisotropic_rotation * val(360.0f)); + n_coat_tangent_rotate.set_input("axis", normal); + + NodeItem n_coat_tangent_rotate_normalize = create_node("normalize", NodeItem::Type::Vector3); + n_coat_tangent_rotate_normalize.set_input("in", n_coat_tangent_rotate); + + NodeItem n_coat_tangent = anisotropic.if_else( + NodeItem::CompareOp::Greater, val(0.0f), n_coat_tangent_rotate_normalize, tangent); + + n_coat_bsdf.set_input("tangent", n_coat_tangent); + } + + NodeItem n_thin_film_bsdf = create_node("thin_film_bsdf", NodeItem::Type::BSDF); + n_thin_film_bsdf.set_input("thickness", val(0.0f)); + n_thin_film_bsdf.set_input("ior", val(1.5f)); + + NodeItem n_artistic_ior = create_node("artistic_ior", NodeItem::Type::Multioutput); + n_artistic_ior.set_input("reflectivity", base_color * val(1.0f)); + n_artistic_ior.set_input("edge_color", base_color * specular); + NodeItem n_ior_out = n_artistic_ior.add_output("ior", NodeItem::Type::Color3); + NodeItem n_extinction_out = n_artistic_ior.add_output("extinction", NodeItem::Type::Color3); + + NodeItem n_coat_affect_roughness_multiply2 = coat * val(0.0f) * roughness; + NodeItem n_coat_affected_roughness = create_node("mix", NodeItem::Type::Float); + n_coat_affected_roughness.set_input("fg", val(1.0f)); + n_coat_affected_roughness.set_input("bg", roughness); + n_coat_affected_roughness.set_input("mix", n_coat_affect_roughness_multiply2); + + NodeItem n_main_roughness = create_node("roughness_anisotropy", NodeItem::Type::Vector2); + n_main_roughness.set_input("roughness", n_coat_affected_roughness); + n_main_roughness.set_input("anisotropy", anisotropic); + + NodeItem n_metal_bsdf = create_node("conductor_bsdf", NodeItem::Type::BSDF); + n_metal_bsdf.set_input("ior", n_ior_out); + n_metal_bsdf.set_input("extinction", n_extinction_out); + n_metal_bsdf.set_input("roughness", n_main_roughness); + if (normal) { + n_metal_bsdf.set_input("normal", normal); + } + if (n_main_tangent) { + n_metal_bsdf.set_input("tangent", n_main_tangent); + } + + NodeItem n_specular_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); + n_specular_bsdf.set_input("weight", specular); + n_specular_bsdf.set_input("tint", base_color); + n_specular_bsdf.set_input("ior", ior); + n_specular_bsdf.set_input("scatter_mode", val(std::string("R"))); + n_specular_bsdf.set_input("roughness", n_main_roughness); + if (normal) { + n_specular_bsdf.set_input("normal", normal); + } + if (n_main_tangent) { + n_specular_bsdf.set_input("tangent", n_main_tangent); + } + + NodeItem n_coat_affected_transmission_roughness = create_node("mix", NodeItem::Type::Float); + n_coat_affected_transmission_roughness.set_input("fg", val(1.0f)); + n_coat_affected_transmission_roughness.set_input("bg", + (roughness + roughness).clamp(0.0f, 1.0f)); + n_coat_affected_transmission_roughness.set_input("mix", n_coat_affect_roughness_multiply2); + + NodeItem n_transmission_roughness = create_node("roughness_anisotropy", + NodeItem::Type::Vector2); + n_transmission_roughness.set_input("roughness", n_coat_affected_transmission_roughness); + n_transmission_roughness.set_input("anisotropy", anisotropic); + + NodeItem n_transmission_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); + n_transmission_bsdf.set_input("tint", base_color); + n_transmission_bsdf.set_input("ior", ior); + n_transmission_bsdf.set_input("roughness", n_transmission_roughness); + if (normal) { + n_transmission_bsdf.set_input("normal", normal); + } + if (n_main_tangent) { + n_transmission_bsdf.set_input("tangent", n_main_tangent); + } + + NodeItem n_coat_gamma = coat.clamp(0.0f, 1.0f) * val(0.0f) + val(1.0f); + NodeItem n_coat_affected_subsurface_color = base_color.max(val(0.0f)) ^ n_coat_gamma; + NodeItem n_translucent_bsdf = create_node("translucent_bsdf", NodeItem::Type::BSDF); + n_translucent_bsdf.set_input("color", n_coat_affected_subsurface_color); + if (normal) { + n_translucent_bsdf.set_input("normal", normal); + } + + NodeItem n_subsurface_bsdf = create_node("subsurface_bsdf", NodeItem::Type::BSDF); + n_subsurface_bsdf.set_input("color", n_coat_affected_subsurface_color); + n_subsurface_bsdf.set_input("radius", subsurface_radius); + n_subsurface_bsdf.set_input("anisotropy", anisotropic); + if (normal) { + n_subsurface_bsdf.set_input("normal", normal); + } + + NodeItem n_selected_subsurface_bsdf = create_node("mix", NodeItem::Type::BSDF); + n_selected_subsurface_bsdf.set_input("fg", n_translucent_bsdf); + n_selected_subsurface_bsdf.set_input("bg", n_subsurface_bsdf); + n_selected_subsurface_bsdf.set_input("mix", val(0.0f)); + + NodeItem n_sheen_bsdf = create_node("sheen_bsdf", NodeItem::Type::BSDF); + n_sheen_bsdf.set_input("weight", sheen); + n_sheen_bsdf.set_input("color", base_color); + n_sheen_bsdf.set_input("roughness", roughness); + if (normal) { + n_sheen_bsdf.set_input("normal", normal); + } + + NodeItem n_diffuse_bsdf = create_node("oren_nayar_diffuse_bsdf", NodeItem::Type::BSDF); + n_diffuse_bsdf.set_input("color", base_color.max(val(0.0f)) ^ n_coat_gamma); + n_diffuse_bsdf.set_input("roughness", roughness); + n_diffuse_bsdf.set_input("weight", val(1.0f)); + if (normal) { + n_diffuse_bsdf.set_input("normal", normal); + } + + NodeItem n_subsurface_mix = create_node("mix", NodeItem::Type::BSDF); + n_subsurface_mix.set_input("fg", n_selected_subsurface_bsdf); + n_subsurface_mix.set_input("bg", n_diffuse_bsdf); + n_subsurface_mix.set_input("mix", subsurface); + + NodeItem n_sheen_layer = create_node("layer", NodeItem::Type::BSDF); + n_sheen_layer.set_input("top", n_sheen_bsdf); + n_sheen_layer.set_input("base", n_subsurface_mix); + + NodeItem n_transmission_mix = create_node("mix", NodeItem::Type::BSDF); + n_transmission_mix.set_input("fg", n_transmission_bsdf); + n_transmission_mix.set_input("bg", n_sheen_layer); + n_transmission_mix.set_input("mix", transmission); + + NodeItem n_specular_layer = create_node("layer", NodeItem::Type::BSDF); + n_specular_layer.set_input("top", n_specular_bsdf); + n_specular_layer.set_input("base", n_transmission_mix); + + NodeItem n_metalness_mix = create_node("mix", NodeItem::Type::BSDF); + n_metalness_mix.set_input("fg", n_metal_bsdf); + n_metalness_mix.set_input("bg", n_specular_layer); + n_metalness_mix.set_input("mix", metallic); + + NodeItem n_thin_film_layer = create_node("layer", NodeItem::Type::BSDF); + n_thin_film_layer.set_input("top", n_thin_film_bsdf); + n_thin_film_layer.set_input("base", n_metalness_mix); + + NodeItem n_opacity_luminance = create_node("luminance", NodeItem::Type::Color3); + n_opacity_luminance.set_input("in", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); + + NodeItem n_coat_attenuation = create_node("mix", NodeItem::Type::Color3); + n_coat_attenuation.set_input("fg", base_color); + n_coat_attenuation.set_input("bg", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); + n_coat_attenuation.set_input("mix", coat); + + res = create_node("layer", NodeItem::Type::BSDF); + res.set_input("top", n_coat_bsdf); + res.set_input("base", n_thin_film_layer * n_coat_attenuation); + + return res; } - - NodeItem n_coat_roughness_vector = create_node("roughness_anisotropy", NodeItem::Type::Vector2); - n_coat_roughness_vector.set_input("roughness", roughness); - n_coat_roughness_vector.set_input("anisotropy", anisotropic); - - NodeItem n_coat_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); - n_coat_bsdf.set_input("weight", coat); - n_coat_bsdf.set_input("tint", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); - n_coat_bsdf.set_input("ior", ior); - n_coat_bsdf.set_input("scatter_mode", val(std::string("R"))); - n_coat_bsdf.set_input("roughness", n_coat_roughness_vector); - if (normal) { - n_coat_bsdf.set_input("normal", normal); + else if (to_type_ == NodeItem::Type::EDF) { + res = create_node("uniform_edf", NodeItem::Type::EDF); + res.set_input("color", emission * emission_strength); } + else if (to_type_ == NodeItem::Type::SurfaceShader) { + res = create_node("standard_surface", NodeItem::Type::SurfaceShader); + res.set_input("base", val(1.0f)); + res.set_input("base_color", base_color); + res.set_input("diffuse_roughness", roughness); + if (normal) { + res.set_input("normal", normal); + } + if (tangent) { + res.set_input("tangent", tangent); + } + res.set_input("metalness", metallic); - if (tangent) { - NodeItem n_coat_tangent_rotate = create_node("rotate3d", NodeItem::Type::Vector3); - n_coat_tangent_rotate.set_input("in", tangent); - n_coat_tangent_rotate.set_input("amount", anisotropic_rotation * val(360.0f)); - n_coat_tangent_rotate.set_input("axis", normal); + res.set_input("specular", specular); + res.set_input("specular_color", base_color); + res.set_input("specular_roughness", roughness); + res.set_input("specular_IOR", ior); + res.set_input("specular_anisotropy", anisotropic); + res.set_input("specular_rotation", anisotropic_rotation); - NodeItem n_coat_tangent_rotate_normalize = create_node("normalize", NodeItem::Type::Vector3); - n_coat_tangent_rotate_normalize.set_input("in", n_coat_tangent_rotate); + res.set_input("transmission", transmission); + res.set_input("transmission_color", base_color); + res.set_input("transmission_extra_roughness", roughness); - NodeItem n_coat_tangent = anisotropic.if_else( - NodeItem::CompareOp::Greater, val(0.0f), n_coat_tangent_rotate_normalize, tangent); + res.set_input("subsurface", subsurface); + res.set_input("subsurface_color", base_color); + res.set_input("subsurface_radius", subsurface_radius); + res.set_input("subsurface_anisotropy", anisotropic); - n_coat_bsdf.set_input("tangent", n_coat_tangent); + res.set_input("sheen", sheen); + res.set_input("sheen_color", base_color); + res.set_input("sheen_roughness", roughness); + + res.set_input("coat", coat); + res.set_input("coat_color", base_color); + res.set_input("coat_roughness", coat_roughness); + res.set_input("coat_IOR", ior); + res.set_input("coat_anisotropy", anisotropic); + res.set_input("coat_rotation", anisotropic_rotation); + if (coat_normal) { + res.set_input("coat_normal", coat_normal); + } + + res.set_input("emission", emission_strength); + res.set_input("emission_color", emission); } - NodeItem n_thin_film_bsdf = create_node("thin_film_bsdf", NodeItem::Type::BSDF); - n_thin_film_bsdf.set_input("thickness", val(0.0f)); - n_thin_film_bsdf.set_input("ior", val(1.5f)); - - NodeItem n_artistic_ior = create_node("artistic_ior", NodeItem::Type::Multioutput); - n_artistic_ior.set_input("reflectivity", base_color * val(1.0f)); - n_artistic_ior.set_input("edge_color", base_color * specular); - NodeItem n_ior_out = n_artistic_ior.add_output("ior", NodeItem::Type::Color3); - NodeItem n_extinction_out = n_artistic_ior.add_output("extinction", NodeItem::Type::Color3); - - NodeItem n_coat_affect_roughness_multiply2 = coat * val(0.0f) * roughness; - NodeItem n_coat_affected_roughness = create_node("mix", NodeItem::Type::Float); - n_coat_affected_roughness.set_input("fg", val(1.0f)); - n_coat_affected_roughness.set_input("bg", roughness); - n_coat_affected_roughness.set_input("mix", n_coat_affect_roughness_multiply2); - - NodeItem n_main_roughness = create_node("roughness_anisotropy", NodeItem::Type::Vector2); - n_main_roughness.set_input("roughness", n_coat_affected_roughness); - n_main_roughness.set_input("anisotropy", anisotropic); - - NodeItem n_metal_bsdf = create_node("conductor_bsdf", NodeItem::Type::BSDF); - n_metal_bsdf.set_input("ior", n_ior_out); - n_metal_bsdf.set_input("extinction", n_extinction_out); - n_metal_bsdf.set_input("roughness", n_main_roughness); - if (normal) { - n_metal_bsdf.set_input("normal", normal); - } - if (n_main_tangent) { - n_metal_bsdf.set_input("tangent", n_main_tangent); - } - - NodeItem n_specular_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); - n_specular_bsdf.set_input("weight", specular); - n_specular_bsdf.set_input("tint", base_color); - n_specular_bsdf.set_input("ior", ior); - n_specular_bsdf.set_input("scatter_mode", val(std::string("R"))); - n_specular_bsdf.set_input("roughness", n_main_roughness); - if (normal) { - n_specular_bsdf.set_input("normal", normal); - } - if (n_main_tangent) { - n_specular_bsdf.set_input("tangent", n_main_tangent); - } - - NodeItem n_coat_affected_transmission_roughness = create_node("mix", NodeItem::Type::Float); - n_coat_affected_transmission_roughness.set_input("fg", val(1.0f)); - n_coat_affected_transmission_roughness.set_input("bg", - (roughness + roughness).clamp(0.0f, 1.0f)); - n_coat_affected_transmission_roughness.set_input("mix", n_coat_affect_roughness_multiply2); - - NodeItem n_transmission_roughness = create_node("roughness_anisotropy", NodeItem::Type::Vector2); - n_transmission_roughness.set_input("roughness", n_coat_affected_transmission_roughness); - n_transmission_roughness.set_input("anisotropy", anisotropic); - - NodeItem n_transmission_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); - n_transmission_bsdf.set_input("tint", base_color); - n_transmission_bsdf.set_input("ior", ior); - n_transmission_bsdf.set_input("roughness", n_transmission_roughness); - if (normal) { - n_transmission_bsdf.set_input("normal", normal); - } - if (n_main_tangent) { - n_transmission_bsdf.set_input("tangent", n_main_tangent); - } - - NodeItem n_coat_gamma = coat.clamp(0.0f, 1.0f) * val(0.0f) + val(1.0f); - NodeItem n_coat_affected_subsurface_color = base_color.max(val(0.0f)) ^ n_coat_gamma; - NodeItem n_translucent_bsdf = create_node("translucent_bsdf", NodeItem::Type::BSDF); - n_translucent_bsdf.set_input("color", n_coat_affected_subsurface_color); - if (normal) { - n_translucent_bsdf.set_input("normal", normal); - } - - NodeItem n_subsurface_bsdf = create_node("subsurface_bsdf", NodeItem::Type::BSDF); - n_subsurface_bsdf.set_input("color", n_coat_affected_subsurface_color); - n_subsurface_bsdf.set_input("radius", subsurface_radius); - n_subsurface_bsdf.set_input("anisotropy", anisotropic); - if (normal) { - n_subsurface_bsdf.set_input("normal", normal); - } - - NodeItem n_selected_subsurface_bsdf = create_node("mix", NodeItem::Type::BSDF); - n_selected_subsurface_bsdf.set_input("fg", n_translucent_bsdf); - n_selected_subsurface_bsdf.set_input("bg", n_subsurface_bsdf); - n_selected_subsurface_bsdf.set_input("mix", val(0.0f)); - - NodeItem n_sheen_bsdf = create_node("sheen_bsdf", NodeItem::Type::BSDF); - n_sheen_bsdf.set_input("weight", sheen); - n_sheen_bsdf.set_input("color", base_color); - n_sheen_bsdf.set_input("roughness", roughness); - if (normal) { - n_sheen_bsdf.set_input("normal", normal); - } - - NodeItem n_diffuse_bsdf = create_node("oren_nayar_diffuse_bsdf", NodeItem::Type::BSDF); - n_diffuse_bsdf.set_input("color", base_color.max(val(0.0f)) ^ n_coat_gamma); - n_diffuse_bsdf.set_input("roughness", roughness); - n_diffuse_bsdf.set_input("weight", val(1.0f)); - if (normal) { - n_diffuse_bsdf.set_input("normal", normal); - } - - NodeItem n_subsurface_mix = create_node("mix", NodeItem::Type::BSDF); - n_subsurface_mix.set_input("fg", n_selected_subsurface_bsdf); - n_subsurface_mix.set_input("bg", n_diffuse_bsdf); - n_subsurface_mix.set_input("mix", subsurface); - - NodeItem n_sheen_layer = create_node("layer", NodeItem::Type::BSDF); - n_sheen_layer.set_input("top", n_sheen_bsdf); - n_sheen_layer.set_input("base", n_subsurface_mix); - - NodeItem n_transmission_mix = create_node("mix", NodeItem::Type::BSDF); - n_transmission_mix.set_input("fg", n_transmission_bsdf); - n_transmission_mix.set_input("bg", n_sheen_layer); - n_transmission_mix.set_input("mix", transmission); - - NodeItem n_specular_layer = create_node("layer", NodeItem::Type::BSDF); - n_specular_layer.set_input("top", n_specular_bsdf); - n_specular_layer.set_input("base", n_transmission_mix); - - NodeItem n_metalness_mix = create_node("mix", NodeItem::Type::BSDF); - n_metalness_mix.set_input("fg", n_metal_bsdf); - n_metalness_mix.set_input("bg", n_specular_layer); - n_metalness_mix.set_input("mix", metallic); - - NodeItem n_thin_film_layer = create_node("layer", NodeItem::Type::BSDF); - n_thin_film_layer.set_input("top", n_thin_film_bsdf); - n_thin_film_layer.set_input("base", n_metalness_mix); - - NodeItem n_opacity_luminance = create_node("luminance", NodeItem::Type::Color3); - n_opacity_luminance.set_input("in", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); - - NodeItem n_emission_edf = create_node("uniform_edf", NodeItem::Type::EDF); - n_emission_edf.set_input("color", emission * emission_strength); - - NodeItem n_coat_attenuation = create_node("mix", NodeItem::Type::Color3); - n_coat_attenuation.set_input("fg", base_color); - n_coat_attenuation.set_input("bg", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))); - n_coat_attenuation.set_input("mix", coat); - - NodeItem n_coat_layer = create_node("layer", NodeItem::Type::BSDF); - n_coat_layer.set_input("top", n_coat_bsdf); - n_coat_layer.set_input("base", n_thin_film_layer * n_coat_attenuation); - - NodeItem res = create_node("surface", NodeItem::Type::SurfaceShader); - res.set_input("bsdf", n_coat_layer); - res.set_input("edf", n_emission_edf); - res.set_input("opacity", n_opacity_luminance.extract(0)); - return res; } #endif diff --git a/source/blender/nodes/shader/nodes/node_shader_mix_shader.cc b/source/blender/nodes/shader/nodes/node_shader_mix_shader.cc index 434b54515f05..68f34201611b 100644 --- a/source/blender/nodes/shader/nodes/node_shader_mix_shader.cc +++ b/source/blender/nodes/shader/nodes/node_shader_mix_shader.cc @@ -42,8 +42,8 @@ NODE_SHADER_MATERIALX_BEGIN } else if (shader1 && shader2) { res = create_node("mix", to_type_); - res.set_input("fg", shader1); - res.set_input("bg", shader2); + res.set_input("fg", shader2); + res.set_input("bg", shader1); res.set_input("mix", fac); } break; -- 2.30.2 From 3ebf766be310bd8969ce2975399d7b50abfbdf39 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Wed, 20 Sep 2023 16:09:08 +0300 Subject: [PATCH 04/10] MaterialX: optimization for PrincipleBSDF node --- .../nodes/node_shader_bsdf_principled.cc | 272 +++++++++++------- 1 file changed, 161 insertions(+), 111 deletions(-) diff --git a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc index 1726ba7b8c1b..8d4510086e12 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc @@ -2,6 +2,8 @@ * * SPDX-License-Identifier: GPL-2.0-or-later */ +#include + #include "BLI_string.h" #include "node_shader_util.hh" @@ -287,64 +289,118 @@ static void node_shader_update_principled(bNodeTree *ntree, bNode *node) NODE_SHADER_MATERIALX_BEGIN #ifdef WITH_MATERIALX { - NodeItem base_color = get_input_value("Base Color", NodeItem::Type::Color3); + auto bsdf_inputs = [&]() { + NodeItem base_color = get_input_value("Base Color", NodeItem::Type::Color3); - NodeItem subsurface = get_input_value("Subsurface", NodeItem::Type::Float); - NodeItem subsurface_radius = get_input_value("Subsurface Radius", NodeItem::Type::Vector3); - NodeItem subsurface_scale = get_input_value("Subsurface Scale", NodeItem::Type::Float); + NodeItem subsurface = get_input_value("Subsurface", NodeItem::Type::Float); + NodeItem subsurface_radius = get_input_value("Subsurface Radius", NodeItem::Type::Vector3); + NodeItem subsurface_scale = get_input_value("Subsurface Scale", NodeItem::Type::Float); - NodeItem metallic = get_input_value("Metallic", NodeItem::Type::Float); - NodeItem specular = get_input_value("Specular", NodeItem::Type::Float); - // NodeItem specular_tint = get_input_value("Specular Tint"); - NodeItem roughness = get_input_value("Roughness", NodeItem::Type::Float); + NodeItem metallic = get_input_value("Metallic", NodeItem::Type::Float); + NodeItem specular = get_input_value("Specular", NodeItem::Type::Float); + // NodeItem specular_tint = get_input_value("Specular Tint"); + NodeItem roughness = get_input_value("Roughness", NodeItem::Type::Float); - /* TODO: use Specular Tint input */ - NodeItem anisotropic = get_input_value("Anisotropic", NodeItem::Type::Float); - NodeItem anisotropic_rotation = get_input_value("Anisotropic Rotation", NodeItem::Type::Float); - // anisotropic_rotation = 0.5 - (anisotropic_rotation % 1.0) + /* TODO: use Specular Tint input */ + NodeItem anisotropic = get_input_value("Anisotropic", NodeItem::Type::Float); + NodeItem anisotropic_rotation = get_input_value("Anisotropic Rotation", + NodeItem::Type::Float); + // anisotropic_rotation = 0.5 - (anisotropic_rotation % 1.0) - NodeItem sheen = get_input_value("Sheen", NodeItem::Type::Float); - // sheen_tint = get_input_value("Sheen Tint"); + NodeItem sheen = get_input_value("Sheen", NodeItem::Type::Float); + // sheen_tint = get_input_value("Sheen Tint"); - NodeItem coat = get_input_value("Coat", NodeItem::Type::Float); - NodeItem coat_roughness = get_input_value("Coat Roughness", NodeItem::Type::Float); + NodeItem coat = get_input_value("Coat", NodeItem::Type::Float); + NodeItem coat_roughness = get_input_value("Coat Roughness", NodeItem::Type::Float); - NodeItem ior = get_input_value("IOR", NodeItem::Type::Float); + NodeItem ior = get_input_value("IOR", NodeItem::Type::Float); - NodeItem transmission = get_input_value("Transmission", NodeItem::Type::Float); + NodeItem transmission = get_input_value("Transmission", NodeItem::Type::Float); - NodeItem emission = get_input_value("Emission", NodeItem::Type::Color3); - NodeItem emission_strength = get_input_value("Emission Strength", NodeItem::Type::Float); + NodeItem alpha = get_input_value("Alpha", NodeItem::Type::Float); + // transparency = 1.0 - alpha - NodeItem alpha = get_input_value("Alpha", NodeItem::Type::Float); - // transparency = 1.0 - alpha + NodeItem normal = get_input_link("Normal", NodeItem::Type::Vector3); + NodeItem coat_normal = get_input_link("Coat Normal", NodeItem::Type::Vector3); + NodeItem tangent = get_input_link("Tangent", NodeItem::Type::Vector3); - NodeItem normal = get_input_link("Normal", NodeItem::Type::Vector3); - NodeItem coat_normal = get_input_link("Coat Normal", NodeItem::Type::Vector3); - NodeItem tangent = get_input_link("Tangent", NodeItem::Type::Vector3); + subsurface_radius = subsurface_radius * subsurface_scale; - subsurface_radius = subsurface_radius * subsurface_scale; + return std::map {{"base", val(1.0f)}, + {"base_color", base_color}, + {"diffuse_roughness", roughness}, + {"normal", normal}, + {"tangent", tangent}, + {"metalness", metallic}, + {"specular", specular}, + {"specular_color", base_color}, + {"specular_roughness", roughness}, + {"specular_IOR", ior}, + {"specular_anisotropy", anisotropic}, + {"specular_rotation", anisotropic_rotation}, + {"transmission", transmission}, + {"transmission_color", base_color}, + {"transmission_extra_roughness", roughness}, + {"subsurface", subsurface}, + {"subsurface_color", base_color}, + {"subsurface_radius", subsurface_radius}, + {"subsurface_anisotropy", anisotropic}, + {"sheen", sheen}, + {"sheen_color", base_color}, + {"sheen_roughness", roughness}, + {"coat", coat}, + {"coat_color", base_color}, + {"coat_roughness", coat_roughness}, + {"coat_IOR", ior}, + {"coat_anisotropy", anisotropic}, + {"coat_rotation", anisotropic_rotation}, + {"coat_normal", coat_normal}, + }; + }; + + auto edf_inputs = [&]() { + NodeItem emission = get_input_value("Emission", NodeItem::Type::Color3); + NodeItem emission_strength = get_input_value("Emission Strength", NodeItem::Type::Float); + + return std::map{{"emission", emission_strength}, + {"emission_color", emission}}; + }; NodeItem res = empty(); if (to_type_ == NodeItem::Type::BSDF) { + auto inputs = bsdf_inputs(); + + NodeItem roughness = inputs.find("diffuse_roughness")->second; + NodeItem anisotropy = inputs.find("specular_anisotropy")->second; + NodeItem rotation = inputs.find("specular_rotation")->second; + NodeItem base_color = inputs.find("base_color")->second; + NodeItem specular = inputs.find("specular")->second; + NodeItem coat = inputs.find("coat")->second; + NodeItem ior = inputs.find("specular_IOR")->second; + NodeItem normal = inputs.find("normal")->second; + NodeItem tangent = inputs.find("tangent")->second; + NodeItem n_main_tangent = empty(); if (tangent) { NodeItem n_tangent_rotate = create_node("rotate3d", NodeItem::Type::Vector3); n_tangent_rotate.set_input("in", tangent); - n_tangent_rotate.set_input("amount", anisotropic_rotation * val(360.0f)); + n_tangent_rotate.set_input("amount", rotation * val(360.0f)); n_tangent_rotate.set_input("axis", normal); NodeItem n_tangent_rotate_normalize = create_node("normalize", NodeItem::Type::Vector3); n_tangent_rotate_normalize.set_input("in", n_tangent_rotate); - n_main_tangent = anisotropic.if_else( - NodeItem::CompareOp::Greater, val(0.0f), n_tangent_rotate_normalize, tangent); + n_main_tangent = anisotropy.if_else( + NodeItem::CompareOp::Greater, + val(0.0f), + n_tangent_rotate_normalize, + tangent); } NodeItem n_coat_roughness_vector = create_node("roughness_anisotropy", NodeItem::Type::Vector2); n_coat_roughness_vector.set_input("roughness", roughness); - n_coat_roughness_vector.set_input("anisotropy", anisotropic); + n_coat_roughness_vector.set_input("anisotropy", anisotropy); NodeItem n_coat_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); n_coat_bsdf.set_input("weight", coat); @@ -352,21 +408,22 @@ NODE_SHADER_MATERIALX_BEGIN n_coat_bsdf.set_input("ior", ior); n_coat_bsdf.set_input("scatter_mode", val(std::string("R"))); n_coat_bsdf.set_input("roughness", n_coat_roughness_vector); - if (normal) { - n_coat_bsdf.set_input("normal", normal); - } + n_coat_bsdf.set_input("normal", normal); if (tangent) { NodeItem n_coat_tangent_rotate = create_node("rotate3d", NodeItem::Type::Vector3); n_coat_tangent_rotate.set_input("in", tangent); - n_coat_tangent_rotate.set_input("amount", anisotropic_rotation * val(360.0f)); + n_coat_tangent_rotate.set_input("amount", rotation * val(360.0f)); n_coat_tangent_rotate.set_input("axis", normal); NodeItem n_coat_tangent_rotate_normalize = create_node("normalize", NodeItem::Type::Vector3); n_coat_tangent_rotate_normalize.set_input("in", n_coat_tangent_rotate); - - NodeItem n_coat_tangent = anisotropic.if_else( - NodeItem::CompareOp::Greater, val(0.0f), n_coat_tangent_rotate_normalize, tangent); + + NodeItem n_coat_tangent = anisotropy.if_else( + NodeItem::CompareOp::Greater, + val(0.0f), + n_coat_tangent_rotate_normalize, + tangent); n_coat_bsdf.set_input("tangent", n_coat_tangent); } @@ -377,11 +434,13 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_artistic_ior = create_node("artistic_ior", NodeItem::Type::Multioutput); n_artistic_ior.set_input("reflectivity", base_color * val(1.0f)); - n_artistic_ior.set_input("edge_color", base_color * specular); + n_artistic_ior.set_input( + "edge_color", base_color * specular); NodeItem n_ior_out = n_artistic_ior.add_output("ior", NodeItem::Type::Color3); NodeItem n_extinction_out = n_artistic_ior.add_output("extinction", NodeItem::Type::Color3); - NodeItem n_coat_affect_roughness_multiply2 = coat * val(0.0f) * roughness; + NodeItem n_coat_affect_roughness_multiply2 = coat * val(0.0f) * + roughness; NodeItem n_coat_affected_roughness = create_node("mix", NodeItem::Type::Float); n_coat_affected_roughness.set_input("fg", val(1.0f)); n_coat_affected_roughness.set_input("bg", roughness); @@ -389,95 +448,79 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_main_roughness = create_node("roughness_anisotropy", NodeItem::Type::Vector2); n_main_roughness.set_input("roughness", n_coat_affected_roughness); - n_main_roughness.set_input("anisotropy", anisotropic); + n_main_roughness.set_input("anisotropy", anisotropy); NodeItem n_metal_bsdf = create_node("conductor_bsdf", NodeItem::Type::BSDF); n_metal_bsdf.set_input("ior", n_ior_out); n_metal_bsdf.set_input("extinction", n_extinction_out); n_metal_bsdf.set_input("roughness", n_main_roughness); - if (normal) { - n_metal_bsdf.set_input("normal", normal); - } - if (n_main_tangent) { - n_metal_bsdf.set_input("tangent", n_main_tangent); - } + n_metal_bsdf.set_input("normal", normal); + n_metal_bsdf.set_input("tangent", n_main_tangent); NodeItem n_specular_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); - n_specular_bsdf.set_input("weight", specular); + n_specular_bsdf.set_input("weight", specular); n_specular_bsdf.set_input("tint", base_color); - n_specular_bsdf.set_input("ior", ior); + n_specular_bsdf.set_input("ior", ior); n_specular_bsdf.set_input("scatter_mode", val(std::string("R"))); n_specular_bsdf.set_input("roughness", n_main_roughness); - if (normal) { - n_specular_bsdf.set_input("normal", normal); - } - if (n_main_tangent) { - n_specular_bsdf.set_input("tangent", n_main_tangent); - } - + n_specular_bsdf.set_input("normal", normal); + n_specular_bsdf.set_input("tangent", n_main_tangent); + NodeItem n_coat_affected_transmission_roughness = create_node("mix", NodeItem::Type::Float); n_coat_affected_transmission_roughness.set_input("fg", val(1.0f)); - n_coat_affected_transmission_roughness.set_input("bg", - (roughness + roughness).clamp(0.0f, 1.0f)); + n_coat_affected_transmission_roughness.set_input( + "bg", + (roughness + roughness) + .clamp(0.0f, 1.0f)); n_coat_affected_transmission_roughness.set_input("mix", n_coat_affect_roughness_multiply2); NodeItem n_transmission_roughness = create_node("roughness_anisotropy", NodeItem::Type::Vector2); n_transmission_roughness.set_input("roughness", n_coat_affected_transmission_roughness); - n_transmission_roughness.set_input("anisotropy", anisotropic); + n_transmission_roughness.set_input("anisotropy", anisotropy); NodeItem n_transmission_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF); n_transmission_bsdf.set_input("tint", base_color); n_transmission_bsdf.set_input("ior", ior); n_transmission_bsdf.set_input("roughness", n_transmission_roughness); - if (normal) { - n_transmission_bsdf.set_input("normal", normal); - } - if (n_main_tangent) { - n_transmission_bsdf.set_input("tangent", n_main_tangent); - } - + n_transmission_bsdf.set_input("normal", normal); + n_transmission_bsdf.set_input("tangent", n_main_tangent); + NodeItem n_coat_gamma = coat.clamp(0.0f, 1.0f) * val(0.0f) + val(1.0f); - NodeItem n_coat_affected_subsurface_color = base_color.max(val(0.0f)) ^ n_coat_gamma; + NodeItem n_coat_affected_subsurface_color = base_color.max(val(0.0f)) ^ + n_coat_gamma; NodeItem n_translucent_bsdf = create_node("translucent_bsdf", NodeItem::Type::BSDF); n_translucent_bsdf.set_input("color", n_coat_affected_subsurface_color); - if (normal) { - n_translucent_bsdf.set_input("normal", normal); - } - + n_translucent_bsdf.set_input("normal", normal); + NodeItem n_subsurface_bsdf = create_node("subsurface_bsdf", NodeItem::Type::BSDF); n_subsurface_bsdf.set_input("color", n_coat_affected_subsurface_color); - n_subsurface_bsdf.set_input("radius", subsurface_radius); - n_subsurface_bsdf.set_input("anisotropy", anisotropic); - if (normal) { - n_subsurface_bsdf.set_input("normal", normal); - } - + n_subsurface_bsdf.set_input("radius", inputs.find("subsurface_radius")->second); + n_subsurface_bsdf.set_input("anisotropy", anisotropy); + n_subsurface_bsdf.set_input("normal", normal); + NodeItem n_selected_subsurface_bsdf = create_node("mix", NodeItem::Type::BSDF); n_selected_subsurface_bsdf.set_input("fg", n_translucent_bsdf); n_selected_subsurface_bsdf.set_input("bg", n_subsurface_bsdf); n_selected_subsurface_bsdf.set_input("mix", val(0.0f)); NodeItem n_sheen_bsdf = create_node("sheen_bsdf", NodeItem::Type::BSDF); - n_sheen_bsdf.set_input("weight", sheen); + n_sheen_bsdf.set_input("weight", inputs.find("sheen")->second); n_sheen_bsdf.set_input("color", base_color); n_sheen_bsdf.set_input("roughness", roughness); - if (normal) { - n_sheen_bsdf.set_input("normal", normal); - } - + n_sheen_bsdf.set_input("normal", normal); + NodeItem n_diffuse_bsdf = create_node("oren_nayar_diffuse_bsdf", NodeItem::Type::BSDF); - n_diffuse_bsdf.set_input("color", base_color.max(val(0.0f)) ^ n_coat_gamma); + n_diffuse_bsdf.set_input("color", + base_color.max(val(0.0f)) ^ n_coat_gamma); n_diffuse_bsdf.set_input("roughness", roughness); n_diffuse_bsdf.set_input("weight", val(1.0f)); - if (normal) { - n_diffuse_bsdf.set_input("normal", normal); - } - + n_diffuse_bsdf.set_input("normal", normal); + NodeItem n_subsurface_mix = create_node("mix", NodeItem::Type::BSDF); n_subsurface_mix.set_input("fg", n_selected_subsurface_bsdf); n_subsurface_mix.set_input("bg", n_diffuse_bsdf); - n_subsurface_mix.set_input("mix", subsurface); + n_subsurface_mix.set_input("mix", inputs.find("subsurface")->second); NodeItem n_sheen_layer = create_node("layer", NodeItem::Type::BSDF); n_sheen_layer.set_input("top", n_sheen_bsdf); @@ -486,7 +529,7 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_transmission_mix = create_node("mix", NodeItem::Type::BSDF); n_transmission_mix.set_input("fg", n_transmission_bsdf); n_transmission_mix.set_input("bg", n_sheen_layer); - n_transmission_mix.set_input("mix", transmission); + n_transmission_mix.set_input("mix", inputs.find("transmission")->second); NodeItem n_specular_layer = create_node("layer", NodeItem::Type::BSDF); n_specular_layer.set_input("top", n_specular_bsdf); @@ -495,7 +538,7 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_metalness_mix = create_node("mix", NodeItem::Type::BSDF); n_metalness_mix.set_input("fg", n_metal_bsdf); n_metalness_mix.set_input("bg", n_specular_layer); - n_metalness_mix.set_input("mix", metallic); + n_metalness_mix.set_input("mix", inputs.find("metalness")->second); NodeItem n_thin_film_layer = create_node("layer", NodeItem::Type::BSDF); n_thin_film_layer.set_input("top", n_thin_film_bsdf); @@ -516,54 +559,61 @@ NODE_SHADER_MATERIALX_BEGIN return res; } else if (to_type_ == NodeItem::Type::EDF) { + auto inputs = edf_inputs(); + res = create_node("uniform_edf", NodeItem::Type::EDF); - res.set_input("color", emission * emission_strength); + res.set_input("color", inputs.find("emission_color")->second * inputs.find("emission")->second); } else if (to_type_ == NodeItem::Type::SurfaceShader) { + auto b_inputs = bsdf_inputs(); + auto e_inputs = edf_inputs(); + + NodeItem roughness = b_inputs.find("diffuse_roughness")->second; + NodeItem base_color = b_inputs.find("base_color")->second; + NodeItem anisotropic = b_inputs.find("specular_anisotropy")->second; + NodeItem ior = b_inputs.find("specular_IOR")->second; + NodeItem rotation = b_inputs.find("specular_rotation")->second; + res = create_node("standard_surface", NodeItem::Type::SurfaceShader); res.set_input("base", val(1.0f)); res.set_input("base_color", base_color); res.set_input("diffuse_roughness", roughness); - if (normal) { - res.set_input("normal", normal); - } - if (tangent) { - res.set_input("tangent", tangent); - } - res.set_input("metalness", metallic); - res.set_input("specular", specular); + res.set_input("metalness", b_inputs.find("metalness")->second ); + + res.set_input("specular", b_inputs.find("specular")->second ); res.set_input("specular_color", base_color); res.set_input("specular_roughness", roughness); res.set_input("specular_IOR", ior); res.set_input("specular_anisotropy", anisotropic); - res.set_input("specular_rotation", anisotropic_rotation); + res.set_input("specular_rotation", rotation); - res.set_input("transmission", transmission); + res.set_input("transmission", b_inputs.find("transmission")->second ); res.set_input("transmission_color", base_color); res.set_input("transmission_extra_roughness", roughness); - res.set_input("subsurface", subsurface); + res.set_input("subsurface", b_inputs.find("subsurface")->second ); res.set_input("subsurface_color", base_color); - res.set_input("subsurface_radius", subsurface_radius); + res.set_input("subsurface_radius", b_inputs.find("subsurface_radius")->second ); res.set_input("subsurface_anisotropy", anisotropic); - res.set_input("sheen", sheen); + res.set_input("sheen", b_inputs.find("sheen")->second ); res.set_input("sheen_color", base_color); res.set_input("sheen_roughness", roughness); - res.set_input("coat", coat); + res.set_input("coat", b_inputs.find("coat")->second ); res.set_input("coat_color", base_color); - res.set_input("coat_roughness", coat_roughness); + res.set_input("coat_roughness", b_inputs.find("coat_roughness")->second ); res.set_input("coat_IOR", ior); res.set_input("coat_anisotropy", anisotropic); - res.set_input("coat_rotation", anisotropic_rotation); - if (coat_normal) { - res.set_input("coat_normal", coat_normal); - } + res.set_input("coat_rotation", rotation); + res.set_input("coat_normal", b_inputs.find("coat_normal")->second ); - res.set_input("emission", emission_strength); - res.set_input("emission_color", emission); + res.set_input("emission", e_inputs.find("emission")->second); + res.set_input("emission_color", e_inputs.find("emission_color")->second); + + res.set_input("normal", b_inputs.find("normal")->second); + res.set_input("tangent", b_inputs.find("tangent")->second); } return res; -- 2.30.2 From d7e53b9d7eef21796dedf90aca5d2f2a195f2864 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Wed, 20 Sep 2023 16:44:53 +0300 Subject: [PATCH 05/10] MaterialX: make format --- .../nodes/node_shader_bsdf_principled.cc | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc index a9d3aaea6728..f51356c44aa9 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc @@ -650,40 +650,40 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem rotation = b_inputs.find("specular_rotation")->second; res = create_node("standard_surface", - NodeItem::Type::SurfaceShader, - { - {"base", val(1.0f)}, - {"base_color", base_color}, - {"diffuse_roughness", roughness}, - {"metalness", b_inputs.find("metalness")->second}, - {"specular", b_inputs.find("specular")->second}, - {"specular_color", base_color}, - {"specular_roughness", roughness}, - {"specular_IOR", ior}, - {"specular_anisotropy", anisotropic}, - {"specular_rotation", rotation}, - {"transmission", b_inputs.find("transmission")->second}, - {"transmission_color", base_color}, - {"transmission_extra_roughness", roughness}, - {"subsurface", b_inputs.find("subsurface")->second}, - {"subsurface_color", base_color}, - {"subsurface_radius", b_inputs.find("subsurface_radius")->second}, - {"subsurface_anisotropy", anisotropic}, - {"sheen", b_inputs.find("sheen")->second}, - {"sheen_color", base_color}, - {"sheen_roughness", roughness}, - {"coat", b_inputs.find("coat")->second}, - {"coat_color", base_color}, - {"coat_roughness", b_inputs.find("coat_roughness")->second}, - {"coat_IOR", ior}, - {"coat_anisotropy", anisotropic}, - {"coat_rotation", rotation}, - {"coat_normal", b_inputs.find("coat_normal")->second}, - {"emission", e_inputs.find("emission")->second}, - {"emission_color", e_inputs.find("emission_color")->second}, - {"normal", b_inputs.find("normal")->second}, - {"tangent", b_inputs.find("tangent")->second}, - }); + NodeItem::Type::SurfaceShader, + { + {"base", val(1.0f)}, + {"base_color", base_color}, + {"diffuse_roughness", roughness}, + {"metalness", b_inputs.find("metalness")->second}, + {"specular", b_inputs.find("specular")->second}, + {"specular_color", base_color}, + {"specular_roughness", roughness}, + {"specular_IOR", ior}, + {"specular_anisotropy", anisotropic}, + {"specular_rotation", rotation}, + {"transmission", b_inputs.find("transmission")->second}, + {"transmission_color", base_color}, + {"transmission_extra_roughness", roughness}, + {"subsurface", b_inputs.find("subsurface")->second}, + {"subsurface_color", base_color}, + {"subsurface_radius", b_inputs.find("subsurface_radius")->second}, + {"subsurface_anisotropy", anisotropic}, + {"sheen", b_inputs.find("sheen")->second}, + {"sheen_color", base_color}, + {"sheen_roughness", roughness}, + {"coat", b_inputs.find("coat")->second}, + {"coat_color", base_color}, + {"coat_roughness", b_inputs.find("coat_roughness")->second}, + {"coat_IOR", ior}, + {"coat_anisotropy", anisotropic}, + {"coat_rotation", rotation}, + {"coat_normal", b_inputs.find("coat_normal")->second}, + {"emission", e_inputs.find("emission")->second}, + {"emission_color", e_inputs.find("emission_color")->second}, + {"normal", b_inputs.find("normal")->second}, + {"tangent", b_inputs.find("tangent")->second}, + }); } return res; -- 2.30.2 From 2b876e34027ad704ecb3cb8e4329fd40f55588ca Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Wed, 20 Sep 2023 17:18:20 +0300 Subject: [PATCH 06/10] MaterialX: reformat style --- .../nodes/node_shader_bsdf_principled.cc | 318 +++++++----------- 1 file changed, 122 insertions(+), 196 deletions(-) diff --git a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc index f51356c44aa9..42e9cc51e8c5 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc @@ -382,13 +382,10 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_main_tangent = empty(); if (tangent) { - NodeItem n_tangent_rotate = create_node("rotate3d", - NodeItem::Type::Vector3, - { - {"in", tangent}, - {"amount", rotation * val(360.0f)}, - {"axis", normal}, - }); + NodeItem n_tangent_rotate = create_node( + "rotate3d", + NodeItem::Type::Vector3, + {{"in", tangent}, {"amount", rotation * val(360.0f)}, {"axis", normal}}); NodeItem n_tangent_rotate_normalize = create_node( "normalize", NodeItem::Type::Vector3, {{"in", n_tangent_rotate}}); @@ -397,38 +394,28 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem::CompareOp::Greater, val(0.0f), n_tangent_rotate_normalize, tangent); } - NodeItem n_coat_roughness_vector = create_node("roughness_anisotropy", - NodeItem::Type::Vector2, - { - {"roughness", roughness}, - {"anisotropy", anisotropy}, - }); + NodeItem n_coat_roughness_vector = create_node( + "roughness_anisotropy", + NodeItem::Type::Vector2, + {{"roughness", roughness}, {"anisotropy", anisotropy}}); NodeItem n_coat_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF, - { - {"weight", coat}, - {"tint", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))}, - {"ior", ior}, - {"scatter_mode", val(std::string("R"))}, - {"roughness", n_coat_roughness_vector}, - {"normal", normal}, - }); + {{"weight", coat}, + {"tint", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))}, + {"ior", ior}, + {"scatter_mode", val(std::string("R"))}, + {"roughness", n_coat_roughness_vector}, + {"normal", normal}}); if (tangent) { - NodeItem n_coat_tangent_rotate = create_node("rotate3d", - NodeItem::Type::Vector3, - { - {"in", tangent}, - {"amount", rotation * val(360.0f)}, - {"axis", normal}, - }); + NodeItem n_coat_tangent_rotate = create_node( + "rotate3d", + NodeItem::Type::Vector3, + {{"in", tangent}, {"amount", rotation * val(360.0f)}, {"axis", normal}}); - NodeItem n_coat_tangent_rotate_normalize = create_node("normalize", - NodeItem::Type::Vector3, - { - {"in", n_coat_tangent_rotate}, - }); + NodeItem n_coat_tangent_rotate_normalize = create_node( + "normalize", NodeItem::Type::Vector3, {{"in", n_coat_tangent_rotate}}); NodeItem n_coat_tangent = anisotropy.if_else( NodeItem::CompareOp::Greater, val(0.0f), n_coat_tangent_rotate_normalize, tangent); @@ -436,19 +423,13 @@ NODE_SHADER_MATERIALX_BEGIN n_coat_bsdf.set_input("tangent", n_coat_tangent); } - NodeItem n_thin_film_bsdf = create_node("thin_film_bsdf", - NodeItem::Type::BSDF, - { - {"thickness", val(0.0f)}, - {"ior", val(1.5f)}, - }); + NodeItem n_thin_film_bsdf = create_node( + "thin_film_bsdf", NodeItem::Type::BSDF, {{"thickness", val(0.0f)}, {"ior", val(1.5f)}}); - NodeItem n_artistic_ior = create_node("artistic_ior", - NodeItem::Type::Multioutput, - { - {"reflectivity", base_color * val(1.0f)}, - {"edge_color", base_color * specular}, - }); + NodeItem n_artistic_ior = create_node( + "artistic_ior", + NodeItem::Type::Multioutput, + {{"reflectivity", base_color * val(1.0f)}, {"edge_color", base_color * specular}}); NodeItem n_ior_out = n_artistic_ior.add_output("ior", NodeItem::Type::Color3); NodeItem n_extinction_out = n_artistic_ior.add_output("extinction", NodeItem::Type::Color3); @@ -457,40 +438,30 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_coat_affected_roughness = create_node( "mix", NodeItem::Type::Float, - { - {"fg", val(1.0f)}, - {"bg", roughness}, - {"mix", n_coat_affect_roughness_multiply2}, - }); + {{"fg", val(1.0f)}, {"bg", roughness}, {"mix", n_coat_affect_roughness_multiply2}}); - NodeItem n_main_roughness = create_node("roughness_anisotropy", - NodeItem::Type::Vector2, - { - {"roughness", n_coat_affected_roughness}, - {"anisotropy", anisotropy}, - }); + NodeItem n_main_roughness = create_node( + "roughness_anisotropy", + NodeItem::Type::Vector2, + {{"roughness", n_coat_affected_roughness}, {"anisotropy", anisotropy}}); NodeItem n_metal_bsdf = create_node("conductor_bsdf", NodeItem::Type::BSDF, - { - {"ior", n_ior_out}, - {"extinction", n_extinction_out}, - {"roughness", n_main_roughness}, - {"normal", normal}, - {"tangent", n_main_tangent}, - }); + {{"ior", n_ior_out}, + {"extinction", n_extinction_out}, + {"roughness", n_main_roughness}, + {"normal", normal}, + {"tangent", n_main_tangent}}); NodeItem n_specular_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF, - { - {"weight", specular}, - {"tint", base_color}, - {"ior", ior}, - {"scatter_mode", val(std::string("R"))}, - {"roughness", n_main_roughness}, - {"normal", normal}, - {"tangent", n_main_tangent}, - }); + {{"weight", specular}, + {"tint", base_color}, + {"ior", ior}, + {"scatter_mode", val(std::string("R"))}, + {"roughness", n_main_roughness}, + {"normal", normal}, + {"tangent", n_main_tangent}}); NodeItem n_coat_affected_transmission_roughness = create_node( "mix", @@ -504,128 +475,87 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_transmission_roughness = create_node( "roughness_anisotropy", NodeItem::Type::Vector2, - { - {"roughness", n_coat_affected_transmission_roughness}, - {"anisotropy", anisotropy}, - }); + {{"roughness", n_coat_affected_transmission_roughness}, {"anisotropy", anisotropy}}); NodeItem n_transmission_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF, - { - {"tint", base_color}, - {"ior", ior}, - {"roughness", n_transmission_roughness}, - {"normal", normal}, - {"tangent", n_main_tangent}, - }); + {{"tint", base_color}, + {"ior", ior}, + {"roughness", n_transmission_roughness}, + {"normal", normal}, + {"tangent", n_main_tangent}}); NodeItem n_coat_gamma = coat.clamp(0.0f, 1.0f) * val(0.0f) + val(1.0f); NodeItem n_coat_affected_subsurface_color = base_color.max(val(0.0f)) ^ n_coat_gamma; - NodeItem n_translucent_bsdf = create_node("translucent_bsdf", - NodeItem::Type::BSDF, - { - {"color", n_coat_affected_subsurface_color}, - {"normal", normal}, - }); - - NodeItem n_subsurface_bsdf = create_node( - "subsurface_bsdf", + NodeItem n_translucent_bsdf = create_node( + "translucent_bsdf", NodeItem::Type::BSDF, - { - {"color", n_coat_affected_subsurface_color}, - {"radius", inputs.find("subsurface_radius")->second}, - {"anisotropy", anisotropy}, - {"normal", normal}, - }); + {{"color", n_coat_affected_subsurface_color}, {"normal", normal}}); - NodeItem n_selected_subsurface_bsdf = create_node("mix", - NodeItem::Type::BSDF, - { - {"fg", n_translucent_bsdf}, - {"bg", n_subsurface_bsdf}, - {"mix", val(0.0f)}, - }); + NodeItem n_subsurface_bsdf = create_node("subsurface_bsdf", + NodeItem::Type::BSDF, + {{"color", n_coat_affected_subsurface_color}, + {"radius", inputs.find("subsurface_radius")->second}, + {"anisotropy", anisotropy}, + {"normal", normal}}); + + NodeItem n_selected_subsurface_bsdf = create_node( + "mix", + NodeItem::Type::BSDF, + {{"fg", n_translucent_bsdf}, {"bg", n_subsurface_bsdf}, {"mix", val(0.0f)}}); NodeItem n_sheen_bsdf = create_node("sheen_bsdf", NodeItem::Type::BSDF, - { - {"weight", inputs.find("sheen")->second}, - {"color", base_color}, - {"roughness", roughness}, - {"normal", normal}, - }); + {{"weight", inputs.find("sheen")->second}, + {"color", base_color}, + {"roughness", roughness}, + {"normal", normal}}); NodeItem n_diffuse_bsdf = create_node("oren_nayar_diffuse_bsdf", NodeItem::Type::BSDF, - { - {"color", base_color.max(val(0.0f)) ^ n_coat_gamma}, - {"roughness", roughness}, - {"weight", val(1.0f)}, - {"normal", normal}, - }); + {{"color", base_color.max(val(0.0f)) ^ n_coat_gamma}, + {"roughness", roughness}, + {"weight", val(1.0f)}, + {"normal", normal}}); NodeItem n_subsurface_mix = create_node("mix", NodeItem::Type::BSDF, - { - {"fg", n_selected_subsurface_bsdf}, - {"bg", n_diffuse_bsdf}, - {"mix", inputs.find("subsurface")->second}, - }); + {{"fg", n_selected_subsurface_bsdf}, + {"bg", n_diffuse_bsdf}, + {"mix", inputs.find("subsurface")->second}}); - NodeItem n_sheen_layer = create_node("layer", - NodeItem::Type::BSDF, - { - {"top", n_sheen_bsdf}, - {"base", n_subsurface_mix}, - }); + NodeItem n_sheen_layer = create_node( + "layer", NodeItem::Type::BSDF, {{"top", n_sheen_bsdf}, {"base", n_subsurface_mix}}); NodeItem n_transmission_mix = create_node("mix", NodeItem::Type::BSDF, - { - {"fg", n_transmission_bsdf}, - {"bg", n_sheen_layer}, - {"mix", inputs.find("transmission")->second}, - }); + {{"fg", n_transmission_bsdf}, + {"bg", n_sheen_layer}, + {"mix", inputs.find("transmission")->second}}); - NodeItem n_specular_layer = create_node("layer", - NodeItem::Type::BSDF, - { - {"top", n_specular_bsdf}, - {"base", n_transmission_mix}, - }); + NodeItem n_specular_layer = create_node( + "layer", NodeItem::Type::BSDF, {{"top", n_specular_bsdf}, {"base", n_transmission_mix}}); NodeItem n_metalness_mix = create_node("mix", NodeItem::Type::BSDF, - { - {"fg", n_metal_bsdf}, - {"bg", n_specular_layer}, - {"mix", inputs.find("metalness")->second}, - }); + {{"fg", n_metal_bsdf}, + {"bg", n_specular_layer}, + {"mix", inputs.find("metalness")->second}}); NodeItem n_thin_film_layer = create_node( "layer", NodeItem::Type::BSDF, {{"top", n_thin_film_bsdf}, {"base", n_metalness_mix}}); NodeItem n_opacity_luminance = create_node( - "luminance", - NodeItem::Type::Color3, - { - {"in", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))}, - }); + "luminance", NodeItem::Type::Color3, {{"in", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))}}); - NodeItem n_coat_attenuation = create_node("mix", - NodeItem::Type::Color3, - { - {"fg", base_color}, - {"bg", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))}, - {"mix", coat}, - }); + NodeItem n_coat_attenuation = create_node( + "mix", + NodeItem::Type::Color3, + {{"fg", base_color}, {"bg", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))}, {"mix", coat}}); res = create_node("layer", NodeItem::Type::BSDF, - { - {"top", n_coat_bsdf}, - {"base", n_thin_film_layer * n_coat_attenuation}, - }); + {{"top", n_coat_bsdf}, {"base", n_thin_film_layer * n_coat_attenuation}}); return res; } @@ -635,9 +565,7 @@ NODE_SHADER_MATERIALX_BEGIN res = create_node( "uniform_edf", NodeItem::Type::EDF, - { - {"color", inputs.find("emission_color")->second * inputs.find("emission")->second}, - }); + {{"color", inputs.find("emission_color")->second * inputs.find("emission")->second}}); } else if (to_type_ == NodeItem::Type::SurfaceShader) { auto b_inputs = bsdf_inputs(); @@ -651,39 +579,37 @@ NODE_SHADER_MATERIALX_BEGIN res = create_node("standard_surface", NodeItem::Type::SurfaceShader, - { - {"base", val(1.0f)}, - {"base_color", base_color}, - {"diffuse_roughness", roughness}, - {"metalness", b_inputs.find("metalness")->second}, - {"specular", b_inputs.find("specular")->second}, - {"specular_color", base_color}, - {"specular_roughness", roughness}, - {"specular_IOR", ior}, - {"specular_anisotropy", anisotropic}, - {"specular_rotation", rotation}, - {"transmission", b_inputs.find("transmission")->second}, - {"transmission_color", base_color}, - {"transmission_extra_roughness", roughness}, - {"subsurface", b_inputs.find("subsurface")->second}, - {"subsurface_color", base_color}, - {"subsurface_radius", b_inputs.find("subsurface_radius")->second}, - {"subsurface_anisotropy", anisotropic}, - {"sheen", b_inputs.find("sheen")->second}, - {"sheen_color", base_color}, - {"sheen_roughness", roughness}, - {"coat", b_inputs.find("coat")->second}, - {"coat_color", base_color}, - {"coat_roughness", b_inputs.find("coat_roughness")->second}, - {"coat_IOR", ior}, - {"coat_anisotropy", anisotropic}, - {"coat_rotation", rotation}, - {"coat_normal", b_inputs.find("coat_normal")->second}, - {"emission", e_inputs.find("emission")->second}, - {"emission_color", e_inputs.find("emission_color")->second}, - {"normal", b_inputs.find("normal")->second}, - {"tangent", b_inputs.find("tangent")->second}, - }); + {{"base", val(1.0f)}, + {"base_color", base_color}, + {"diffuse_roughness", roughness}, + {"metalness", b_inputs.find("metalness")->second}, + {"specular", b_inputs.find("specular")->second}, + {"specular_color", base_color}, + {"specular_roughness", roughness}, + {"specular_IOR", ior}, + {"specular_anisotropy", anisotropic}, + {"specular_rotation", rotation}, + {"transmission", b_inputs.find("transmission")->second}, + {"transmission_color", base_color}, + {"transmission_extra_roughness", roughness}, + {"subsurface", b_inputs.find("subsurface")->second}, + {"subsurface_color", base_color}, + {"subsurface_radius", b_inputs.find("subsurface_radius")->second}, + {"subsurface_anisotropy", anisotropic}, + {"sheen", b_inputs.find("sheen")->second}, + {"sheen_color", base_color}, + {"sheen_roughness", roughness}, + {"coat", b_inputs.find("coat")->second}, + {"coat_color", base_color}, + {"coat_roughness", b_inputs.find("coat_roughness")->second}, + {"coat_IOR", ior}, + {"coat_anisotropy", anisotropic}, + {"coat_rotation", rotation}, + {"coat_normal", b_inputs.find("coat_normal")->second}, + {"emission", e_inputs.find("emission")->second}, + {"emission_color", e_inputs.find("emission_color")->second}, + {"normal", b_inputs.find("normal")->second}, + {"tangent", b_inputs.find("tangent")->second}}); } return res; -- 2.30.2 From 2c67395a10f593bf017928ecd2efaff9ea4aaca5 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Wed, 20 Sep 2023 17:20:03 +0300 Subject: [PATCH 07/10] MaterialX: reformat style --- .../nodes/shader/nodes/node_shader_bsdf_principled.cc | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc index 42e9cc51e8c5..24e4ca7d82c9 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc @@ -466,11 +466,9 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_coat_affected_transmission_roughness = create_node( "mix", NodeItem::Type::Float, - { - {"fg", val(1.0f)}, - {"bg", (roughness + roughness).clamp(0.0f, 1.0f)}, - {"mix", n_coat_affect_roughness_multiply2}, - }); + {{"fg", val(1.0f)}, + {"bg", (roughness + roughness).clamp(0.0f, 1.0f)}, + {"mix", n_coat_affect_roughness_multiply2}}); NodeItem n_transmission_roughness = create_node( "roughness_anisotropy", -- 2.30.2 From 7a2d2ae8e698bac3636b21881aef93045c0a797f Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Wed, 20 Sep 2023 19:28:16 +0300 Subject: [PATCH 08/10] MaterialX: PrincipleBSDF code improvements --- .../nodes/node_shader_bsdf_principled.cc | 235 ++++++++---------- 1 file changed, 101 insertions(+), 134 deletions(-) diff --git a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc index 24e4ca7d82c9..539d1bb02c82 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc @@ -289,99 +289,60 @@ static void node_shader_update_principled(bNodeTree *ntree, bNode *node) NODE_SHADER_MATERIALX_BEGIN #ifdef WITH_MATERIALX { + /* NOTE: commented inputs aren't used for node creation. */ auto bsdf_inputs = [&]() { - NodeItem base_color = get_input_value("Base Color", NodeItem::Type::Color3); - - NodeItem subsurface = get_input_value("Subsurface", NodeItem::Type::Float); - NodeItem subsurface_radius = get_input_value("Subsurface Radius", NodeItem::Type::Vector3); - NodeItem subsurface_scale = get_input_value("Subsurface Scale", NodeItem::Type::Float); - - NodeItem metallic = get_input_value("Metallic", NodeItem::Type::Float); - NodeItem specular = get_input_value("Specular", NodeItem::Type::Float); - // NodeItem specular_tint = get_input_value("Specular Tint"); - NodeItem roughness = get_input_value("Roughness", NodeItem::Type::Float); - - /* TODO: use Specular Tint input */ - NodeItem anisotropic = get_input_value("Anisotropic", NodeItem::Type::Float); - NodeItem anisotropic_rotation = get_input_value("Anisotropic Rotation", NodeItem::Type::Float); - // anisotropic_rotation = 0.5 - (anisotropic_rotation % 1.0) - - NodeItem sheen = get_input_value("Sheen", NodeItem::Type::Float); - // sheen_tint = get_input_value("Sheen Tint"); - - NodeItem coat = get_input_value("Coat", NodeItem::Type::Float); - NodeItem coat_roughness = get_input_value("Coat Roughness", NodeItem::Type::Float); - - NodeItem ior = get_input_value("IOR", NodeItem::Type::Float); - - NodeItem transmission = get_input_value("Transmission", NodeItem::Type::Float); - - NodeItem alpha = get_input_value("Alpha", NodeItem::Type::Float); - // transparency = 1.0 - alpha - - NodeItem normal = get_input_link("Normal", NodeItem::Type::Vector3); - NodeItem coat_normal = get_input_link("Coat Normal", NodeItem::Type::Vector3); - NodeItem tangent = get_input_link("Tangent", NodeItem::Type::Vector3); - - subsurface_radius = subsurface_radius * subsurface_scale; - return std::map{ - {"base", val(1.0f)}, - {"base_color", base_color}, - {"diffuse_roughness", roughness}, - {"normal", normal}, - {"tangent", tangent}, - {"metalness", metallic}, - {"specular", specular}, - {"specular_color", base_color}, - {"specular_roughness", roughness}, - {"specular_IOR", ior}, - {"specular_anisotropy", anisotropic}, - {"specular_rotation", anisotropic_rotation}, - {"transmission", transmission}, - {"transmission_color", base_color}, - {"transmission_extra_roughness", roughness}, - {"subsurface", subsurface}, - {"subsurface_color", base_color}, - {"subsurface_radius", subsurface_radius}, - {"subsurface_anisotropy", anisotropic}, - {"sheen", sheen}, - {"sheen_color", base_color}, - {"sheen_roughness", roughness}, - {"coat", coat}, - {"coat_color", base_color}, - {"coat_roughness", coat_roughness}, - {"coat_IOR", ior}, - {"coat_anisotropy", anisotropic}, - {"coat_rotation", anisotropic_rotation}, - {"coat_normal", coat_normal}, + {"base_color", get_input_value("Base Color", NodeItem::Type::Color3)}, + {"subsurface", get_input_value("Subsurface", NodeItem::Type::Float)}, + {"subsurface_scale", get_input_value("Subsurface Scale", NodeItem::Type::Float)}, + {"subsurface_radius", get_input_value("Subsurface Radius", NodeItem::Type::Vector3)}, + //{"subsurface_ior", get_input_value("Subsurface IOR", NodeItem::Type::Vector3)}, + {"subsurface_anisotropy", get_input_value("Subsurface Anisotropy", NodeItem::Type::Float)}, + {"metallic", get_input_value("Metallic", NodeItem::Type::Float)}, + {"specular", get_input_value("Specular", NodeItem::Type::Float)}, + {"specular_tint", get_input_value("Specular Tint", NodeItem::Type::Color3)}, + {"roughness", get_input_value("Roughness", NodeItem::Type::Float)}, + {"anisotropic", get_input_value("Anisotropic", NodeItem::Type::Float)}, + {"anisotropic_rotation", get_input_value("Anisotropic Rotation", NodeItem::Type::Float)}, + {"sheen", get_input_value("Sheen", NodeItem::Type::Float)}, + {"sheen_roughness", get_input_value("Sheen Roughness", NodeItem::Type::Float)}, + {"sheen_tint", get_input_value("Sheen Tint", NodeItem::Type::Color3)}, + {"coat", get_input_value("Coat", NodeItem::Type::Float)}, + {"coat_roughness", get_input_value("Coat Roughness", NodeItem::Type::Float)}, + {"coat_ior", get_input_value("Coat IOR", NodeItem::Type::Float)}, + {"coat_tint", get_input_value("Coat Tint", NodeItem::Type::Color3)}, + {"ior", get_input_value("IOR", NodeItem::Type::Float)}, + {"transmission", get_input_value("Transmission", NodeItem::Type::Float)}, + //{"alpha", get_input_value("Alpha", NodeItem::Type::Float)}, + {"normal", get_input_link("Normal", NodeItem::Type::Vector3)}, + {"coat_normal", get_input_link("Coat Normal", NodeItem::Type::Vector3)}, + {"tangent", get_input_link("Tangent", NodeItem::Type::Vector3)}, }; }; auto edf_inputs = [&]() { - NodeItem emission = get_input_value("Emission", NodeItem::Type::Color3); - NodeItem emission_strength = get_input_value("Emission Strength", NodeItem::Type::Float); - - return std::map{{"emission", emission_strength}, - {"emission_color", emission}}; + return std::map{ + {"emission", get_input_value("Emission Strength", NodeItem::Type::Float)}, + {"emission_color", get_input_value("Emission", NodeItem::Type::Color3)}}; }; NodeItem res = empty(); if (to_type_ == NodeItem::Type::BSDF) { auto inputs = bsdf_inputs(); - NodeItem roughness = inputs.find("diffuse_roughness")->second; - NodeItem anisotropy = inputs.find("specular_anisotropy")->second; - NodeItem rotation = inputs.find("specular_rotation")->second; + NodeItem roughness = inputs.find("roughness")->second; + NodeItem anisotropy = inputs.find("anisotropic")->second; + NodeItem rotation = inputs.find("anisotropic_rotation")->second; NodeItem base_color = inputs.find("base_color")->second; NodeItem specular = inputs.find("specular")->second; NodeItem coat = inputs.find("coat")->second; - NodeItem ior = inputs.find("specular_IOR")->second; + NodeItem ior = inputs.find("ior")->second; NodeItem normal = inputs.find("normal")->second; NodeItem tangent = inputs.find("tangent")->second; + NodeItem coat_normal = inputs.find("coat_normal")->second; NodeItem n_main_tangent = empty(); - if (tangent) { + if (tangent && normal) { NodeItem n_tangent_rotate = create_node( "rotate3d", NodeItem::Type::Vector3, @@ -397,22 +358,22 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_coat_roughness_vector = create_node( "roughness_anisotropy", NodeItem::Type::Vector2, - {{"roughness", roughness}, {"anisotropy", anisotropy}}); + {{"roughness", inputs.find("coat_roughness")->second}, {"anisotropy", anisotropy}}); NodeItem n_coat_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF, {{"weight", coat}, - {"tint", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))}, - {"ior", ior}, + {"tint", inputs.find("coat_tint")->second}, + {"ior", inputs.find("coat_ior")->second}, {"scatter_mode", val(std::string("R"))}, {"roughness", n_coat_roughness_vector}, - {"normal", normal}}); + {"normal", coat_normal}}); - if (tangent) { + if (tangent && coat_normal) { NodeItem n_coat_tangent_rotate = create_node( "rotate3d", NodeItem::Type::Vector3, - {{"in", tangent}, {"amount", rotation * val(360.0f)}, {"axis", normal}}); + {{"in", tangent}, {"amount", rotation * val(360.0f)}, {"axis", coat_normal}}); NodeItem n_coat_tangent_rotate_normalize = create_node( "normalize", NodeItem::Type::Vector3, {{"in", n_coat_tangent_rotate}}); @@ -434,7 +395,8 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_ior_out = n_artistic_ior.add_output("ior", NodeItem::Type::Color3); NodeItem n_extinction_out = n_artistic_ior.add_output("extinction", NodeItem::Type::Color3); - NodeItem n_coat_affect_roughness_multiply2 = coat * val(0.0f) * roughness; + NodeItem n_coat_affect_roughness_multiply2 = coat * val(0.0f) * + inputs.find("coat_roughness")->second; NodeItem n_coat_affected_roughness = create_node( "mix", NodeItem::Type::Float, @@ -456,7 +418,7 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_specular_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF, {{"weight", specular}, - {"tint", base_color}, + {"tint", inputs.find("specular_tint")->second}, {"ior", ior}, {"scatter_mode", val(std::string("R"))}, {"roughness", n_main_roughness}, @@ -490,12 +452,14 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem::Type::BSDF, {{"color", n_coat_affected_subsurface_color}, {"normal", normal}}); - NodeItem n_subsurface_bsdf = create_node("subsurface_bsdf", - NodeItem::Type::BSDF, - {{"color", n_coat_affected_subsurface_color}, - {"radius", inputs.find("subsurface_radius")->second}, - {"anisotropy", anisotropy}, - {"normal", normal}}); + NodeItem n_subsurface_bsdf = create_node( + "subsurface_bsdf", + NodeItem::Type::BSDF, + {{"color", n_coat_affected_subsurface_color}, + {"radius", + inputs.find("subsurface_radius")->second * inputs.find("subsurface_scale")->second}, + {"anisotropy", inputs.find("subsurface_anisotropy")->second}, + {"normal", normal}}); NodeItem n_selected_subsurface_bsdf = create_node( "mix", @@ -505,8 +469,8 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_sheen_bsdf = create_node("sheen_bsdf", NodeItem::Type::BSDF, {{"weight", inputs.find("sheen")->second}, - {"color", base_color}, - {"roughness", roughness}, + {"color", inputs.find("sheen_tint")->second}, + {"roughness", inputs.find("sheen_roughness")->second}, {"normal", normal}}); NodeItem n_diffuse_bsdf = create_node("oren_nayar_diffuse_bsdf", @@ -538,7 +502,7 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem::Type::BSDF, {{"fg", n_metal_bsdf}, {"bg", n_specular_layer}, - {"mix", inputs.find("metalness")->second}}); + {"mix", inputs.find("metallic")->second}}); NodeItem n_thin_film_layer = create_node( "layer", NodeItem::Type::BSDF, {{"top", n_thin_film_bsdf}, {"base", n_metalness_mix}}); @@ -546,16 +510,15 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_opacity_luminance = create_node( "luminance", NodeItem::Type::Color3, {{"in", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))}}); - NodeItem n_coat_attenuation = create_node( - "mix", - NodeItem::Type::Color3, - {{"fg", base_color}, {"bg", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))}, {"mix", coat}}); + NodeItem n_coat_attenuation = create_node("mix", + NodeItem::Type::Color3, + {{"fg", inputs.find("coat_tint")->second}, + {"bg", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))}, + {"mix", coat}}); res = create_node("layer", NodeItem::Type::BSDF, {{"top", n_coat_bsdf}, {"base", n_thin_film_layer * n_coat_attenuation}}); - - return res; } else if (to_type_ == NodeItem::Type::EDF) { auto inputs = edf_inputs(); @@ -569,45 +532,49 @@ NODE_SHADER_MATERIALX_BEGIN auto b_inputs = bsdf_inputs(); auto e_inputs = edf_inputs(); - NodeItem roughness = b_inputs.find("diffuse_roughness")->second; + NodeItem roughness = b_inputs.find("roughness")->second; NodeItem base_color = b_inputs.find("base_color")->second; - NodeItem anisotropic = b_inputs.find("specular_anisotropy")->second; - NodeItem ior = b_inputs.find("specular_IOR")->second; - NodeItem rotation = b_inputs.find("specular_rotation")->second; + NodeItem anisotropic = b_inputs.find("anisotropic")->second; + NodeItem rotation = b_inputs.find("anisotropic_rotation")->second; - res = create_node("standard_surface", - NodeItem::Type::SurfaceShader, - {{"base", val(1.0f)}, - {"base_color", base_color}, - {"diffuse_roughness", roughness}, - {"metalness", b_inputs.find("metalness")->second}, - {"specular", b_inputs.find("specular")->second}, - {"specular_color", base_color}, - {"specular_roughness", roughness}, - {"specular_IOR", ior}, - {"specular_anisotropy", anisotropic}, - {"specular_rotation", rotation}, - {"transmission", b_inputs.find("transmission")->second}, - {"transmission_color", base_color}, - {"transmission_extra_roughness", roughness}, - {"subsurface", b_inputs.find("subsurface")->second}, - {"subsurface_color", base_color}, - {"subsurface_radius", b_inputs.find("subsurface_radius")->second}, - {"subsurface_anisotropy", anisotropic}, - {"sheen", b_inputs.find("sheen")->second}, - {"sheen_color", base_color}, - {"sheen_roughness", roughness}, - {"coat", b_inputs.find("coat")->second}, - {"coat_color", base_color}, - {"coat_roughness", b_inputs.find("coat_roughness")->second}, - {"coat_IOR", ior}, - {"coat_anisotropy", anisotropic}, - {"coat_rotation", rotation}, - {"coat_normal", b_inputs.find("coat_normal")->second}, - {"emission", e_inputs.find("emission")->second}, - {"emission_color", e_inputs.find("emission_color")->second}, - {"normal", b_inputs.find("normal")->second}, - {"tangent", b_inputs.find("tangent")->second}}); + res = create_node( + "standard_surface", + NodeItem::Type::SurfaceShader, + {{"base", val(1.0f)}, + {"base_color", base_color}, + {"diffuse_roughness", roughness}, + {"metalness", b_inputs.find("metallic")->second}, + {"specular", b_inputs.find("specular")->second}, + {"specular_color", b_inputs.find("specular_tint")->second}, + {"specular_roughness", roughness}, + {"specular_IOR", b_inputs.find("ior")->second}, + {"specular_anisotropy", anisotropic}, + {"specular_rotation", rotation}, + {"transmission", b_inputs.find("transmission")->second}, + {"transmission_color", base_color}, + {"transmission_extra_roughness", roughness}, + {"subsurface", b_inputs.find("subsurface")->second}, + {"subsurface_color", base_color}, + {"subsurface_radius", + b_inputs.find("subsurface_radius")->second * b_inputs.find("subsurface_scale")->second}, + {"subsurface_anisotropy", b_inputs.find("subsurface_anisotropy")->second}, + {"sheen", b_inputs.find("sheen")->second}, + {"sheen_color", b_inputs.find("sheen_tint")->second}, + {"sheen_roughness", b_inputs.find("sheen_roughness")->second}, + {"coat", b_inputs.find("coat")->second}, + {"coat_color", b_inputs.find("coat_tint")->second}, + {"coat_roughness", b_inputs.find("coat_roughness")->second}, + {"coat_IOR", b_inputs.find("coat_ior")->second}, + {"coat_anisotropy", anisotropic}, + {"coat_rotation", rotation}, + {"coat_normal", b_inputs.find("coat_normal")->second}, + {"emission", e_inputs.find("emission")->second}, + {"emission_color", e_inputs.find("emission_color")->second}, + {"normal", b_inputs.find("normal")->second}, + {"tangent", b_inputs.find("tangent")->second}}); + } + else { + BLI_assert_unreachable(); } return res; -- 2.30.2 From 0201383a4ca2246ce03b83fab0e1fd0588071fa6 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Thu, 21 Sep 2023 11:11:59 +0300 Subject: [PATCH 09/10] MaterialX: PrincipleBSDF code improvements --- .../nodes/shader/materialx/node_item.h | 4 +- .../nodes/node_shader_bsdf_principled.cc | 160 +++++++++--------- 2 files changed, 79 insertions(+), 85 deletions(-) diff --git a/source/blender/nodes/shader/materialx/node_item.h b/source/blender/nodes/shader/materialx/node_item.h index 40be1153031b..ff0efe385737 100644 --- a/source/blender/nodes/shader/materialx/node_item.h +++ b/source/blender/nodes/shader/materialx/node_item.h @@ -51,9 +51,11 @@ class NodeItem { MaterialX::OutputPtr output; private: - MaterialX::GraphElement *graph_; + MaterialX::GraphElement *graph_ = nullptr; public: + /* Default constructor added to allow easy work with std::map. Don't use this constructor to create NodeItem. */ + NodeItem() = default; NodeItem(MaterialX::GraphElement *graph); ~NodeItem() = default; diff --git a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc index 539d1bb02c82..ada2bb7be864 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc @@ -328,18 +328,18 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem res = empty(); if (to_type_ == NodeItem::Type::BSDF) { - auto inputs = bsdf_inputs(); + auto in = bsdf_inputs(); - NodeItem roughness = inputs.find("roughness")->second; - NodeItem anisotropy = inputs.find("anisotropic")->second; - NodeItem rotation = inputs.find("anisotropic_rotation")->second; - NodeItem base_color = inputs.find("base_color")->second; - NodeItem specular = inputs.find("specular")->second; - NodeItem coat = inputs.find("coat")->second; - NodeItem ior = inputs.find("ior")->second; - NodeItem normal = inputs.find("normal")->second; - NodeItem tangent = inputs.find("tangent")->second; - NodeItem coat_normal = inputs.find("coat_normal")->second; + NodeItem roughness = in["roughness"]; + NodeItem anisotropy = in["anisotropic"]; + NodeItem rotation = in["anisotropic_rotation"]; + NodeItem base_color = in["base_color"]; + NodeItem specular = in["specular"]; + NodeItem coat = in["coat"]; + NodeItem ior = in["ior"]; + NodeItem normal = in["normal"]; + NodeItem tangent = in["tangent"]; + NodeItem coat_normal = in["coat_normal"]; NodeItem n_main_tangent = empty(); if (tangent && normal) { @@ -358,13 +358,13 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_coat_roughness_vector = create_node( "roughness_anisotropy", NodeItem::Type::Vector2, - {{"roughness", inputs.find("coat_roughness")->second}, {"anisotropy", anisotropy}}); + {{"roughness", in["coat_roughness"]}, {"anisotropy", anisotropy}}); NodeItem n_coat_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF, {{"weight", coat}, - {"tint", inputs.find("coat_tint")->second}, - {"ior", inputs.find("coat_ior")->second}, + {"tint", in["coat_tint"]}, + {"ior", in["coat_ior"]}, {"scatter_mode", val(std::string("R"))}, {"roughness", n_coat_roughness_vector}, {"normal", coat_normal}}); @@ -395,8 +395,7 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_ior_out = n_artistic_ior.add_output("ior", NodeItem::Type::Color3); NodeItem n_extinction_out = n_artistic_ior.add_output("extinction", NodeItem::Type::Color3); - NodeItem n_coat_affect_roughness_multiply2 = coat * val(0.0f) * - inputs.find("coat_roughness")->second; + NodeItem n_coat_affect_roughness_multiply2 = coat * val(0.0f) * in["coat_roughness"]; NodeItem n_coat_affected_roughness = create_node( "mix", NodeItem::Type::Float, @@ -418,7 +417,7 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_specular_bsdf = create_node("dielectric_bsdf", NodeItem::Type::BSDF, {{"weight", specular}, - {"tint", inputs.find("specular_tint")->second}, + {"tint", in["specular_tint"]}, {"ior", ior}, {"scatter_mode", val(std::string("R"))}, {"roughness", n_main_roughness}, @@ -456,9 +455,8 @@ NODE_SHADER_MATERIALX_BEGIN "subsurface_bsdf", NodeItem::Type::BSDF, {{"color", n_coat_affected_subsurface_color}, - {"radius", - inputs.find("subsurface_radius")->second * inputs.find("subsurface_scale")->second}, - {"anisotropy", inputs.find("subsurface_anisotropy")->second}, + {"radius", in["subsurface_radius"] * in["subsurface_scale"]}, + {"anisotropy", in["subsurface_anisotropy"]}, {"normal", normal}}); NodeItem n_selected_subsurface_bsdf = create_node( @@ -468,9 +466,9 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_sheen_bsdf = create_node("sheen_bsdf", NodeItem::Type::BSDF, - {{"weight", inputs.find("sheen")->second}, - {"color", inputs.find("sheen_tint")->second}, - {"roughness", inputs.find("sheen_roughness")->second}, + {{"weight", in["sheen"]}, + {"color", in["sheen_tint"]}, + {"roughness", in["sheen_roughness"]}, {"normal", normal}}); NodeItem n_diffuse_bsdf = create_node("oren_nayar_diffuse_bsdf", @@ -480,29 +478,26 @@ NODE_SHADER_MATERIALX_BEGIN {"weight", val(1.0f)}, {"normal", normal}}); - NodeItem n_subsurface_mix = create_node("mix", - NodeItem::Type::BSDF, - {{"fg", n_selected_subsurface_bsdf}, - {"bg", n_diffuse_bsdf}, - {"mix", inputs.find("subsurface")->second}}); + NodeItem n_subsurface_mix = create_node( + "mix", + NodeItem::Type::BSDF, + {{"fg", n_selected_subsurface_bsdf}, {"bg", n_diffuse_bsdf}, {"mix", in["subsurface"]}}); NodeItem n_sheen_layer = create_node( "layer", NodeItem::Type::BSDF, {{"top", n_sheen_bsdf}, {"base", n_subsurface_mix}}); - NodeItem n_transmission_mix = create_node("mix", - NodeItem::Type::BSDF, - {{"fg", n_transmission_bsdf}, - {"bg", n_sheen_layer}, - {"mix", inputs.find("transmission")->second}}); + NodeItem n_transmission_mix = create_node( + "mix", + NodeItem::Type::BSDF, + {{"fg", n_transmission_bsdf}, {"bg", n_sheen_layer}, {"mix", in["transmission"]}}); NodeItem n_specular_layer = create_node( "layer", NodeItem::Type::BSDF, {{"top", n_specular_bsdf}, {"base", n_transmission_mix}}); - NodeItem n_metalness_mix = create_node("mix", - NodeItem::Type::BSDF, - {{"fg", n_metal_bsdf}, - {"bg", n_specular_layer}, - {"mix", inputs.find("metallic")->second}}); + NodeItem n_metalness_mix = create_node( + "mix", + NodeItem::Type::BSDF, + {{"fg", n_metal_bsdf}, {"bg", n_specular_layer}, {"mix", in["metallic"]}}); NodeItem n_thin_film_layer = create_node( "layer", NodeItem::Type::BSDF, {{"top", n_thin_film_bsdf}, {"base", n_metalness_mix}}); @@ -512,7 +507,7 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem n_coat_attenuation = create_node("mix", NodeItem::Type::Color3, - {{"fg", inputs.find("coat_tint")->second}, + {{"fg", in["coat_tint"]}, {"bg", val(MaterialX::Color3(1.0f, 1.0f, 1.0f))}, {"mix", coat}}); @@ -521,57 +516,54 @@ NODE_SHADER_MATERIALX_BEGIN {{"top", n_coat_bsdf}, {"base", n_thin_film_layer * n_coat_attenuation}}); } else if (to_type_ == NodeItem::Type::EDF) { - auto inputs = edf_inputs(); + auto in = edf_inputs(); res = create_node( - "uniform_edf", - NodeItem::Type::EDF, - {{"color", inputs.find("emission_color")->second * inputs.find("emission")->second}}); + "uniform_edf", NodeItem::Type::EDF, {{"color", in["emission_color"] * in["emission"]}}); } else if (to_type_ == NodeItem::Type::SurfaceShader) { - auto b_inputs = bsdf_inputs(); - auto e_inputs = edf_inputs(); + auto in = bsdf_inputs(); + auto e_in = edf_inputs(); + in.insert(e_in.begin(), e_in.end()); - NodeItem roughness = b_inputs.find("roughness")->second; - NodeItem base_color = b_inputs.find("base_color")->second; - NodeItem anisotropic = b_inputs.find("anisotropic")->second; - NodeItem rotation = b_inputs.find("anisotropic_rotation")->second; + NodeItem roughness = in["roughness"]; + NodeItem base_color = in["base_color"]; + NodeItem anisotropic = in["anisotropic"]; + NodeItem rotation = in["anisotropic_rotation"]; - res = create_node( - "standard_surface", - NodeItem::Type::SurfaceShader, - {{"base", val(1.0f)}, - {"base_color", base_color}, - {"diffuse_roughness", roughness}, - {"metalness", b_inputs.find("metallic")->second}, - {"specular", b_inputs.find("specular")->second}, - {"specular_color", b_inputs.find("specular_tint")->second}, - {"specular_roughness", roughness}, - {"specular_IOR", b_inputs.find("ior")->second}, - {"specular_anisotropy", anisotropic}, - {"specular_rotation", rotation}, - {"transmission", b_inputs.find("transmission")->second}, - {"transmission_color", base_color}, - {"transmission_extra_roughness", roughness}, - {"subsurface", b_inputs.find("subsurface")->second}, - {"subsurface_color", base_color}, - {"subsurface_radius", - b_inputs.find("subsurface_radius")->second * b_inputs.find("subsurface_scale")->second}, - {"subsurface_anisotropy", b_inputs.find("subsurface_anisotropy")->second}, - {"sheen", b_inputs.find("sheen")->second}, - {"sheen_color", b_inputs.find("sheen_tint")->second}, - {"sheen_roughness", b_inputs.find("sheen_roughness")->second}, - {"coat", b_inputs.find("coat")->second}, - {"coat_color", b_inputs.find("coat_tint")->second}, - {"coat_roughness", b_inputs.find("coat_roughness")->second}, - {"coat_IOR", b_inputs.find("coat_ior")->second}, - {"coat_anisotropy", anisotropic}, - {"coat_rotation", rotation}, - {"coat_normal", b_inputs.find("coat_normal")->second}, - {"emission", e_inputs.find("emission")->second}, - {"emission_color", e_inputs.find("emission_color")->second}, - {"normal", b_inputs.find("normal")->second}, - {"tangent", b_inputs.find("tangent")->second}}); + res = create_node("standard_surface", + NodeItem::Type::SurfaceShader, + {{"base", val(1.0f)}, + {"base_color", base_color}, + {"diffuse_roughness", roughness}, + {"metalness", in["metallic"]}, + {"specular", in["specular"]}, + {"specular_color", in["specular_tint"]}, + {"specular_roughness", roughness}, + {"specular_IOR", in["ior"]}, + {"specular_anisotropy", anisotropic}, + {"specular_rotation", rotation}, + {"transmission", in["transmission"]}, + {"transmission_color", base_color}, + {"transmission_extra_roughness", roughness}, + {"subsurface", in["subsurface"]}, + {"subsurface_color", base_color}, + {"subsurface_radius", in["subsurface_radius"] * in["subsurface_scale"]}, + {"subsurface_anisotropy", in["subsurface_anisotropy"]}, + {"sheen", in["sheen"]}, + {"sheen_color", in["sheen_tint"]}, + {"sheen_roughness", in["sheen_roughness"]}, + {"coat", in["coat"]}, + {"coat_color", in["coat_tint"]}, + {"coat_roughness", in["coat_roughness"]}, + {"coat_IOR", in["coat_ior"]}, + {"coat_anisotropy", anisotropic}, + {"coat_rotation", rotation}, + {"coat_normal", in["coat_normal"]}, + {"emission", in["emission"]}, + {"emission_color", in["emission_color"]}, + {"normal", in["normal"]}, + {"tangent", in["tangent"]}}); } else { BLI_assert_unreachable(); -- 2.30.2 From 3fdc75b13c7bb0a77277d46384629ac315329ad7 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Thu, 21 Sep 2023 11:36:47 +0300 Subject: [PATCH 10/10] MaterialX: fix comment --- source/blender/nodes/shader/materialx/node_item.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/blender/nodes/shader/materialx/node_item.h b/source/blender/nodes/shader/materialx/node_item.h index ff0efe385737..cd367827bee1 100644 --- a/source/blender/nodes/shader/materialx/node_item.h +++ b/source/blender/nodes/shader/materialx/node_item.h @@ -54,7 +54,8 @@ class NodeItem { MaterialX::GraphElement *graph_ = nullptr; public: - /* Default constructor added to allow easy work with std::map. Don't use this constructor to create NodeItem. */ + /* NOTE: Default constructor added to allow easy work with std::map. + * Don't use this constructor to create NodeItem. */ NodeItem() = default; NodeItem(MaterialX::GraphElement *graph); ~NodeItem() = default; -- 2.30.2