forked from blender/blender
Move the MaterialX export code into the existing shader node files #18
@ -63,6 +63,8 @@ NodeItem::Type NodeItem::type(const std::string &type_str)
|
||||
std::string NodeItem::type(Type type)
|
||||
{
|
||||
switch (type) {
|
||||
case Type::Any:
|
||||
return "";
|
||||
case Type::String:
|
||||
return "string";
|
||||
case Type::Filename:
|
||||
|
@ -15,18 +15,25 @@ NodeParser::NodeParser(MaterialX::GraphElement *graph,
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node,
|
||||
const bNodeSocket *socket_out)
|
||||
const bNodeSocket *socket_out,
|
||||
NodeItem::Type shader_type)
|
||||
: graph_(graph),
|
||||
depsgraph_(depsgraph),
|
||||
material_(material),
|
||||
node_(node),
|
||||
socket_out_(socket_out)
|
||||
socket_out_(socket_out),
|
||||
shader_type_(shader_type)
|
||||
{
|
||||
}
|
||||
|
||||
NodeItem NodeParser::compute_full()
|
||||
{
|
||||
CLOG_INFO(LOG_MATERIALX_SHADER, 1, "%s [%d]", node_->name, node_->typeinfo->type);
|
||||
CLOG_INFO(LOG_MATERIALX_SHADER,
|
||||
1,
|
||||
"%s [%d] %s",
|
||||
node_->name,
|
||||
node_->typeinfo->type,
|
||||
NodeItem::type(shader_type_).c_str());
|
||||
NodeItem res = compute();
|
||||
if (res.node) {
|
||||
res.node->setName(node_name());
|
||||
@ -36,9 +43,14 @@ NodeItem NodeParser::compute_full()
|
||||
|
||||
std::string NodeParser::node_name()
|
||||
{
|
||||
return MaterialX::createValidName(node_->output_sockets().size() <= 1 ?
|
||||
std::string(node_->name) :
|
||||
std::string(node_->name) + "_" + socket_out_->name);
|
||||
std::string name = node_->name;
|
||||
if (node_->output_sockets().size() > 1) {
|
||||
name += std::string("_") + socket_out_->name;
|
||||
}
|
||||
if (ELEM(shader_type_, NodeItem::Type::BSDF, NodeItem::Type::EDF)) {
|
||||
name += "_" + NodeItem::type(shader_type_);
|
||||
}
|
||||
return MaterialX::createValidName(name);
|
||||
}
|
||||
|
||||
NodeItem NodeParser::create_node(const std::string &category, NodeItem::Type type)
|
||||
@ -145,7 +157,8 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket, NodeItem::Type to
|
||||
|
||||
#define CASE_NODE_TYPE(type, T) \
|
||||
case type: \
|
||||
parser = std::make_unique<T>(graph_, depsgraph_, material_, from_node, link->fromsock); \
|
||||
parser = std::make_unique<T>( \
|
||||
graph_, depsgraph_, material_, from_node, link->fromsock, NodeItem::Type::Any); \
|
||||
break;
|
||||
|
||||
switch (from_node->typeinfo->type) {
|
||||
@ -206,51 +219,17 @@ NodeItem NodeParser::get_input_value(const bNodeSocket &socket, NodeItem::Type t
|
||||
return res;
|
||||
}
|
||||
|
||||
ShaderNodeParser::ShaderNodeParser(MaterialX::GraphElement *graph,
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node,
|
||||
const bNodeSocket *socket_out,
|
||||
NodeItem::Type shader_type)
|
||||
: NodeParser(graph, depsgraph, material, node, socket_out), shader_type_(shader_type)
|
||||
{
|
||||
}
|
||||
|
||||
NodeItem ShaderNodeParser::compute_full()
|
||||
{
|
||||
CLOG_INFO(LOG_MATERIALX_SHADER,
|
||||
1,
|
||||
"%s [%d] - %s",
|
||||
node_->name,
|
||||
node_->typeinfo->type,
|
||||
NodeItem::type(shader_type_).c_str());
|
||||
NodeItem res = compute();
|
||||
if (res.node) {
|
||||
res.node->setName(node_name());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string ShaderNodeParser::node_name()
|
||||
{
|
||||
std::string name = NodeParser::node_name();
|
||||
if (shader_type_ != NodeItem::Type::SurfaceShader) {
|
||||
name += "_" + NodeItem::type(shader_type_);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
NodeItem ShaderNodeParser::get_input_shader(const std::string &name, NodeItem::Type shader_type)
|
||||
NodeItem NodeParser::get_input_shader(const std::string &name, NodeItem::Type shader_type)
|
||||
{
|
||||
return get_input_shader(node_->input_by_identifier(name), shader_type);
|
||||
}
|
||||
|
||||
NodeItem ShaderNodeParser::get_input_shader(int index, NodeItem::Type shader_type)
|
||||
NodeItem NodeParser::get_input_shader(int index, NodeItem::Type shader_type)
|
||||
{
|
||||
return get_input_shader(node_->input_socket(index), shader_type);
|
||||
}
|
||||
|
||||
NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem::Type shader_type)
|
||||
NodeItem NodeParser::get_input_shader(const bNodeSocket &socket, NodeItem::Type shader_type)
|
||||
{
|
||||
NodeItem res = empty();
|
||||
|
||||
@ -271,7 +250,7 @@ NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem:
|
||||
}
|
||||
|
||||
/* Creating required ShaderNodeParser object */
|
||||
std::unique_ptr<ShaderNodeParser> parser;
|
||||
std::unique_ptr<NodeParser> parser;
|
||||
|
||||
#define CASE_SHADER_NODE_TYPE(type, T) \
|
||||
case type: \
|
||||
|
@ -19,13 +19,15 @@ class NodeParser {
|
||||
const Material *material_;
|
||||
const bNode *node_;
|
||||
const bNodeSocket *socket_out_;
|
||||
NodeItem::Type shader_type_;
|
||||
|
||||
public:
|
||||
NodeParser(MaterialX::GraphElement *graph,
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node,
|
||||
const bNodeSocket *socket_out);
|
||||
const bNodeSocket *socket_out,
|
||||
NodeItem::Type shader_type);
|
||||
virtual ~NodeParser() = default;
|
||||
|
||||
virtual NodeItem compute() = 0;
|
||||
@ -40,6 +42,8 @@ class NodeParser {
|
||||
NodeItem get_input_link(int index, NodeItem::Type to_type);
|
||||
NodeItem get_input_value(const std::string &name, NodeItem::Type to_type);
|
||||
NodeItem get_input_value(int index, NodeItem::Type to_type);
|
||||
NodeItem get_input_shader(const std::string &name, NodeItem::Type shader_type);
|
||||
NodeItem get_input_shader(int index, NodeItem::Type shader_type);
|
||||
NodeItem empty() const;
|
||||
template<class T> NodeItem val(const T &data) const;
|
||||
NodeItem texcoord_node();
|
||||
@ -48,27 +52,6 @@ class NodeParser {
|
||||
NodeItem get_input_default(const bNodeSocket &socket, NodeItem::Type to_type);
|
||||
NodeItem get_input_link(const bNodeSocket &socket, NodeItem::Type to_type);
|
||||
NodeItem get_input_value(const bNodeSocket &socket, NodeItem::Type to_type);
|
||||
};
|
||||
|
||||
class ShaderNodeParser : public NodeParser {
|
||||
protected:
|
||||
NodeItem::Type shader_type_;
|
||||
|
||||
public:
|
||||
ShaderNodeParser(MaterialX::GraphElement *graph,
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node,
|
||||
const bNodeSocket *socket_out,
|
||||
NodeItem::Type shader_type);
|
||||
|
||||
protected:
|
||||
NodeItem compute_full() override;
|
||||
std::string node_name() override;
|
||||
NodeItem get_input_shader(const std::string &name, NodeItem::Type shader_type);
|
||||
NodeItem get_input_shader(int index, NodeItem::Type shader_type);
|
||||
|
||||
private:
|
||||
NodeItem get_input_shader(const bNodeSocket &socket, NodeItem::Type shader_type);
|
||||
};
|
||||
|
||||
@ -118,25 +101,26 @@ DECLARE_NODE_PARSER(TexNoiseNodeParser)
|
||||
DECLARE_NODE_PARSER(VectorMathNodeParser)
|
||||
DECLARE_NODE_PARSER(WavelengthNodeParser)
|
||||
|
||||
DECLARE_SHADER_NODE_PARSER(AddShaderNodeParser)
|
||||
DECLARE_SHADER_NODE_PARSER(BSDFDiffuseNodeParser)
|
||||
DECLARE_SHADER_NODE_PARSER(BSDFGlassNodeParser)
|
||||
DECLARE_SHADER_NODE_PARSER(BSDFGlossyNodeParser)
|
||||
DECLARE_SHADER_NODE_PARSER(BSDFPrincipledNodeParser)
|
||||
DECLARE_SHADER_NODE_PARSER(BSDFRefractionNodeParser)
|
||||
DECLARE_SHADER_NODE_PARSER(BSDFSheenNodeParser)
|
||||
DECLARE_SHADER_NODE_PARSER(BSDFToonNodeParser)
|
||||
DECLARE_SHADER_NODE_PARSER(BSDFTranslucentNodeParser)
|
||||
DECLARE_SHADER_NODE_PARSER(BSDFTransparentNodeParser)
|
||||
DECLARE_SHADER_NODE_PARSER(EmissionNodeParser)
|
||||
DECLARE_SHADER_NODE_PARSER(MixShaderNodeParser)
|
||||
DECLARE_SHADER_NODE_PARSER(SubsurfaceScatteringNodeParser)
|
||||
DECLARE_NODE_PARSER(AddShaderNodeParser)
|
||||
DECLARE_NODE_PARSER(BSDFDiffuseNodeParser)
|
||||
DECLARE_NODE_PARSER(BSDFGlassNodeParser)
|
||||
DECLARE_NODE_PARSER(BSDFGlossyNodeParser)
|
||||
DECLARE_NODE_PARSER(BSDFPrincipledNodeParser)
|
||||
DECLARE_NODE_PARSER(BSDFRefractionNodeParser)
|
||||
DECLARE_NODE_PARSER(BSDFSheenNodeParser)
|
||||
DECLARE_NODE_PARSER(BSDFToonNodeParser)
|
||||
DECLARE_NODE_PARSER(BSDFTranslucentNodeParser)
|
||||
DECLARE_NODE_PARSER(BSDFTransparentNodeParser)
|
||||
DECLARE_NODE_PARSER(EmissionNodeParser)
|
||||
DECLARE_NODE_PARSER(MixShaderNodeParser)
|
||||
DECLARE_NODE_PARSER(SubsurfaceScatteringNodeParser)
|
||||
|
||||
|
||||
struct NodeParserData {
|
||||
MaterialX::GraphElement *graph;
|
||||
const Depsgraph *depsgraph;
|
||||
const Material *material;
|
||||
NodeItem::Type shader_type;
|
||||
NodeItem result;
|
||||
};
|
||||
|
||||
@ -144,7 +128,7 @@ template<class T>
|
||||
void node_shader_materialx(void *data, struct bNode *node, struct bNodeSocket *out)
|
||||
{
|
||||
materialx::NodeParserData *d = reinterpret_cast<materialx::NodeParserData *>(data);
|
||||
T parser(d->graph, d->depsgraph, d->material, node, out);
|
||||
T parser(d->graph, d->depsgraph, d->material, node, out, d->shader_type);
|
||||
d->result = parser.compute();
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@ OutputMaterialNodeParser::OutputMaterialNodeParser(MaterialX::GraphElement *grap
|
||||
const Depsgraph *depsgraph,
|
||||
const Material *material,
|
||||
const bNode *node)
|
||||
: ShaderNodeParser(graph, depsgraph, material, node, nullptr, NodeItem::Type::Material)
|
||||
: NodeParser(graph, depsgraph, material, node, nullptr, NodeItem::Type::Material)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
namespace blender::nodes::materialx {
|
||||
|
||||
class OutputMaterialNodeParser : public ShaderNodeParser {
|
||||
class OutputMaterialNodeParser : public NodeParser {
|
||||
public:
|
||||
OutputMaterialNodeParser(MaterialX::GraphElement *graph,
|
||||
const Depsgraph *depsgraph,
|
||||
@ -16,7 +16,7 @@ class OutputMaterialNodeParser : public ShaderNodeParser {
|
||||
const bNode *node);
|
||||
NodeItem compute() override;
|
||||
|
||||
using ShaderNodeParser::compute_full;
|
||||
using NodeParser::compute_full;
|
||||
NodeItem compute_default();
|
||||
|
||||
protected:
|
||||
|
Loading…
Reference in New Issue
Block a user