[Bf-blender-cvs] [5c3496f3cd9] geometry-nodes: Geometry Nodes: initial Random Attribute node

Jacques Lucke noreply at git.blender.org
Wed Nov 18 12:29:28 CET 2020


Commit: 5c3496f3cd9fb46bc412177c1a60960bb31329f1
Author: Jacques Lucke
Date:   Wed Nov 18 12:28:57 2020 +0100
Branches: geometry-nodes
https://developer.blender.org/rB5c3496f3cd9fb46bc412177c1a60960bb31329f1

Geometry Nodes: initial Random Attribute node

This adds some ui boilerplate code for the Random Attribute node and
provides an initial implementation.

Note, while the implementation can already randomize attributes, it might
not behave as expected under all circumstances yet. It's still work in progress.

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

M	release/scripts/startup/nodeitems_builtins.py
M	source/blender/blenkernel/BKE_node.h
M	source/blender/blenkernel/intern/node.c
M	source/blender/editors/space_node/drawnode.c
M	source/blender/makesrna/intern/rna_nodetree.c
M	source/blender/nodes/CMakeLists.txt
M	source/blender/nodes/NOD_geometry.h
M	source/blender/nodes/NOD_static_types.h
A	source/blender/nodes/geometry/nodes/node_geo_random_attribute.cc

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

diff --git a/release/scripts/startup/nodeitems_builtins.py b/release/scripts/startup/nodeitems_builtins.py
index 1f5284eddfb..93c40ceb30a 100644
--- a/release/scripts/startup/nodeitems_builtins.py
+++ b/release/scripts/startup/nodeitems_builtins.py
@@ -499,6 +499,9 @@ geometry_node_categories = [
         NodeItem("GeometryNodePointDistribute"),
         NodeItem("GeometryNodePointInstance"),
     ]),
