[Bf-blender-cvs] [0700441578c] master: Geometry Nodes: Expose "shade smooth" as an attribute

Hans Goudey noreply at git.blender.org
Tue Mar 9 15:27:51 CET 2021


Commit: 0700441578c9bb4d3f45d59183f26d3293499113
Author: Hans Goudey
Date:   Tue Mar 9 09:27:44 2021 -0500
Branches: master
https://developer.blender.org/rB0700441578c9bb4d3f45d59183f26d3293499113

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

===================================================================

M	source/blender/blenkernel/intern/geometry_component_mesh.cc
M	source/blender/blenkernel/intern/geometry_set_instances.cc
M	source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc

===================================================================

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<DerivedArrayReadAttribute<MPoly, bool, get_shade_smooth>>(
+      ATTR_DOMAIN_POLYGON, Span<MPoly>((const MPoly *)data, domain_size));
+}
+
+static WriteAttributePtr make_shade_smooth_write_attribute(void *data, const int domain_size)
+{
+  return std::make_unique<
+      DerivedArrayWriteAttribute<MPoly, bool, get_shade_smooth, set_shade_smooth>>(
+      ATTR_DOMAIN_POLYGON, MutableSpan<MPoly>((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<GeometryInstanceGroup> set_groups,
 
   /* Don't copy attributes that are stored directly in the mesh data structs. */
   Map<std::string, AttributeKind> 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<GeometryComponent &>(dst_component));
 }
diff --git a/source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc b/source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc
index 9dce52c072d..45aaf81d63f 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc
@@ -224,7 +224,7 @@ static void join_components(Span<const MeshComponent *> src_components, Geometry
   /* Don't copy attributes that are stored directly in the mesh data structs. */
   join_attributes(to_base_components(src_components),
                   dst_component,
-                  {"position", "material_index", "vertex_normal"});
+                  {"position", "material_index", "vertex_normal", "shade_smooth"});
 }
 
 static void join_components(Span<const PointCloudComponent *> src_components, GeometrySet &result)



More information about the Bf-blender-cvs mailing list