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
5 changed files with 35 additions and 27 deletions
Showing only changes of commit de75c7bf6e - Show all commits

View File

@ -54,7 +54,7 @@ NodeItem::Type NodeItem::type(const std::string &type_str)
return Type::Material;
}
BLI_assert_unreachable();
return Type::None;
return Type::Empty;
}
std::string NodeItem::type(Type type)
@ -151,7 +151,7 @@ bool NodeItem::operator==(const NodeItem &other) const
NodeItem item1 = *this;
NodeItem item2 = other;
Type to_type = adjust_types(item1, item2);
if (to_type == Type::None) {
if (to_type == Type::Empty) {
return false;
}
return item1.value->getValueString() == item2.value->getValueString();
@ -529,7 +529,7 @@ NodeItem NodeItem::if_else(CompareOp op,
auto item1 = if_val;
auto item2 = else_val;
Type to_type = adjust_types(item1, item2);
if (to_type == Type::None) {
if (to_type == Type::Empty) {
return res;
}
@ -579,7 +579,7 @@ NodeItem::Type NodeItem::type() const
if (node) {
return type(node->getType());
}
return Type::None;
return Type::Empty;
}
void NodeItem::set_input(const std::string &name,
@ -647,7 +647,7 @@ NodeItem::Type NodeItem::adjust_types(NodeItem &item1, NodeItem &item2)
return t1;
}
if (!is_arithmetic(t1) || !is_arithmetic(t2)) {
return Type::None;
return Type::Empty;
}
if (t1 < t2) {
item1 = item1.convert(t2);
@ -728,7 +728,7 @@ NodeItem NodeItem::arithmetic(const NodeItem &other,
NodeItem item1 = *this;
NodeItem item2 = other;
Type to_type = adjust_types(item1, item2);
if (to_type == Type::None) {
if (to_type == Type::Empty) {
return res;
}

View File

@ -11,7 +11,8 @@ namespace blender::nodes::materialx {
class NodeItem {
public:
enum class Type {
None = 0,
Empty = 0,
Any,
/* Value types */
String,
@ -30,7 +31,6 @@ class NodeItem {
EDF,
SurfaceShader,
Material,
};
enum class CompareOp { Less = 0, LessEq, Eq, GreaterEq, Greater, NotEq };

View File

@ -129,22 +129,17 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket)
from_node = link->fromnode;
}
/* Checking if node was already computed */
res.node = graph_->getNode(node_name());
if (res.node) {
return res;
}
/* Creating required NodeParser object */
std::unique_ptr<NodeParser> parser;
/* 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(); \
parser = \
std::make_unique<T>(graph_, depsgraph_, material_, from_node, link->fromsock); \
break;
switch (from_node->typeinfo->type) {
CASE_NODE_TYPE(SH_NODE_BRIGHTCONTRAST, BrightContrastNodeParser)
//CASE_NODE_TYPE(SH_NODE_BSDF_PRINCIPLED, BSDFPrincipledNodeParser)
CASE_NODE_TYPE(SH_NODE_COMBINE_COLOR, CombineColorNodeParser)
CASE_NODE_TYPE(SH_NODE_COMBXYZ, CombineXYZNodeParser)
CASE_NODE_TYPE(SH_NODE_HUE_SAT, HueSatValNodeParser)
@ -167,6 +162,14 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket)
from_node->typeinfo->type);
}
/* Checking if node was already computed */
res.node = graph_->getNode(parser->node_name());
if (res.node) {
return res;
}
/* Computing */
res = parser->compute_full();
return res;
}
@ -243,16 +246,13 @@ NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem:
from_node = link->fromnode;
}
/* Checking if node was already computed */
res.node = graph_->getNode(node_name());
if (res.node) {
return res;
}
/* Creating required ShaderNodeParser object */
std::unique_ptr<ShaderNodeParser> parser;
/* Computing from_node with required NodeParser object */
#define CASE_SHADER_NODE_TYPE(type, T) \
case type: \
res = T(graph_, depsgraph_, material_, from_node, link->fromsock, shader_type).compute_full(); \
parser = std::make_unique<T>( \
graph_, depsgraph_, material_, from_node, link->fromsock, shader_type); \
break;
switch (from_node->typeinfo->type) {
@ -265,6 +265,14 @@ NodeItem ShaderNodeParser::get_input_shader(const bNodeSocket &socket, NodeItem:
from_node->typeinfo->type);
}
/* Checking if node was already computed */
res.node = graph_->getNode(parser->node_name());
if (res.node) {
return res;
}
/* Computing */
res = parser->compute_full();
return res;
}

View File

@ -79,7 +79,7 @@ NodeItem OutputMaterialNodeParser::compute_default()
std::string OutputMaterialNodeParser::node_name()
{
return node_->name;
return NodeParser::node_name();
}
} // namespace blender::nodes::materialx

View File

@ -20,8 +20,8 @@ NodeItem TexCheckerNodeParser::compute()
NodeItem mix = (vector.extract(0).floor() + vector.extract(1).floor())
.if_else(NodeItem::CompareOp::Eq, value(1.0f), value(1.0f), value(0.0f));
NodeItem res = create_node("mix", "color3");
res.set_input("fg", color1, NodeItem::Type::Color3);
res.set_input("bg", color2, NodeItem::Type::Color3);
res.set_input("fg", color1);
res.set_input("bg", color2);
res.set_input("mix", mix);
return res;
}