From f6d9ff9a8c0c88170dafe49c2a5954665b9eb7d3 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 26 Sep 2023 11:54:17 +0300 Subject: [PATCH 1/4] Implemented BSDF tranparent node. Added SurfaceOpacity type --- .../nodes/shader/materialx/node_item.h | 3 +++ .../shader/nodes/node_shader_add_shader.cc | 5 ++++- .../nodes/node_shader_bsdf_principled.cc | 11 ++++++++-- .../nodes/node_shader_bsdf_transparent.cc | 21 +++++++++++++++++++ .../shader/nodes/node_shader_light_falloff.cc | 2 +- .../shader/nodes/node_shader_mix_shader.cc | 2 +- .../nodes/node_shader_output_material.cc | 11 ++++------ 7 files changed, 43 insertions(+), 12 deletions(-) diff --git a/source/blender/nodes/shader/materialx/node_item.h b/source/blender/nodes/shader/materialx/node_item.h index 0b6069c406a0..3ceed5defc1c 100644 --- a/source/blender/nodes/shader/materialx/node_item.h +++ b/source/blender/nodes/shader/materialx/node_item.h @@ -44,6 +44,9 @@ class NodeItem { Displacementshader, SurfaceShader, Material, + + /* Special type to retrieve opacity for */ + SurfaceOpacity, }; enum class CompareOp { Less = 0, LessEq, Eq, GreaterEq, Greater, NotEq }; diff --git a/source/blender/nodes/shader/nodes/node_shader_add_shader.cc b/source/blender/nodes/shader/nodes/node_shader_add_shader.cc index 6e0e0a81a516..13e76464437e 100644 --- a/source/blender/nodes/shader/nodes/node_shader_add_shader.cc +++ b/source/blender/nodes/shader/nodes/node_shader_add_shader.cc @@ -25,7 +25,7 @@ static int node_shader_gpu_add_shader(GPUMaterial *mat, NODE_SHADER_MATERIALX_BEGIN #ifdef WITH_MATERIALX { - if (!ELEM(to_type_, NodeItem::Type::BSDF, NodeItem::Type::EDF)) { + if (!ELEM(to_type_, NodeItem::Type::BSDF, NodeItem::Type::EDF, NodeItem::Type::SurfaceOpacity)) { return empty(); } @@ -41,6 +41,9 @@ NODE_SHADER_MATERIALX_BEGIN if (!shader1 && shader2) { return shader2; } + if (to_type_ == NodeItem::Type::SurfaceOpacity) { + return (shader1 + shader2) * val(0.5f); + } return shader1 + shader2; } #endif 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 18b8bba388fb..99499e222a6a 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc @@ -350,7 +350,7 @@ NODE_SHADER_MATERIALX_BEGIN {"coat_tint", get_input_value("Coat Tint", NodeItem::Type::Color3)}, {"ior", get_input_value("IOR", NodeItem::Type::Float)}, {"transmission", get_input_value("Transmission Weight", NodeItem::Type::Float)}, - //{"alpha", get_input_value("Alpha", 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)}, @@ -572,9 +572,16 @@ NODE_SHADER_MATERIALX_BEGIN {"emission", in["emission"]}, {"emission_color", in["emission_color"]}, {"normal", in["normal"]}, - {"tangent", in["tangent"]}}); + {"tangent", in["tangent"]}, + {"opacity", in["alpha"].convert(NodeItem::Type::Color3)}}); break; } + + case NodeItem::Type::SurfaceOpacity: { + res = get_input_value("Alpha", NodeItem::Type::Float); + break; + } + default: BLI_assert_unreachable(); } diff --git a/source/blender/nodes/shader/nodes/node_shader_bsdf_transparent.cc b/source/blender/nodes/shader/nodes/node_shader_bsdf_transparent.cc index 5de21efe7911..46de861eafd8 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_transparent.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_transparent.cc @@ -27,6 +27,26 @@ static int node_shader_gpu_bsdf_transparent(GPUMaterial *mat, return GPU_stack_link(mat, node, "node_bsdf_transparent", in, out); } +NODE_SHADER_MATERIALX_BEGIN +#ifdef WITH_MATERIALX +{ + switch (to_type_) { + case NodeItem::Type::BSDF: { + NodeItem color = get_input_value("Color", NodeItem::Type::Color3); + return create_node("oren_nayar_diffuse_bsdf", NodeItem::Type::BSDF, {{"color", color}}); + } + case NodeItem::Type::SurfaceOpacity: { + NodeItem color = get_input_value("Color", NodeItem::Type::Color3); + return val(1.0f) - color.dotproduct(val(1.0f / 3.0f)); + } + default: + break; + } + return empty(); +} +#endif +NODE_SHADER_MATERIALX_END + } // namespace blender::nodes::node_shader_bsdf_transparent_cc /* node type definition */ @@ -40,6 +60,7 @@ void register_node_type_sh_bsdf_transparent() ntype.add_ui_poll = object_shader_nodes_poll; ntype.declare = file_ns::node_declare; ntype.gpu_fn = file_ns::node_shader_gpu_bsdf_transparent; + ntype.materialx_fn = file_ns::node_shader_materialx; nodeRegisterType(&ntype); } diff --git a/source/blender/nodes/shader/nodes/node_shader_light_falloff.cc b/source/blender/nodes/shader/nodes/node_shader_light_falloff.cc index bd5db49387f0..ba77c71673b4 100644 --- a/source/blender/nodes/shader/nodes/node_shader_light_falloff.cc +++ b/source/blender/nodes/shader/nodes/node_shader_light_falloff.cc @@ -32,7 +32,7 @@ NODE_SHADER_MATERIALX_BEGIN /* This node isn't supported by MaterialX. This formula was given from OSL shader code in Cycles * node_light_falloff.osl. Considered ray_length=1.0f. */ - return strength * val(1.0f) / (smooth + val(1.0f)); + return strength / (smooth + val(1.0f)); } #endif NODE_SHADER_MATERIALX_END 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 249d4f85c7ab..f73efb8394af 100644 --- a/source/blender/nodes/shader/nodes/node_shader_mix_shader.cc +++ b/source/blender/nodes/shader/nodes/node_shader_mix_shader.cc @@ -26,7 +26,7 @@ static int node_shader_gpu_mix_shader(GPUMaterial *mat, NODE_SHADER_MATERIALX_BEGIN #ifdef WITH_MATERIALX { - if (!ELEM(to_type_, NodeItem::Type::BSDF, NodeItem::Type::EDF)) { + if (!ELEM(to_type_, NodeItem::Type::BSDF, NodeItem::Type::EDF, NodeItem::Type::SurfaceOpacity)) { return empty(); } diff --git a/source/blender/nodes/shader/nodes/node_shader_output_material.cc b/source/blender/nodes/shader/nodes/node_shader_output_material.cc index b405c40e8af6..9995292c9a07 100644 --- a/source/blender/nodes/shader/nodes/node_shader_output_material.cc +++ b/source/blender/nodes/shader/nodes/node_shader_output_material.cc @@ -51,13 +51,10 @@ NODE_SHADER_MATERIALX_BEGIN NodeItem bsdf = get_input_link("Surface", NodeItem::Type::BSDF); NodeItem edf = get_input_link("Surface", NodeItem::Type::EDF); if (bsdf || edf) { - surface = create_node("surface", NodeItem::Type::SurfaceShader); - if (bsdf) { - surface.set_input("bsdf", bsdf); - } - if (edf) { - surface.set_input("edf", edf); - } + NodeItem opacity = get_input_link("Surface", NodeItem::Type::SurfaceOpacity); + surface = create_node("surface", + NodeItem::Type::SurfaceShader, + {{"bsdf", bsdf}, {"edf", edf}, {"opacity", opacity}}); } } return create_node("surfacematerial", NodeItem::Type::Material, {{"surfaceshader", surface}}); -- 2.30.2 From 9ee454d4910f87f44abb2ddbbc3b342c94970073 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 26 Sep 2023 12:19:41 +0300 Subject: [PATCH 2/4] Fixed name for opacity node --- source/blender/nodes/shader/materialx/node_item.cc | 2 ++ source/blender/nodes/shader/materialx/node_parser.cc | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/source/blender/nodes/shader/materialx/node_item.cc b/source/blender/nodes/shader/materialx/node_item.cc index 453aa6ed5b3e..5246caca4dd1 100644 --- a/source/blender/nodes/shader/materialx/node_item.cc +++ b/source/blender/nodes/shader/materialx/node_item.cc @@ -103,6 +103,8 @@ std::string NodeItem::type(Type type) return "surfaceshader"; case Type::Material: return "material"; + case Type::SurfaceOpacity: + return "opacity"; default: BLI_assert_unreachable(); } diff --git a/source/blender/nodes/shader/materialx/node_parser.cc b/source/blender/nodes/shader/materialx/node_parser.cc index beedcab34710..eeb7ae88320b 100644 --- a/source/blender/nodes/shader/materialx/node_parser.cc +++ b/source/blender/nodes/shader/materialx/node_parser.cc @@ -64,7 +64,7 @@ std::string NodeParser::node_name() const if (node_->output_sockets().size() > 1) { name += std::string("_") + socket_out_->name; } - if (ELEM(to_type_, NodeItem::Type::BSDF, NodeItem::Type::EDF)) { + if (ELEM(to_type_, NodeItem::Type::BSDF, NodeItem::Type::EDF, NodeItem::Type::SurfaceOpacity)) { name += "_" + NodeItem::type(to_type_); } #ifdef USE_MATERIALX_NODEGRAPH -- 2.30.2 From b5e4a4bbad7822e543ab17e080ada2a5fee840cb Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 26 Sep 2023 12:27:11 +0300 Subject: [PATCH 3/4] comments --- .../blender/nodes/shader/nodes/node_shader_bsdf_transparent.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/blender/nodes/shader/nodes/node_shader_bsdf_transparent.cc b/source/blender/nodes/shader/nodes/node_shader_bsdf_transparent.cc index 46de861eafd8..bf71cbe232f5 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_transparent.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_transparent.cc @@ -33,10 +33,12 @@ NODE_SHADER_MATERIALX_BEGIN switch (to_type_) { case NodeItem::Type::BSDF: { NodeItem color = get_input_value("Color", NodeItem::Type::Color3); + /* Returning diffuse node as BSDF component */ return create_node("oren_nayar_diffuse_bsdf", NodeItem::Type::BSDF, {{"color", color}}); } case NodeItem::Type::SurfaceOpacity: { NodeItem color = get_input_value("Color", NodeItem::Type::Color3); + /* Returning: 1 - */ return val(1.0f) - color.dotproduct(val(1.0f / 3.0f)); } default: -- 2.30.2 From 4de77963a210c00f7b1c50cde39bbc3f8c9c847c Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 26 Sep 2023 16:50:06 +0300 Subject: [PATCH 4/4] Added comment + removed redundant code --- source/blender/nodes/shader/materialx/node_item.cc | 1 + .../blender/nodes/shader/nodes/node_shader_bsdf_principled.cc | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/source/blender/nodes/shader/materialx/node_item.cc b/source/blender/nodes/shader/materialx/node_item.cc index 5246caca4dd1..5cc1c32db29b 100644 --- a/source/blender/nodes/shader/materialx/node_item.cc +++ b/source/blender/nodes/shader/materialx/node_item.cc @@ -14,6 +14,7 @@ NodeItem::NodeItem(MaterialX::GraphElement *graph) : graph_(graph) {} NodeItem::Type NodeItem::type(const std::string &type_str) { + /* Converting only MaterialX supported types */ if (type_str == "multioutput") { return Type::Multioutput; } 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 99499e222a6a..865df5b1fe60 100644 --- a/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc +++ b/source/blender/nodes/shader/nodes/node_shader_bsdf_principled.cc @@ -509,9 +509,6 @@ NODE_SHADER_MATERIALX_BEGIN 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))}}); - NodeItem n_coat_attenuation = coat.mix(val(MaterialX::Color3(1.0f, 1.0f, 1.0f)), in["coat_tint"]); -- 2.30.2