Add basic supported nodes #1

Merged
Bogdan Nagirniak merged 5 commits from add-basic-nodes into matx-export-material 2023-08-24 10:23:00 +02:00
11 changed files with 99 additions and 17 deletions
Showing only changes of commit c12ac57625 - Show all commits

View File

@ -141,11 +141,13 @@ if(WITH_MATERIALX)
materialx/nodes/node.cc
materialx/nodes/material_output.cc
materialx/nodes/principled_bsdf.cc
materialx/nodes/image.cc
materialx/material.h
materialx/nodes/node.h
materialx/nodes/material_output.h
materialx/nodes/principled_bsdf.h
materialx/nodes/image.h
)
endif()

View File

@ -11,12 +11,12 @@
namespace blender::nodes::materialx {
static void export_nodegraph(MaterialX::DocumentPtr doc, Material *material)
static void export_nodegraph(MaterialX::DocumentPtr doc, Depsgraph *depsgraph, Material *material)
{
material->nodetree->ensure_topology_cache();
bNode *output_node = ntreeShaderOutputNode(material->nodetree, SHD_OUTPUT_ALL);
MaterialXMaterialOutputNode material_node(doc, material, output_node);
MaterialXMaterialOutputNode material_node(doc, depsgraph, material, output_node);
material_node.convert();
}
@ -35,11 +35,11 @@ static void create_standard_surface(MaterialX::DocumentPtr doc, Material *materi
->setNodeName(standard_surface->getName());
}
MaterialX::DocumentPtr export_to_materialx(Material *material)
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material)
{
MaterialX::DocumentPtr doc = MaterialX::createDocument();
if (material->use_nodes) {
export_nodegraph(doc, material);
export_nodegraph(doc, depsgraph, material);
}
else {
create_standard_surface(doc, material);

View File

@ -6,10 +6,11 @@
#include <MaterialXCore/Document.h>
#include "DEG_depsgraph.h"
#include "DNA_material_types.h"
namespace blender::nodes::materialx {
MaterialX::DocumentPtr export_to_materialx(Material *material);
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material);
} // namespace blender::nodes::materialx

View File

@ -0,0 +1,41 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "node.h"
#include "image.h"
#include "DEG_depsgraph_query.h"
namespace blender::nodes::materialx {
const MaterialX::Color3 MaterialXTexImageNode::texture_error_color_{1.0, 0.0, 1.0};
MaterialXTexImageNode::MaterialXTexImageNode(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph,
const Material *material,
const bNode *node)
: MaterialXNode(doc, depsgraph, material, node)
{
matx_node = doc->addNode("image", node->name, "color3");
}
MaterialX::NodePtr MaterialXTexImageNode::convert()
{
const Image *image = (Image *)node->id;
const NodeTexImage *tex = static_cast<NodeTexImage *>(node->storage);
Scene *scene = DEG_get_input_scene(depsgraph);
Main *bmain = DEG_get_bmain(depsgraph);
std::string image_path;
#ifdef WITH_HYDRA
image_path = 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;
}
} // namespace blender::nodes::materialx

View File

@ -0,0 +1,24 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
#include "node.h"
namespace blender::nodes::materialx {
class MaterialXTexImageNode : public MaterialXNode {
protected:
/* Following Cycles color for wrong Texture nodes. */
static const MaterialX::Color3 texture_error_color_;
public:
MaterialXTexImageNode(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph,
const Material *material,
const bNode *node);
MaterialX::NodePtr convert() override;
};
} // namespace blender::nodes::materialx

View File

@ -8,9 +8,10 @@
namespace blender::nodes::materialx {
MaterialXMaterialOutputNode::MaterialXMaterialOutputNode(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph,
const Material *material,
const bNode *node)
: MaterialXNode(doc, material, node)
: MaterialXNode(doc, depsgraph, material, node)
{
matx_node = doc->addNode("surfacematerial", node->name, "material");
}
@ -23,7 +24,7 @@ MaterialX::NodePtr MaterialXMaterialOutputNode::convert()
}
if (STREQ(sock->name, "Surface")) {
const bNode *inode = sock->link->fromnode;
MaterialXPrincipledBSDFNode surface_node(doc, material, inode);
MaterialXPrincipledBSDFNode surface_node(doc, depsgraph, material, inode);
surface_node.convert();
matx_node->addInput("surfaceshader", "surfaceshader")->setNodeName(inode->name);
}

View File

@ -11,6 +11,7 @@ namespace blender::nodes::materialx {
class MaterialXMaterialOutputNode : public MaterialXNode {
public:
MaterialXMaterialOutputNode(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph,
const Material *material,
const bNode *node);
MaterialX::NodePtr convert() override;

View File

@ -7,9 +7,10 @@
namespace blender::nodes::materialx {
MaterialXNode::MaterialXNode(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph,
const Material *material,
const bNode *node)
: material(material), node(node), doc(doc)
: depsgraph(depsgraph), material(material), node(node), doc(doc)
{
}

View File

@ -6,6 +6,7 @@
#include <MaterialXCore/Document.h>
#include "DEG_depsgraph.h"
#include "DNA_material_types.h"
#include "DNA_node_types.h"
@ -13,13 +14,17 @@ namespace blender::nodes::materialx {
class MaterialXNode {
public:
const Material *material;
const bNode *node;
const Depsgraph *depsgraph = nullptr;
const Material *material = nullptr;
const bNode *node = nullptr;
MaterialX::NodePtr matx_node;
MaterialX::DocumentPtr doc;
public:
MaterialXNode(MaterialX::DocumentPtr doc, const Material *material, const bNode *node);
MaterialXNode(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph,
const Material *material,
const bNode *node);
virtual ~MaterialXNode() = default;
virtual MaterialX::NodePtr convert() = 0;

View File

@ -8,17 +8,19 @@
namespace blender::nodes::materialx {
const MaterialX::Color3 MaterialXPrincipledBSDFNode::default_white_color_{1.0, 1.0, 1.0};
MaterialXPrincipledBSDFNode::MaterialXPrincipledBSDFNode(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph,
const Material *material,
const bNode *node)
: MaterialXNode(doc, material, node)
: MaterialXNode(doc, depsgraph, material, node)
{
matx_node = doc->addNode("standard_surface", node->name, "surfaceshader");
}
MaterialX::NodePtr MaterialXPrincipledBSDFNode::convert()
{
MaterialX::Color3 default_white_color = MaterialX::Color3(1.0, 1.0, 1.0);
#pragma region get inputs
const float *base_color =
node->input_by_identifier("Base Color").default_value_typed<bNodeSocketValueRGBA>()->value;
@ -81,14 +83,14 @@ MaterialX::NodePtr MaterialXPrincipledBSDFNode::convert()
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_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_color", "color3")->setValue(default_white_color_);
matx_node->addInput("transmission_extra_roughness", "float")->setValue(roughness);
matx_node->addInput("subsurface", "float")->setValue(subsurface);
@ -100,11 +102,11 @@ MaterialX::NodePtr MaterialXPrincipledBSDFNode::convert()
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_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_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);

View File

@ -9,8 +9,12 @@
namespace blender::nodes::materialx {
class MaterialXPrincipledBSDFNode : public MaterialXNode {
protected:
static const MaterialX::Color3 default_white_color_;
public:
MaterialXPrincipledBSDFNode(MaterialX::DocumentPtr doc,
const Depsgraph *depsgraph,
const Material *material,
const bNode *node);
MaterialX::NodePtr convert() override;