Implement type conversion for NodeItem #9

Merged
Bogdan Nagirniak merged 12 commits from BogdanNagirniak/blender:matx-nodeitem-type into matx-export-material 2023-09-05 12:03:24 +02:00
4 changed files with 20 additions and 18 deletions
Showing only changes of commit 8c13cb78c4 - Show all commits

View File

@ -109,10 +109,10 @@ NodeItem MathNodeParser::compute()
res = x.max(y); res = x.max(y);
break; break;
case NODE_MATH_LESS_THAN: case NODE_MATH_LESS_THAN:
res = x.if_else(NodeItem::IfType::Less, y, value(1.0f), value(0.0f)); res = x.if_else(NodeItem::CompareOp::Less, y, value(1.0f), value(0.0f));
break; break;
case NODE_MATH_GREATER_THAN: case NODE_MATH_GREATER_THAN:
res = x.if_else(NodeItem::IfType::Greater, y, value(1.0f), value(0.0f)); res = x.if_else(NodeItem::CompareOp::Greater, y, value(1.0f), value(0.0f));
break; break;
case NODE_MATH_MODULO: case NODE_MATH_MODULO:
res = x % y; res = x % y;
@ -138,7 +138,7 @@ NodeItem MathNodeParser::compute()
CLOG_WARN(LOG_MATERIALX_SHADER, "Unimplemented math operation %d", op); CLOG_WARN(LOG_MATERIALX_SHADER, "Unimplemented math operation %d", op);
break; break;
case NODE_MATH_COMPARE: case NODE_MATH_COMPARE:
res = z.if_else(NodeItem::IfType::Less, (x - y).abs(), value(1.0f), value(0.0f)); res = z.if_else(NodeItem::CompareOp::Less, (x - y).abs(), value(1.0f), value(0.0f));
break; break;
case NODE_MATH_MULTIPLY_ADD: case NODE_MATH_MULTIPLY_ADD:
res = x * y + z; res = x * y + z;

View File

@ -416,18 +416,18 @@ NodeItem NodeItem::convert(Type to_type) const
return res; return res;
} }
NodeItem NodeItem::if_else(IfType condition, NodeItem NodeItem::if_else(CompareOp op,
const NodeItem &other, const NodeItem &other,
const NodeItem &if_val, const NodeItem &if_val,
const NodeItem &else_val) const const NodeItem &else_val) const
{ {
switch (condition) { switch (op) {
case IfType::Less: case CompareOp::Less:
return other.if_else(IfType::Greater, *this, else_val, if_val); return other.if_else(CompareOp::Greater, *this, else_val, if_val);
case IfType::LessEq: case CompareOp::LessEq:
return other.if_else(IfType::GreaterEq, *this, else_val, if_val); return other.if_else(CompareOp::GreaterEq, *this, else_val, if_val);
case IfType::NotEq: case CompareOp::NotEq:
return if_else(IfType::Eq, other, else_val, if_val); return if_else(CompareOp::Eq, other, else_val, if_val);
default: default:
break; break;
} }
@ -446,16 +446,16 @@ NodeItem NodeItem::if_else(IfType condition,
std::function<bool(float, float)> func = nullptr; std::function<bool(float, float)> func = nullptr;
std::string mx_category; std::string mx_category;
switch (condition) { switch (op) {
case IfType::Greater: case CompareOp::Greater:
mx_category = "ifgreater"; mx_category = "ifgreater";
func = [](float a, float b) { return a > b; }; func = [](float a, float b) { return a > b; };
break; break;
case IfType::GreaterEq: case CompareOp::GreaterEq:
mx_category = "ifgreatereq"; mx_category = "ifgreatereq";
func = [](float a, float b) { return a >= b; }; func = [](float a, float b) { return a >= b; };
break; break;
case IfType::Eq: case CompareOp::Eq:
mx_category = "ifequal"; mx_category = "ifequal";
func = [](float a, float b) { return a == b; }; func = [](float a, float b) { return a == b; };
break; break;
@ -685,6 +685,8 @@ NodeItem NodeItem::arithmetic(const std::string &category, std::function<float(f
} }
} }
else { else {
/* TODO: Some of math functions (sin, cos, ...) doesn't work with Color types,
* we have to convert to Vector */
res.node = graph_->addNode(category, MaterialX::EMPTY_STRING, type(tp)); res.node = graph_->addNode(category, MaterialX::EMPTY_STRING, type(tp));
res.set_input("in", *this); res.set_input("in", *this);
} }

View File

@ -22,7 +22,7 @@ class NodeItem {
Color3, Color3,
Color4 Color4
}; };
enum class IfType { Less = 0, LessEq, Eq, GreaterEq, Greater, NotEq }; enum class CompareOp { Less = 0, LessEq, Eq, GreaterEq, Greater, NotEq };
public: public:
MaterialX::ValuePtr value; MaterialX::ValuePtr value;
@ -72,7 +72,7 @@ class NodeItem {
NodeItem sign() const; NodeItem sign() const;
NodeItem exp() const; NodeItem exp() const;
NodeItem convert(Type to_type) const; NodeItem convert(Type to_type) const;
NodeItem if_else(IfType condition, NodeItem if_else(CompareOp op,
const NodeItem &other, const NodeItem &other,
const NodeItem &if_val, const NodeItem &if_val,
const NodeItem &else_val) const; const NodeItem &else_val) const;

View File

@ -32,7 +32,7 @@ NodeItem TexCheckerNodeParser::compute()
modulo_y.set_input("in2", value(2.0f)); modulo_y.set_input("in2", value(2.0f));
NodeItem ifequal = (modulo_x.floor() + modulo_y.floor()) NodeItem ifequal = (modulo_x.floor() + modulo_y.floor())
.if_else(NodeItem::IfType::Eq, value(1.0f), value(0.0f), value(1.0f)); .if_else(NodeItem::CompareOp::Eq, value(1.0f), value(0.0f), value(1.0f));
NodeItem res = create_node("mix", "color3"); NodeItem res = create_node("mix", "color3");
res.set_input("bg", color1, NodeItem::Type::Color3); res.set_input("bg", color1, NodeItem::Type::Color3);