forked from blender/blender
Implement export of Math node. Continue other arithmetic support for NodeItem #6
@ -29,25 +29,28 @@ void NodeItem::set_input(const std::string &name, const NodeItem &item)
|
|||||||
void NodeItem::set_input(const std::string &name, const MaterialX::ValuePtr value)
|
void NodeItem::set_input(const std::string &name, const MaterialX::ValuePtr value)
|
||||||
{
|
{
|
||||||
std::string mx_type = value->getTypeString();
|
std::string mx_type = value->getTypeString();
|
||||||
if (value->isA<float>()) {
|
if (mx_type == "float") {
|
||||||
set_input(name, value->asA<float>(), mx_type);
|
set_input(name, value->asA<float>(), mx_type);
|
||||||
}
|
}
|
||||||
else if (value->isA<MaterialX::Vector2>()) {
|
else if (mx_type == "integer") {
|
||||||
|
set_input(name, value->asA<int>(), mx_type);
|
||||||
|
}
|
||||||
|
else if (mx_type == "vector2") {
|
||||||
set_input(name, value->asA<MaterialX::Vector2>(), mx_type);
|
set_input(name, value->asA<MaterialX::Vector2>(), mx_type);
|
||||||
}
|
}
|
||||||
else if (value->isA<MaterialX::Vector3>()) {
|
else if (mx_type == "vector3") {
|
||||||
set_input(name, value->asA<MaterialX::Vector3>(), mx_type);
|
set_input(name, value->asA<MaterialX::Vector3>(), mx_type);
|
||||||
}
|
}
|
||||||
else if (value->isA<MaterialX::Vector4>()) {
|
else if (mx_type == "vector4") {
|
||||||
set_input(name, value->asA<MaterialX::Vector4>(), mx_type);
|
set_input(name, value->asA<MaterialX::Vector4>(), mx_type);
|
||||||
}
|
}
|
||||||
else if (value->isA<MaterialX::Color3>()) {
|
else if (mx_type == "color3") {
|
||||||
set_input(name, value->asA<MaterialX::Color3>(), mx_type);
|
set_input(name, value->asA<MaterialX::Color3>(), mx_type);
|
||||||
}
|
}
|
||||||
else if (value->isA<MaterialX::Color4>()) {
|
else if (mx_type == "color4") {
|
||||||
set_input(name, value->asA<MaterialX::Color4>(), mx_type);
|
set_input(name, value->asA<MaterialX::Color4>(), mx_type);
|
||||||
}
|
}
|
||||||
else if (value->isA<std::string>()) {
|
else if (mx_type == "string") {
|
||||||
set_input(name, value->asA<std::string>(), mx_type);
|
set_input(name, value->asA<std::string>(), mx_type);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -116,28 +119,28 @@ bool NodeItem::operator==(const NodeItem &other) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string t;
|
std::string mx_type;
|
||||||
auto val1 = value;
|
auto val1 = value;
|
||||||
auto val2 = other.value;
|
auto val2 = other.value;
|
||||||
if (!adjust_types(val1, val2, t)) {
|
if (!adjust_types(val1, val2, mx_type)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (t == "float") {
|
if (mx_type == "float") {
|
||||||
return val1->asA<float>() == val2->asA<float>();
|
return val1->asA<float>() == val2->asA<float>();
|
||||||
}
|
}
|
||||||
if (t == "color3") {
|
if (mx_type == "color3") {
|
||||||
return val1->asA<MaterialX::Color3>() == val2->asA<MaterialX::Color3>();
|
return val1->asA<MaterialX::Color3>() == val2->asA<MaterialX::Color3>();
|
||||||
}
|
}
|
||||||
if (t == "color4") {
|
if (mx_type == "color4") {
|
||||||
return val1->asA<MaterialX::Color4>() == val2->asA<MaterialX::Color4>();
|
return val1->asA<MaterialX::Color4>() == val2->asA<MaterialX::Color4>();
|
||||||
}
|
}
|
||||||
if (t == "vector2") {
|
if (mx_type == "vector2") {
|
||||||
return val1->asA<MaterialX::Vector2>() == val2->asA<MaterialX::Vector2>();
|
return val1->asA<MaterialX::Vector2>() == val2->asA<MaterialX::Vector2>();
|
||||||
}
|
}
|
||||||
if (t == "vector3") {
|
if (mx_type == "vector3") {
|
||||||
return val1->asA<MaterialX::Vector3>() == val2->asA<MaterialX::Vector3>();
|
return val1->asA<MaterialX::Vector3>() == val2->asA<MaterialX::Vector3>();
|
||||||
}
|
}
|
||||||
if (t == "vector4") {
|
if (mx_type == "vector4") {
|
||||||
return val1->asA<MaterialX::Vector4>() == val2->asA<MaterialX::Vector4>();
|
return val1->asA<MaterialX::Vector4>() == val2->asA<MaterialX::Vector4>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,28 +181,28 @@ NodeItem NodeItem::dotproduct(const NodeItem &other) const
|
|||||||
{
|
{
|
||||||
NodeItem d = arithmetic(other, "dotproduct", [](float a, float b) { return a * b; });
|
NodeItem d = arithmetic(other, "dotproduct", [](float a, float b) { return a * b; });
|
||||||
if (d.value) {
|
if (d.value) {
|
||||||
std::string t = d.type();
|
std::string mx_type = d.type();
|
||||||
float f = 0.0f;
|
float f = 0.0f;
|
||||||
if (t == "float") {
|
if (mx_type == "float") {
|
||||||
f = value->asA<float>();
|
f = value->asA<float>();
|
||||||
}
|
}
|
||||||
else if (t == "color3") {
|
else if (mx_type == "color3") {
|
||||||
auto v = value->asA<MaterialX::Color3>();
|
auto v = value->asA<MaterialX::Color3>();
|
||||||
f = v[0] + v[1] + v[2];
|
f = v[0] + v[1] + v[2];
|
||||||
}
|
}
|
||||||
else if (t == "color4") {
|
else if (mx_type == "color4") {
|
||||||
auto v = value->asA<MaterialX::Color4>();
|
auto v = value->asA<MaterialX::Color4>();
|
||||||
f = v[0] + v[1] + v[2] + v[3];
|
f = v[0] + v[1] + v[2] + v[3];
|
||||||
}
|
}
|
||||||
else if (t == "vector2") {
|
else if (mx_type == "vector2") {
|
||||||
auto v = value->asA<MaterialX::Vector2>();
|
auto v = value->asA<MaterialX::Vector2>();
|
||||||
f = v[0] + v[1];
|
f = v[0] + v[1];
|
||||||
}
|
}
|
||||||
else if (t == "vector3") {
|
else if (mx_type == "vector3") {
|
||||||
auto v = value->asA<MaterialX::Vector3>();
|
auto v = value->asA<MaterialX::Vector3>();
|
||||||
f = v[0] + v[1] + v[2];
|
f = v[0] + v[1] + v[2];
|
||||||
}
|
}
|
||||||
else if (t == "vector4") {
|
else if (mx_type == "vector4") {
|
||||||
auto v = value->asA<MaterialX::Vector4>();
|
auto v = value->asA<MaterialX::Vector4>();
|
||||||
f = v[0] + v[1] + v[2] + v[3];
|
f = v[0] + v[1] + v[2] + v[3];
|
||||||
}
|
}
|
||||||
@ -230,8 +233,8 @@ NodeItem NodeItem::if_else(const std::string &condition,
|
|||||||
|
|
||||||
auto val1 = if_val;
|
auto val1 = if_val;
|
||||||
auto val2 = else_val;
|
auto val2 = else_val;
|
||||||
std::string t;
|
std::string mx_type;
|
||||||
if (!adjust_types(val1, val2, t)) {
|
if (!adjust_types(val1, val2, mx_type)) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -257,7 +260,7 @@ NodeItem NodeItem::if_else(const std::string &condition,
|
|||||||
res = func(value->asA<float>(), other.value->asA<float>()) ? val1 : val2;
|
res = func(value->asA<float>(), other.value->asA<float>()) ? val1 : val2;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, t);
|
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, mx_type);
|
||||||
res.set_input("value1", *this);
|
res.set_input("value1", *this);
|
||||||
res.set_input("value2", other);
|
res.set_input("value2", other);
|
||||||
res.set_input("in1", val1);
|
res.set_input("in1", val1);
|
||||||
@ -461,44 +464,44 @@ NodeItem NodeItem::arithmetic(const NodeItem &other,
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string t;
|
std::string mx_type;
|
||||||
if (value && other.value) {
|
if (value && other.value) {
|
||||||
auto val1 = value;
|
auto val1 = value;
|
||||||
auto val2 = other.value;
|
auto val2 = other.value;
|
||||||
if (!adjust_types(val1, val2, t)) {
|
if (!adjust_types(val1, val2, mx_type)) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (t == "float") {
|
if (mx_type == "float") {
|
||||||
float v1 = val1->asA<float>();
|
float v1 = val1->asA<float>();
|
||||||
float v2 = val2->asA<float>();
|
float v2 = val2->asA<float>();
|
||||||
res.value = MaterialX::Value::createValue<float>(func(v1, v2));
|
res.value = MaterialX::Value::createValue<float>(func(v1, v2));
|
||||||
}
|
}
|
||||||
else if (t == "color3") {
|
else if (mx_type == "color3") {
|
||||||
auto v1 = val1->asA<MaterialX::Color3>();
|
auto v1 = val1->asA<MaterialX::Color3>();
|
||||||
auto v2 = val2->asA<MaterialX::Color3>();
|
auto v2 = val2->asA<MaterialX::Color3>();
|
||||||
res.value = MaterialX::Value::createValue<MaterialX::Color3>(
|
res.value = MaterialX::Value::createValue<MaterialX::Color3>(
|
||||||
{func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2])});
|
{func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2])});
|
||||||
}
|
}
|
||||||
else if (t == "color4") {
|
else if (mx_type == "color4") {
|
||||||
auto v1 = val1->asA<MaterialX::Color4>();
|
auto v1 = val1->asA<MaterialX::Color4>();
|
||||||
auto v2 = val2->asA<MaterialX::Color4>();
|
auto v2 = val2->asA<MaterialX::Color4>();
|
||||||
res.value = MaterialX::Value::createValue<MaterialX::Color4>(
|
res.value = MaterialX::Value::createValue<MaterialX::Color4>(
|
||||||
{func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2]), func(v1[3], v2[3])});
|
{func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2]), func(v1[3], v2[3])});
|
||||||
}
|
}
|
||||||
else if (t == "vector2") {
|
else if (mx_type == "vector2") {
|
||||||
auto v1 = val1->asA<MaterialX::Vector2>();
|
auto v1 = val1->asA<MaterialX::Vector2>();
|
||||||
auto v2 = val2->asA<MaterialX::Vector2>();
|
auto v2 = val2->asA<MaterialX::Vector2>();
|
||||||
res.value = MaterialX::Value::createValue<MaterialX::Vector2>(
|
res.value = MaterialX::Value::createValue<MaterialX::Vector2>(
|
||||||
{func(v1[0], v2[0]), func(v1[1], v2[1])});
|
{func(v1[0], v2[0]), func(v1[1], v2[1])});
|
||||||
}
|
}
|
||||||
else if (t == "vector3") {
|
else if (mx_type == "vector3") {
|
||||||
auto v1 = val1->asA<MaterialX::Vector3>();
|
auto v1 = val1->asA<MaterialX::Vector3>();
|
||||||
auto v2 = val2->asA<MaterialX::Vector3>();
|
auto v2 = val2->asA<MaterialX::Vector3>();
|
||||||
res.value = MaterialX::Value::createValue<MaterialX::Vector3>(
|
res.value = MaterialX::Value::createValue<MaterialX::Vector3>(
|
||||||
{func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2])});
|
{func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2])});
|
||||||
}
|
}
|
||||||
else if (t == "vector4") {
|
else if (mx_type == "vector4") {
|
||||||
auto v1 = val1->asA<MaterialX::Vector4>();
|
auto v1 = val1->asA<MaterialX::Vector4>();
|
||||||
auto v2 = val2->asA<MaterialX::Vector4>();
|
auto v2 = val2->asA<MaterialX::Vector4>();
|
||||||
res.value = MaterialX::Value::createValue<MaterialX::Vector4>(
|
res.value = MaterialX::Value::createValue<MaterialX::Vector4>(
|
||||||
@ -511,35 +514,35 @@ NodeItem NodeItem::arithmetic(const NodeItem &other,
|
|||||||
else {
|
else {
|
||||||
auto val1 = *this;
|
auto val1 = *this;
|
||||||
auto val2 = other;
|
auto val2 = other;
|
||||||
if (!adjust_types(val1, val2, t)) {
|
if (!adjust_types(val1, val2, mx_type)) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, t);
|
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, mx_type);
|
||||||
res.set_input("in1", val1);
|
res.set_input("in1", val1);
|
||||||
res.set_input("in2", val2);
|
res.set_input("in2", val2);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
MaterialX::ValuePtr NodeItem::float_to_type(float v, std::string t)
|
MaterialX::ValuePtr NodeItem::float_to_type(float v, std::string mx_type)
|
||||||
{
|
{
|
||||||
if (t == "float") {
|
if (mx_type == "float") {
|
||||||
return MaterialX::Value::createValue<float>(v);
|
return MaterialX::Value::createValue<float>(v);
|
||||||
}
|
}
|
||||||
if (t == "color3") {
|
if (mx_type == "color3") {
|
||||||
return MaterialX::Value::createValue<MaterialX::Color3>({v, v, v});
|
return MaterialX::Value::createValue<MaterialX::Color3>({v, v, v});
|
||||||
}
|
}
|
||||||
if (t == "color4") {
|
if (mx_type == "color4") {
|
||||||
return MaterialX::Value::createValue<MaterialX::Color4>({v, v, v, 1.0f});
|
return MaterialX::Value::createValue<MaterialX::Color4>({v, v, v, 1.0f});
|
||||||
}
|
}
|
||||||
if (t == "vector2") {
|
if (mx_type == "vector2") {
|
||||||
return MaterialX::Value::createValue<MaterialX::Vector2>({v, v});
|
return MaterialX::Value::createValue<MaterialX::Vector2>({v, v});
|
||||||
}
|
}
|
||||||
if (t == "vector3") {
|
if (mx_type == "vector3") {
|
||||||
return MaterialX::Value::createValue<MaterialX::Vector3>({v, v, v});
|
return MaterialX::Value::createValue<MaterialX::Vector3>({v, v, v});
|
||||||
}
|
}
|
||||||
if (t == "vector4") {
|
if (mx_type == "vector4") {
|
||||||
return MaterialX::Value::createValue<MaterialX::Vector4>({v, v, v, 1.0f});
|
return MaterialX::Value::createValue<MaterialX::Vector4>({v, v, v, 1.0f});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -547,48 +550,48 @@ MaterialX::ValuePtr NodeItem::float_to_type(float v, std::string t)
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NodeItem::adjust_types(MaterialX::ValuePtr &val1, MaterialX::ValuePtr &val2, std::string &t)
|
bool NodeItem::adjust_types(MaterialX::ValuePtr &val1, MaterialX::ValuePtr &val2, std::string &mx_type)
|
||||||
{
|
{
|
||||||
std::string t1 = val1->getTypeString();
|
std::string t1 = val1->getTypeString();
|
||||||
std::string t2 = val2->getTypeString();
|
std::string t2 = val2->getTypeString();
|
||||||
if (t1 != t2) {
|
if (t1 != t2) {
|
||||||
if (t1 == "float") {
|
if (t1 == "float") {
|
||||||
val1 = float_to_type(val1->asA<float>(), t2);
|
val1 = float_to_type(val1->asA<float>(), t2);
|
||||||
t = t2;
|
mx_type = t2;
|
||||||
}
|
}
|
||||||
else if (t2 == "float") {
|
else if (t2 == "float") {
|
||||||
val2 = float_to_type(val2->asA<float>(), t1);
|
val2 = float_to_type(val2->asA<float>(), t1);
|
||||||
t = t1;
|
mx_type = t1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
t = t1;
|
mx_type = t1;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NodeItem::adjust_types(NodeItem &val1, NodeItem &val2, std::string &t)
|
bool NodeItem::adjust_types(NodeItem &val1, NodeItem &val2, std::string &mx_type)
|
||||||
{
|
{
|
||||||
std::string t1 = val1.type();
|
std::string t1 = val1.type();
|
||||||
std::string t2 = val2.type();
|
std::string t2 = val2.type();
|
||||||
if (t1 != t2) {
|
if (t1 != t2) {
|
||||||
if (val1.value && t1 == "float") {
|
if (val1.value && t1 == "float") {
|
||||||
val1.value = float_to_type(val1.value->asA<float>(), t2);
|
val1.value = float_to_type(val1.value->asA<float>(), t2);
|
||||||
t = t2;
|
mx_type = t2;
|
||||||
}
|
}
|
||||||
else if (val2.value && t2 == "float") {
|
else if (val2.value && t2 == "float") {
|
||||||
val2.value = float_to_type(val2.value->asA<float>(), t1);
|
val2.value = float_to_type(val2.value->asA<float>(), t1);
|
||||||
t = t2;
|
mx_type = t1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
t = t1;
|
mx_type = t1;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -77,9 +77,10 @@ class NodeItem {
|
|||||||
NodeItem arithmetic(const NodeItem &other,
|
NodeItem arithmetic(const NodeItem &other,
|
||||||
const std::string &mx_category,
|
const std::string &mx_category,
|
||||||
std::function<float(float, float)> func) const;
|
std::function<float(float, float)> func) const;
|
||||||
static MaterialX::ValuePtr float_to_type(float v, std::string t);
|
static MaterialX::ValuePtr float_to_type(float v, std::string mx_type);
|
||||||
static bool adjust_types(MaterialX::ValuePtr &val1, MaterialX::ValuePtr &val2, std::string &t);
|
/* Functions for adjusting values to make equal types */
|
||||||
static bool adjust_types(NodeItem &val1, NodeItem &val2, std::string &t);
|
static bool adjust_types(MaterialX::ValuePtr &val1, MaterialX::ValuePtr &val2, std::string &mx_type);
|
||||||
|
static bool adjust_types(NodeItem &val1, NodeItem &val2, std::string &mx_type);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T> NodeItem NodeItem::val(const T &data) const
|
template<class T> NodeItem NodeItem::val(const T &data) const
|
||||||
|
Loading…
Reference in New Issue
Block a user