forked from blender/blender
Implement export of Shader BSDF nodes #13
@ -17,9 +17,7 @@ namespace blender::nodes::materialx {
|
||||
|
||||
CLG_LOGREF_DECLARE_GLOBAL(LOG_MATERIALX_SHADER, "materialx.shader");
|
||||
|
||||
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph,
|
||||
Material *material,
|
||||
const std::string &socket_name)
|
||||
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material)
|
||||
{
|
||||
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) {
|
||||
material->nodetree->ensure_topology_cache();
|
||||
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 {
|
||||
OutputMaterialNodeParser(doc.get(), depsgraph, material, nullptr).compute_default();
|
||||
OutputMaterialNodeParser(
|
||||
doc.get(), depsgraph, material, nullptr, NodeParser::ShaderType::SurfaceMaterial)
|
||||
.compute_default();
|
||||
}
|
||||
|
||||
CLOG_INFO(LOG_MATERIALX_SHADER,
|
||||
|
@ -15,8 +15,6 @@ namespace blender::nodes::materialx {
|
||||
|
||||
extern struct CLG_LogRef *LOG_MATERIALX_SHADER;
|
||||
|
||||
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph,
|
||||
Material *material,
|
||||
const std::string &socket_name = "Surface");
|
||||
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph, Material *material);
|
||||
|
||||
} // namespace blender::nodes::materialx
|
||||
|
@ -13,12 +13,14 @@ NodeParser::NodeParser(MaterialX::GraphElement *graph,
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node,
|
||||
const bNodeSocket *socket_out)
|
||||
const bNodeSocket *socket_out,
|
||||
ShaderType shader)
|
||||
: graph_(graph),
|
||||
depsgraph_(depsgraph),
|
||||
material_(material),
|
||||
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));
|
||||
}
|
||||
|
||||
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)
|
||||
@ -99,7 +101,7 @@ NodeItem NodeParser::get_input_default(const bNodeSocket &socket)
|
||||
return res;
|
||||
}
|
||||
|
||||
NodeItem NodeParser::get_input_link(const bNodeSocket &socket)
|
||||
NodeItem NodeParser::get_input_link(const bNodeSocket &socket, ShaderType channel)
|
||||
{
|
||||
NodeItem res = empty();
|
||||
|
||||
@ -128,7 +130,7 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket)
|
||||
/* Computing from_node with required NodeParser object */
|
||||
#define CASE_NODE_TYPE(type, T) \
|
||||
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;
|
||||
|
||||
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 res = get_input_link(socket);
|
||||
NodeItem res = get_input_link(socket, ShaderType::Value);
|
||||
if (!res) {
|
||||
res = get_input_default(socket);
|
||||
}
|
||||
|
@ -13,19 +13,24 @@
|
||||
namespace blender::nodes::materialx {
|
||||
|
||||
class NodeParser {
|
||||
public:
|
||||
enum class ShaderType { Value = 0, BSDF, EDF, VDF, SurfaceShader, SurfaceMaterial };
|
||||
|
||||
protected:
|
||||
MaterialX::GraphElement *graph_;
|
||||
const Depsgraph *depsgraph_;
|
||||
const Material *material_;
|
||||
const bNode *node_;
|
||||
const bNodeSocket *socket_out_;
|
||||
ShaderType shader_;
|
||||
|
||||
public:
|
||||
NodeParser(MaterialX::GraphElement *graph,
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node,
|
||||
const bNodeSocket *socket_out);
|
||||
const bNodeSocket *socket_out,
|
||||
ShaderType shader);
|
||||
virtual ~NodeParser() = default;
|
||||
virtual NodeItem compute() = 0;
|
||||
|
||||
@ -34,8 +39,8 @@ class NodeParser {
|
||||
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(int index);
|
||||
NodeItem get_input_link(const std::string &name);
|
||||
NodeItem get_input_link(int index);
|
||||
NodeItem get_input_link(const std::string &name, ShaderType shader = ShaderType::Value);
|
||||
NodeItem get_input_link(int index, ShaderType shader = ShaderType::Value);
|
||||
NodeItem get_input_value(const std::string &name);
|
||||
NodeItem get_input_value(int index);
|
||||
NodeItem empty() const;
|
||||
@ -43,7 +48,7 @@ class NodeParser {
|
||||
|
||||
private:
|
||||
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 compute_full();
|
||||
};
|
||||
|
@ -8,21 +8,17 @@ namespace blender::nodes::materialx {
|
||||
OutputMaterialNodeParser::OutputMaterialNodeParser(MaterialX::GraphElement *graph,
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node)
|
||||
: NodeParser(graph, depsgraph, material, node, nullptr)
|
||||
const bNode *node,
|
||||
ShaderType shader)
|
||||
: NodeParser(graph, depsgraph, material, node, nullptr, shader)
|
||||
{
|
||||
}
|
||||
|
||||
NodeItem OutputMaterialNodeParser::compute()
|
||||
{
|
||||
return empty();
|
||||
}
|
||||
|
||||
NodeItem OutputMaterialNodeParser::compute(const std::string &socket_name)
|
||||
{
|
||||
NodeItem surface = empty();
|
||||
if (node_) {
|
||||
surface = get_input_link(socket_name);
|
||||
surface = get_input_link("Surface", ShaderType::SurfaceShader);
|
||||
}
|
||||
else {
|
||||
surface = create_node("standard_surface", "surfaceshader");
|
||||
|
@ -13,9 +13,9 @@ class OutputMaterialNodeParser : public NodeParser {
|
||||
OutputMaterialNodeParser(MaterialX::GraphElement *graph,
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node);
|
||||
const bNode *node,
|
||||
ShaderType shader);
|
||||
NodeItem compute() override;
|
||||
NodeItem compute(const std::string &socket_name);
|
||||
NodeItem compute_default();
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user