From fb36767d88da13aef181be370d91f3abe3dcd60c Mon Sep 17 00:00:00 2001 From: Martijn Versteegh Date: Mon, 17 Apr 2023 23:42:45 +0200 Subject: [PATCH 1/5] Pass on color_attribute metadata through instancing, only for mesh --- source/blender/blenkernel/BKE_instances.hh | 7 ++++++- source/blender/blenkernel/intern/instances.cc | 10 ++++++++++ source/blender/geometry/intern/realize_instances.cc | 11 +++++++++++ .../geometry/nodes/node_geo_instance_on_points.cc | 5 +++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/source/blender/blenkernel/BKE_instances.hh b/source/blender/blenkernel/BKE_instances.hh index 4e191eb8153..74d1ee5389c 100644 --- a/source/blender/blenkernel/BKE_instances.hh +++ b/source/blender/blenkernel/BKE_instances.hh @@ -103,7 +103,8 @@ class Instances { mutable blender::Array almost_unique_ids_; CustomDataAttributes attributes_; - + std::string default_color_attribute_; + std::string active_color_attribute_; public: Instances() = default; Instances(const Instances &other); @@ -176,6 +177,10 @@ class Instances { bool owns_direct_data() const; void ensure_owns_direct_data(); + + void set_color_attribute_metadata(const char *default_color_attribute, const char *active_color_attribute); + const std::string &default_color_attribute_name() const { return default_color_attribute_; } + const std::string &active_color_attribute_name() const { return active_color_attribute_; } }; /* -------------------------------------------------------------------- */ diff --git a/source/blender/blenkernel/intern/instances.cc b/source/blender/blenkernel/intern/instances.cc index 8384566f4dd..5c14f12f3ba 100644 --- a/source/blender/blenkernel/intern/instances.cc +++ b/source/blender/blenkernel/intern/instances.cc @@ -330,4 +330,14 @@ Span Instances::almost_unique_ids() const return almost_unique_ids_; } +void Instances::set_color_attribute_metadata(const char *default_color_attribute, const char *active_color_attribute) +{ + if (default_color_attribute) { + default_color_attribute_ = default_color_attribute; + } + if (active_color_attribute) { + active_color_attribute_ = active_color_attribute; + } +} + } // namespace blender::bke diff --git a/source/blender/geometry/intern/realize_instances.cc b/source/blender/geometry/intern/realize_instances.cc index c7e24edcbdc..786ecbd4d97 100644 --- a/source/blender/geometry/intern/realize_instances.cc +++ b/source/blender/geometry/intern/realize_instances.cc @@ -1558,6 +1558,17 @@ bke::GeometrySet realize_instances(bke::GeometrySet geometry_set, new_geometry_set.add(*gather_info.r_tasks.first_edit_data); } + if (new_geometry_set.has_mesh()) + { + Mesh *dst_mesh = new_geometry_set.get_mesh_for_write(); + if (geometry_set.get_instances_for_read()->default_color_attribute_name().size()) { + BKE_id_attributes_default_color_set(&dst_mesh->id, geometry_set.get_instances_for_read()->default_color_attribute_name().c_str()); + } + if (geometry_set.get_instances_for_read()->active_color_attribute_name().size()) { + BKE_id_attributes_active_color_set(&dst_mesh->id, geometry_set.get_instances_for_read()->active_color_attribute_name().c_str()); + } + } + return new_geometry_set; } diff --git a/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc b/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc index dba27756be0..300ca6ee947 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc @@ -14,6 +14,7 @@ #include "BKE_attribute_math.hh" #include "BKE_instances.hh" +#include "BKE_mesh.h" #include "node_geometry_util.hh" @@ -218,6 +219,10 @@ static void node_geo_exec(GeoNodeExecParams params) instance, params, attributes_to_propagate); + if (type == GEO_COMPONENT_TYPE_MESH) { + const Mesh *src_mesh = ((MeshComponent *)geometry_set.get_component_for_read(type))->get_for_read(); + dst_instances->set_color_attribute_metadata(src_mesh->default_color_attribute, src_mesh->active_color_attribute); + } } } geometry_set.remove_geometry_during_modify(); -- 2.30.2 From f32328293f2dd97af929b55e003c646b3e3ea07d Mon Sep 17 00:00:00 2001 From: Martijn Versteegh Date: Tue, 18 Apr 2023 11:51:59 +0200 Subject: [PATCH 2/5] DNA_mesh_types.h instead of BKE_mesh.h --- .../nodes/geometry/nodes/node_geo_instance_on_points.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc b/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc index 300ca6ee947..4f01369cf02 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc @@ -14,7 +14,8 @@ #include "BKE_attribute_math.hh" #include "BKE_instances.hh" -#include "BKE_mesh.h" + +#include "DNA_mesh_types.h" #include "node_geometry_util.hh" -- 2.30.2 From 3ffc9f360cb556d0ccf48c59c06fc1e528b8996e Mon Sep 17 00:00:00 2001 From: Martijn Versteegh Date: Thu, 20 Apr 2023 19:55:01 +0200 Subject: [PATCH 3/5] If the 'instancer' mesh does not have an active/default color attribute pick the first encountered from the instanced meshes. --- .../geometry/intern/realize_instances.cc | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/source/blender/geometry/intern/realize_instances.cc b/source/blender/geometry/intern/realize_instances.cc index 786ecbd4d97..e28eccbe490 100644 --- a/source/blender/geometry/intern/realize_instances.cc +++ b/source/blender/geometry/intern/realize_instances.cc @@ -206,6 +206,8 @@ struct AllMeshesInfo { /** True if we know that there are no loose edges in any of the input meshes. */ bool no_loose_edges_hint = false; bool no_loose_verts_hint = false; + char const *active_color_attribute = nullptr; + char const *default_color_attribute = nullptr; }; struct AllCurvesInfo { @@ -902,6 +904,13 @@ static AllMeshesInfo preprocess_meshes(const bke::GeometrySet &geometry_set, info.materials.add(material); } } + if (info.default_color_attribute == nullptr) { + info.default_color_attribute = mesh->default_color_attribute; + } + if (info.active_color_attribute == nullptr) { + info.active_color_attribute = mesh->active_color_attribute; + } + } info.create_material_index_attribute |= info.materials.size() > 1; info.realize_info.reinitialize(info.order.size()); @@ -1561,11 +1570,17 @@ bke::GeometrySet realize_instances(bke::GeometrySet geometry_set, if (new_geometry_set.has_mesh()) { Mesh *dst_mesh = new_geometry_set.get_mesh_for_write(); - if (geometry_set.get_instances_for_read()->default_color_attribute_name().size()) { - BKE_id_attributes_default_color_set(&dst_mesh->id, geometry_set.get_instances_for_read()->default_color_attribute_name().c_str()); + + /* Get the default color attribute from the instance domain, this + * corresponds to the default specified on the instancer. */ + const std::string &default_color_attribute = geometry_set.get_instances_for_read()->default_color_attribute_name(); + if (default_color_attribute.size()) { + BKE_id_attributes_default_color_set(&dst_mesh->id, default_color_attribute.c_str()); } - if (geometry_set.get_instances_for_read()->active_color_attribute_name().size()) { - BKE_id_attributes_active_color_set(&dst_mesh->id, geometry_set.get_instances_for_read()->active_color_attribute_name().c_str()); + + const std::string &active_color_attribute = geometry_set.get_instances_for_read()->active_color_attribute_name(); + if (active_color_attribute.size()) { + BKE_id_attributes_active_color_set(&dst_mesh->id, active_color_attribute.c_str()); } } -- 2.30.2 From 3f83adf70f67d0faf619af2264c5768618a7bc94 Mon Sep 17 00:00:00 2001 From: Martijn Versteegh Date: Thu, 20 Apr 2023 21:10:40 +0200 Subject: [PATCH 4/5] Cleanup: simplify by using get_mesh_for_read() --- .../blender/nodes/geometry/nodes/node_geo_instance_on_points.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc b/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc index 4f01369cf02..40319cef71a 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc @@ -221,7 +221,7 @@ static void node_geo_exec(GeoNodeExecParams params) params, attributes_to_propagate); if (type == GEO_COMPONENT_TYPE_MESH) { - const Mesh *src_mesh = ((MeshComponent *)geometry_set.get_component_for_read(type))->get_for_read(); + const Mesh *src_mesh = geometry_set.get_mesh_for_read(); dst_instances->set_color_attribute_metadata(src_mesh->default_color_attribute, src_mesh->active_color_attribute); } } -- 2.30.2 From 6f84d27c61d5e3bb8ba49306acc87624e8ee9b4a Mon Sep 17 00:00:00 2001 From: Martijn Versteegh Date: Thu, 13 Jul 2023 01:46:50 +0200 Subject: [PATCH 5/5] Port geometry component type check to new method. --- .../blender/nodes/geometry/nodes/node_geo_instance_on_points.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc b/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc index 40319cef71a..07b6b06213b 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc @@ -220,7 +220,7 @@ static void node_geo_exec(GeoNodeExecParams params) instance, params, attributes_to_propagate); - if (type == GEO_COMPONENT_TYPE_MESH) { + if (type == GeometryComponent::Type::Mesh) { const Mesh *src_mesh = geometry_set.get_mesh_for_read(); dst_instances->set_color_attribute_metadata(src_mesh->default_color_attribute, src_mesh->active_color_attribute); } -- 2.30.2