Geometry Nodes: Propagate attributes in Instances to Points node

This is part of T92926, and is very similar to 221b7b27fc, except
slightly similar, because it only transfers from one component, and
it doesn't handle multi-threading at the moment.
This commit is contained in:
Hans Goudey
2021-11-30 23:30:35 -05:00
parent d723e331f1
commit 386b112f76

View File

@@ -14,9 +14,11 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "BKE_pointcloud.h"
#include "DNA_pointcloud_types.h"
#include "BKE_attribute_math.hh"
#include "BKE_pointcloud.h"
#include "node_geometry_util.hh"
namespace blender::nodes::node_geo_instances_to_points_cc {
@@ -76,14 +78,31 @@ static void convert_instances_to_points(GeometrySet &geometry_set,
const VArray<float> &radii = evaluator.get_evaluated<float>(1);
copy_attribute_to_points(radii, selection, {pointcloud->radius, pointcloud->totpoint});
if (!instances.instance_ids().is_empty()) {
OutputAttribute_Typed<int> id_attribute = points.attribute_try_get_for_output<int>(
"id", ATTR_DOMAIN_POINT, CD_PROP_INT32);
MutableSpan<int> ids = id_attribute.as_span();
for (const int i : selection.index_range()) {
ids[i] = instances.instance_ids()[selection[i]];
}
id_attribute.save();
Map<AttributeIDRef, AttributeKind> attributes_to_propagate;
geometry_set.gather_attributes_for_propagation({GEO_COMPONENT_TYPE_INSTANCES},
GEO_COMPONENT_TYPE_POINT_CLOUD,
false,
attributes_to_propagate);
/* These two attributes are added by the implicit inputs above. */
attributes_to_propagate.remove("position");
attributes_to_propagate.remove("radius");
for (const auto &item : attributes_to_propagate.items()) {
const AttributeIDRef &attribute_id = item.key;
const AttributeKind attribute_kind = item.value;
const GVArray src = instances.attribute_get_for_read(
attribute_id, ATTR_DOMAIN_INSTANCE, attribute_kind.data_type);
BLI_assert(src);
OutputAttribute dst = points.attribute_try_get_for_output_only(
attribute_id, ATTR_DOMAIN_POINT, attribute_kind.data_type);
BLI_assert(dst);
attribute_math::convert_to_static_type(attribute_kind.data_type, [&](auto dummy) {
using T = decltype(dummy);
copy_attribute_to_points(src.typed<T>(), selection, dst.as_span().typed<T>());
});
dst.save();
}
}