Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2021-09-27 11:16:38 +0300
committerJacques Lucke <jacques@blender.org>2021-09-27 11:17:17 +0300
commit617954c1438096810ce8e47f09c25c8311baac4d (patch)
tree8cbca34265f3ee5a867bbe206520309055aab20f /source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc
parent547f7d23cafb682d3ac6d1348d70caef411ecc51 (diff)
Geometry Nodes: new Instance on Points node
This adds a new Instance on Points node that is a replacement for the old Point Instance node. Contrary to the old node, it does not have a mode to instance objects or collections directly. Instead, the node has to be used with an Object/ Collection Info to achieve the same effect. Rotation and scale of the instances can be adjusted in the node directly or can be controlled with a field to get some variation between instances. The node supports placing different instances on different points. The user has control over which instance is placed on which point using an Instance Index input. If that functionality is used, the Instance Geometry has to contain multiple instances that can are instanced separately. Differential Revision: https://developer.blender.org/D12478
Diffstat (limited to 'source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc')
-rw-r--r--source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc203
1 files changed, 203 insertions, 0 deletions
diff --git a/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc b/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc
new file mode 100644
index 00000000000..21a130da8f9
--- /dev/null
+++ b/source/blender/nodes/geometry/nodes/node_geo_instance_on_points.cc
@@ -0,0 +1,203 @@
+/*
+ * 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 "DNA_collection_types.h"
+
+#include "BLI_hash.h"
+#include "BLI_task.hh"
+
+#include "UI_interface.h"
+#include "UI_resources.h"
+
+#include "node_geometry_util.hh"
+
+namespace blender::nodes {
+
+static void geo_node_instance_on_points_declare(NodeDeclarationBuilder &b)
+{
+ b.add_input<decl::Geometry>("Points").description("Points to instance on");
+ b.add_input<decl::Geometry>("Instance").description("Geometry that is instanced on the points");
+ b.add_input<decl::Bool>("Pick Instance")
+ .supports_field()
+ .description("Place different instances on different points");
+ b.add_input<decl::Int>("Instance Index")
+ .implicit_field()
+ .description(
+ "Index of the instance that used for each point. This is only used when Pick Instances "
+ "is on. By default the point index is used");
+ b.add_input<decl::Vector>("Rotation")
+ .subtype(PROP_EULER)
+ .supports_field()
+ .description("Rotation of the instances");
+ b.add_input<decl::Vector>("Scale")
+ .default_value({1.0f, 1.0f, 1.0f})
+ .supports_field()
+ .description("Scale of the instances");
+ b.add_input<decl::Int>("Stable ID")
+ .supports_field()
+ .description(
+ "ID for every instance that is used to identify it over time even when the number of "
+ "instances changes. Used for example for motion blur");
+
+ b.add_output<decl::Geometry>("Instances");
+}
+
+static void add_instances_from_component(InstancesComponent &dst_component,
+ const GeometryComponent &src_component,
+ const GeoNodeExecParams &params)
+{
+ GeometrySet instance = params.get_input<GeometrySet>("Instance");
+
+ const AttributeDomain domain = ATTR_DOMAIN_POINT;
+ const int domain_size = src_component.attribute_domain_size(domain);
+
+ /* The initial size of the component might be non-zero when this function is called for multiple
+ * component types. */
+ const int start_len = dst_component.instances_amount();
+ dst_component.resize(start_len + domain_size);
+ MutableSpan<int> dst_handles = dst_component.instance_reference_handles().slice(start_len,
+ domain_size);
+ MutableSpan<float4x4> dst_transforms = dst_component.instance_transforms().slice(start_len,
+ domain_size);
+ MutableSpan<int> dst_stable_ids = dst_component.instance_ids().slice(start_len, domain_size);
+
+ GeometryComponentFieldContext field_context{src_component, domain};
+ FieldEvaluator field_evaluator{field_context, domain_size};
+
+ const VArray<bool> *pick_instance = nullptr;
+ const VArray<int> *indices = nullptr;
+ const VArray<float3> *rotations = nullptr;
+ const VArray<float3> *scales = nullptr;
+ field_evaluator.add(params.get_input<Field<bool>>("Pick Instance"), &pick_instance);
+ field_evaluator.add(params.get_input<Field<int>>("Instance Index"), &indices);
+ field_evaluator.add(params.get_input<Field<float3>>("Rotation"), &rotations);
+ field_evaluator.add(params.get_input<Field<float3>>("Scale"), &scales);
+ field_evaluator.add_with_destination(params.get_input<Field<int>>("Stable ID"), dst_stable_ids);
+ field_evaluator.evaluate();
+
+ GVArray_Typed<float3> positions = src_component.attribute_get_for_read<float3>(
+ "position", domain, {0, 0, 0});
+
+ const InstancesComponent *src_instances = instance.get_component_for_read<InstancesComponent>();
+
+ /* Maps handles from the source instances to handles on the new instance. */
+ Array<int> handle_mapping;
+ /* Only fill #handle_mapping when it may be used below. */
+ if (src_instances != nullptr &&
+ (!pick_instance->is_single() || pick_instance->get_internal_single())) {
+ Span<InstanceReference> src_references = src_instances->references();
+ handle_mapping.reinitialize(src_references.size());
+ for (const int src_instance_handle : src_references.index_range()) {
+ const InstanceReference &reference = src_references[src_instance_handle];
+ const int dst_instance_handle = dst_component.add_reference(reference);
+ handle_mapping[src_instance_handle] = dst_instance_handle;
+ }
+ }
+
+ const int full_instance_handle = dst_component.add_reference(instance);
+ /* Add this reference last, because it is the most likely one to be removed later on. */
+ const int empty_reference_handle = dst_component.add_reference(InstanceReference());
+
+ threading::parallel_for(IndexRange(domain_size), 1024, [&](IndexRange range) {
+ for (const int i : range) {
+ /* Compute base transform for every instances. */
+ float4x4 &dst_transform = dst_transforms[i];
+ dst_transform = float4x4::from_loc_eul_scale(
+ positions[i], rotations->get(i), scales->get(i));
+
+ /* Reference that will be used by this new instance. */
+ int dst_handle = empty_reference_handle;
+
+ const bool use_individual_instance = pick_instance->get(i);
+ if (use_individual_instance) {
+ if (src_instances != nullptr) {
+ const int src_instances_amount = src_instances->instances_amount();
+ const int original_index = indices->get(i);
+ /* Use #mod_i instead of `%` to get the desirable wrap around behavior where -1 refers to
+ * the last element. */
+ const int index = mod_i(original_index, std::max(src_instances_amount, 1));
+ if (index < src_instances_amount) {
+ /* Get the reference to the source instance. */
+ const int src_handle = src_instances->instance_reference_handles()[index];
+ dst_handle = handle_mapping[src_handle];
+
+ /* Take transforms of the source instance into account. */
+ mul_m4_m4_post(dst_transform.values,
+ src_instances->instance_transforms()[index].values);
+ }
+ }
+ }
+ else {
+ /* Use entire source geometry as instance. */
+ dst_handle = full_instance_handle;
+ }
+ /* Set properties of new instance. */
+ dst_handles[i] = dst_handle;
+ }
+ });
+
+ if (pick_instance->is_single()) {
+ if (pick_instance->get_internal_single()) {
+ if (instance.has_realized_data()) {
+ params.error_message_add(
+ NodeWarningType::Info,
+ TIP_("Realized geometry is not used when pick instances is true"));
+ }
+ }
+ }
+}
+
+static void geo_node_instance_on_points_exec(GeoNodeExecParams params)
+{
+ GeometrySet geometry_set = params.extract_input<GeometrySet>("Points");
+ GeometrySet geometry_set_out;
+
+ geometry_set = geometry_set_realize_instances(geometry_set);
+
+ InstancesComponent &instances = geometry_set_out.get_component_for_write<InstancesComponent>();
+
+ if (geometry_set.has<MeshComponent>()) {
+ add_instances_from_component(
+ instances, *geometry_set.get_component_for_read<MeshComponent>(), params);
+ }
+ if (geometry_set.has<PointCloudComponent>()) {
+ add_instances_from_component(
+ instances, *geometry_set.get_component_for_read<PointCloudComponent>(), params);
+ }
+ if (geometry_set.has<CurveComponent>()) {
+ add_instances_from_component(
+ instances, *geometry_set.get_component_for_read<CurveComponent>(), params);
+ }
+
+ /* Unused references may have been added above. Remove those now so that other nodes don't
+ * process them needlessly. */
+ instances.remove_unused_references();
+
+ params.set_output("Instances", std::move(geometry_set_out));
+}
+
+} // namespace blender::nodes
+
+void register_node_type_geo_instance_on_points()
+{
+ static bNodeType ntype;
+
+ geo_node_type_base(
+ &ntype, GEO_NODE_INSTANCE_ON_POINTS, "Instance on Points", NODE_CLASS_GEOMETRY, 0);
+ ntype.declare = blender::nodes::geo_node_instance_on_points_declare;
+ ntype.geometry_node_execute = blender::nodes::geo_node_instance_on_points_exec;
+ nodeRegisterType(&ntype);
+}