Implement type conversion for NodeItem #10

Closed
Bogdan Nagirniak wants to merge 9 commits from BogdanNagirniak/blender:matx-nodeitem-type into matx-export-material

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
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,43 +674,49 @@ 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") {
float v = value->asA<float>();
res.value = MaterialX::Value::createValue<float>(func(v));
}
else if (t == "color3") {
auto v = value->asA<MaterialX::Color3>();
res.value = MaterialX::Value::createValue<MaterialX::Color3>(
{func(v[0]), func(v[1]), func(v[2])});
}
else if (t == "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])});
}
else if (t == "vector2") {
auto v = value->asA<MaterialX::Vector2>();
res.value = MaterialX::Value::createValue<MaterialX::Vector2>({func(v[0]), func(v[1])});
}
else if (t == "vector3") {
auto v = value->asA<MaterialX::Vector3>();
res.value = MaterialX::Value::createValue<MaterialX::Vector3>(
{func(v[0]), func(v[1]), func(v[2])});
}
else if (t == "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])});
}
else {
BLI_assert_unreachable();
switch (t) {
case Type::Float: {
float v = value->asA<float>();
res.value = MaterialX::Value::createValue<float>(func(v));
break;
}
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;
}
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;
}
case Type::Vector2: {
auto v = value->asA<MaterialX::Vector2>();
res.value = MaterialX::Value::createValue<MaterialX::Vector2>({func(v[0]), func(v[1])});
}
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;
}
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;
}
default:
BLI_assert_unreachable();
}
}
else {

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: