[Bf-blender-cvs] [f90a83f8165] temp-geometry-nodes-fields-prototype: support field in Point Instance node

Jacques Lucke noreply at git.blender.org
Wed Jul 28 14:14:23 CEST 2021


Commit: f90a83f816551bc2e72f7232c394be915179b2a4
Author: Jacques Lucke
Date:   Wed Jul 28 12:30:56 2021 +0200
Branches: temp-geometry-nodes-fields-prototype
https://developer.blender.org/rBf90a83f816551bc2e72f7232c394be915179b2a4

support field in Point Instance node

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

M	source/blender/nodes/geometry/node_geometry_util.cc
M	source/blender/nodes/geometry/node_geometry_util.hh
M	source/blender/nodes/geometry/nodes/node_geo_attribute_fill.cc
M	source/blender/nodes/geometry/nodes/node_geo_point_instance.cc

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

diff --git a/source/blender/nodes/geometry/node_geometry_util.cc b/source/blender/nodes/geometry/node_geometry_util.cc
index 46e9d36c09c..4504edc4f79 100644
--- a/source/blender/nodes/geometry/node_geometry_util.cc
+++ b/source/blender/nodes/geometry/node_geometry_util.cc
@@ -55,6 +55,25 @@ void update_attribute_input_socket_availabilities(bNode &node,
   }
 }
 
+void prepare_field_inputs(bke::FieldInputs &field_inputs,
+                          const GeometryComponent &component,
+                          const AttributeDomain domain,
+                          Vector<std::unique_ptr<bke::FieldInputValue>> &r_values)
+{
+  for (const bke::FieldInputKey &key : field_inputs) {
+    if (const bke::AttributeFieldInputKey *attribute_key =
+            dynamic_cast<const bke::AttributeFieldInputKey *>(&key)) {
+      const StringRef name = attribute_key->name();
+      const CPPType &cpp_type = attribute_key->type();
+      const CustomDataType type = bke::cpp_type_to_custom_data_type(cpp_type);
+      GVArrayPtr attribute = component.attribute_get_for_read(name, domain, type);
+      auto value = std::make_unique<bke::GVArrayFieldInputValue>(std::move(attribute));
+      field_inputs.set_input(key, *value);
+      r_values.append(std::move(value));
+    }
+  }
+}
+
 }  // namespace blender::nodes
 
 bool geo_node_poll_default(bNodeType *UNUSED(ntype),
diff --git a/source/blender/nodes/geometry/node_geometry_util.hh b/source/blender/nodes/geometry/node_geometry_util.hh
index 956b7b8a005..b2d5a542e3b 100644
--- a/source/blender/nodes/geometry/node_geometry_util.hh
+++ b/source/blender/nodes/geometry/node_geometry_util.hh
@@ -92,4 +92,9 @@ void curve_create_default_rotation_attribute(Span<float3> tangents,
                                              Span<float3> normals,
                                              MutableSpan<float3> rotations);
 
+void prepare_field_inputs(bke::FieldInputs &field_inputs,
+                          const GeometryComponent &component,
+                          const AttributeDomain domain,
+                          Vector<std::unique_ptr<bke::FieldInputValue>> &r_values);
+
 }  // namespace blender::nodes
diff --git a/source/blender/nodes/geometry/nodes/node_geo_attribute_fill.cc b/source/blender/nodes/geometry/nodes/node_geo_attribute_fill.cc
index 16e669813c3..faf7f2316e6 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_attribute_fill.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_attribute_fill.cc
@@ -78,25 +78,6 @@ static AttributeDomain get_result_domain(const GeometryComponent &component, con
   return ATTR_DOMAIN_POINT;
 }
 
