Remove attribute in set shade smooth node

This commit is contained in:
Hans Goudey 2023-01-05 14:43:55 -05:00
parent 794d63bb7e
commit 4c30b950b6
1 changed files with 28 additions and 1 deletions

View File

@ -14,6 +14,30 @@ static void node_declare(NodeDeclarationBuilder &b)
b.add_output<decl::Geometry>(N_("Geometry")).propagate_all();
}
/**
* When the `sharp_face` attribute doesn't exist, all faces are considered smooth. If all faces
* are selected and the sharp value is a constant false value, we can remove the attribute instead,
* as an optimization to avoid propagating and storing it in the future.
*/
static bool try_removing_sharp_attribute(Mesh &mesh,
const Field<bool> &selection_field,
const Field<bool> &sharp_field)
{
if (selection_field.node().depends_on_input() || sharp_field.node().depends_on_input()) {
return false;
}
const bool selection = fn::evaluate_constant_field(selection_field);
if (!selection) {
return true;
}
const bool sharp = fn::evaluate_constant_field(sharp_field);
if (sharp) {
return false;
}
mesh.attributes_for_write().remove("sharp_face");
return true;
}
static void set_smooth(Mesh &mesh,
const Field<bool> &selection_field,
const Field<bool> &sharp_field)
@ -22,6 +46,10 @@ static void set_smooth(Mesh &mesh,
return;
}
if (try_removing_sharp_attribute(mesh, selection_field, sharp_field)) {
return;
}
MutableAttributeAccessor attributes = mesh.attributes_for_write();
AttributeWriter<bool> sharp_faces = attributes.lookup_or_add_for_write<bool>("sharp_face",
ATTR_DOMAIN_FACE);
@ -43,7 +71,6 @@ static void node_geo_exec(GeoNodeExecParams params)
geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
/* TODO: Do this with nodes instead. */
set_smooth(*mesh, selection_field, fn::invert_boolean_field(smooth_field));
}
});