This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/source/blender/nodes/geometry/nodes/node_geo_translate_instances.cc
Hans Goudey 82a46ea6f8 Geometry Nodes: Use separate field context for each geometry type
Using the same `GeometryComponentFieldContext` for all situations,
even when only one geometry type is supported is misleading, and mixes
too many different abstraction levels into code that could be simpler.
With the attribute API moved out of geometry components recently,
the "component" system is just getting in the way here.

This commit adds specific field contexts for geometry types: meshes,
curves, point clouds, and instances. There are also separate field input
helper classes, to help reduce boilerplate for fields that only support
specific geometry types.

Another benefit of this change is that it separates geometry components
from fields, which makes it easier to see the purpose of the two concepts,
and how they relate.

Because we want to be able to evaluate a field on just `CurvesGeometry`
rather than the full `Curves` data-block, the generic "geometry context"
had to be changed to avoid using `GeometryComponent`, since there is
no corresponding geometry component type. The resulting void pointer
is ugly, but only turns up in three places in practice. When Apple clang
supports `std::variant`, that could be used instead.

Differential Revision: https://developer.blender.org/D15519
2022-08-30 11:08:27 -05:00

70 lines
2.6 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later */
#include "BLI_task.hh"
#include "node_geometry_util.hh"
namespace blender::nodes::node_geo_translate_instances_cc {
static void node_declare(NodeDeclarationBuilder &b)
{
b.add_input<decl::Geometry>(N_("Instances")).only_instances();
b.add_input<decl::Bool>(N_("Selection")).default_value(true).hide_value().supports_field();
b.add_input<decl::Vector>(N_("Translation")).subtype(PROP_TRANSLATION).supports_field();
b.add_input<decl::Bool>(N_("Local Space")).default_value(true).supports_field();
b.add_output<decl::Geometry>(N_("Instances"));
}
static void translate_instances(GeoNodeExecParams &params, InstancesComponent &instances_component)
{
const bke::InstancesFieldContext context{instances_component};
fn::FieldEvaluator evaluator{context, instances_component.instances_num()};
evaluator.set_selection(params.extract_input<Field<bool>>("Selection"));
evaluator.add(params.extract_input<Field<float3>>("Translation"));
evaluator.add(params.extract_input<Field<bool>>("Local Space"));
evaluator.evaluate();
const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
const VArray<float3> translations = evaluator.get_evaluated<float3>(0);
const VArray<bool> local_spaces = evaluator.get_evaluated<bool>(1);
MutableSpan<float4x4> instance_transforms = instances_component.instance_transforms();
threading::parallel_for(selection.index_range(), 1024, [&](IndexRange range) {
for (const int i_selection : range) {
const int i = selection[i_selection];
if (local_spaces[i]) {
instance_transforms[i] *= float4x4::from_location(translations[i]);
}
else {
add_v3_v3(instance_transforms[i].values[3], translations[i]);
}
}
});
}
static void node_geo_exec(GeoNodeExecParams params)
{
GeometrySet geometry_set = params.extract_input<GeometrySet>("Instances");
if (geometry_set.has_instances()) {
InstancesComponent &instances = geometry_set.get_component_for_write<InstancesComponent>();
translate_instances(params, instances);
}
params.set_output("Instances", std::move(geometry_set));
}
} // namespace blender::nodes::node_geo_translate_instances_cc
void register_node_type_geo_translate_instances()
{
namespace file_ns = blender::nodes::node_geo_translate_instances_cc;
static bNodeType ntype;
geo_node_type_base(
&ntype, GEO_NODE_TRANSLATE_INSTANCES, "Translate Instances", NODE_CLASS_GEOMETRY);
ntype.geometry_node_execute = file_ns::node_geo_exec;
ntype.declare = file_ns::node_declare;
nodeRegisterType(&ntype);
}