Code improvements + Mix node #30

Merged
Bogdan Nagirniak merged 18 commits from BogdanNagirniak/blender:matx-code-improvements into matx-export-material 2023-09-22 18:23:13 +02:00
6 changed files with 49 additions and 72 deletions
Showing only changes of commit c713a21851 - Show all commits

View File

@ -379,11 +379,8 @@ NodeItem NodeItem::rotate(const NodeItem &angle, const NodeItem &axis)
BLI_assert(angle.type() == Type::Float);
BLI_assert(axis.type() == Type::Vector3);
return create_node("rotate3d",
NodeItem::Type::Vector3,
{{"in", *this},
{"amount", angle},
{"axis", axis}});
return create_node(
"rotate3d", NodeItem::Type::Vector3, {{"in", *this}, {"amount", angle}, {"axis", axis}});
}
NodeItem NodeItem::rotate(const NodeItem &angle_xyz, bool invert)

View File

@ -337,7 +337,7 @@ NODE_SHADER_MATERIALX_BEGIN
NodeItem roughness = in["roughness"];
NodeItem anisotropy = in["anisotropic"];
NodeItem rotation = in["anisotropic_rotation"] * val(float(180.0f / M_PI));;
NodeItem rotation = in["anisotropic_rotation"] * val(float(180.0f / M_PI));
NodeItem base_color = in["base_color"];
NodeItem specular = in["specular"];
NodeItem coat = in["coat"];
@ -348,8 +348,7 @@ NODE_SHADER_MATERIALX_BEGIN
NodeItem n_main_tangent = empty();
if (tangent && normal) {
NodeItem n_tangent_rotate_normalize =
tangent.rotate(rotation, normal).normalize();
NodeItem n_tangent_rotate_normalize = tangent.rotate(rotation, normal).normalize();
n_main_tangent = anisotropy.if_else(
NodeItem::CompareOp::Greater, val(0.0f), n_tangent_rotate_normalize, tangent);
}

View File

@ -77,34 +77,31 @@ static void node_shader_update_mapping(bNodeTree *ntree, bNode *node)
NODE_SHADER_MATERIALX_BEGIN
#ifdef WITH_MATERIALX
{
NodeItem res = empty();
NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector3);
if (!vector) {
return empty();
}
NodeItem vector = get_input_value("Vector", NodeItem::Type::Vector3);
NodeItem scale = get_input_value("Scale", NodeItem::Type::Vector3);
NodeItem location = get_input_value("Location", NodeItem::Type::Vector3);
NodeItem rotation = get_input_value("Rotation", NodeItem::Type::Vector3) *
val(float(180.0f / M_PI));
switch (node_->custom1) {
case NODE_MAPPING_TYPE_POINT:
int type = node_->custom1;
switch (type) {
case NODE_MAPPING_TYPE_POINT: {
NodeItem location = get_input_value("Location", NodeItem::Type::Vector3);
return (vector * scale).rotate(rotation) + location;
break;
case NODE_MAPPING_TYPE_TEXTURE:
res = (vector - location).rotate(rotation, true) / scale;
break;
case NODE_MAPPING_TYPE_VECTOR:
res = (vector * scale).rotate(rotation * val(MaterialX::Vector3(1.0f, 1.0f, -1.0f)));
break;
case NODE_MAPPING_TYPE_NORMAL:
res = (vector / scale).rotate(rotation).normalize();
break;
}
case NODE_MAPPING_TYPE_TEXTURE: {
NodeItem location = get_input_value("Location", NodeItem::Type::Vector3);
return (vector - location).rotate(rotation, true) / scale;
}
case NODE_MAPPING_TYPE_VECTOR: {
return (vector * scale).rotate(rotation * val(MaterialX::Vector3(1.0f, 1.0f, -1.0f)));
}
case NODE_MAPPING_TYPE_NORMAL: {
return (vector / scale).rotate(rotation).normalize();
}
default:
BLI_assert_unreachable();
}
return res;
return empty();
}
#endif
NODE_SHADER_MATERIALX_END

View File

@ -215,61 +215,45 @@ static void node_shader_update_vector_rotate(bNodeTree *ntree, bNode *node)
NODE_SHADER_MATERIALX_BEGIN
#ifdef WITH_MATERIALX
{
NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector3);
/* Axes */
const NodeItem X = val(MaterialX::Vector3(1.0f, 0.0f, 0.0f));
const NodeItem Y = val(MaterialX::Vector3(0.0f, 1.0f, 0.0f));
const NodeItem Z = val(MaterialX::Vector3(0.0f, 0.0f, -1.0f));
if (!vector) {
return empty();
}
NodeItem angle = empty();
NodeItem axis = empty();
NodeItem center = get_input_value("Center", NodeItem::Type::Vector3) *
val(MaterialX::Vector3(1.0f, 1.0f, -1.0f));
NodeItem res = vector - center;
int mode = node_->custom1;
bool invert = node_->custom2;
NodeItem vector = get_input_value("Vector", NodeItem::Type::Vector3);
NodeItem center = get_input_value("Center", NodeItem::Type::Vector3) * (X + Y + Z);
vector = vector - center;
if (mode == NODE_VECTOR_ROTATE_TYPE_EULER_XYZ) {
angle = get_input_value("Rotation", NodeItem::Type::Vector3);
angle = angle * val(MaterialX::Vector3(1.0f, 1.0f, -1.0f));
}
else {
angle = get_input_value("Angle", NodeItem::Type::Float);
}
angle = invert ? angle * val(-1.0f) : angle;
NodeItem rotation = get_input_value("Rotation", NodeItem::Type::Vector3) *
val(float(180.0f / M_PI)) * (X + Y + Z);
return vector.rotate(invert ? -rotation : rotation, invert) + center;
}
NodeItem angle = get_input_value("Angle", NodeItem::Type::Float) * val(float(180.0f / M_PI));
NodeItem axis = empty();
switch (mode) {
case NODE_VECTOR_ROTATE_TYPE_EULER_XYZ: {
return res.rotate(angle, invert) + center;
}
case NODE_VECTOR_ROTATE_TYPE_AXIS: {
axis = get_input_value("Axis", NodeItem::Type::Vector3) *
val(MaterialX::Vector3(1.0f, 1.0f, -1.0f));
case NODE_VECTOR_ROTATE_TYPE_AXIS:
axis = get_input_value("Axis", NodeItem::Type::Vector3) * (X + Y + Z);
break;
}
case NODE_VECTOR_ROTATE_TYPE_AXIS_X: {
axis = val(MaterialX::Vector3(1.0f, 0.0f, 0.0f));
case NODE_VECTOR_ROTATE_TYPE_AXIS_X:
axis = X;
break;
}
case NODE_VECTOR_ROTATE_TYPE_AXIS_Y: {
axis = val(MaterialX::Vector3(0.0f, 1.0f, 0.0f));
case NODE_VECTOR_ROTATE_TYPE_AXIS_Y:
axis = Y;
break;
}
case NODE_VECTOR_ROTATE_TYPE_AXIS_Z: {
axis = val(MaterialX::Vector3(0.0f, 0.0f, -1.0f));
case NODE_VECTOR_ROTATE_TYPE_AXIS_Z:
axis = Z;
break;
}
default: {
default:
BLI_assert_unreachable();
return vector;
}
}
return create_node("rotate3d",
NodeItem::Type::Vector3,
{{"in", res}, {"amount", angle}, {"axis", axis}}) +
center;
return vector.rotate(invert ? -angle : angle, axis) + center;
}
#endif
NODE_SHADER_MATERIALX_END