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
2 changed files with 40 additions and 40 deletions
Showing only changes of commit 266f05721b - Show all commits

View File

@ -650,11 +650,6 @@ NodeItem NodeItem::to_color3() const
return res;
}
bool NodeItem::is_numeric() const
{
return type() >= Type::Float;
}
NodeItem::Type NodeItem::type() const
{
if (value) {
@ -679,42 +674,48 @@ bool NodeItem::is_arithmetic() const
NodeItem NodeItem::arithmetic(const std::string &mx_category,
std::function<float(float)> func) const
{
if (!is_numeric()) {
return empty();
NodeItem res = empty();
Type t = type();
if (!is_arithmetic(t)) {
return res;
}
std::string t = value ? value->getTypeString() : node->getType();
NodeItem res(graph_);
if (value) {
if (t == "float") {
switch (t) {
case Type::Float: {
float v = value->asA<float>();
res.value = MaterialX::Value::createValue<float>(func(v));
break;
}
else if (t == "color3") {
case Type::Color3: {
auto v = value->asA<MaterialX::Color3>();
res.value = MaterialX::Value::createValue<MaterialX::Color3>(
{func(v[0]), func(v[1]), func(v[2])});
break;
}
else if (t == "color4") {
case Type::Color4: {
auto v = value->asA<MaterialX::Color4>();
res.value = MaterialX::Value::createValue<MaterialX::Color4>(
{func(v[0]), func(v[1]), func(v[2]), func(v[3])});
break;
}
else if (t == "vector2") {
case Type::Vector2: {
auto v = value->asA<MaterialX::Vector2>();
res.value = MaterialX::Value::createValue<MaterialX::Vector2>({func(v[0]), func(v[1])});
}
else if (t == "vector3") {
case Type::Vector3: {
auto v = value->asA<MaterialX::Vector3>();
res.value = MaterialX::Value::createValue<MaterialX::Vector3>(
{func(v[0]), func(v[1]), func(v[2])});
break;
}
else if (t == "vector4") {
case Type::Vector4: {
auto v = value->asA<MaterialX::Vector4>();
res.value = MaterialX::Value::createValue<MaterialX::Vector4>(
{func(v[0]), func(v[1]), func(v[2]), func(v[3])});
break;
}
else {
default:
BLI_assert_unreachable();
}
}

View File

@ -81,7 +81,6 @@ class NodeItem {
NodeItem convert(Type to_type) const;
NodeItem to_color3() const;
bool is_numeric() const;
Type type() const;
private: