[Bf-blender-cvs] [ea47e50017c] temp-geometry-nodes-instances-attributes: WIP: Add instances data to spreadsheets
Hans Goudey
noreply at git.blender.org
Tue Mar 16 22:21:55 CET 2021
Commit: ea47e50017cce85ed45918402264e78c63b05d38
Author: Hans Goudey
Date: Tue Mar 16 17:21:47 2021 -0400
Branches: temp-geometry-nodes-instances-attributes
https://developer.blender.org/rBea47e50017cce85ed45918402264e78c63b05d38
WIP: Add instances data to spreadsheets
This commit does not currently compile, I'm pushing it so I can
continue work on another computer.
===================================================================
M source/blender/blenkernel/BKE_geometry_set.hh
M source/blender/blenkernel/intern/geometry_component_instances.cc
M source/blender/blenlib/BLI_float4x4.hh
M source/blender/makesrna/intern/rna_space.c
===================================================================
diff --git a/source/blender/blenkernel/BKE_geometry_set.hh b/source/blender/blenkernel/BKE_geometry_set.hh
index 632fff07575..ddc6f513177 100644
--- a/source/blender/blenkernel/BKE_geometry_set.hh
+++ b/source/blender/blenkernel/BKE_geometry_set.hh
@@ -452,7 +452,12 @@ class InstancesComponent : public GeometryComponent {
bool is_empty() const final;
+ int attribute_domain_size(const AttributeDomain domain) const final;
+
static constexpr inline GeometryComponentType static_type = GEO_COMPONENT_TYPE_INSTANCES;
+
+ private:
+ const blender::bke::ComponentAttributeProviders *get_attribute_providers() const final;
};
/** A geometry component that stores volume grids. */
diff --git a/source/blender/blenkernel/intern/geometry_component_instances.cc b/source/blender/blenkernel/intern/geometry_component_instances.cc
index 68c551645d2..db1257d2296 100644
--- a/source/blender/blenkernel/intern/geometry_component_instances.cc
+++ b/source/blender/blenkernel/intern/geometry_component_instances.cc
@@ -14,6 +14,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
+#include "BLI_float3.hh"
#include "BLI_float4x4.hh"
#include "BLI_map.hh"
#include "BLI_rand.hh"
@@ -23,13 +24,18 @@
#include "DNA_collection_types.h"
+#include "BKE_attribute_access.hh"
#include "BKE_geometry_set.hh"
+#include "attribute_access_intern.hh"
+
+using blender::float3;
using blender::float4x4;
using blender::Map;
using blender::MutableSpan;
using blender::Set;
using blender::Span;
+using blender::bke::ReadAttributePtr;
/* -------------------------------------------------------------------- */
/** \name Geometry Component Implementation
@@ -171,3 +177,111 @@ blender::Span<int> InstancesComponent::almost_unique_ids() const
}
/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Attribute Access
+ * \{ */
+
+int InstancesComponent::attribute_domain_size(const AttributeDomain domain) const
+{
+ BLI_assert(this->attribute_domain_supported(domain));
+ switch (domain) {
+ case ATTR_DOMAIN_POINT:
+ return this->instances_amount();
+ default:
+ BLI_assert(false);
+ break;
+ }
+ return 0;
+}
+
+namespace blender::bke {
+
+static float3 get_matrix_position(const float4x4 &matrix)
+{
+ return matrix.translation();
+}
+
+static void set_matrix_position(float4x4 &matrix, const float3 &translation)
+{
+ copy_v3_v3(matrix.ptr()[3], translation);
+}
+
+template<float3 (*GetFunc)(const float4x4 &), void (*SetFunc)(float4x4 &, const float3 &)>
+class Float4x4AttributeProvider final : public BuiltinAttributeProvider {
+ public:
+ Float4x4AttributeProvider(std::string attribute_name)
+ : BuiltinAttributeProvider(std::move(attribute_name),
+ ATTR_DOMAIN_POINT,
+ CD_PROP_FLOAT3,
+ NonCreatable,
+ Writable,
+ NonDeletable)
+ {
+ }
+
+ ReadAttributePtr try_get_for_read(const GeometryComponent &component) const final
+ {
+ const InstancesComponent &instances_component = static_cast<const InstancesComponent &>(
+ component);
+ if (instances_component.transforms().size() == 0) {
+ return {};
+ }
+
+ return std::make_unique<DerivedArrayReadAttribute<float4x4, float3, GetFunc>>(
+ ATTR_DOMAIN_POINT, instances_component.transforms());
+ }
+
+ WriteAttributePtr try_get_for_write(GeometryComponent &component) const final
+ {
+ const InstancesComponent &instances_component = static_cast<const InstancesComponent &>(
+ component);
+ if (instances_component.transforms().size() == 0) {
+ return {};
+ }
+
+ return std::make_unique<DerivedArrayWriteAttribute<float4x4, float3, GetFunc, SetFunc>>(
+ ATTR_DOMAIN_POINT, instances_component.transforms());
+ }
+
+ bool try_delete(GeometryComponent &UNUSED(component)) const final
+ {
+ return false;
+ }
+
+ bool try_create(GeometryComponent &UNUSED(component)) const final
+ {
+ return false;
+ }
+
+ bool exists(const GeometryComponent &component) const final
+ {
+ return component.attribute_domain_size(ATTR_DOMAIN_POINT) != 0;
+ }
+};
+
+/**
+ * In this function all the attribute providers for the instances component are created. Most data
+ * in this function is statically allocated, because it does not change over time.
+ */
+static ComponentAttributeProviders create_attribute_providers_for_instances()
+{
+ // auto get_position = [](const float4x4 &matrix) { return matrix.translation(); };
+ // auto set_position = [](float4x4 &matrix, const float3 &translation) {
+ // copy_v3_v3(matrix.ptr()[3], translation);
+ // };
+ static Float4x4AttributeProvider<get_matrix_position, set_matrix_position> position("position");
+ return ComponentAttributeProviders({&position /*, &rotation, &scale*/}, {});
+}
+
+} // namespace blender::bke
+
+const blender::bke::ComponentAttributeProviders *InstancesComponent::get_attribute_providers()
+ const
+{
+ static blender::bke::ComponentAttributeProviders providers =
+ blender::bke::create_attribute_providers_for_instances();
+ return &providers;
+}
+
+/** \} */
diff --git a/source/blender/blenlib/BLI_float4x4.hh b/source/blender/blenlib/BLI_float4x4.hh
index d6d759ccfe4..56d1a96b139 100644
--- a/source/blender/blenlib/BLI_float4x4.hh
+++ b/source/blender/blenlib/BLI_float4x4.hh
@@ -79,6 +79,11 @@ struct float4x4 {
return m * float3(v);
}
+ float3 translation() const
+ {
+ return float3(values[3]);
+ }
+
float4x4 inverted() const
{
float4x4 result;
diff --git a/source/blender/makesrna/intern/rna_space.c b/source/blender/makesrna/intern/rna_space.c
index 63600571c0d..5021db761de 100644
--- a/source/blender/makesrna/intern/rna_space.c
+++ b/source/blender/makesrna/intern/rna_space.c
@@ -2999,7 +2999,9 @@ static void rna_SpaceSpreadsheet_geometry_component_type_update(Main *UNUSED(bma
PointerRNA *ptr)
{
SpaceSpreadsheet *sspreadsheet = (SpaceSpreadsheet *)ptr->data;
- if (sspreadsheet->geometry_component_type == GEO_COMPONENT_TYPE_POINT_CLOUD) {
+ if (ELEM(sspreadsheet->geometry_component_type,
+ GEO_COMPONENT_TYPE_POINT_CLOUD,
+ GEO_COMPONENT_TYPE_INSTANCES)) {
sspreadsheet->attribute_domain = ATTR_DOMAIN_POINT;
}
}
@@ -7268,6 +7270,11 @@ static void rna_def_space_spreadsheet(BlenderRNA *brna)
ICON_POINTCLOUD_DATA,
"Point Cloud",
"Point cloud component containing only point data"},
+ {GEO_COMPONENT_TYPE_INSTANCES,
+ "INSTANCES",
+ ICON_EMPTY_AXIS,
+ "Instances",
+ "Instances of objects or collections"},
{0, NULL, 0, NULL, NULL},
};
More information about the Bf-blender-cvs
mailing list