Create parsing system that converts supported nodes and ignores unsupported #2

Merged
Bogdan Nagirniak merged 14 commits from Vasyl-Pidhirskyi/blender:BLEN-500 into matx-export-material 2023-08-28 12:29:46 +02:00
5 changed files with 50 additions and 169 deletions
Showing only changes of commit 6d44247d7d - Show all commits

View File

@ -6,6 +6,7 @@
#include "nodes/output_material.h"
#include <MaterialXCore/Node.h>
#include <MaterialXFormat/XmlIo.h>
#include "NOD_shader.h"
@ -46,6 +47,8 @@ MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *mater
else {
create_standard_surface(doc.get(), material);
}
std::string str = MaterialX::writeToXmlString(doc);
printf("\nMaterial: %s\n%s\n", material->id.name, str.c_str());
return doc;
}

View File

@ -10,8 +10,7 @@ namespace blender::nodes::materialx {
NodeItem BSDFPrincipledNodeParser::compute()
{
auto enabled = [](NodeItem &val) -> bool
{
auto enabled = [](NodeItem &val) -> bool {
if (val.node) {
return true;
}
@ -21,6 +20,10 @@ NodeItem BSDFPrincipledNodeParser::compute()
if (val.value->isA<float>()) {
return val.value->asA<float>() != 0.0f;
}
if (val.value->isA<MaterialX::Color4>()) {
auto c = val.value->asA<MaterialX::Color4>();
return c[0] != 0.0f || c[1] != 0.0f || c[2] != 0.0f;
}
return true;
};
@ -47,8 +50,8 @@ NodeItem BSDFPrincipledNodeParser::compute()
/* TODO: use Specular Tint input */
anisotropic = get_input_value("Anisotropic");
if (enabled(anisotropic)) {
anisotropic_rotation = get_input_value("Anisotropic Rotation");
// anisotropic_rotation = 0.5 - (anisotropic_rotation % 1.0)
anisotropic_rotation = get_input_value("Anisotropic Rotation");
// anisotropic_rotation = 0.5 - (anisotropic_rotation % 1.0)
}
}
@ -84,7 +87,7 @@ NodeItem BSDFPrincipledNodeParser::compute()
/* Creating standard_surface */
NodeItem res = create_node("standard_surface", "surfaceshader");
res.set_input("base", 1.0, "float");
res.set_input("base_color", base_color);
res.set_input("base_color", base_color.to_color3());
res.set_input("diffuse_roughness", roughness);
res.set_input("normal", normal);
res.set_input("tangent", tangent);
@ -95,7 +98,7 @@ NodeItem BSDFPrincipledNodeParser::compute()
if (enabled(specular)) {
res.set_input("specular", specular);
res.set_input("specular_color", base_color);
res.set_input("specular_color", base_color.to_color3());
res.set_input("specular_roughness", roughness);
res.set_input("specular_IOR", ior);
res.set_input("specular_anisotropy", anisotropic);
@ -104,7 +107,7 @@ NodeItem BSDFPrincipledNodeParser::compute()
if (enabled(transmission)) {
res.set_input("transmission", transmission);
res.set_input("transmission_color", base_color);
res.set_input("transmission_color", base_color.to_color3());
res.set_input("transmission_extra_roughness", transmission_roughness);
}
@ -117,13 +120,13 @@ NodeItem BSDFPrincipledNodeParser::compute()
if (enabled(sheen)) {
res.set_input("sheen", sheen);
res.set_input("sheen_color", base_color);
res.set_input("sheen_color", base_color.to_color3());
res.set_input("sheen_roughness", roughness);
}
if (enabled(clearcoat)) {
res.set_input("coat", clearcoat);
res.set_input("coat_color", base_color);
res.set_input("coat_color", base_color.to_color3());
res.set_input("coat_roughness", clearcoat_roughness);
res.set_input("coat_IOR", ior);
res.set_input("coat_anisotropy", anisotropic);
@ -137,119 +140,6 @@ NodeItem BSDFPrincipledNodeParser::compute()
}
return res;
//
// MaterialX::Color3 default_white_color = MaterialX::Color3(1.0, 1.0, 1.0);
//#pragma region get inputs
// const bNodeSocket base_color_socket = res->input_by_identifier("Base Color");
// const char *matx_input = "base_color";
// if (base_color_socket.link) {
// get_input_link_node(&base_color_socket, matx_node, matx_input);
// }
// else {
// const float *base_color = base_color_socket.default_value_typed<bNodeSocketValueRGBA>()->value;
// matx_node->addInput("base_color", "color3")
// ->setValue(MaterialX::Color3(base_color[0], base_color[1], base_color[2]));
// }
//
// const float *base_color =
// res->input_by_identifier("Base Color").default_value_typed<bNodeSocketValueRGBA>()->value;
// const float subsurface =
// res->input_by_identifier("Subsurface").default_value_typed<bNodeSocketValueFloat>()->value;
//
// const float *subsurface_radius = res->input_by_identifier("Subsurface Radius")
// .default_value_typed<bNodeSocketValueVector>()
// ->value;
// const float *subsurface_color = res->input_by_identifier("Subsurface Color")
// .default_value_typed<bNodeSocketValueRGBA>()
// ->value;
// const float metallic =
// res->input_by_identifier("Metallic").default_value_typed<bNodeSocketValueFloat>()->value;
// const float specular =
// res->input_by_identifier("Specular").default_value_typed<bNodeSocketValueFloat>()->value;
// const float roughness =
// res->input_by_identifier("Roughness").default_value_typed<bNodeSocketValueFloat>()->value;
// const float anisotropic =
// res->input_by_identifier("Anisotropic").default_value_typed<bNodeSocketValueFloat>()->value;
// const float anisotropic_rot = res->input_by_identifier("Anisotropic Rotation")
// .default_value_typed<bNodeSocketValueFloat>()
// ->value;
// const float sheen =
// res->input_by_identifier("Sheen").default_value_typed<bNodeSocketValueFloat>()->value;
// const float clearcoat =
// res->input_by_identifier("Clearcoat").default_value_typed<bNodeSocketValueFloat>()->value;
// const float clearcoat_roughness = res->input_by_identifier("Clearcoat Roughness")
// .default_value_typed<bNodeSocketValueFloat>()
// ->value;
// const float IOR =
// res->input_by_identifier("IOR").default_value_typed<bNodeSocketValueFloat>()->value;
// const float transmission = res->input_by_identifier("Transmission")
// .default_value_typed<bNodeSocketValueFloat>()
// ->value;
// const float *emission =
// res->input_by_identifier("Emission").default_value_typed<bNodeSocketValueRGBA>()->value;
// const float emission_str = res->input_by_identifier("Emission Strength")
// .default_value_typed<bNodeSocketValueFloat>()
// ->value;
// const float *normal =
// res->input_by_identifier("Normal").default_value_typed<bNodeSocketValueVector>()->value;
// const float *clearcoat_normal = res->input_by_identifier("Clearcoat Normal")
// .default_value_typed<bNodeSocketValueVector>()
// ->value;
// const float *tangent =
// res->input_by_identifier("Tangent").default_value_typed<bNodeSocketValueVector>()->value;
//#pragma endregion get inputs
//
//#pragma region set inputs
// matx_node->addInput("base", "float")->setValue(1.0);
// matx_node->addInput("diffuse_roughness", "float")->setValue(roughness);
// matx_node->addInput("normal", "vector3")
// ->setValue(MaterialX::Vector3(normal[0], normal[1], normal[2]));
// matx_node->addInput("tangent", "vector3")
// ->setValue(MaterialX::Vector3(tangent[0], tangent[1], tangent[2]));
//
// matx_node->addInput("metalness", "float")->setValue(metallic);
//
// matx_node->addInput("specular", "float")->setValue(specular);
// matx_node->addInput("specular_color", "color3")->setValue(default_white_color);
// matx_node->addInput("specular_roughness", "float")->setValue(roughness);
// matx_node->addInput("specular_IOR", "float")->setValue(IOR);
// matx_node->addInput("specular_anisotropy", "float")->setValue(anisotropic);
// matx_node->addInput("specular_rotation", "float")->setValue(anisotropic_rot);
//
// matx_node->addInput("transmission", "float")->setValue(transmission);
// matx_node->addInput("transmission_color", "color3")->setValue(default_white_color);
// matx_node->addInput("transmission_extra_roughness", "float")->setValue(roughness);
//
// matx_node->addInput("subsurface", "float")->setValue(subsurface);
// matx_node->addInput("subsurface_color", "color3")
// ->setValue(MaterialX::Color3(subsurface_color[0], subsurface_color[1], subsurface_color[2]));
// matx_node->addInput("subsurface_radius", "color3")
// ->setValue(
// MaterialX::Color3(subsurface_radius[0], subsurface_radius[1], subsurface_radius[2]));
// matx_node->addInput("subsurface_anisotropy", "float")->setValue(anisotropic);
//
// matx_node->addInput("sheen", "float")->setValue(sheen);
// matx_node->addInput("sheen_color", "color3")->setValue(default_white_color);
// matx_node->addInput("sheen_roughness", "float")->setValue(roughness);
//
// matx_node->addInput("coat", "float")->setValue(clearcoat);
// matx_node->addInput("coat_color", "color3")->setValue(default_white_color);
// matx_node->addInput("coat_roughness", "float")->setValue(clearcoat_roughness);
// matx_node->addInput("coat_IOR", "float")->setValue(IOR);
// matx_node->addInput("coat_anisotropy", "float")->setValue(anisotropic);
// matx_node->addInput("coat_rotation", "float")->setValue(anisotropic_rot);
// matx_node->addInput("coat_normal", "vector3")
// ->setValue(
// MaterialX::Vector3(clearcoat_normal[0], clearcoat_normal[1], clearcoat_normal[2]));
//
// matx_node->addInput("emission", "float")->setValue(emission_str);
// matx_node->addInput("emission_color", "color3")
// ->setValue(MaterialX::Color3(emission[0], emission[1], emission[2]));
//#pragma endregion set inputs
// return matx_node;
}
} // namespace blender::nodes::materialx

View File

@ -11,18 +11,15 @@
namespace blender::nodes::materialx {
NodeItem::NodeItem(MaterialX::GraphElement *graph)
: graph_(graph)
{
}
NodeItem::NodeItem(MaterialX::GraphElement *graph) : graph_(graph) {}
void NodeItem::set_input(const std::string &name, const NodeItem &res)
void NodeItem::set_input(const std::string &name, const NodeItem &item)
{
if (res.value) {
set_input(name, res.value);
if (item.value) {
set_input(name, item.value);
}
else if (res.node) {
set_input(name, res.node);
else if (item.node) {
set_input(name, item.node);
}
}
@ -50,7 +47,7 @@ void NodeItem::set_input(const std::string &name, const MaterialX::ValuePtr valu
void NodeItem::set_input(const std::string &name, const MaterialX::NodePtr node)
{
node->setConnectedNode(name, node);
this->node->setConnectedNode(name, node);
}
NodeItem::operator bool() const
@ -58,6 +55,29 @@ NodeItem::operator bool() const
return value || node;
}
NodeItem NodeItem::to_color3()
{
NodeItem res(graph_);
if (value) {
if (value->isA<float>()) {
float v = value->asA<float>();
res.value = MaterialX::Value::createValue<MaterialX::Color3>(MaterialX::Color3(v, v, v));
}
else if (value->isA<MaterialX::Color3>()) {
res.value = value;
}
else if (value->isA<MaterialX::Color4>()) {
auto c = value->asA<MaterialX::Color4>();
res.value = MaterialX::Value::createValue<MaterialX::Color3>(
MaterialX::Color3(c[0], c[1], c[2]));
}
}
else if (node) {
res.node = node;
}
return res;
}
NodeParser::NodeParser(MaterialX::GraphElement *graph,
const Depsgraph *depsgraph,
const Material *material,
@ -66,7 +86,9 @@ NodeParser::NodeParser(MaterialX::GraphElement *graph,
{
}
NodeItem NodeParser::create_node(const std::string &mx_category, const std::string &mx_type, bool accessory)
NodeItem NodeParser::create_node(const std::string &mx_category,
const std::string &mx_type,
bool accessory)
{
NodeItem res = empty_value();
res.node = graph->addNode(mx_category,
@ -103,7 +125,6 @@ NodeItem NodeParser::get_input_default(const std::string &name)
return res;
}
NodeItem NodeParser::get_input_link(const std::string &name)
{
NodeItem res = empty_value();
@ -156,36 +177,4 @@ NodeItem NodeParser::empty_value()
return NodeItem(graph);
}
std::string NodeParser::get_mx_type(const bNodeSocket *sock)
{
std::string mx_sock_type;
switch (sock->type) {
case (SOCK_FLOAT): {
mx_sock_type = "float";
break;
}
case (SOCK_VECTOR): {
mx_sock_type = "vector3";
break;
}
case (SOCK_RGBA): {
mx_sock_type = "color3";
break;
}
case (SOCK_BOOLEAN): {
mx_sock_type = "boolean";
break;
}
case (SOCK_INT): {
mx_sock_type = "integer";
break;
}
case (SOCK_SHADER): {
mx_sock_type = "surfaceshader";
break;
}
}
return mx_sock_type;
}
} // namespace blender::nodes::materialx

View File

@ -31,6 +31,8 @@ class NodeItem {
void set_input(const std::string &name, const MaterialX::NodePtr node);
operator bool() const;
NodeItem to_color3();
};
template<class T>
@ -63,8 +65,6 @@ class NodeParser {
NodeItem get_input_link(const std::string &name);
NodeItem get_input_value(const std::string &name);
NodeItem empty_value();
std::string get_mx_type(const bNodeSocket *sock);
};
} // namespace blender::nodes::materialx

View File

@ -11,7 +11,6 @@
namespace blender::nodes::materialx {
NodeItem TexImageNodeParser::compute()
{
Image *image = (Image *)node->id;