From 894cc9c91592041a47c33300d784eac4e6412162 Mon Sep 17 00:00:00 2001 From: Sebastian Parborg Date: Wed, 3 Feb 2021 17:47:46 +0100 Subject: Geometry Nodes: Add Collection Info Node Implements a node to get collection objects. These objects are then passed along as instances in the node tree. Follow up tasks: Multiple nodes does not support instancing yet: T85159 Changing collection offset does not trigger a refresh: T85274 Reviewed By: Jacques, Dalai, Hans Differential Revision: http://developer.blender.org/D10151 --- release/scripts/startup/nodeitems_builtins.py | 1 + source/blender/blenkernel/BKE_node.h | 1 + source/blender/blenkernel/intern/node.cc | 1 + source/blender/editors/space_node/drawnode.c | 10 +++ source/blender/makesdna/DNA_node_types.h | 7 ++ source/blender/makesrna/intern/rna_nodetree.c | 27 ++++++ source/blender/nodes/CMakeLists.txt | 1 + source/blender/nodes/NOD_geometry.h | 1 + source/blender/nodes/NOD_static_types.h | 1 + .../geometry/nodes/node_geo_collection_info.cc | 99 ++++++++++++++++++++++ 10 files changed, 149 insertions(+) create mode 100644 source/blender/nodes/geometry/nodes/node_geo_collection_info.cc diff --git a/release/scripts/startup/nodeitems_builtins.py b/release/scripts/startup/nodeitems_builtins.py index 56f0b5c0ba4..f04a3f6eaf6 100644 --- a/release/scripts/startup/nodeitems_builtins.py +++ b/release/scripts/startup/nodeitems_builtins.py @@ -502,6 +502,7 @@ geometry_node_categories = [ ]), GeometryNodeCategory("GEO_INPUT", "Input", items=[ NodeItem("GeometryNodeObjectInfo"), + NodeItem("GeometryNodeCollectionInfo"), NodeItem("FunctionNodeRandomFloat"), NodeItem("ShaderNodeValue"), NodeItem("FunctionNodeInputVector"), diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h index bfcfe84cbe7..17f116912fa 100644 --- a/source/blender/blenkernel/BKE_node.h +++ b/source/blender/blenkernel/BKE_node.h @@ -1364,6 +1364,7 @@ int ntreeTexExecTree(struct bNodeTree *ntree, #define GEO_NODE_POINT_SCALE 1020 #define GEO_NODE_ATTRIBUTE_SAMPLE_TEXTURE 1021 #define GEO_NODE_POINTS_TO_VOLUME 1022 +#define GEO_NODE_COLLECTION_INFO 1023 /** \} */ diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc index 39e23e34341..4f9557298c7 100644 --- a/source/blender/blenkernel/intern/node.cc +++ b/source/blender/blenkernel/intern/node.cc @@ -4770,6 +4770,7 @@ static void registerGeometryNodes() register_node_type_geo_align_rotation_to_vector(); register_node_type_geo_sample_texture(); register_node_type_geo_points_to_volume(); + register_node_type_geo_collection_info(); } static void registerFunctionNodes() diff --git a/source/blender/editors/space_node/drawnode.c b/source/blender/editors/space_node/drawnode.c index 7905483fac9..1d66119d569 100644 --- a/source/blender/editors/space_node/drawnode.c +++ b/source/blender/editors/space_node/drawnode.c @@ -3374,6 +3374,13 @@ static void node_geometry_buts_points_to_volume(uiLayout *layout, uiItemR(layout, ptr, "input_type_radius", DEFAULT_FLAGS, IFACE_("Radius"), ICON_NONE); } +static void node_geometry_buts_collection_info(uiLayout *layout, + bContext *UNUSED(C), + PointerRNA *ptr) +{ + uiItemR(layout, ptr, "transform_space", UI_ITEM_R_EXPAND, NULL, ICON_NONE); +} + static void node_geometry_set_butfunc(bNodeType *ntype) { switch (ntype->type) { @@ -3434,6 +3441,9 @@ static void node_geometry_set_butfunc(bNodeType *ntype) case GEO_NODE_POINTS_TO_VOLUME: ntype->draw_buttons = node_geometry_buts_points_to_volume; break; + case GEO_NODE_COLLECTION_INFO: + ntype->draw_buttons = node_geometry_buts_collection_info; + break; } } diff --git a/source/blender/makesdna/DNA_node_types.h b/source/blender/makesdna/DNA_node_types.h index 0c92099e23b..c4d8c33ce7a 100644 --- a/source/blender/makesdna/DNA_node_types.h +++ b/source/blender/makesdna/DNA_node_types.h @@ -1193,6 +1193,13 @@ typedef struct NodeGeometryPointsToVolume { char _pad[6]; } NodeGeometryPointsToVolume; +typedef struct NodeGeometryCollectionInfo { + /* GeometryNodeTransformSpace. */ + uint8_t transform_space; + + char _pad[7]; +} NodeGeometryCollectionInfo; + /* script node mode */ #define NODE_SCRIPT_INTERNAL 0 #define NODE_SCRIPT_EXTERNAL 1 diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index a5a73d9d8ca..ab6d4fa985c 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -8911,6 +8911,33 @@ static void def_geo_points_to_volume(StructRNA *srna) RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update"); } +static void def_geo_collection_info(StructRNA *srna) +{ + PropertyRNA *prop; + + static const EnumPropertyItem rna_node_geometry_collection_info_transform_space_items[] = { + {GEO_NODE_TRANSFORM_SPACE_ORIGINAL, + "ORIGINAL", + 0, + "Original", + "Output the geometry relative to the collection offset"}, + {GEO_NODE_TRANSFORM_SPACE_RELATIVE, + "RELATIVE", + 0, + "Relative", + "Bring the input collection geometry into the modified object, maintaining the relative " + "position between the objects in the scene."}, + {0, NULL, 0, NULL, NULL}, + }; + + RNA_def_struct_sdna_from(srna, "NodeGeometryCollectionInfo", "storage"); + + prop = RNA_def_property(srna, "transform_space", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_items(prop, rna_node_geometry_collection_info_transform_space_items); + RNA_def_property_ui_text(prop, "Transform Space", "The transformation of the geometry output"); + RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update"); +} + /* -------------------------------------------------------------------------- */ static void rna_def_shader_node(BlenderRNA *brna) diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt index 405a8dcbf46..9982962cabd 100644 --- a/source/blender/nodes/CMakeLists.txt +++ b/source/blender/nodes/CMakeLists.txt @@ -150,6 +150,7 @@ set(SRC geometry/nodes/node_geo_attribute_randomize.cc geometry/nodes/node_geo_attribute_vector_math.cc geometry/nodes/node_geo_boolean.cc + geometry/nodes/node_geo_collection_info.cc geometry/nodes/node_geo_common.cc geometry/nodes/node_geo_edge_split.cc geometry/nodes/node_geo_join_geometry.cc diff --git a/source/blender/nodes/NOD_geometry.h b/source/blender/nodes/NOD_geometry.h index d78f76e0b52..178831d5d96 100644 --- a/source/blender/nodes/NOD_geometry.h +++ b/source/blender/nodes/NOD_geometry.h @@ -49,6 +49,7 @@ void register_node_type_geo_point_rotate(void); void register_node_type_geo_align_rotation_to_vector(void); void register_node_type_geo_sample_texture(void); void register_node_type_geo_points_to_volume(void); +void register_node_type_geo_collection_info(void); #ifdef __cplusplus } diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h index cc2f6a294f2..cbc5f715db0 100644 --- a/source/blender/nodes/NOD_static_types.h +++ b/source/blender/nodes/NOD_static_types.h @@ -291,6 +291,7 @@ DefNode(GeometryNode, GEO_NODE_POINT_SCALE, def_geo_point_scale, "POINT_SCALE", DefNode(GeometryNode, GEO_NODE_POINT_TRANSLATE, def_geo_point_translate, "POINT_TRANSLATE", PointTranslate, "Point Translate", "") DefNode(GeometryNode, GEO_NODE_ATTRIBUTE_SAMPLE_TEXTURE, def_geo_attribute_sample_texture, "ATTRIBUTE_SAMPLE_TEXTURE", AttributeSampleTexture, "Attribute Sample Texture", "") DefNode(GeometryNode, GEO_NODE_POINTS_TO_VOLUME, def_geo_points_to_volume, "POINTS_TO_VOLUME", PointsToVolume, "Points to Volume", "") +DefNode(GeometryNode, GEO_NODE_COLLECTION_INFO, def_geo_collection_info, "COLLECTION_INFO", CollectionInfo, "Collection Info", "") /* undefine macros */ #undef DefNode diff --git a/source/blender/nodes/geometry/nodes/node_geo_collection_info.cc b/source/blender/nodes/geometry/nodes/node_geo_collection_info.cc new file mode 100644 index 00000000000..637a117e2af --- /dev/null +++ b/source/blender/nodes/geometry/nodes/node_geo_collection_info.cc @@ -0,0 +1,99 @@ +/* + * 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_math_matrix.h" + +#include "DNA_collection_types.h" + +static bNodeSocketTemplate geo_node_collection_info_in[] = { + {SOCK_COLLECTION, N_("Collection")}, + {-1, ""}, +}; + +static bNodeSocketTemplate geo_node_collection_info_out[] = { + {SOCK_GEOMETRY, N_("Geometry")}, + {-1, ""}, +}; + +namespace blender::nodes { + +static void geo_node_collection_info_exec(GeoNodeExecParams params) +{ + bke::PersistentCollectionHandle collection_handle = + params.extract_input("Collection"); + Collection *collection = params.handle_map().lookup(collection_handle); + + GeometrySet geometry_set_out; + + if (collection == nullptr) { + params.set_output("Geometry", geometry_set_out); + return; + } + + const bNode &bnode = params.node(); + NodeGeometryCollectionInfo *node_storage = (NodeGeometryCollectionInfo *)bnode.storage; + const bool transform_space_relative = (node_storage->transform_space == + GEO_NODE_TRANSFORM_SPACE_RELATIVE); + + InstancedData instance; + instance.type = INSTANCE_DATA_TYPE_COLLECTION; + instance.data.collection = collection; + + InstancesComponent &instances = geometry_set_out.get_component_for_write(); + + float transform_mat[4][4]; + unit_m4(transform_mat); + const Object *self_object = params.self_object(); + + if (transform_space_relative) { + copy_v3_v3(transform_mat[3], collection->instance_offset); + + mul_m4_m4_pre(transform_mat, self_object->imat); + + float3 self_loc; + copy_v3_v3(self_loc, self_object->obmat[3]); + } + instances.add_instance(instance, transform_mat, -1); + + params.set_output("Geometry", geometry_set_out); +} + +static void geo_node_collection_info_node_init(bNodeTree *UNUSED(tree), bNode *node) +{ + NodeGeometryCollectionInfo *data = (NodeGeometryCollectionInfo *)MEM_callocN( + sizeof(NodeGeometryCollectionInfo), __func__); + data->transform_space = GEO_NODE_TRANSFORM_SPACE_ORIGINAL; + node->storage = data; +} + +} // namespace blender::nodes + +void register_node_type_geo_collection_info() +{ + static bNodeType ntype; + + geo_node_type_base(&ntype, GEO_NODE_COLLECTION_INFO, "Collection Info", NODE_CLASS_INPUT, 0); + node_type_socket_templates(&ntype, geo_node_collection_info_in, geo_node_collection_info_out); + node_type_init(&ntype, blender::nodes::geo_node_collection_info_node_init); + node_type_storage(&ntype, + "NodeGeometryCollectionInfo", + node_free_standard_storage, + node_copy_standard_storage); + ntype.geometry_node_execute = blender::nodes::geo_node_collection_info_exec; + nodeRegisterType(&ntype); +} -- cgit v1.2.3