forked from blender/blender
Create parsing system that converts supported nodes and ignores unsupported #2
@ -11,26 +11,28 @@
|
||||
|
||||
namespace blender::nodes::materialx {
|
||||
|
||||
static void export_nodegraph(MaterialX::DocumentPtr doc, Depsgraph *depsgraph, Material *material)
|
||||
static void export_nodegraph(MaterialX::GraphElement *graph,
|
||||
Depsgraph *depsgraph,
|
||||
Material *material)
|
||||
{
|
||||
material->nodetree->ensure_topology_cache();
|
||||
|
||||
bNode *output_node = ntreeShaderOutputNode(material->nodetree, SHD_OUTPUT_ALL);
|
||||
OutputMaterialNodeParser material_node(doc, depsgraph, material, output_node);
|
||||
material_node.compute();
|
||||
OutputMaterialNodeParser parser(graph, depsgraph, material, output_node);
|
||||
parser.compute();
|
||||
}
|
||||
|
||||
static void create_standard_surface(MaterialX::DocumentPtr doc, Material *material)
|
||||
static void create_standard_surface(MaterialX::GraphElement *graph, Material *material)
|
||||
{
|
||||
MaterialX::NodePtr surfacematerial = doc->addNode(
|
||||
"surfacematerial", MaterialX::EMPTY_STRING, "material");
|
||||
MaterialX::NodePtr standard_surface = doc->addNode(
|
||||
MaterialX::NodePtr standard_surface = graph->addNode(
|
||||
"standard_surface", MaterialX::EMPTY_STRING, "surfaceshader");
|
||||
|
||||
standard_surface->addInput("base", "float")->setValue(1.0);
|
||||
standard_surface->addInput("base_color", "color3")
|
||||
->setValue(MaterialX::Color3(material->r, material->g, material->b));
|
||||
|
||||
MaterialX::NodePtr surfacematerial = graph->addNode(
|
||||
"surfacematerial", MaterialX::EMPTY_STRING, "material");
|
||||
surfacematerial->addInput(standard_surface->getType(), standard_surface->getType())
|
||||
->setNodeName(standard_surface->getName());
|
||||
}
|
||||
@ -39,10 +41,10 @@ MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *mater
|
||||
{
|
||||
MaterialX::DocumentPtr doc = MaterialX::createDocument();
|
||||
if (material->use_nodes) {
|
||||
export_nodegraph(doc, depsgraph, material);
|
||||
export_nodegraph(doc.get(), depsgraph, material);
|
||||
}
|
||||
else {
|
||||
create_standard_surface(doc, material);
|
||||
create_standard_surface(doc.get(), material);
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
@ -12,7 +12,5 @@
|
||||
namespace blender::nodes::materialx {
|
||||
|
||||
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material);
|
||||
static void create_standard_surface(MaterialX::DocumentPtr doc, Material *material);
|
||||
static void export_nodegraph(MaterialX::DocumentPtr doc, Depsgraph *depsgraph, Material *material);
|
||||
|
||||
} // namespace blender::nodes::materialx
|
||||
|
@ -10,117 +10,246 @@ namespace blender::nodes::materialx {
|
||||
|
||||
NodeItem BSDFPrincipledNodeParser::compute()
|
||||
{
|
||||
NodeItem node = create_node("standard_surface", "surfaceshader");
|
||||
|
||||
MaterialX::Color3 default_white_color = MaterialX::Color3(1.0, 1.0, 1.0);
|
||||
#pragma region get inputs
|
||||
const bNodeSocket base_color_socket = node->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);
|
||||
auto enabled = [](NodeItem &val) -> bool
|
||||
{
|
||||
if (val.node) {
|
||||
return true;
|
||||
}
|
||||
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]));
|
||||
if (!val.value) {
|
||||
return false;
|
||||
}
|
||||
if (val.value->isA<float>()) {
|
||||
return val.value->asA<float>() != 0.0f;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
/* Getting required inputs
|
||||
* Note: if some inputs are not needed they won't be taken */
|
||||
NodeItem base_color = get_input_value("Base Color");
|
||||
|
||||
NodeItem subsurface = get_input_value("Subsurface");
|
||||
NodeItem subsurface_radius = empty_value();
|
||||
NodeItem subsurface_color = empty_value();
|
||||
if (enabled(subsurface)) {
|
||||
subsurface_radius = get_input_value("Subsurface Radius");
|
||||
subsurface_color = get_input_value("Subsurface Color");
|
||||
}
|
||||
|
||||
const float *base_color =
|
||||
node->input_by_identifier("Base Color").default_value_typed<bNodeSocketValueRGBA>()->value;
|
||||
const float subsurface =
|
||||
node->input_by_identifier("Subsurface").default_value_typed<bNodeSocketValueFloat>()->value;
|
||||
NodeItem metallic = get_input_value("Metallic");
|
||||
NodeItem specular = get_input_value("Specular");
|
||||
// NodeItem specular_tint = get_input_value("Specular Tint");
|
||||
NodeItem roughness = get_input_value("Roughness");
|
||||
|
||||
const float *subsurface_radius = node->input_by_identifier("Subsurface Radius")
|
||||
.default_value_typed<bNodeSocketValueVector>()
|
||||
->value;
|
||||
const float *subsurface_color = node->input_by_identifier("Subsurface Color")
|
||||
.default_value_typed<bNodeSocketValueRGBA>()
|
||||
->value;
|
||||
const float metallic =
|
||||
node->input_by_identifier("Metallic").default_value_typed<bNodeSocketValueFloat>()->value;
|
||||
const float specular =
|
||||
node->input_by_identifier("Specular").default_value_typed<bNodeSocketValueFloat>()->value;
|
||||
const float roughness =
|
||||
node->input_by_identifier("Roughness").default_value_typed<bNodeSocketValueFloat>()->value;
|
||||
const float anisotropic =
|
||||
node->input_by_identifier("Anisotropic").default_value_typed<bNodeSocketValueFloat>()->value;
|
||||
const float anisotropic_rot = node->input_by_identifier("Anisotropic Rotation")
|
||||
.default_value_typed<bNodeSocketValueFloat>()
|
||||
->value;
|
||||
const float sheen =
|
||||
node->input_by_identifier("Sheen").default_value_typed<bNodeSocketValueFloat>()->value;
|
||||
const float clearcoat =
|
||||
node->input_by_identifier("Clearcoat").default_value_typed<bNodeSocketValueFloat>()->value;
|
||||
const float clearcoat_roughness = node->input_by_identifier("Clearcoat Roughness")
|
||||
.default_value_typed<bNodeSocketValueFloat>()
|
||||
->value;
|
||||
const float IOR =
|
||||
node->input_by_identifier("IOR").default_value_typed<bNodeSocketValueFloat>()->value;
|
||||
const float transmission = node->input_by_identifier("Transmission")
|
||||
.default_value_typed<bNodeSocketValueFloat>()
|
||||
->value;
|
||||
const float *emission =
|
||||
node->input_by_identifier("Emission").default_value_typed<bNodeSocketValueRGBA>()->value;
|
||||
const float emission_str = node->input_by_identifier("Emission Strength")
|
||||
.default_value_typed<bNodeSocketValueFloat>()
|
||||
->value;
|
||||
const float *normal =
|
||||
node->input_by_identifier("Normal").default_value_typed<bNodeSocketValueVector>()->value;
|
||||
const float *clearcoat_normal = node->input_by_identifier("Clearcoat Normal")
|
||||
.default_value_typed<bNodeSocketValueVector>()
|
||||
->value;
|
||||
const float *tangent =
|
||||
node->input_by_identifier("Tangent").default_value_typed<bNodeSocketValueVector>()->value;
|
||||
#pragma endregion get inputs
|
||||
NodeItem anisotropic = empty_value();
|
||||
NodeItem anisotropic_rotation = empty_value();
|
||||
if (enabled(metallic)) {
|
||||
/* 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)
|
||||
}
|
||||
}
|
||||
|
||||
#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]));
|
||||
NodeItem sheen = get_input_value("Sheen");
|
||||
// sheen_tint = empty_value();
|
||||
// if enabled(sheen):
|
||||
// sheen_tint = get_input_value("Sheen Tint");
|
||||
|
||||
matx_node->addInput("metalness", "float")->setValue(metallic);
|
||||
NodeItem clearcoat = get_input_value("Clearcoat");
|
||||
NodeItem clearcoat_roughness = empty_value();
|
||||
if (enabled(clearcoat)) {
|
||||
clearcoat_roughness = get_input_value("Clearcoat Roughness");
|
||||
}
|
||||
|
||||
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);
|
||||
NodeItem ior = get_input_value("IOR");
|
||||
|
||||
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);
|
||||
NodeItem transmission = get_input_value("Transmission");
|
||||
NodeItem transmission_roughness = empty_value();
|
||||
if (enabled(transmission)) {
|
||||
transmission_roughness = get_input_value("Transmission 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);
|
||||
NodeItem emission = get_input_value("Emission");
|
||||
NodeItem emission_strength = get_input_value("Emission Strength");
|
||||
|
||||
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);
|
||||
NodeItem alpha = get_input_value("Alpha");
|
||||
// transparency = 1.0 - alpha
|
||||
|
||||
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]));
|
||||
NodeItem normal = get_input_link("Normal");
|
||||
NodeItem clearcoat_normal = get_input_link("Clearcoat Normal");
|
||||
NodeItem tangent = get_input_link("Tangent");
|
||||
|
||||
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;
|
||||
/* 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("diffuse_roughness", roughness);
|
||||
res.set_input("normal", normal);
|
||||
res.set_input("tangent", tangent);
|
||||
|
||||
if (enabled(metallic)) {
|
||||
res.set_input("metalness", metallic);
|
||||
}
|
||||
|
||||
if (enabled(specular)) {
|
||||
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);
|
||||
}
|
||||
|
||||
if (enabled(transmission)) {
|
||||
res.set_input("transmission", transmission);
|
||||
res.set_input("transmission_color", base_color);
|
||||
res.set_input("transmission_extra_roughness", transmission_roughness);
|
||||
}
|
||||
|
||||
if (enabled(subsurface)) {
|
||||
res.set_input("subsurface", subsurface);
|
||||
res.set_input("subsurface_color", subsurface_color);
|
||||
res.set_input("subsurface_radius", subsurface_radius);
|
||||
res.set_input("subsurface_anisotropy", anisotropic);
|
||||
}
|
||||
|
||||
if (enabled(sheen)) {
|
||||
res.set_input("sheen", sheen);
|
||||
res.set_input("sheen_color", base_color);
|
||||
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_roughness", clearcoat_roughness);
|
||||
res.set_input("coat_IOR", ior);
|
||||
res.set_input("coat_anisotropy", anisotropic);
|
||||
res.set_input("coat_rotation", anisotropic_rotation);
|
||||
res.set_input("coat_normal", clearcoat_normal);
|
||||
}
|
||||
|
||||
if (enabled(emission)) {
|
||||
res.set_input("emission", emission_strength);
|
||||
res.set_input("emission_color", emission);
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -16,13 +16,13 @@ NodeItem::NodeItem(MaterialX::GraphElement *graph)
|
||||
{
|
||||
}
|
||||
|
||||
void NodeItem::set_input(const std::string &name, const NodeItem &item)
|
||||
void NodeItem::set_input(const std::string &name, const NodeItem &res)
|
||||
{
|
||||
if (item.value) {
|
||||
set_input(name, item.value);
|
||||
if (res.value) {
|
||||
set_input(name, res.value);
|
||||
}
|
||||
else {
|
||||
set_input(name, item.node);
|
||||
set_input(name, res.node);
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,47 +66,51 @@ NodeParser::NodeParser(MaterialX::GraphElement *graph,
|
||||
{
|
||||
}
|
||||
|
||||
NodeItem NodeParser::create_node(const std::string &mx_category, const std::string &mx_type)
|
||||
NodeItem NodeParser::create_node(const std::string &mx_category, const std::string &mx_type, bool accessory)
|
||||
{
|
||||
NodeItem item(graph);
|
||||
item.node = graph->addNode(mx_category, MaterialX::createValidName(node->name), mx_type);
|
||||
return item;
|
||||
NodeItem res = empty_value();
|
||||
res.node = graph->addNode(mx_category,
|
||||
accessory ? MaterialX::EMPTY_STRING :
|
||||
MaterialX::createValidName(node->name),
|
||||
mx_type);
|
||||
return res;
|
||||
}
|
||||
|
||||
NodeItem NodeParser::get_input_default(const std::string &name)
|
||||
{
|
||||
NodeItem res = empty_value();
|
||||
|
||||
const bNodeSocket &socket = node->input_by_identifier(name);
|
||||
NodeItem item(graph);
|
||||
switch (socket.type) {
|
||||
case SOCK_FLOAT: {
|
||||
float v = socket.default_value_typed<bNodeSocketValueFloat>()->value;
|
||||
item.value = MaterialX::Value::createValue<float>(v);
|
||||
res.value = MaterialX::Value::createValue<float>(v);
|
||||
} break;
|
||||
case SOCK_VECTOR: {
|
||||
const float *v = socket.default_value_typed<bNodeSocketValueVector>()->value;
|
||||
item.value = MaterialX::Value::createValue<MaterialX::Vector3>(
|
||||
res.value = MaterialX::Value::createValue<MaterialX::Vector3>(
|
||||
MaterialX::Vector3(v[0], v[1], v[2]));
|
||||
} break;
|
||||
case SOCK_RGBA: {
|
||||
const float *v = socket.default_value_typed<bNodeSocketValueRGBA>()->value;
|
||||
item.value = MaterialX::Value::createValue<MaterialX::Color4>(
|
||||
res.value = MaterialX::Value::createValue<MaterialX::Color4>(
|
||||
MaterialX::Color4(v[0], v[1], v[2], v[3]));
|
||||
} break;
|
||||
default: {
|
||||
// TODO log warn
|
||||
}
|
||||
}
|
||||
return item;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
NodeItem NodeParser::get_input_link(const std::string &name)
|
||||
{
|
||||
NodeItem item(graph);
|
||||
NodeItem res = empty_value();
|
||||
|
||||
const bNodeLink *link = node->input_by_identifier(name).link;
|
||||
if (!link->is_used()) {
|
||||
return item;
|
||||
return res;
|
||||
}
|
||||
|
||||
const bNode *in_node = link->fromnode;
|
||||
@ -115,7 +119,7 @@ NodeItem NodeParser::get_input_link(const std::string &name)
|
||||
while (in_node->type == NODE_REROUTE) {
|
||||
link = in_node->input_socket(0).link;
|
||||
if (!link->is_used()) {
|
||||
return item;
|
||||
return res;
|
||||
}
|
||||
in_node = link->fromnode;
|
||||
}
|
||||
@ -131,20 +135,25 @@ NodeItem NodeParser::get_input_link(const std::string &name)
|
||||
break;
|
||||
default:
|
||||
// TODO: warning log
|
||||
return item;
|
||||
return res;
|
||||
}
|
||||
|
||||
item = parser->compute();
|
||||
return item;
|
||||
res = parser->compute();
|
||||
return res;
|
||||
}
|
||||
|
||||
NodeItem NodeParser::get_input_value(const std::string &name)
|
||||
{
|
||||
NodeItem item = get_input_link(name);
|
||||
if (!item) {
|
||||
item = get_input_value(name);
|
||||
NodeItem res = get_input_link(name);
|
||||
if (!res) {
|
||||
res = get_input_value(name);
|
||||
}
|
||||
return item;
|
||||
return res;
|
||||
}
|
||||
|
||||
NodeItem NodeParser::empty_value()
|
||||
{
|
||||
return NodeItem(graph);
|
||||
}
|
||||
|
||||
std::string NodeParser::get_mx_type(const bNodeSocket *sock)
|
||||
|
@ -56,10 +56,13 @@ class NodeParser {
|
||||
virtual NodeItem compute() = 0;
|
||||
|
||||
protected:
|
||||
NodeItem create_node(const std::string &mx_category, const std::string &mx_type);
|
||||
NodeItem create_node(const std::string &mx_category,
|
||||
const std::string &mx_type,
|
||||
bool accessory = false);
|
||||
NodeItem get_input_default(const std::string &name);
|
||||
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);
|
||||
};
|
||||
|
@ -6,21 +6,15 @@
|
||||
|
||||
namespace blender::nodes::materialx {
|
||||
|
||||
OutputMaterialNodeParser::OutputMaterialNodeParser(MaterialX::DocumentPtr doc,
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node)
|
||||
: NodeParser(doc, depsgraph, material, node)
|
||||
NodeItem OutputMaterialNodeParser::compute()
|
||||
{
|
||||
matx_node = doc->addNode("surfacematerial", MaterialX::createValidName(node->name), "material");
|
||||
}
|
||||
|
||||
MaterialX::NodePtr OutputMaterialNodeParser::compute()
|
||||
{
|
||||
const char *matx_socket = "surfaceshader";
|
||||
const bNodeSocket sock = node->input_by_identifier("Surface");
|
||||
get_input_link_node(&sock, matx_node, matx_socket);
|
||||
return matx_node;
|
||||
NodeItem node = empty_value();
|
||||
NodeItem surface = get_input_link("Surface");
|
||||
if (surface) {
|
||||
node = create_node("surfacematerial", "material");
|
||||
node.set_input("surfaceshader", surface);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
} // namespace blender::nodes::materialx
|
||||
|
@ -10,11 +10,8 @@ namespace blender::nodes::materialx {
|
||||
|
||||
class OutputMaterialNodeParser : public NodeParser {
|
||||
public:
|
||||
OutputMaterialNodeParser(MaterialX::DocumentPtr doc,
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node);
|
||||
MaterialX::NodePtr compute() override;
|
||||
using NodeParser::NodeParser;
|
||||
NodeItem compute() override;
|
||||
};
|
||||
|
||||
} // namespace blender::nodes::materialx
|
||||
|
@ -11,18 +11,8 @@
|
||||
|
||||
namespace blender::nodes::materialx {
|
||||
|
||||
const MaterialX::Color3 TexImageNodeParser::texture_error_color_{1.0, 0.0, 1.0};
|
||||
|
||||
TexImageNodeParser::TexImageNodeParser(MaterialX::DocumentPtr doc,
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node)
|
||||
: NodeParser(doc, depsgraph, material, node)
|
||||
{
|
||||
matx_node = doc->addNode("image", MaterialX::createValidName(node->name), "color3");
|
||||
}
|
||||
|
||||
MaterialX::NodePtr TexImageNodeParser::compute()
|
||||
NodeItem TexImageNodeParser::compute()
|
||||
{
|
||||
Image *image = (Image *)node->id;
|
||||
NodeTexImage *tex = static_cast<NodeTexImage *>(node->storage);
|
||||
@ -34,12 +24,12 @@ MaterialX::NodePtr TexImageNodeParser::compute()
|
||||
#ifdef WITH_HYDRA
|
||||
image_path = io::hydra::cache_or_get_image_file(bmain, scene, image, &tex->iuser);
|
||||
#endif
|
||||
MaterialX::NodePtr uv_node = doc->addNode("texcoord", MaterialX::EMPTY_STRING, "vector2");
|
||||
|
||||
matx_node->addInput("file", "filename")->setValue(image_path);
|
||||
matx_node->addInput("texcoord", "vector2")->setNodeName(uv_node->getName());
|
||||
|
||||
return matx_node;
|
||||
NodeItem texcoord = create_node("texcoord", "vector2", true);
|
||||
NodeItem res = create_node("image", "color3");
|
||||
res.set_input("file", image_path, "filename");
|
||||
res.set_input("texcoord", texcoord);
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace blender::nodes::materialx
|
||||
|
@ -9,16 +9,9 @@
|
||||
namespace blender::nodes::materialx {
|
||||
|
||||
class TexImageNodeParser : public NodeParser {
|
||||
protected:
|
||||
/* Following Cycles color for wrong Texture nodes. */
|
||||
static const MaterialX::Color3 texture_error_color_;
|
||||
|
||||
public:
|
||||
TexImageNodeParser(MaterialX::DocumentPtr doc,
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node);
|
||||
MaterialX::NodePtr compute() override;
|
||||
using NodeParser::NodeParser;
|
||||
NodeItem compute() override;
|
||||
};
|
||||
|
||||
} // namespace blender::nodes::materialx
|
||||
|
Loading…
Reference in New Issue
Block a user