+    GeometryNodeCategory("GEO_ATTRIBUTES", "Attributes", items=[
+        NodeItem("GeometryNodeRandomAttribute"),
+    ]),
     GeometryNodeCategory("GEO_MATH", "Misc", items=[
         NodeItem("ShaderNodeMapRange"),
         NodeItem("ShaderNodeClamp"),
diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index 5c2346f2c94..d2cb3891d4f 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -1345,6 +1345,7 @@ int ntreeTexExecTree(struct bNodeTree *ntree,
 #define GEO_NODE_POINT_INSTANCE 1005
 #define GEO_NODE_SUBDIVISION_SURFACE 1006
 #define GEO_NODE_OBJECT_INFO 1007
+#define GEO_NODE_RANDOM_ATTRIBUTE 1008
 
 /** \} */
 
diff --git a/source/blender/blenkernel/intern/node.c b/source/blender/blenkernel/intern/node.c
index 685017eb37f..c3f5e58c307 100644
--- a/source/blender/blenkernel/intern/node.c
+++ b/source/blender/blenkernel/intern/node.c
@@ -4688,6 +4688,7 @@ static void registerGeometryNodes(void)
   register_node_type_geo_point_distribute();
   register_node_type_geo_point_instance();
   register_node_type_geo_object_info();
+  register_node_type_geo_random_attribute();
 }
 
 static void registerFunctionNodes(void)
diff --git a/source/blender/editors/space_node/drawnode.c b/source/blender/editors/space_node/drawnode.c
index fef4a1da532..6e9da7982f2 100644
--- a/source/blender/editors/space_node/drawnode.c
+++ b/source/blender/editors/space_node/drawnode.c
@@ -3163,6 +3163,14 @@ static void node_geometry_buts_triangulate(uiLayout *layout, bContext *UNUSED(C)
   uiItemR(layout, ptr, "ngon_method", DEFAULT_FLAGS, "", ICON_NONE);
 }
 
+static void node_geometry_buts_random_attribute(uiLayout *layout,
+                                                bContext *UNUSED(C),
+                                                PointerRNA *ptr)
+{
+  uiItemR(layout, ptr, "data_type", DEFAULT_FLAGS, "", ICON_NONE);
+  uiItemR(layout, ptr, "domain", DEFAULT_FLAGS, "", ICON_NONE);
+}
+
 static void node_geometry_set_butfunc(bNodeType *ntype)
 {
   switch (ntype->type) {
@@ -3175,6 +3183,9 @@ static void node_geometry_set_butfunc(bNodeType *ntype)
     case GEO_NODE_TRIANGULATE:
       ntype->draw_buttons = node_geometry_buts_triangulate;
       break;
+    case GEO_NODE_RANDOM_ATTRIBUTE:
+      ntype->draw_buttons = node_geometry_buts_random_attribute;
+      break;
   }
 }
 
diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c
index ba164684ee4..701cf78d38b 100644
--- a/source/blender/makesrna/intern/rna_nodetree.c
+++ b/source/blender/makesrna/intern/rna_nodetree.c
@@ -36,6 +36,7 @@
 #include "DNA_texture_types.h"
 
 #include "BKE_animsys.h"
+#include "BKE_attribute.h"
 #include "BKE_image.h"
 #include "BKE_node.h"
 #include "BKE_texture.h"
@@ -8258,6 +8259,30 @@ static void def_geo_triangulate(StructRNA *srna)
   RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
 }
 
+static void def_geo_attribute_create_common(StructRNA *srna)
+{
+  PropertyRNA *prop;
+
+  prop = RNA_def_property(srna, "data_type", PROP_ENUM, PROP_NONE);
+  RNA_def_property_enum_sdna(prop, NULL, "custom1");
+  RNA_def_property_enum_items(prop, rna_enum_attribute_type_items);
+  RNA_def_property_enum_default(prop, CD_PROP_FLOAT);
+  RNA_def_property_ui_text(prop, "Data Type", "Type of data stored in attribute");
+  RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
+
+  prop = RNA_def_property(srna, "domain", PROP_ENUM, PROP_NONE);
+  RNA_def_property_enum_sdna(prop, NULL, "custom2");
+  RNA_def_property_enum_items(prop, rna_enum_attribute_domain_items);
+  RNA_def_property_enum_default(prop, ATTR_DOMAIN_VERTEX);
+  RNA_def_property_ui_text(prop, "Domain", "");
+  RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
+}
+
+static void def_geo_random_attribute(StructRNA *srna)
+{
+  def_geo_attribute_create_common(srna);
+}
+
 /* -------------------------------------------------------------------------- */
 
 static void rna_def_shader_node(BlenderRNA *brna)
diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt
index 74bdd44cc7a..570fc5e8bd2 100644
--- a/source/blender/nodes/CMakeLists.txt
+++ b/source/blender/nodes/CMakeLists.txt
@@ -145,6 +145,7 @@ set(SRC
   geometry/nodes/node_geo_subdivision_surface.cc
   geometry/nodes/node_geo_point_distribute.cc
   geometry/nodes/node_geo_point_instance.cc
+  geometry/nodes/node_geo_random_attribute.cc
   geometry/nodes/node_geo_transform.cc
   geometry/nodes/node_geo_triangulate.cc
   geometry/node_geometry_exec.cc
diff --git a/source/blender/nodes/NOD_geometry.h b/source/blender/nodes/NOD_geometry.h
index 16b99070cba..9c86b02a3b1 100644
--- a/source/blender/nodes/NOD_geometry.h
+++ b/source/blender/nodes/NOD_geometry.h
@@ -34,6 +34,7 @@ void register_node_type_geo_triangulate(void);
 void register_node_type_geo_point_distribute(void);
 void register_node_type_geo_point_instance(void);
 void register_node_type_geo_object_info(void);
+void register_node_type_geo_random_attribute(void);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h
index af07ba87c84..e332dc358da 100644
--- a/source/blender/nodes/NOD_static_types.h
+++ b/source/blender/nodes/NOD_static_types.h
@@ -274,6 +274,7 @@ DefNode(GeometryNode, GEO_NODE_BOOLEAN, def_geo_boolean, "BOOLEAN", Boolean, "Bo
 DefNode(GeometryNode, GEO_NODE_POINT_DISTRIBUTE, 0, "POINT_DISTRIBUTE", PointDistribute, "Point Distribute", "")
 DefNode(GeometryNode, GEO_NODE_POINT_INSTANCE, 0, "POINT_INSTANCE", PointInstance, "Point Instance", "")
 DefNode(GeometryNode, GEO_NODE_OBJECT_INFO, 0, "OBJECT_INFO", ObjectInfo, "Object Info", "")
+DefNode(GeometryNode, GEO_NODE_RANDOM_ATTRIBUTE, def_geo_random_attribute, "RANDOM_ATTRIBUTE", RandomAttribute, "Random Attribute", "")
 
 /* undefine macros */
 #undef DefNode
diff --git a/source/blender/nodes/geometry/nodes/node_geo_random_attribute.cc b/source/blender/nodes/geometry/nodes/node_geo_random_attribute.cc
new file mode 100644
index 00000000000..f78549e828a
--- /dev/null
+++ b/source/blender/nodes/geometry/nodes/node_geo_random_attribute.cc
@@ -0,0 +1,108 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "node_geometry_util.hh"
+
+#include "BLI_rand.hh"
+
+#include "DNA_mesh_types.h"
+
+static bNodeSocketTemplate geo_node_random_attribute_in[] = {
+    {SOCK_GEOMETRY, N_("Geometry")},
+    {SOCK_STRING, N_("Attribute")},
+    {SOCK_VECTOR, N_("Min"), 0.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX},
+    {SOCK_VECTOR, N_("Max"), 1.0f, 1.0f, 1.0f, 0.0f, -FLT_MAX, FLT_MAX},
+    {SOCK_INT, N_("Seed"), 0, 0, 0, 0, -10000, 10000},
+    {-1, ""},
+};
+
+static bNodeSocketTemplate geo_node_random_attribute_out[] = {
+    {SOCK_GEOMETRY, N_("Geometry")},
+    {-1, ""},
+};
+
+static void geo_attribute_random_init(bNodeTree *UNUSED(tree), bNode *node)
+{
+  node->custom1 = CD_PROP_FLOAT;
+}
+
+namespace blender::nodes {
+
+static void geo_random_attribute_exec(GeoNodeExecParams params)
+{
+  const bNode &node = params.node();
+  const int data_type = node.custom1;
+  const AttributeDomain domain = static_cast<AttributeDomain>(node.custom2);
+
+  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
+  const std::string attribute_name = params.extract_input<std::string>("Attribute");
+  const float3 min_value = params.extract_input<float3>("Min");
+  const float3 max_value = params.extract_input<float3>("Max");
+  const int seed = params.extract_input<int>("Seed");
+
+  MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
+  Mesh *mesh = mesh_component.get_for_write();
+  if (mesh == nullptr) {
+    params.set_output("Geometry", geometry_set);
+    return;
+  }
+
+  WriteAttributePtr attribute = bke::mesh_attribute_get_for_write(mesh_component, attribute_name);
+
+  if (!attribute) {
+    BKE_id_attribute_new(&mesh->id, attribute_name.c_str(), data_type, domain, nullptr);
+    attribute = bke::mesh_attribute_get_for_write(mesh_component, attribute_name);
+  }
+
+  RandomNumberGenerator rng;
+  rng.seed_random(seed);
+
+  if (attribute) {
+    const int size = attribute->size();
+    if (attribute->cpp_type().is<float>()) {
+      FloatWriteAttribute float_attribute = std::move(attribute);
+      for (const int i : IndexRange(size)) {
+        const float value = rng.get_float() * (max_value.x - min_value.x) + min_value.x;
+        float_attribute.set(i, value);
+      }
+    }
+    else if (attribute->cpp_type().is<float3>()) {
+      Float3WriteAttribute float3_attribute = std::move(attribute);
+      for (const int i : IndexRange(size)) {
+        const float x = rng.get_float();
+        const float y = rng.get_float();
+        const float z = rng.get_float();
+        const float3 value = float3(x, y, z) * (max_value - min_value) + min_value;
+        float3_attribute.set(i, value);
+      }
+    }
+  }
+
+  params.set_output("Geometry", geometry_set);
+}
+
+}  // namespace blender::nodes
+
+void register_node_type_geo_random_attribute()
+{
+  s

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list