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);
break;
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;
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;
case NODE_MATH_MODULO:
res = x % y;
@ -138,7 +138,7 @@ NodeItem MathNodeParser::compute()
CLOG_WARN(LOG_MATERIALX_SHADER, "Unimplemented math operation %d", op);
break;
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;
case NODE_MATH_MULTIPLY_ADD:
res = x * y + z;

View File

@ -416,18 +416,18 @@ NodeItem NodeItem::convert(Type to_type) const
return res;
}
NodeItem NodeItem::if_else(IfType condition,
NodeItem NodeItem::if_else(CompareOp op,
const NodeItem &other,
const NodeItem &if_val,
const NodeItem &else_val) const
{
switch (condition) {
case IfType::Less:
return other.if_else(IfType::Greater, *this, else_val, if_val);
case IfType::LessEq:
return other.if_else(IfType::GreaterEq, *this, else_val, if_val);
case IfType::NotEq:
return if_else(IfType::Eq, other, else_val, if_val);
switch (op) {
case CompareOp::Less:
return other.if_else(CompareOp::Greater, *this, else_val, if_val);
case CompareOp::LessEq:
return other.if_else(CompareOp::GreaterEq, *this, else_val, if_val);
case CompareOp::NotEq:
return if_else(CompareOp::Eq, other, else_val, if_val);
default:
break;
}
@ -446,16 +446,16 @@ NodeItem NodeItem::if_else(IfType condition,
std::function<bool(float, float)> func = nullptr;
std::string mx_category;
switch (condition) {
case IfType::Greater:
switch (op) {
case CompareOp::Greater:
mx_category = "ifgreater";
func = [](float a, float b) { return a > b; };
break;
case IfType::GreaterEq:
case CompareOp::GreaterEq:
mx_category = "ifgreatereq";
func = [](float a, float b) { return a >= b; };
break;
case IfType::Eq:
case CompareOp::Eq:
mx_category = "ifequal";
func = [](float a, float b) { return a == b; };
break;
@ -685,6 +685,8 @@ NodeItem NodeItem::arithmetic(const std::string &category, std::function<float(f
}
}
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.set_input("in", *this);
}

View File

@ -22,7 +22,7 @@ class NodeItem {
Color3,
Color4
};
enum class IfType { Less = 0, LessEq, Eq, GreaterEq, Greater, NotEq };
enum class CompareOp { Less = 0, LessEq, Eq, GreaterEq, Greater, NotEq };
BogdanNagirniak marked this conversation as resolved Outdated

IfType -> ComparisonType

IfType -> ComparisonType
public:
MaterialX::ValuePtr value;
@ -72,7 +72,7 @@ class NodeItem {
NodeItem sign() const;
NodeItem exp() const;
NodeItem convert(Type to_type) const;
NodeItem if_else(IfType condition,
NodeItem if_else(CompareOp op,
const NodeItem &other,
const NodeItem &if_val,
const NodeItem &else_val) const;

View File

@ -32,7 +32,7 @@ NodeItem TexCheckerNodeParser::compute()
modulo_y.set_input("in2", value(2.0f));
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");
res.set_input("bg", color1, NodeItem::Type::Color3);