-static void prepare_field_inputs(bke::FieldInputs &field_inputs,
-                                 const GeometryComponent &component,
-                                 const AttributeDomain domain,
-                                 Vector<std::unique_ptr<bke::FieldInputValue>> &r_values)
-{
-  for (const bke::FieldInputKey &key : field_inputs) {
-    if (const bke::AttributeFieldInputKey *attribute_key =
-            dynamic_cast<const bke::AttributeFieldInputKey *>(&key)) {
-      const StringRef name = attribute_key->name();
-      const CPPType &cpp_type = attribute_key->type();
-      const CustomDataType type = bke::cpp_type_to_custom_data_type(cpp_type);
-      GVArrayPtr attribute = component.attribute_get_for_read(name, domain, type);
-      auto value = std::make_unique<bke::GVArrayFieldInputValue>(std::move(attribute));
-      field_inputs.set_input(key, *value);
-      r_values.append(std::move(value));
-    }
-  }
-}
-
 template<typename T>
 void fill_attribute_impl(GeometryComponent &component,
                          OutputAttribute &attribute,
diff --git a/source/blender/nodes/geometry/nodes/node_geo_point_instance.cc b/source/blender/nodes/geometry/nodes/node_geo_point_instance.cc
index b119b7b31e9..250e8f2d132 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_point_instance.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_point_instance.cc
@@ -38,6 +38,10 @@ static bNodeSocketTemplate geo_node_point_instance_in[] = {
      PROP_NONE,
      SOCK_HIDE_LABEL},
     {SOCK_INT, N_("Seed"), 0, 0, 0, 0, -10000, 10000},
+    {SOCK_VECTOR, N_("Position"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
+    {SOCK_VECTOR, N_("Rotation"), 0.0f, 0.0f, 0.0f, 0.0f, -10000.0f, 10000.0f, PROP_EULER},
+    {SOCK_VECTOR, N_("Scale"), 1.0f, 1.0f, 1.0f, 0.0f, -10000.0f, 10000.0f, PROP_NONE},
+    {SOCK_INT, N_("ID"), -1.0f, 0.0f, 0.0f, 0.0f, -10000.0f, 10000.0f, PROP_NONE},
     {-1, ""},
 };
 
@@ -170,13 +174,27 @@ static void add_instances_from_component(InstancesComponent &instances,
 
   const int domain_size = src_geometry.attribute_domain_size(domain);
 
-  GVArray_Typed<float3> positions = src_geometry.attribute_get_for_read<float3>(
-      "position", domain, {0, 0, 0});
-  GVArray_Typed<float3> rotations = src_geometry.attribute_get_for_read<float3>(
-      "rotation", domain, {0, 0, 0});
-  GVArray_Typed<float3> scales = src_geometry.attribute_get_for_read<float3>(
-      "scale", domain, {1, 1, 1});
-  GVArray_Typed<int> id_attribute = src_geometry.attribute_get_for_read<int>("id", domain, -1);
+  bke::FieldRef<float3> position_field = params.get_input_field<float3>("Position");
+  bke::FieldRef<float3> rotation_field = params.get_input_field<float3>("Rotation");
+  bke::FieldRef<float3> scale_field = params.get_input_field<float3>("Scale");
+  bke::FieldRef<int> id_field = params.get_input_field<int>("ID");
+
+  Vector<std::unique_ptr<bke::FieldInputValue>> all_inputs;
+
+  bke::FieldInputs position_inputs = position_field->prepare_inputs();
+  prepare_field_inputs(position_inputs, src_geometry, ATTR_DOMAIN_POINT, all_inputs);
+  bke::FieldInputs rotation_inputs = rotation_field->prepare_inputs();
+  prepare_field_inputs(rotation_inputs, src_geometry, ATTR_DOMAIN_POINT, all_inputs);
+  bke::FieldInputs scale_inputs = scale_field->prepare_inputs();
+  prepare_field_inputs(scale_inputs, src_geometry, ATTR_DOMAIN_POINT, all_inputs);
+  bke::FieldInputs id_inputs = id_field->prepare_inputs();
+  prepare_field_inputs(id_inputs, src_geometry, ATTR_DOMAIN_POINT, all_inputs);
+
+  IndexMask mask = IndexRange(domain_size);
+  bke::FieldOutput positions = position_field->evaluate(mask, position_inputs);
+  bke::FieldOutput rotations = rotation_field->evaluate(mask, rotation_inputs);
+  bke::FieldOutput scales = scale_field->evaluate(mask, scale_inputs);
+  bke::FieldOutput ids = id_field->evaluate(mask, id_inputs);
 
   /* The initial size of the component might be non-zero if there are two component types. */
   const int start_len = instances.instances_amount();
@@ -192,21 +210,40 @@ static void add_instances_from_component(InstancesComponent &instances,
     threading::parallel_for(IndexRange(domain_size), 1024, [&](IndexRange range) {
       for (const int i : range) {
         handles[i] = handle;
-        transforms[i] = float4x4::from_loc_eul_scale(positions[i], rotations[i], scales[i]);
-        instance_ids[i] = id_attribute[i];
+
+        float3 position;
+        positions.varray_ref().get(i, &position);
+        float3 rotation;
+        rotations.varray_ref().get(i, &rotation);
+        float3 scale;
+        scales.varray_ref().get(i, &scale);
+        int id;
+        ids.varray_ref().get(i, &id);
+
+        transforms[i] = float4x4::from_loc_eul_scale(position, rotation, scale);
+        instance_ids[i] = id;
       }
     });
   }
   else {
     const int seed = params.get_input<int>("Seed");
-    Array<uint32_t> ids = get_geometry_element_ids_as_uints(src_geometry, ATTR_DOMAIN_POINT);
     threading::parallel_for(IndexRange(domain_size), 1024, [&](IndexRange range) {
       for (const int i : range) {
-        const int index = BLI_hash_int_2d(ids[i], seed) % possible_handles.size();
+        float3 position;
+        positions.varray_ref().get(i, &position);
+        float3 rotation;
+        rotations.varray_ref().get(i, &rotation);
+        float3 scale;
+        scales.varray_ref().get(i, &scale);
+        int id;
+        ids.varray_ref().get(i, &id);
+
+        const int index = BLI_hash_int_2d(id, seed) % possible_handles.size();
         const int handle = possible_handles[index];
         handles[i] = handle;
-        transforms[i] = float4x4::from_loc_eul_scale(positions[i], rotations[i], scales[i]);
-        instance_ids[i] = id_attribute[i];
+
+        transforms[i] = float4x4::from_loc_eul_scale(position, rotation, scale);
+        instance_ids[i] = id;
       }
     });
   }



More information about the Bf-blender-cvs mailing list