Implement export of Shader BSDF nodes #13

Merged
Bogdan Nagirniak merged 13 commits from BogdanNagirniak/blender:matx-shader-bsdf-nodes into matx-export-material 2023-09-07 11:22:44 +02:00
6 changed files with 34 additions and 31 deletions
Showing only changes of commit e26075c9a2 - Show all commits

View File

@ -17,9 +17,7 @@ namespace blender::nodes::materialx {
CLG_LOGREF_DECLARE_GLOBAL(LOG_MATERIALX_SHADER, "materialx.shader"); CLG_LOGREF_DECLARE_GLOBAL(LOG_MATERIALX_SHADER, "materialx.shader");
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material)
Material *material,
const std::string &socket_name)
{ {
CLOG_INFO(LOG_MATERIALX_SHADER, 0, "Material: %s", material->id.name); CLOG_INFO(LOG_MATERIALX_SHADER, 0, "Material: %s", material->id.name);
@ -27,10 +25,14 @@ MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph,
if (material->use_nodes) { if (material->use_nodes) {
material->nodetree->ensure_topology_cache(); material->nodetree->ensure_topology_cache();
bNode *output_node = ntreeShaderOutputNode(material->nodetree, SHD_OUTPUT_ALL); bNode *output_node = ntreeShaderOutputNode(material->nodetree, SHD_OUTPUT_ALL);
OutputMaterialNodeParser(doc.get(), depsgraph, material, output_node).compute(socket_name); OutputMaterialNodeParser(
doc.get(), depsgraph, material, output_node, NodeParser::ShaderType::SurfaceMaterial)
.compute();
} }
else { else {
OutputMaterialNodeParser(doc.get(), depsgraph, material, nullptr).compute_default(); OutputMaterialNodeParser(
doc.get(), depsgraph, material, nullptr, NodeParser::ShaderType::SurfaceMaterial)
.compute_default();
} }
CLOG_INFO(LOG_MATERIALX_SHADER, CLOG_INFO(LOG_MATERIALX_SHADER,

View File

@ -15,8 +15,6 @@ namespace blender::nodes::materialx {
extern struct CLG_LogRef *LOG_MATERIALX_SHADER; extern struct CLG_LogRef *LOG_MATERIALX_SHADER;
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material);
Material *material,
const std::string &socket_name = "Surface");
} // namespace blender::nodes::materialx } // namespace blender::nodes::materialx

View File

@ -13,12 +13,14 @@ NodeParser::NodeParser(MaterialX::GraphElement *graph,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
const bNode *node, const bNode *node,
const bNodeSocket *socket_out) const bNodeSocket *socket_out,
ShaderType shader)
: graph_(graph), : graph_(graph),
depsgraph_(depsgraph), depsgraph_(depsgraph),
material_(material), material_(material),
node_(node), node_(node),
socket_out_(socket_out) socket_out_(socket_out),
shader_(shader)
{ {
} }
@ -46,14 +48,14 @@ NodeItem NodeParser::get_input_default(int index)
return get_input_default(node_->input_socket(index)); return get_input_default(node_->input_socket(index));
} }
NodeItem NodeParser::get_input_link(const std::string &name) NodeItem NodeParser::get_input_link(const std::string &name, ShaderType shader)
{ {
return get_input_link(node_->input_by_identifier(name)); return get_input_link(node_->input_by_identifier(name), shader);
} }
NodeItem NodeParser::get_input_link(int index) NodeItem NodeParser::get_input_link(int index, ShaderType shader)
{ {
return get_input_link(node_->input_socket(index)); return get_input_link(node_->input_socket(index), shader);
} }
NodeItem NodeParser::get_input_value(const std::string &name) NodeItem NodeParser::get_input_value(const std::string &name)
@ -99,7 +101,7 @@ NodeItem NodeParser::get_input_default(const bNodeSocket &socket)
return res; return res;
} }
NodeItem NodeParser::get_input_link(const bNodeSocket &socket) NodeItem NodeParser::get_input_link(const bNodeSocket &socket, ShaderType channel)
{ {
NodeItem res = empty(); NodeItem res = empty();
@ -128,7 +130,7 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket)
/* Computing from_node with required NodeParser object */ /* Computing from_node with required NodeParser object */
#define CASE_NODE_TYPE(type, T) \ #define CASE_NODE_TYPE(type, T) \
case type: \ case type: \
res = T(graph_, depsgraph_, material_, from_node, link->fromsock).compute_full(); \ res = T(graph_, depsgraph_, material_, from_node, link->fromsock, channel).compute_full(); \
break; break;
switch (from_node->typeinfo->type) { switch (from_node->typeinfo->type) {
@ -153,7 +155,7 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket)
NodeItem NodeParser::get_input_value(const bNodeSocket &socket) NodeItem NodeParser::get_input_value(const bNodeSocket &socket)
{ {
NodeItem res = get_input_link(socket); NodeItem res = get_input_link(socket, ShaderType::Value);
if (!res) { if (!res) {
res = get_input_default(socket); res = get_input_default(socket);
} }

View File

@ -13,19 +13,24 @@
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
class NodeParser { class NodeParser {
public:
enum class ShaderType { Value = 0, BSDF, EDF, VDF, SurfaceShader, SurfaceMaterial };
protected: protected:
MaterialX::GraphElement *graph_; MaterialX::GraphElement *graph_;
const Depsgraph *depsgraph_; const Depsgraph *depsgraph_;
const Material *material_; const Material *material_;
const bNode *node_; const bNode *node_;
const bNodeSocket *socket_out_; const bNodeSocket *socket_out_;
ShaderType shader_;
public: public:
NodeParser(MaterialX::GraphElement *graph, NodeParser(MaterialX::GraphElement *graph,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
const bNode *node, const bNode *node,
const bNodeSocket *socket_out); const bNodeSocket *socket_out,
ShaderType shader);
virtual ~NodeParser() = default; virtual ~NodeParser() = default;
virtual NodeItem compute() = 0; virtual NodeItem compute() = 0;
@ -34,8 +39,8 @@ class NodeParser {
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);
NodeItem get_input_default(const std::string &name); NodeItem get_input_default(const std::string &name);
NodeItem get_input_default(int index); NodeItem get_input_default(int index);
NodeItem get_input_link(const std::string &name); NodeItem get_input_link(const std::string &name, ShaderType shader = ShaderType::Value);
NodeItem get_input_link(int index); NodeItem get_input_link(int index, ShaderType shader = ShaderType::Value);
NodeItem get_input_value(const std::string &name); NodeItem get_input_value(const std::string &name);
NodeItem get_input_value(int index); NodeItem get_input_value(int index);
NodeItem empty() const; NodeItem empty() const;
@ -43,7 +48,7 @@ class NodeParser {
private: private:
NodeItem get_input_default(const bNodeSocket &socket); NodeItem get_input_default(const bNodeSocket &socket);
NodeItem get_input_link(const bNodeSocket &socket); NodeItem get_input_link(const bNodeSocket &socket, ShaderType shader);
NodeItem get_input_value(const bNodeSocket &socket); NodeItem get_input_value(const bNodeSocket &socket);
NodeItem compute_full(); NodeItem compute_full();
}; };

View File

@ -8,21 +8,17 @@ namespace blender::nodes::materialx {
OutputMaterialNodeParser::OutputMaterialNodeParser(MaterialX::GraphElement *graph, OutputMaterialNodeParser::OutputMaterialNodeParser(MaterialX::GraphElement *graph,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
const bNode *node) const bNode *node,
: NodeParser(graph, depsgraph, material, node, nullptr) ShaderType shader)
: NodeParser(graph, depsgraph, material, node, nullptr, shader)
{ {
} }
NodeItem OutputMaterialNodeParser::compute() NodeItem OutputMaterialNodeParser::compute()
{
return empty();
}
NodeItem OutputMaterialNodeParser::compute(const std::string &socket_name)
{ {
NodeItem surface = empty(); NodeItem surface = empty();
if (node_) { if (node_) {
surface = get_input_link(socket_name); surface = get_input_link("Surface", ShaderType::SurfaceShader);
} }
else { else {
surface = create_node("standard_surface", "surfaceshader"); surface = create_node("standard_surface", "surfaceshader");

View File

@ -13,9 +13,9 @@ class OutputMaterialNodeParser : public NodeParser {
OutputMaterialNodeParser(MaterialX::GraphElement *graph, OutputMaterialNodeParser(MaterialX::GraphElement *graph,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
const bNode *node); const bNode *node,
ShaderType shader);
NodeItem compute() override; NodeItem compute() override;
NodeItem compute(const std::string &socket_name);
NodeItem compute_default(); NodeItem compute_default();
}; };