From 0700441578c9bb4d3f45d59183f26d3293499113 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Tue, 9 Mar 2021 09:27:44 -0500 Subject: Geometry Nodes: Expose "shade smooth" as an attribute This patch exposes the "Shade Smooth" value as a boolean attribute. This setting is exposed as a check-box in the mesh data properties, but the value is actually stored for every face, allowing some faces to be shaded smooth with a simple per-face control. One bonus, this allows at least a workaround to the lack of control of whether meshes created by nodes are shaded smooth or not: just use an attribute fill node. Differential Revision: https://developer.blender.org/D10538 --- .../blenkernel/intern/geometry_component_mesh.cc | 38 +++++++++++++++++++++- .../blenkernel/intern/geometry_set_instances.cc | 6 ++-- 2 files changed, 41 insertions(+), 3 deletions(-) (limited to 'source/blender/blenkernel') diff --git a/source/blender/blenkernel/intern/geometry_component_mesh.cc b/source/blender/blenkernel/intern/geometry_component_mesh.cc index 03b938942a7..31809b1ffec 100644 --- a/source/blender/blenkernel/intern/geometry_component_mesh.cc +++ b/source/blender/blenkernel/intern/geometry_component_mesh.cc @@ -405,6 +405,29 @@ static void update_vertex_normals_when_dirty(const GeometryComponent &component) } } +static bool get_shade_smooth(const MPoly &mpoly) +{ + return mpoly.flag & ME_SMOOTH; +} + +static void set_shade_smooth(MPoly &mpoly, const bool &value) +{ + SET_FLAG_FROM_TEST(mpoly.flag, value, ME_SMOOTH); +} + +static ReadAttributePtr make_shade_smooth_read_attribute(const void *data, const int domain_size) +{ + return std::make_unique>( + ATTR_DOMAIN_POLYGON, Span((const MPoly *)data, domain_size)); +} + +static WriteAttributePtr make_shade_smooth_write_attribute(void *data, const int domain_size) +{ + return std::make_unique< + DerivedArrayWriteAttribute>( + ATTR_DOMAIN_POLYGON, MutableSpan((MPoly *)data, domain_size)); +} + static float2 get_loop_uv(const MLoopUV &uv) { return float2(uv.uv); @@ -680,6 +703,19 @@ static ComponentAttributeProviders create_attribute_providers_for_mesh() nullptr, nullptr); + static BuiltinCustomDataLayerProvider shade_smooth("shade_smooth", + ATTR_DOMAIN_POLYGON, + CD_PROP_BOOL, + CD_MPOLY, + BuiltinAttributeProvider::NonCreatable, + BuiltinAttributeProvider::Writable, + BuiltinAttributeProvider::NonDeletable, + polygon_access, + make_shade_smooth_read_attribute, + make_shade_smooth_write_attribute, + nullptr, + nullptr); + static BuiltinCustomDataLayerProvider vertex_normal("vertex_normal", ATTR_DOMAIN_POINT, CD_PROP_FLOAT3, @@ -713,7 +749,7 @@ static ComponentAttributeProviders create_attribute_providers_for_mesh() static CustomDataAttributeProvider edge_custom_data(ATTR_DOMAIN_EDGE, edge_access); static CustomDataAttributeProvider polygon_custom_data(ATTR_DOMAIN_POLYGON, polygon_access); - return ComponentAttributeProviders({&position, &material_index, &vertex_normal}, + return ComponentAttributeProviders({&position, &material_index, &vertex_normal, &shade_smooth}, {&uvs, &vertex_colors, &corner_custom_data, diff --git a/source/blender/blenkernel/intern/geometry_set_instances.cc b/source/blender/blenkernel/intern/geometry_set_instances.cc index 6c4c3231667..f3006385da3 100644 --- a/source/blender/blenkernel/intern/geometry_set_instances.cc +++ b/source/blender/blenkernel/intern/geometry_set_instances.cc @@ -378,8 +378,10 @@ static void join_instance_groups_mesh(Span set_groups, /* Don't copy attributes that are stored directly in the mesh data structs. */ Map attributes; - gather_attribute_info( - attributes, component_types, set_groups, {"position", "material_index", "vertex_normal"}); + gather_attribute_info(attributes, + component_types, + set_groups, + {"position", "material_index", "vertex_normal", "shade_smooth"}); join_attributes( set_groups, component_types, attributes, static_cast(dst_component)); } -- cgit v1.2.3