Parsing Improvements #14

Merged
Bogdan Nagirniak merged 5 commits from matx-parsing-improvements into matx-export-material 2023-09-07 15:10:17 +02:00
18 changed files with 64 additions and 67 deletions
Showing only changes of commit d6152cffe5 - Show all commits

View File

@ -13,7 +13,7 @@ NodeItem BrightContrastNodeParser::compute()
NodeItem contrast = get_input_value("Contrast", NodeItem::Type::Float);
/* This formula was given from OSL shader code in Cycles. */
return (bright + color * (contrast + value(1.0f)) - contrast * value(0.5f)).max(value(0.0f));
return (bright + color * (contrast + val(1.0f)) - contrast * val(0.5f)).max(val(0.0f));
}
} // namespace blender::nodes::materialx

View File

@ -16,7 +16,7 @@ NodeItem BSDFDiffuseNodeParser::compute()
NodeItem roughness = get_input_value("Roughness", NodeItem::Type::Float);
NodeItem normal = get_input_link("Normal", NodeItem::Type::Vector3);
NodeItem res = create_node("oren_nayar_diffuse_bsdf", shader_type_);
NodeItem res = create_node("oren_nayar_diffuse_bsdf", NodeItem::Type::BSDF);
res.set_input("color", color);
res.set_input("roughness", roughness);
if (normal) {

View File

@ -50,8 +50,8 @@ NodeItem BSDFPrincipledNodeParser::compute()
NodeItem tangent = get_input_link("Tangent", NodeItem::Type::Vector3);
/* Creating standard_surface */
NodeItem res = create_node("standard_surface", shader_type_);
res.set_input("base", 1.0, "float");
NodeItem res = create_node("standard_surface", NodeItem::Type::SurfaceShader);
res.set_input("base", val(1.0f));
res.set_input("base_color", base_color);
res.set_input("diffuse_roughness", roughness);
if (normal) {

View File

@ -15,7 +15,7 @@ NodeItem EmissionNodeParser::compute()
NodeItem color = get_input_value("Color", NodeItem::Type::Color3);
NodeItem strength = get_input_value("Strength", NodeItem::Type::Float);
NodeItem res = create_node("uniform_edf", shader_type_);
NodeItem res = create_node("uniform_edf", NodeItem::Type::EDF);
res.set_input("color", color * strength);
return res;
}

View File

@ -12,17 +12,17 @@ NodeItem HueSatValNodeParser::compute()
* source\blender\nodes\texture\nodes\node_texture_hueSatVal.cc */
NodeItem hue = get_input_value("Hue", NodeItem::Type::Float);
NodeItem saturation = get_input_value("Saturation", NodeItem::Type::Float);
NodeItem val = get_input_value("Value", NodeItem::Type::Float);
NodeItem value = get_input_value("Value", NodeItem::Type::Float);
NodeItem fac = get_input_value("Fac", NodeItem::Type::Float);
NodeItem color = get_input_value("Color", NodeItem::Type::Color3);
/* Modifier to follow Cycles result */
hue = hue - value(0.5f);
hue = hue - val(0.5f);
NodeItem combine = create_node("combine3", NodeItem::Type::Vector3);
combine.set_input("in1", hue);
combine.set_input("in2", saturation);
combine.set_input("in3", val);
combine.set_input("in3", value);
NodeItem res = create_node("hsvadjust", NodeItem::Type::Color3);
res.set_input("in", color);

View File

@ -35,7 +35,7 @@ NodeItem MathNodeParser::compute()
res = x.atan();
break;
case NODE_MATH_ROUND:
res = (x + value(0.5f)).floor();
res = (x + val(0.5f)).floor();
break;
case NODE_MATH_ABSOLUTE:
res = x.abs();
@ -47,13 +47,13 @@ NodeItem MathNodeParser::compute()
res = x.ceil();
break;
case NODE_MATH_FRACTION:
res = x % value(1.0f);
res = x % val(1.0f);
break;
case NODE_MATH_SQRT:
res = x.sqrt();
break;
case NODE_MATH_INV_SQRT:
res = value(1.0f) / x.sqrt();
res = val(1.0f) / x.sqrt();
break;
case NODE_MATH_SIGN:
res = x.sign();
@ -62,10 +62,10 @@ NodeItem MathNodeParser::compute()
res = x.exp();
break;
case NODE_MATH_RADIANS:
res = x * value(float(M_PI) / 180.0f);
res = x * val(float(M_PI) / 180.0f);
break;
case NODE_MATH_DEGREES:
res = x * value(180.0f * float(M_1_PI));
res = x * val(180.0f * float(M_1_PI));
break;
case NODE_MATH_SINH:
res = x.sinh();
@ -109,10 +109,10 @@ NodeItem MathNodeParser::compute()
res = x.max(y);
break;
case NODE_MATH_LESS_THAN:
res = x.if_else(NodeItem::CompareOp::Less, y, value(1.0f), value(0.0f));
res = x.if_else(NodeItem::CompareOp::Less, y, val(1.0f), val(0.0f));
break;
case NODE_MATH_GREATER_THAN:
res = x.if_else(NodeItem::CompareOp::Greater, y, value(1.0f), value(0.0f));
res = x.if_else(NodeItem::CompareOp::Greater, y, val(1.0f), val(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::CompareOp::Less, (x - y).abs(), value(1.0f), value(0.0f));
res = z.if_else(NodeItem::CompareOp::Less, (x - y).abs(), val(1.0f), val(0.0f));
break;
case NODE_MATH_MULTIPLY_ADD:
res = x * y + z;

View File

@ -17,7 +17,7 @@ NodeItem MixShaderNodeParser::compute()
NodeItem shader2 = get_input_shader(2, shader_type_);
if (shader1 && !shader2) {
res = shader1 * (value(1.0f) - fac);
res = shader1 * (val(1.0f) - fac);
}
else if (!shader1 && shader2) {
res = shader2 * fac;
@ -31,9 +31,9 @@ NodeItem MixShaderNodeParser::compute()
break;
}
case NodeItem::Type::SurfaceShader: {
res = get_input_shader(1, shader_type_);
res = get_input_shader(1, NodeItem::Type::SurfaceShader);
if (!res) {
res = get_input_shader(2, shader_type_);
res = get_input_shader(2, NodeItem::Type::SurfaceShader);
}
break;
}

View File

@ -222,9 +222,8 @@ NodeItem NodeItem::dotproduct(const NodeItem &other) const
{
NodeItem d = arithmetic(other, "dotproduct", [](float a, float b) { return a * b; });
if (d.value) {
Type mx_type = d.type();
float f = 0.0f;
switch (mx_type) {
switch (d.type()) {
case Type::Float: {
f = value->asA<float>();
break;
@ -569,18 +568,18 @@ NodeItem NodeItem::if_else(CompareOp op,
}
std::function<bool(float, float)> func = nullptr;
std::string mx_category;
std::string category;
switch (op) {
case CompareOp::Greater:
mx_category = "ifgreater";
category = "ifgreater";
func = [](float a, float b) { return a > b; };
break;
case CompareOp::GreaterEq:
mx_category = "ifgreatereq";
category = "ifgreatereq";
func = [](float a, float b) { return a >= b; };
break;
case CompareOp::Eq:
mx_category = "ifequal";
category = "ifequal";
func = [](float a, float b) { return a == b; };
break;
default:
@ -591,7 +590,7 @@ NodeItem NodeItem::if_else(CompareOp op,
res = func(value->asA<float>(), other.value->asA<float>()) ? item1 : item2;
}
else {
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, type(to_type));
res.node = graph_->addNode(category, MaterialX::EMPTY_STRING, type(to_type));
res.set_input("value1", *this);
res.set_input("value2", other);
res.set_input("in1", item1);
@ -617,11 +616,11 @@ NodeItem::Type NodeItem::type() const
return Type::Empty;
}
NodeItem NodeItem::create_node(const std::string &mx_category, NodeItem::Type type)
NodeItem NodeItem::create_node(const std::string &category, NodeItem::Type type)
{
std::string mx_type = this->type(type);
CLOG_INFO(LOG_MATERIALX_SHADER, 2, "%s, %s", mx_category.c_str(), mx_type.c_str());
node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, mx_type);
std::string type_str = this->type(type);
CLOG_INFO(LOG_MATERIALX_SHADER, 2, "<%s type=%s>", category.c_str(), type_str.c_str());
node = graph_->addNode(category, MaterialX::EMPTY_STRING, type_str);
return *this;
}
@ -629,31 +628,30 @@ void NodeItem::set_input(const std::string &in_name, const NodeItem &item)
{
if (item.value) {
Type item_type = item.type();
std::string mx_type = type(item_type);
switch (item_type) {
case Type::String:
set_input(in_name, item.value->asA<std::string>(), mx_type);
set_input(in_name, item.value->asA<std::string>(), item_type);
break;
case Type::Integer:
set_input(in_name, item.value->asA<int>(), mx_type);
set_input(in_name, item.value->asA<int>(), item_type);
break;
case Type::Float:
set_input(in_name, item.value->asA<float>(), mx_type);
set_input(in_name, item.value->asA<float>(), item_type);
break;
case Type::Vector2:
set_input(in_name, item.value->asA<MaterialX::Vector2>(), mx_type);
set_input(in_name, item.value->asA<MaterialX::Vector2>(), item_type);
break;
case Type::Vector3:
set_input(in_name, item.value->asA<MaterialX::Vector3>(), mx_type);
set_input(in_name, item.value->asA<MaterialX::Vector3>(), item_type);
break;
case Type::Vector4:
set_input(in_name, item.value->asA<MaterialX::Vector4>(), mx_type);
set_input(in_name, item.value->asA<MaterialX::Vector4>(), item_type);
break;
case Type::Color3:
set_input(in_name, item.value->asA<MaterialX::Color3>(), mx_type);
set_input(in_name, item.value->asA<MaterialX::Color3>(), item_type);
break;
case Type::Color4:
set_input(in_name, item.value->asA<MaterialX::Color4>(), mx_type);
set_input(in_name, item.value->asA<MaterialX::Color4>(), item_type);
break;
default:
BLI_assert_unreachable();

View File

@ -97,11 +97,10 @@ class NodeItem {
NodeItem empty() const;
template<class T> NodeItem val(const T &data) const;
Type type() const;
NodeItem create_node(const std::string &mx_category, NodeItem::Type type);
NodeItem create_node(const std::string &category, NodeItem::Type type);
/* Functions to set input and output */
template<class T>
void set_input(const std::string &in_name, const T &value, const std::string &in_type);
template<class T> void set_input(const std::string &in_name, const T &value, Type in_type);
void set_input(const std::string &in_name, const NodeItem &item);
void set_input_output(const std::string &in_name,
const NodeItem &item,
@ -127,9 +126,9 @@ template<class T> NodeItem NodeItem::val(const T &data) const
}
template<class T>
void NodeItem::set_input(const std::string &in_name, const T &value, const std::string &in_type)
void NodeItem::set_input(const std::string &in_name, const T &value, Type in_type)
{
node->setInputValue(in_name, value, in_type);
node->setInputValue(in_name, value, type(in_type));
}
} // namespace blender::nodes::materialx

View File

@ -39,9 +39,9 @@ std::string NodeParser::node_name()
std::string(node_->name) + "_" + socket_out_->name);
}
NodeItem NodeParser::create_node(const std::string &mx_category, NodeItem::Type type)
NodeItem NodeParser::create_node(const std::string &category, NodeItem::Type type)
{
return empty().create_node(mx_category, type);
return empty().create_node(category, type);
}
NodeItem NodeParser::get_input_default(const std::string &name, NodeItem::Type to_type)

View File

@ -33,7 +33,7 @@ class NodeParser {
protected:
virtual NodeItem compute_full();
virtual std::string node_name();
NodeItem create_node(const std::string &mx_category, NodeItem::Type type);
NodeItem create_node(const std::string &category, NodeItem::Type type);
NodeItem get_input_default(const std::string &name, NodeItem::Type to_type);
NodeItem get_input_default(int index, NodeItem::Type to_type);
NodeItem get_input_link(const std::string &name, NodeItem::Type to_type);
@ -41,7 +41,7 @@ class NodeParser {
NodeItem get_input_value(const std::string &name, NodeItem::Type to_type);
NodeItem get_input_value(int index, NodeItem::Type to_type);
NodeItem empty() const;
template<class T> NodeItem value(const T &data) const;
template<class T> NodeItem val(const T &data) const;
private:
NodeItem get_input_default(const bNodeSocket &socket, NodeItem::Type to_type);
@ -71,7 +71,7 @@ class ShaderNodeParser : public NodeParser {
NodeItem get_input_shader(const bNodeSocket &socket, NodeItem::Type shader_type);
};
template<class T> NodeItem NodeParser::value(const T &data) const
template<class T> NodeItem NodeParser::val(const T &data) const
{
return empty().val(data);
}

View File

@ -20,13 +20,13 @@ NodeItem NormalMapNodeParser::compute()
switch (normal_map_node->space) {
case SHD_SPACE_TANGENT:
res.set_input("space", value(std::string("tangent")));
res.set_input("space", val(std::string("tangent")));
break;
case SHD_SPACE_OBJECT:
res.set_input("space", value(std::string("tangent")));
res.set_input("space", val(std::string("tangent")));
break;
default:
res.set_input("space", value(default_space));
res.set_input("space", val(default_space));
CLOG_WARN(LOG_MATERIALX_SHADER,
"Ignoring unsupported Space: %d %s (%d), %s will be used",
normal_map_node->space,

View File

@ -34,9 +34,9 @@ NodeItem OutputMaterialNodeParser::compute()
}
else {
surface = create_node("standard_surface", NodeItem::Type::SurfaceShader);
surface.set_input("base_color", value(MaterialX::Color3(1.0f, 0.0f, 1.0f)));
surface.set_input("base_color", val(MaterialX::Color3(1.0f, 0.0f, 1.0f)));
}
NodeItem res = create_node("surfacematerial", shader_type_);
NodeItem res = create_node("surfacematerial", NodeItem::Type::Material);
res.set_input("surfaceshader", surface);
return res;
}
@ -45,18 +45,18 @@ NodeItem OutputMaterialNodeParser::compute_default()
{
NodeItem surface = create_node("standard_surface", NodeItem::Type::SurfaceShader);
surface.set_input("base_color",
value(MaterialX::Color3(material_->r, material_->g, material_->b)));
surface.set_input("diffuse_roughness", value(material_->roughness));
val(MaterialX::Color3(material_->r, material_->g, material_->b)));
surface.set_input("diffuse_roughness", val(material_->roughness));
if (material_->metallic > 0.0f) {
surface.set_input("metalness", value(material_->metallic));
surface.set_input("metalness", val(material_->metallic));
}
if (material_->spec) {
surface.set_input("specular", value(material_->spec));
surface.set_input("specular_color", value(material_->spec));
surface.set_input("specular_roughness", value(material_->roughness));
surface.set_input("specular", val(material_->spec));
surface.set_input("specular_color", val(material_->spec));
surface.set_input("specular_roughness", val(material_->roughness));
}
NodeItem res = create_node("surfacematerial", shader_type_);
NodeItem res = create_node("surfacematerial", NodeItem::Type::Material);
res.node->setName("Material_Default");
res.set_input("surfaceshader", surface);
return res;

View File

@ -16,9 +16,9 @@ NodeItem TexCheckerNodeParser::compute()
if (!vector) {
vector = create_node("texcoord", NodeItem::Type::Vector2);
}
vector = (vector * scale) % value(2.0f);
vector = (vector * scale) % val(2.0f);
NodeItem mix = (vector.extract(0).floor() + vector.extract(1).floor())
.if_else(NodeItem::CompareOp::Eq, value(1.0f), value(1.0f), value(0.0f));
.if_else(NodeItem::CompareOp::Eq, val(1.0f), val(1.0f), val(0.0f));
NodeItem res = create_node("mix", NodeItem::Type::Color3);
res.set_input("fg", color1);
res.set_input("bg", color2);

View File

@ -25,7 +25,7 @@ NodeItem TexEnvironmentNodeParser::compute()
NodeItem texcoord = create_node("texcoord", NodeItem::Type::Vector2);
NodeItem res = create_node("image", NodeItem::Type::Color3);
res.set_input("file", image_path, "filename");
res.set_input("file", image_path, NodeItem::Type::Filename);
res.set_input("texcoord", texcoord);
return res;
}

View File

@ -25,7 +25,7 @@ NodeItem TexImageNodeParser::compute()
NodeItem texcoord = create_node("texcoord", NodeItem::Type::Vector2);
NodeItem res = create_node("image", NodeItem::Type::Color3);
res.set_input("file", image_path, "filename");
res.set_input("file", image_path, NodeItem::Type::Filename);
res.set_input("texcoord", texcoord);
return res;
}

View File

@ -13,7 +13,7 @@ NodeItem TexNoiseNodeParser::compute()
NodeItem lacunarity = get_input_value("Lacunarity", NodeItem::Type::Float);
if (detail.value && detail.type() == NodeItem::Type::Float) {
detail = value(int(detail.value->asA<float>()));
detail = val(int(detail.value->asA<float>()));
}
NodeItem position = create_node("position", NodeItem::Type::Vector3);

View File

@ -35,7 +35,7 @@ NodeItem VectorMathNodeParser::compute()
res = x.ceil();
break;
case NODE_VECTOR_MATH_FRACTION:
res = x % value(1.0f);
res = x % val(1.0f);
break;
case NODE_VECTOR_MATH_LENGTH:
CLOG_WARN(LOG_MATERIALX_SHADER, "Unimplemented math operation %d", op);