[Bf-blender-cvs] [174014b3564] temp-modifiers-instancing: support instances in transform node

Jacques Lucke noreply at git.blender.org
Tue Nov 10 18:21:54 CET 2020


Commit: 174014b3564021ed763f0f6660802304603e425d
Author: Jacques Lucke
Date:   Tue Nov 10 18:18:51 2020 +0100
Branches: temp-modifiers-instancing
https://developer.blender.org/rB174014b3564021ed763f0f6660802304603e425d

support instances in transform node

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

M	source/blender/blenkernel/BKE_geometry_set.hh
M	source/blender/blenkernel/intern/geometry_set.cc
M	source/blender/nodes/NOD_geometry_exec.hh
M	source/blender/nodes/geometry/nodes/node_geo_transform.cc

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

diff --git a/source/blender/blenkernel/BKE_geometry_set.hh b/source/blender/blenkernel/BKE_geometry_set.hh
index f5c8fd40c3b..72d60dc917d 100644
--- a/source/blender/blenkernel/BKE_geometry_set.hh
+++ b/source/blender/blenkernel/BKE_geometry_set.hh
@@ -149,6 +149,7 @@ class GeometrySet {
   /* Utility methods for access. */
   bool has_mesh() const;
   bool has_pointcloud() const;
+  bool has_instances() const;
   const Mesh *get_mesh_for_read() const;
   const PointCloud *get_pointcloud_for_read() const;
   Mesh *get_mesh_for_write();
@@ -220,6 +221,7 @@ class InstancesComponent : public GeometryComponent {
 
   Span<const Object *> objects() const;
   Span<float3> positions() const;
+  MutableSpan<float3> positions();
   int instances_amount() const;
 
   static constexpr inline GeometryComponentType type = GeometryComponentType::Instances;
diff --git a/source/blender/blenkernel/intern/geometry_set.cc b/source/blender/blenkernel/intern/geometry_set.cc
index 48bbdff201a..cf6861cfec7 100644
--- a/source/blender/blenkernel/intern/geometry_set.cc
+++ b/source/blender/blenkernel/intern/geometry_set.cc
@@ -164,6 +164,13 @@ bool GeometrySet::has_pointcloud() const
   return component != nullptr && component->has_pointcloud();
 }
 
+/* Returns true when the geometry set has an instances component that has at least one instance. */
+bool GeometrySet::has_instances() const
+{
+  const InstancesComponent *component = this->get_component_for_read<InstancesComponent>();
+  return component != nullptr && component->instances_amount() >= 1;
+}
+
 /* Create a new geometry set that only contains the given mesh. */
 GeometrySetPtr GeometrySet::create_with_mesh(Mesh *mesh, GeometryOwnershipType ownership)
 {
@@ -423,6 +430,11 @@ Span<float3> InstancesComponent::positions() const
   return positions_;
 }
 
+MutableSpan<float3> InstancesComponent::positions()
+{
+  return positions_;
+}
+
 int InstancesComponent::instances_amount() const
 {
   BLI_assert(positions_.size() == objects_.size());
diff --git a/source/blender/nodes/NOD_geometry_exec.hh b/source/blender/nodes/NOD_geometry_exec.hh
index 668cf80fab7..db28cdbd844 100644
--- a/source/blender/nodes/NOD_geometry_exec.hh
+++ b/source/blender/nodes/NOD_geometry_exec.hh
@@ -28,6 +28,7 @@ namespace blender::nodes {
 using bke::GeometryOwnershipType;
 using bke::GeometrySet;
 using bke::GeometrySetPtr;
+using bke::InstancesComponent;
 using bke::make_geometry_set_mutable;
 using bke::MeshComponent;
 using bke::PersistentDataHandleMap;
diff --git a/source/blender/nodes/geometry/nodes/node_geo_transform.cc b/source/blender/nodes/geometry/nodes/node_geo_transform.cc
index b3c48845eb6..80c32ba8535 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_transform.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_transform.cc
@@ -35,7 +35,7 @@ static bNodeSocketTemplate geo_node_transform_out[] = {
     {-1, ""},
 };
 
-using blender::float3;
+namespace blender::nodes {
 
 static bool use_translate(const float3 rotation, const float3 scale)
 {
@@ -85,7 +85,28 @@ static void transform_pointcloud(PointCloud *pointcloud,
   }
 }
 
-namespace blender::nodes {
+static void transform_instances(InstancesComponent &instances,
+                                const float3 translation,
+                                const float3 rotation,
+                                const float3 scale)
+{
+  MutableSpan<float3> positions = instances.positions();
+
+  /* Use only translation if rotation and scale don't apply. */
+  if (use_translate(rotation, scale)) {
+    for (float3 &position : positions) {
+      add_v3_v3(position, translation);
+    }
+  }
+  else {
+    float mat[4][4];
+    loc_eul_size_to_mat4(mat, translation, rotation, scale);
+    for (float3 &position : positions) {
+      mul_m4_v3(mat, position);
+    }
+  }
+}
+
 static void geo_transform_exec(bNode *UNUSED(node), GeoNodeInputs inputs, GeoNodeOutputs outputs)
 {
   GeometrySetPtr geometry_set = inputs.extract<GeometrySetPtr>("Geometry");
@@ -111,6 +132,11 @@ static void geo_transform_exec(bNode *UNUSED(node), GeoNodeInputs inputs, GeoNod
     transform_pointcloud(pointcloud, translation, rotation, scale);
   }
 
+  if (geometry_set->has_instances()) {
+    InstancesComponent &instances = geometry_set->get_component_for_write<InstancesComponent>();
+    transform_instances(instances, translation, rotation, scale);
+  }
+
   outputs.set("Geometry", std::move(geometry_set));
 }
 }  // namespace blender::nodes



More information about the Bf-blender-cvs mailing list