MaterialX: add support for nodes #11

Merged
Bogdan Nagirniak merged 17 commits from matx-add-other-nodes into matx-export-material 2023-09-06 10:37:26 +02:00
6 changed files with 91 additions and 6 deletions
Showing only changes of commit a8505529be - Show all commits

View File

@ -149,16 +149,17 @@ if(WITH_MATERIALX)
materialx/material.cc
materialx/nodes/bsdf_principled.cc
materialx/nodes/invert.cc
materialx/nodes/hueSatVal.cc
materialx/nodes/math.cc
materialx/nodes/mix_rgb.cc
materialx/nodes/node_item.cc
materialx/nodes/node_parser.cc
materialx/nodes/normal_map.cc
materialx/nodes/output_material.cc
materialx/nodes/tex_image.cc
materialx/nodes/tex_environment.cc
materialx/nodes/tex_noise.cc
materialx/nodes/tex_checker.cc
materialx/nodes/hueSatVal.cc
materialx/material.h
materialx/nodes/node_item.h

View File

@ -414,6 +414,46 @@ NodeItem NodeItem::to_color3() const
return res;
}
NodeItem NodeItem::to_vector3() const
{
std::string mx_type = type();
NodeItem res = empty();
if (value) {
MaterialX::Vector3 c;
if (mx_type == "float") {
float v = value->asA<float>();
c = {v, v, v};
}
else if (mx_type == "color3") {
auto v = value->asA<MaterialX::Color3>();
c = {v[0], v[1], v[2]};
}
else if (mx_type == "color4") {
auto v = value->asA<MaterialX::Color4>();
c = {v[0], v[1], v[2]};
}
else if (mx_type == "vector3") {
auto v = value->asA<MaterialX::Vector3>();
c = {v[0], v[1], v[2]};
}
else if (mx_type == "vector4") {
auto v = value->asA<MaterialX::Vector4>();
c = {v[0], v[1], v[2]};
}
else {
return res;
}
res.value = MaterialX::Value::createValue<MaterialX::Vector3>(c);
}
else if (node) {
if (mx_type != "vector3") {
return res;
}
res.node = node;
}
return res;
}
bool NodeItem::is_numeric() const
{
std::string t = type();

View File

@ -74,6 +74,7 @@ class NodeItem {
NodeItem exp() const;
NodeItem to_color3() const;
NodeItem to_vector3() const;
bool is_numeric() const;
std::string type() const;

View File

@ -118,12 +118,18 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket)
case SH_NODE_INVERT:
parser = std::make_unique<InvertNodeParser>(graph, depsgraph, material, in_node);
break;
case SH_NODE_HUE_SAT:
parser = std::make_unique<HueSatValNodeParser>(graph, depsgraph, material, in_node);
break;
case SH_NODE_MATH:
parser = std::make_unique<MathNodeParser>(graph, depsgraph, material, in_node);
break;
case SH_NODE_MIX_RGB_LEGACY:
parser = std::make_unique<MixRGBNodeParser>(graph, depsgraph, material, in_node);
break;
case SH_NODE_NORMAL_MAP:
parser = std::make_unique<NormalMapNodeParser>(graph, depsgraph, material, in_node);
break;
case SH_NODE_TEX_IMAGE:
parser = std::make_unique<TexImageNodeParser>(graph, depsgraph, material, in_node);
break;
@ -136,9 +142,6 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket)
case SH_NODE_TEX_CHECKER:
parser = std::make_unique<TexCheckerNodeParser>(graph, depsgraph, material, in_node);
break;
case SH_NODE_HUE_SAT:
parser = std::make_unique<HueSatValNodeParser>(graph, depsgraph, material, in_node);
break;
default:
CLOG_WARN(LOG_MATERIALX_SHADER, "Unsupported node: %s (%d)", in_node->name, in_node->type);
return res;

View File

@ -60,14 +60,15 @@ template<class T> NodeItem NodeParser::value(const T &data) const
};
DECLARE_PARSER(BSDFPrincipledNodeParser)
DECLARE_PARSER(HueSatValNodeParser)
DECLARE_PARSER(InvertNodeParser)
DECLARE_PARSER(MathNodeParser)
DECLARE_PARSER(MixRGBNodeParser)
DECLARE_PARSER(NormalMapNodeParser)
DECLARE_PARSER(OutputMaterialNodeParser)
DECLARE_PARSER(TexCheckerNodeParser)
DECLARE_PARSER(TexEnvironmentNodeParser)
DECLARE_PARSER(TexImageNodeParser)
DECLARE_PARSER(TexNoiseNodeParser)
DECLARE_PARSER(HueSatValNodeParser)
} // namespace blender::nodes::materialx

View File

@ -0,0 +1,39 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "node_parser.h"
namespace blender::nodes::materialx {
NodeItem NormalMapNodeParser::compute()
{
std::string default_space = "object";
NodeShaderNormalMap *normal_map_node = static_cast<NodeShaderNormalMap *>(node->storage);
NodeItem color = get_input_value("Color");
NodeItem strength = get_input_value("Strength");
NodeItem res = create_node("normalmap", "vector3", false);
res.set_input("in", color.to_vector3());
res.set_input("scale", strength);
switch (normal_map_node->space) {
case SHD_SPACE_TANGENT:
res.set_input("space", value(std::string("tangent")));
break;
case SHD_SPACE_OBJECT:
res.set_input("space", value(std::string("tangent")));
break;
default:
res.set_input("space", value(default_space));
CLOG_WARN(LOG_MATERIALX_SHADER,
"Ignoring unsupported Space: %d %s (%d), %s will be used",
normal_map_node->space,
node->name,
node->type,
default_space);
}
return res;
}
} // namespace blender::nodes::materialx