|
|
|
@ -2,20 +2,14 @@
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
|
|
|
|
|
|
#include "GEO_join_geometries.hh"
|
|
|
|
|
#include "GEO_realize_instances.hh"
|
|
|
|
|
|
|
|
|
|
#include "DNA_collection_types.h"
|
|
|
|
|
#include "DNA_layer_types.h"
|
|
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
#include "DNA_pointcloud_types.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_listbase.h"
|
|
|
|
|
#include "BLI_math_matrix.hh"
|
|
|
|
|
#include "BLI_math_rotation.hh"
|
|
|
|
|
#include "BLI_array_utils.hh"
|
|
|
|
|
#include "BLI_noise.hh"
|
|
|
|
|
#include "BLI_task.hh"
|
|
|
|
|
|
|
|
|
|
#include "BKE_collection.hh"
|
|
|
|
|
#include "BKE_curves.hh"
|
|
|
|
|
#include "BKE_customdata.hh"
|
|
|
|
|
#include "BKE_geometry_set_instances.hh"
|
|
|
|
@ -225,6 +219,15 @@ struct AllCurvesInfo {
|
|
|
|
|
bool create_custom_normal_attribute = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct AllInstancesInfo {
|
|
|
|
|
/** store an array of void pointer to attributes for each component. */
|
|
|
|
|
Vector<AttributeFallbacksArray> attribute_fallback;
|
|
|
|
|
/** Instance components to merge for output geometry. */
|
|
|
|
|
Vector<bke::GeometryComponentPtr> instances_components_to_merge;
|
|
|
|
|
/** Base transform for each instance component. */
|
|
|
|
|
Vector<float4x4> instances_components_transforms;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Collects all tasks that need to be executed to realize all instances. */
|
|
|
|
|
struct GatherTasks {
|
|
|
|
|
Vector<RealizePointCloudTask> pointcloud_tasks;
|
|
|
|
@ -249,8 +252,15 @@ struct GatherTasksInfo {
|
|
|
|
|
const AllPointCloudsInfo &pointclouds;
|
|
|
|
|
const AllMeshesInfo &meshes;
|
|
|
|
|
const AllCurvesInfo &curves;
|
|
|
|
|
const OrderedAttributes &instances_attriubutes;
|
|
|
|
|
bool create_id_attribute_on_any_component = false;
|
|
|
|
|
|
|
|
|
|
/** Selection for top-level instances to realize. */
|
|
|
|
|
IndexMask selection;
|
|
|
|
|
|
|
|
|
|
/** Depth to realize instances for each selected top-level instance. */
|
|
|
|
|
const VArray<int> &depths;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Under some circumstances, temporary arrays need to be allocated during the gather operation.
|
|
|
|
|
* For example, when an instance attribute has to be realized as a different data type. This
|
|
|
|
@ -259,6 +269,8 @@ struct GatherTasksInfo {
|
|
|
|
|
*/
|
|
|
|
|
Vector<std::unique_ptr<GArray<>>> &r_temporary_arrays;
|
|
|
|
|
|
|
|
|
|
AllInstancesInfo instances;
|
|
|
|
|
|
|
|
|
|
/** All gathered tasks. */
|
|
|
|
|
GatherTasks r_tasks;
|
|
|
|
|
/** Current offsets while gathering tasks. */
|
|
|
|
@ -275,14 +287,18 @@ struct InstanceContext {
|
|
|
|
|
AttributeFallbacksArray meshes;
|
|
|
|
|
/** Ordered by #AllCurvesInfo.attributes. */
|
|
|
|
|
AttributeFallbacksArray curves;
|
|
|
|
|
/** Ordered by #AllInstancesInfo.attributes. */
|
|
|
|
|
AttributeFallbacksArray instances;
|
|
|
|
|
/** Id mixed from all parent instances. */
|
|
|
|
|
uint32_t id = 0;
|
|
|
|
|
|
|
|
|
|
InstanceContext(const GatherTasksInfo &gather_info)
|
|
|
|
|
: pointclouds(gather_info.pointclouds.attributes.size()),
|
|
|
|
|
meshes(gather_info.meshes.attributes.size()),
|
|
|
|
|
curves(gather_info.curves.attributes.size())
|
|
|
|
|
curves(gather_info.curves.attributes.size()),
|
|
|
|
|
instances(gather_info.instances_attriubutes.size())
|
|
|
|
|
{
|
|
|
|
|
//empty
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -304,6 +320,25 @@ static int64_t get_final_points_num(const GatherTasks &tasks)
|
|
|
|
|
return points_num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void realize_collections(Collection *collection, bke::Instances *instances)
|
|
|
|
|
{
|
|
|
|
|
LISTBASE_FOREACH (CollectionChild *, collection_child, &collection->children) {
|
|
|
|
|
float4x4 transform = float4x4::identity();
|
|
|
|
|
transform.location() += float3((collection_child->collection)->instance_offset);
|
|
|
|
|
transform.location() -= float3(collection->instance_offset);
|
|
|
|
|
const int handle = instances->add_reference(*(collection_child->collection));
|
|
|
|
|
instances->add_instance(handle, transform);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LISTBASE_FOREACH (CollectionObject *, collection_object, &collection->gobject) {
|
|
|
|
|
float4x4 transform = float4x4::identity();
|
|
|
|
|
transform.location() -= float3(collection->instance_offset);
|
|
|
|
|
transform *= (collection_object->ob)->object_to_world();
|
|
|
|
|
const int handle = instances->add_reference(*(collection_object->ob));
|
|
|
|
|
instances->add_instance(handle, transform);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void copy_transformed_positions(const Span<float3> src,
|
|
|
|
|
const float4x4 &transform,
|
|
|
|
|
MutableSpan<float3> dst)
|
|
|
|
@ -377,12 +412,12 @@ static void copy_generic_attributes_to_result(
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void create_result_ids(const RealizeInstancesOptions &options,
|
|
|
|
|
static void create_result_ids(bool keep_original_ids,
|
|
|
|
|
Span<int> stored_ids,
|
|
|
|
|
const int task_id,
|
|
|
|
|
MutableSpan<int> dst_ids)
|
|
|
|
|
{
|
|
|
|
|
if (options.keep_original_ids) {
|
|
|
|
|
if (keep_original_ids) {
|
|
|
|
|
if (stored_ids.is_empty()) {
|
|
|
|
|
dst_ids.fill(0);
|
|
|
|
|
}
|
|
|
|
@ -414,6 +449,8 @@ static void create_result_ids(const RealizeInstancesOptions &options,
|
|
|
|
|
|
|
|
|
|
/* Forward declaration. */
|
|
|
|
|
static void gather_realize_tasks_recursive(GatherTasksInfo &gather_info,
|
|
|
|
|
const int current_depth,
|
|
|
|
|
const int target_depth,
|
|
|
|
|
const bke::GeometrySet &geometry_set,
|
|
|
|
|
const float4x4 &base_transform,
|
|
|
|
|
const InstanceContext &base_instance_context);
|
|
|
|
@ -463,6 +500,38 @@ static Vector<std::pair<int, GSpan>> prepare_attribute_fallbacks(
|
|
|
|
|
return attributes_to_override;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bke::GeometrySet &geometry_set_from_reference(const InstanceReference &reference,
|
|
|
|
|
bke::GeometrySet &r_geometry_set)
|
|
|
|
|
{
|
|
|
|
|
switch (reference.type()) {
|
|
|
|
|
case InstanceReference::Type::Object: {
|
|
|
|
|
const Object &object = reference.object();
|
|
|
|
|
r_geometry_set = bke::object_get_evaluated_geometry_set(object);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case InstanceReference::Type::Collection: {
|
|
|
|
|
Collection *collection_ptr = &reference.collection();
|
|
|
|
|
std::unique_ptr<bke::Instances> instances = std::make_unique<bke::Instances>();
|
|
|
|
|
realize_collections(collection_ptr, instances.get());
|
|
|
|
|
r_geometry_set.replace_instances(instances.release());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case InstanceReference::Type::GeometrySet: {
|
|
|
|
|
r_geometry_set = reference.geometry_set();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case InstanceReference::Type::None: {
|
|
|
|
|
r_geometry_set = bke::GeometrySet(); // Return an empty GeometrySet for None type
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
r_geometry_set = bke::GeometrySet();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return r_geometry_set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calls #fn for every geometry in the given #InstanceReference. Also passes on the transformation
|
|
|
|
|
* that is applied to every instance.
|
|
|
|
@ -474,40 +543,14 @@ static void foreach_geometry_in_reference(
|
|
|
|
|
FunctionRef<void(const bke::GeometrySet &geometry_set, const float4x4 &transform, uint32_t id)>
|
|
|
|
|
fn)
|
|
|
|
|
{
|
|
|
|
|
switch (reference.type()) {
|
|
|
|
|
case InstanceReference::Type::Object: {
|
|
|
|
|
const Object &object = reference.object();
|
|
|
|
|
const bke::GeometrySet object_geometry = bke::object_get_evaluated_geometry_set(object);
|
|
|
|
|
fn(object_geometry, base_transform, id);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case InstanceReference::Type::Collection: {
|
|
|
|
|
Collection &collection = reference.collection();
|
|
|
|
|
float4x4 offset_matrix = float4x4::identity();
|
|
|
|
|
offset_matrix.location() -= collection.instance_offset;
|
|
|
|
|
int index = 0;
|
|
|
|
|
FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN (&collection, object) {
|
|
|
|
|
const bke::GeometrySet object_geometry = bke::object_get_evaluated_geometry_set(*object);
|
|
|
|
|
const float4x4 matrix = base_transform * offset_matrix * object->object_to_world();
|
|
|
|
|
const int sub_id = noise::hash(id, index);
|
|
|
|
|
fn(object_geometry, matrix, sub_id);
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case InstanceReference::Type::GeometrySet: {
|
|
|
|
|
const bke::GeometrySet &instance_geometry_set = reference.geometry_set();
|
|
|
|
|
fn(instance_geometry_set, base_transform, id);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case InstanceReference::Type::None: {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bke::GeometrySet geometry_set;
|
|
|
|
|
geometry_set_from_reference(reference, geometry_set);
|
|
|
|
|
fn(geometry_set, base_transform, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void gather_realize_tasks_for_instances(GatherTasksInfo &gather_info,
|
|
|
|
|
const int current_depth,
|
|
|
|
|
const int target_depth,
|
|
|
|
|
const Instances &instances,
|
|
|
|
|
const float4x4 &base_transform,
|
|
|
|
|
const InstanceContext &base_instance_context)
|
|
|
|
@ -532,8 +575,17 @@ static void gather_realize_tasks_for_instances(GatherTasksInfo &gather_info,
|
|
|
|
|
gather_info, instances, gather_info.meshes.attributes);
|
|
|
|
|
Vector<std::pair<int, GSpan>> curve_attributes_to_override = prepare_attribute_fallbacks(
|
|
|
|
|
gather_info, instances, gather_info.curves.attributes);
|
|
|
|
|
Vector<std::pair<int, GSpan>> instance_attributes_to_override = prepare_attribute_fallbacks(
|
|
|
|
|
gather_info, instances, gather_info.instances_attriubutes);
|
|
|
|
|
|
|
|
|
|
for (const int i : transforms.index_range()) {
|
|
|
|
|
/* If at top level, get instance indices from selection field, else use all instances. */
|
|
|
|
|
const IndexMask indices = current_depth == 0 ? gather_info.selection :
|
|
|
|
|
IndexMask(IndexRange(instances.instances_num()));
|
|
|
|
|
for (const int mask_index : indices.index_range()) {
|
|
|
|
|
const int i = indices[mask_index];
|
|
|
|
|
|
|
|
|
|
/* If at top level, retrieve depth from gather_info, else continue with target_depth. */
|
|
|
|
|
const int depth_target = current_depth == 0 ? gather_info.depths[mask_index] : target_depth;
|
|
|
|
|
const int handle = handles[i];
|
|
|
|
|
const float4x4 &transform = transforms[i];
|
|
|
|
|
const InstanceReference &reference = references[handle];
|
|
|
|
@ -549,6 +601,9 @@ static void gather_realize_tasks_for_instances(GatherTasksInfo &gather_info,
|
|
|
|
|
for (const std::pair<int, GSpan> &pair : curve_attributes_to_override) {
|
|
|
|
|
instance_context.curves.array[pair.first] = pair.second[i];
|
|
|
|
|
}
|
|
|
|
|
for (const std::pair<int, GSpan> &pair : instance_attributes_to_override) {
|
|
|
|
|
instance_context.instances.array[pair.first] = pair.second[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t local_instance_id = 0;
|
|
|
|
|
if (gather_info.create_id_attribute_on_any_component) {
|
|
|
|
@ -570,6 +625,8 @@ static void gather_realize_tasks_for_instances(GatherTasksInfo &gather_info,
|
|
|
|
|
const uint32_t id) {
|
|
|
|
|
instance_context.id = id;
|
|
|
|
|
gather_realize_tasks_recursive(gather_info,
|
|
|
|
|
current_depth + 1,
|
|
|
|
|
depth_target,
|
|
|
|
|
instance_geometry_set,
|
|
|
|
|
transform,
|
|
|
|
|
instance_context);
|
|
|
|
@ -581,6 +638,8 @@ static void gather_realize_tasks_for_instances(GatherTasksInfo &gather_info,
|
|
|
|
|
* Gather tasks for all geometries in the #geometry_set.
|
|
|
|
|
*/
|
|
|
|
|
static void gather_realize_tasks_recursive(GatherTasksInfo &gather_info,
|
|
|
|
|
const int current_depth,
|
|
|
|
|
const int target_depth,
|
|
|
|
|
const bke::GeometrySet &geometry_set,
|
|
|
|
|
const float4x4 &base_transform,
|
|
|
|
|
const InstanceContext &base_instance_context)
|
|
|
|
@ -589,9 +648,7 @@ static void gather_realize_tasks_recursive(GatherTasksInfo &gather_info,
|
|
|
|
|
const bke::GeometryComponent::Type type = component->type();
|
|
|
|
|
switch (type) {
|
|
|
|
|
case bke::GeometryComponent::Type::Mesh: {
|
|
|
|
|
const bke::MeshComponent &mesh_component = *static_cast<const bke::MeshComponent *>(
|
|
|
|
|
component);
|
|
|
|
|
const Mesh *mesh = mesh_component.get();
|
|
|
|
|
const Mesh *mesh = (*static_cast<const bke::MeshComponent *>(component)).get();
|
|
|
|
|
if (mesh != nullptr && mesh->verts_num > 0) {
|
|
|
|
|
const int mesh_index = gather_info.meshes.order.index_of(mesh);
|
|
|
|
|
const MeshRealizeInfo &mesh_info = gather_info.meshes.realize_info[mesh_index];
|
|
|
|
@ -641,17 +698,29 @@ static void gather_realize_tasks_recursive(GatherTasksInfo &gather_info,
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case bke::GeometryComponent::Type::Instance: {
|
|
|
|
|
const auto &instances_component = *static_cast<const bke::InstancesComponent *>(component);
|
|
|
|
|
const Instances *instances = instances_component.get();
|
|
|
|
|
if (current_depth == target_depth) {
|
|
|
|
|
gather_info.instances.attribute_fallback.append(base_instance_context.instances);
|
|
|
|
|
gather_info.instances.instances_components_to_merge.append(component->copy());
|
|
|
|
|
gather_info.instances.instances_components_transforms.append(base_transform);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const Instances *instances =
|
|
|
|
|
(*static_cast<const bke::InstancesComponent *>(component)).get();
|
|
|
|
|
if (instances != nullptr && instances->instances_num() > 0) {
|
|
|
|
|
gather_realize_tasks_for_instances(
|
|
|
|
|
gather_info, *instances, base_transform, base_instance_context);
|
|
|
|
|
gather_realize_tasks_for_instances(gather_info,
|
|
|
|
|
current_depth,
|
|
|
|
|
target_depth,
|
|
|
|
|
*instances,
|
|
|
|
|
base_transform,
|
|
|
|
|
base_instance_context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case bke::GeometryComponent::Type::Volume: {
|
|
|
|
|
const auto *volume_component = static_cast<const bke::VolumeComponent *>(component);
|
|
|
|
|
if (!gather_info.r_tasks.first_volume) {
|
|
|
|
|
const bke::VolumeComponent *volume_component = static_cast<const bke::VolumeComponent *>(
|
|
|
|
|
component);
|
|
|
|
|
volume_component->add_user();
|
|
|
|
|
gather_info.r_tasks.first_volume = ImplicitSharingPtr<const bke::VolumeComponent>(
|
|
|
|
|
volume_component);
|
|
|
|
@ -659,9 +728,9 @@ static void gather_realize_tasks_recursive(GatherTasksInfo &gather_info,
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case bke::GeometryComponent::Type::Edit: {
|
|
|
|
|
const auto *edit_component = static_cast<const bke::GeometryComponentEditData *>(
|
|
|
|
|
component);
|
|
|
|
|
if (!gather_info.r_tasks.first_edit_data) {
|
|
|
|
|
const bke::GeometryComponentEditData *edit_component =
|
|
|
|
|
static_cast<const bke::GeometryComponentEditData *>(component);
|
|
|
|
|
edit_component->add_user();
|
|
|
|
|
gather_info.r_tasks.first_edit_data =
|
|
|
|
|
ImplicitSharingPtr<const bke::GeometryComponentEditData>(edit_component);
|
|
|
|
@ -676,6 +745,278 @@ static void gather_realize_tasks_recursive(GatherTasksInfo &gather_info,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This function iterates through a set of geometries, applying a callback to each attribute of
|
|
|
|
|
* eligible children based on specified conditions. Attributes should not be removed or added
|
|
|
|
|
* by the callback. Relevant children are determined by three criteria: the component type
|
|
|
|
|
* (e.g., mesh, curve), a depth value greater than 0 and a selection. If the primary component
|
|
|
|
|
* is an instance, the condition is true only when the depth is exactly 0. Additionally, the
|
|
|
|
|
* function extends its operation to instances if any of their nested children meet the first
|
|
|
|
|
* condition.
|
|
|
|
|
*
|
|
|
|
|
* Based on bke::GeometrySet::attribute_foreach
|
|
|
|
|
*/
|
|
|
|
|
static bool attribute_foreach(const bke::GeometrySet &geometry_set,
|
|
|
|
|
const Span<bke::GeometryComponent::Type> component_types,
|
|
|
|
|
const int current_depth,
|
|
|
|
|
const int depth_target,
|
|
|
|
|
const VArray<int> instance_depth,
|
|
|
|
|
const IndexMask selection,
|
|
|
|
|
const bke::GeometrySet::AttributeForeachCallback callback)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/*Initialize flag to track if child instances have the specified components.*/
|
|
|
|
|
bool child_has_component = true;
|
|
|
|
|
|
|
|
|
|
if (geometry_set.has_instances()) {
|
|
|
|
|
child_has_component = false;
|
|
|
|
|
|
|
|
|
|
const Instances &instances = *geometry_set.get_instances();
|
|
|
|
|
const IndexMask indices = (0 == current_depth) ?
|
|
|
|
|
selection :
|
|
|
|
|
IndexMask(IndexRange(instances.instances_num()));
|
|
|
|
|
for (const int index : indices.index_range()) {
|
|
|
|
|
const int i = indices[index];
|
|
|
|
|
const int depth_target_tmp = (0 == current_depth) ? instance_depth[i] : depth_target;
|
|
|
|
|
bke::GeometrySet instance_geometry_set;
|
|
|
|
|
geometry_set_from_reference(instances.references()[instances.reference_handles()[i]],
|
|
|
|
|
instance_geometry_set);
|
|
|
|
|
/*Process child instances with a recursive call.*/
|
|
|
|
|
if (current_depth != depth_target_tmp) {
|
|
|
|
|
child_has_component = child_has_component | attribute_foreach(instance_geometry_set,
|
|
|
|
|
component_types,
|
|
|
|
|
current_depth + 1,
|
|
|
|
|
depth_target_tmp,
|
|
|
|
|
instance_depth,
|
|
|
|
|
selection,
|
|
|
|
|
callback);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*Flag to track if any relevant attributes were found.*/
|
|
|
|
|
bool is_relevant = false;
|
|
|
|
|
|
|
|
|
|
for (const bke::GeometryComponent::Type component_type : component_types) {
|
|
|
|
|
if (geometry_set.has(component_type)) {
|
|
|
|
|
/*Check if the current instance components is the main one*/
|
|
|
|
|
const bool is_special_instance = (bke::GeometryComponent::Type::Instance ==
|
|
|
|
|
component_type) &&
|
|
|
|
|
(component_types.size() > 1);
|
|
|
|
|
if (!is_special_instance || child_has_component) {
|
|
|
|
|
/*Process attributes for the current component.*/
|
|
|
|
|
const bke::GeometryComponent &component = *geometry_set.get_component(component_type);
|
|
|
|
|
const std::optional<bke::AttributeAccessor> attributes = component.attributes();
|
|
|
|
|
if (attributes.has_value()) {
|
|
|
|
|
attributes->for_all(
|
|
|
|
|
[&](const AttributeIDRef &attributeId, const AttributeMetaData &metaData) {
|
|
|
|
|
callback(attributeId, metaData, component);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
is_relevant = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return is_relevant;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Based on bke::GeometrySet::gather_attributes_for_propagation.
|
|
|
|
|
* Specialized for Specialized attribute_foreach to get:
|
|
|
|
|
* current_depth, depth_target, instance_depth and selection.
|
|
|
|
|
*/
|
|
|
|
|
void static gather_attributes_for_propagation(
|
|
|
|
|
bke::GeometrySet re_geometry_set,
|
|
|
|
|
const Span<bke::GeometryComponent::Type> component_types,
|
|
|
|
|
const bke::GeometryComponent::Type dst_component_type,
|
|
|
|
|
const VArray<int> instance_depth,
|
|
|
|
|
const IndexMask selection,
|
|
|
|
|
const bke::AnonymousAttributePropagationInfo &propagation_info,
|
|
|
|
|
Map<AttributeIDRef, AttributeKind> &r_attributes)
|
|
|
|
|
{
|
|
|
|
|
/* Only needed right now to check if an attribute is built-in on this component type.
|
|
|
|
|
* TODO: Get rid of the dummy component. */
|
|
|
|
|
const bke::GeometryComponentPtr dummy_component = bke::GeometryComponent::create(
|
|
|
|
|
dst_component_type);
|
|
|
|
|
attribute_foreach(re_geometry_set,
|
|
|
|
|
component_types,
|
|
|
|
|
0,
|
|
|
|
|
VariedDepthOption::MAX_DEPTH,
|
|
|
|
|
instance_depth,
|
|
|
|
|
selection,
|
|
|
|
|
[&](const AttributeIDRef &attribute_id,
|
|
|
|
|
const AttributeMetaData &meta_data,
|
|
|
|
|
const bke::GeometryComponent &component) {
|
|
|
|
|
if (component.attributes()->is_builtin(attribute_id)) {
|
|
|
|
|
if (!dummy_component->attributes()->is_builtin(attribute_id)) {
|
|
|
|
|
/* Don't propagate built-in attributes that are not built-in on the
|
|
|
|
|
* destination component. */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (meta_data.data_type == CD_PROP_STRING) {
|
|
|
|
|
/* Propagating string attributes is not supported yet. */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (attribute_id.is_anonymous() &&
|
|
|
|
|
!propagation_info.propagate(attribute_id.anonymous_id())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AttrDomain domain = meta_data.domain;
|
|
|
|
|
if (dst_component_type != bke::GeometryComponent::Type::Instance &&
|
|
|
|
|
domain == AttrDomain::Instance) {
|
|
|
|
|
domain = AttrDomain::Point;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto add_info = [&](AttributeKind *attribute_kind) {
|
|
|
|
|
attribute_kind->domain = domain;
|
|
|
|
|
attribute_kind->data_type = meta_data.data_type;
|
|
|
|
|
};
|
|
|
|
|
auto modify_info = [&](AttributeKind *attribute_kind) {
|
|
|
|
|
attribute_kind->domain = bke::attribute_domain_highest_priority(
|
|
|
|
|
{attribute_kind->domain, domain});
|
|
|
|
|
attribute_kind->data_type = bke::attribute_data_type_highest_complexity(
|
|
|
|
|
{attribute_kind->data_type, meta_data.data_type});
|
|
|
|
|
};
|
|
|
|
|
r_attributes.add_or_modify(attribute_id, add_info, modify_info);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Instance
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static OrderedAttributes gather_generic_instance_attributes_to_propagate(
|
|
|
|
|
const bke::GeometrySet &in_geometry_set,
|
|
|
|
|
const RealizeInstancesOptions &options,
|
|
|
|
|
const VariedDepthOption &varied_depth_option)
|
|
|
|
|
{
|
|
|
|
|
Vector<bke::GeometryComponent::Type> src_component_types;
|
|
|
|
|
src_component_types.append(bke::GeometryComponent::Type::Instance);
|
|
|
|
|
|
|
|
|
|
Map<AttributeIDRef, AttributeKind> attributes_to_propagate;
|
|
|
|
|
gather_attributes_for_propagation(in_geometry_set,
|
|
|
|
|
src_component_types,
|
|
|
|
|
bke::GeometryComponent::Type::Instance,
|
|
|
|
|
varied_depth_option.depths,
|
|
|
|
|
varied_depth_option.selection,
|
|
|
|
|
options.propagation_info,
|
|
|
|
|
attributes_to_propagate);
|
|
|
|
|
attributes_to_propagate.remove("position");
|
|
|
|
|
attributes_to_propagate.remove("radius");
|
|
|
|
|
attributes_to_propagate.pop_try("id").has_value();
|
|
|
|
|
OrderedAttributes ordered_attributes;
|
|
|
|
|
for (const auto item : attributes_to_propagate.items()) {
|
|
|
|
|
ordered_attributes.ids.add_new(item.key);
|
|
|
|
|
ordered_attributes.kinds.append(item.value);
|
|
|
|
|
}
|
|
|
|
|
return ordered_attributes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void execute_instances_tasks(
|
|
|
|
|
const Span<bke::GeometryComponentPtr> src_components,
|
|
|
|
|
Span<blender::float4x4> src_base_transforms,
|
|
|
|
|
OrderedAttributes all_instances_attributes,
|
|
|
|
|
Span<blender::geometry::AttributeFallbacksArray> attribute_fallback,
|
|
|
|
|
bke::GeometrySet &r_realized_geometry)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(src_components.size() == src_base_transforms.size() &&
|
|
|
|
|
src_components.size() == attribute_fallback.size());
|
|
|
|
|
if (src_components.is_empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VArray<blender::float4x4>::ForSpan(src_base_transforms);
|
|
|
|
|
Array<int> offsets_data(src_components.size() + 1);
|
|
|
|
|
for (const int component_index : src_components.index_range()) {
|
|
|
|
|
const bke::InstancesComponent &src_component = static_cast<const bke::InstancesComponent &>(
|
|
|
|
|
*src_components[component_index]);
|
|
|
|
|
offsets_data[component_index] = src_component.get()->instances_num();
|
|
|
|
|
}
|
|
|
|
|
const OffsetIndices offsets = offset_indices::accumulate_counts_to_offsets(offsets_data);
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<bke::Instances> dst_instances = std::make_unique<bke::Instances>();
|
|
|
|
|
dst_instances->resize(offsets.total_size());
|
|
|
|
|
|
|
|
|
|
/* Prepare generic output attributes. */
|
|
|
|
|
for (const int attribute_index : all_instances_attributes.index_range()) {
|
|
|
|
|
bke::AttrDomain domain = bke::AttrDomain::Instance;
|
|
|
|
|
bke::AttributeIDRef id = all_instances_attributes.ids[attribute_index];
|
|
|
|
|
eCustomDataType type = all_instances_attributes.kinds[attribute_index].data_type;
|
|
|
|
|
blender::bke::MutableAttributeAccessor attr = dst_instances->attributes_for_write();
|
|
|
|
|
attr.lookup_or_add_for_write_only_span(id, domain, type).finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MutableSpan<float4x4> all_transforms = dst_instances->transforms_for_write();
|
|
|
|
|
MutableSpan<int> all_handles = dst_instances->reference_handles_for_write();
|
|
|
|
|
|
|
|
|
|
for (const int component_index : src_components.index_range()) {
|
|
|
|
|
const bke::InstancesComponent &src_component = static_cast<const bke::InstancesComponent &>(
|
|
|
|
|
*src_components[component_index]);
|
|
|
|
|
const bke::Instances &src_instances = *src_component.get();
|
|
|
|
|
const blender::float4x4 src_base_transform = src_base_transforms[component_index];
|
|
|
|
|
const Array<const void *> attribute_fallback_array = attribute_fallback[component_index].array;
|
|
|
|
|
const Span<bke::InstanceReference> src_references = src_instances.references();
|
|
|
|
|
Array<int> handle_map(src_references.size());
|
|
|
|
|
|
|
|
|
|
for (const int src_handle : src_references.index_range()) {
|
|
|
|
|
handle_map[src_handle] = dst_instances->add_reference(src_references[src_handle]);
|
|
|
|
|
}
|
|
|
|
|
const IndexRange dst_range = offsets[component_index];
|
|
|
|
|
for (const int attribute_index : all_instances_attributes.index_range()) {
|
|
|
|
|
bke::AttributeIDRef id = all_instances_attributes.ids[attribute_index];
|
|
|
|
|
eCustomDataType type = all_instances_attributes.kinds[attribute_index].data_type;
|
|
|
|
|
const CPPType *cpp_type = bke::custom_data_type_to_cpp_type(type);
|
|
|
|
|
BLI_assert(cpp_type != nullptr);
|
|
|
|
|
bke::GSpanAttributeWriter write_attribute =
|
|
|
|
|
dst_instances->attributes_for_write().lookup_for_write_span(id);
|
|
|
|
|
GMutableSpan dst_span = write_attribute.span;
|
|
|
|
|
|
|
|
|
|
if (!write_attribute) { // do not override existing attributes
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const void *attribute_ptr;
|
|
|
|
|
if (attribute_fallback_array[attribute_index] != nullptr) {
|
|
|
|
|
attribute_ptr = attribute_fallback_array[attribute_index];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
attribute_ptr = cpp_type->default_value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GVArray src_span = GVArray::ForSingle(*cpp_type, dst_range.size(), attribute_ptr);
|
|
|
|
|
array_utils::copy(src_span, dst_span.slice(dst_range));
|
|
|
|
|
write_attribute.finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Span<int> src_handles = src_instances.reference_handles();
|
|
|
|
|
array_utils::gather(handle_map.as_span(), src_handles, all_handles.slice(dst_range));
|
|
|
|
|
array_utils::copy(src_instances.transforms(), all_transforms.slice(dst_range));
|
|
|
|
|
|
|
|
|
|
for (blender::float4x4 &transfrom : all_transforms.slice(dst_range)) {
|
|
|
|
|
transfrom *= src_base_transform;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r_realized_geometry.replace_instances(dst_instances.release());
|
|
|
|
|
auto &dst_component = r_realized_geometry.get_component_for_write<bke::InstancesComponent>();
|
|
|
|
|
|
|
|
|
|
Vector<const bke::GeometryComponent *> for_join_attributes;
|
|
|
|
|
for (bke::GeometryComponentPtr compent : src_components) {
|
|
|
|
|
for_join_attributes.append(compent.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
join_attributes(
|
|
|
|
|
for_join_attributes, dst_component, {"position", ".reference_index", "instance_transform"});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
@ -685,6 +1026,7 @@ static void gather_realize_tasks_recursive(GatherTasksInfo &gather_info,
|
|
|
|
|
static OrderedAttributes gather_generic_pointcloud_attributes_to_propagate(
|
|
|
|
|
const bke::GeometrySet &in_geometry_set,
|
|
|
|
|
const RealizeInstancesOptions &options,
|
|
|
|
|
const VariedDepthOption &varied_depth_option,
|
|
|
|
|
bool &r_create_radii,
|
|
|
|
|
bool &r_create_id)
|
|
|
|
|
{
|
|
|
|
@ -695,11 +1037,14 @@ static OrderedAttributes gather_generic_pointcloud_attributes_to_propagate(
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<AttributeIDRef, AttributeKind> attributes_to_propagate;
|
|
|
|
|
in_geometry_set.gather_attributes_for_propagation(src_component_types,
|
|
|
|
|
gather_attributes_for_propagation(in_geometry_set,
|
|
|
|
|
src_component_types,
|
|
|
|
|
bke::GeometryComponent::Type::PointCloud,
|
|
|
|
|
true,
|
|
|
|
|
varied_depth_option.depths,
|
|
|
|
|
varied_depth_option.selection,
|
|
|
|
|
options.propagation_info,
|
|
|
|
|
attributes_to_propagate);
|
|
|
|
|
|
|
|
|
|
attributes_to_propagate.remove("position");
|
|
|
|
|
r_create_id = attributes_to_propagate.pop_try("id").has_value();
|
|
|
|
|
r_create_radii = attributes_to_propagate.pop_try("radius").has_value();
|
|
|
|
@ -727,11 +1072,15 @@ static void gather_pointclouds_to_realize(const bke::GeometrySet &geometry_set,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static AllPointCloudsInfo preprocess_pointclouds(const bke::GeometrySet &geometry_set,
|
|
|
|
|
const RealizeInstancesOptions &options)
|
|
|
|
|
const RealizeInstancesOptions &options,
|
|
|
|
|
const VariedDepthOption &varied_depth_option)
|
|
|
|
|
{
|
|
|
|
|
AllPointCloudsInfo info;
|
|
|
|
|
info.attributes = gather_generic_pointcloud_attributes_to_propagate(
|
|
|
|
|
geometry_set, options, info.create_radius_attribute, info.create_id_attribute);
|
|
|
|
|
info.attributes = gather_generic_pointcloud_attributes_to_propagate(geometry_set,
|
|
|
|
|
options,
|
|
|
|
|
varied_depth_option,
|
|
|
|
|
info.create_radius_attribute,
|
|
|
|
|
info.create_id_attribute);
|
|
|
|
|
|
|
|
|
|
gather_pointclouds_to_realize(geometry_set, info.order);
|
|
|
|
|
info.realize_info.reinitialize(info.order.size());
|
|
|
|
@ -770,7 +1119,7 @@ static AllPointCloudsInfo preprocess_pointclouds(const bke::GeometrySet &geometr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void execute_realize_pointcloud_task(
|
|
|
|
|
const RealizeInstancesOptions &options,
|
|
|
|
|
bool keep_original_ids,
|
|
|
|
|
const RealizePointCloudTask &task,
|
|
|
|
|
const OrderedAttributes &ordered_attributes,
|
|
|
|
|
MutableSpan<GSpanAttributeWriter> dst_attribute_writers,
|
|
|
|
@ -788,7 +1137,7 @@ static void execute_realize_pointcloud_task(
|
|
|
|
|
/* Create point ids. */
|
|
|
|
|
if (!all_dst_ids.is_empty()) {
|
|
|
|
|
create_result_ids(
|
|
|
|
|
options, pointcloud_info.stored_ids, task.id, all_dst_ids.slice(point_slice));
|
|
|
|
|
keep_original_ids, pointcloud_info.stored_ids, task.id, all_dst_ids.slice(point_slice));
|
|
|
|
|
}
|
|
|
|
|
if (!all_dst_radii.is_empty()) {
|
|
|
|
|
pointcloud_info.radii.materialize(all_dst_radii.slice(point_slice));
|
|
|
|
@ -806,7 +1155,7 @@ static void execute_realize_pointcloud_task(
|
|
|
|
|
dst_attribute_writers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void execute_realize_pointcloud_tasks(const RealizeInstancesOptions &options,
|
|
|
|
|
static void execute_realize_pointcloud_tasks(bool keep_original_ids,
|
|
|
|
|
const AllPointCloudsInfo &all_pointclouds_info,
|
|
|
|
|
const Span<RealizePointCloudTask> tasks,
|
|
|
|
|
const OrderedAttributes &ordered_attributes,
|
|
|
|
@ -858,7 +1207,7 @@ static void execute_realize_pointcloud_tasks(const RealizeInstancesOptions &opti
|
|
|
|
|
threading::parallel_for(tasks.index_range(), 100, [&](const IndexRange task_range) {
|
|
|
|
|
for (const int task_index : task_range) {
|
|
|
|
|
const RealizePointCloudTask &task = tasks[task_index];
|
|
|
|
|
execute_realize_pointcloud_task(options,
|
|
|
|
|
execute_realize_pointcloud_task(keep_original_ids,
|
|
|
|
|
task,
|
|
|
|
|
ordered_attributes,
|
|
|
|
|
dst_attribute_writers,
|
|
|
|
@ -886,6 +1235,7 @@ static void execute_realize_pointcloud_tasks(const RealizeInstancesOptions &opti
|
|
|
|
|
static OrderedAttributes gather_generic_mesh_attributes_to_propagate(
|
|
|
|
|
const bke::GeometrySet &in_geometry_set,
|
|
|
|
|
const RealizeInstancesOptions &options,
|
|
|
|
|
const VariedDepthOption &varied_depth_option,
|
|
|
|
|
bool &r_create_id,
|
|
|
|
|
bool &r_create_material_index)
|
|
|
|
|
{
|
|
|
|
@ -896,9 +1246,11 @@ static OrderedAttributes gather_generic_mesh_attributes_to_propagate(
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<AttributeIDRef, AttributeKind> attributes_to_propagate;
|
|
|
|
|
in_geometry_set.gather_attributes_for_propagation(src_component_types,
|
|
|
|
|
gather_attributes_for_propagation(in_geometry_set,
|
|
|
|
|
src_component_types,
|
|
|
|
|
bke::GeometryComponent::Type::Mesh,
|
|
|
|
|
true,
|
|
|
|
|
varied_depth_option.depths,
|
|
|
|
|
varied_depth_option.selection,
|
|
|
|
|
options.propagation_info,
|
|
|
|
|
attributes_to_propagate);
|
|
|
|
|
attributes_to_propagate.remove("position");
|
|
|
|
@ -931,11 +1283,16 @@ static void gather_meshes_to_realize(const bke::GeometrySet &geometry_set,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static AllMeshesInfo preprocess_meshes(const bke::GeometrySet &geometry_set,
|
|
|
|
|
const RealizeInstancesOptions &options)
|
|
|
|
|
const RealizeInstancesOptions &options,
|
|
|
|
|
const VariedDepthOption &varied_depth_option)
|
|
|
|
|
{
|
|
|
|
|
AllMeshesInfo info;
|
|
|
|
|
info.attributes = gather_generic_mesh_attributes_to_propagate(
|
|
|
|
|
geometry_set, options, info.create_id_attribute, info.create_material_index_attribute);
|
|
|
|
|
geometry_set,
|
|
|
|
|
options,
|
|
|
|
|
varied_depth_option,
|
|
|
|
|
info.create_id_attribute,
|
|
|
|
|
info.create_material_index_attribute);
|
|
|
|
|
|
|
|
|
|
gather_meshes_to_realize(geometry_set, info.order);
|
|
|
|
|
for (const Mesh *mesh : info.order) {
|
|
|
|
@ -1013,7 +1370,7 @@ static AllMeshesInfo preprocess_meshes(const bke::GeometrySet &geometry_set,
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void execute_realize_mesh_task(const RealizeInstancesOptions &options,
|
|
|
|
|
static void execute_realize_mesh_task(bool keep_original_ids,
|
|
|
|
|
const RealizeMeshTask &task,
|
|
|
|
|
const OrderedAttributes &ordered_attributes,
|
|
|
|
|
MutableSpan<GSpanAttributeWriter> dst_attribute_writers,
|
|
|
|
@ -1097,7 +1454,7 @@ static void execute_realize_mesh_task(const RealizeInstancesOptions &options,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!all_dst_vertex_ids.is_empty()) {
|
|
|
|
|
create_result_ids(options,
|
|
|
|
|
create_result_ids(keep_original_ids,
|
|
|
|
|
mesh_info.stored_vertex_ids,
|
|
|
|
|
task.id,
|
|
|
|
|
all_dst_vertex_ids.slice(task.start_indices.vertex, mesh.verts_num));
|
|
|
|
@ -1125,7 +1482,7 @@ static void execute_realize_mesh_task(const RealizeInstancesOptions &options,
|
|
|
|
|
dst_attribute_writers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void execute_realize_mesh_tasks(const RealizeInstancesOptions &options,
|
|
|
|
|
static void execute_realize_mesh_tasks(bool keep_original_ids,
|
|
|
|
|
const AllMeshesInfo &all_meshes_info,
|
|
|
|
|
const Span<RealizeMeshTask> tasks,
|
|
|
|
|
const OrderedAttributes &ordered_attributes,
|
|
|
|
@ -1208,7 +1565,7 @@ static void execute_realize_mesh_tasks(const RealizeInstancesOptions &options,
|
|
|
|
|
threading::parallel_for(tasks.index_range(), 100, [&](const IndexRange task_range) {
|
|
|
|
|
for (const int task_index : task_range) {
|
|
|
|
|
const RealizeMeshTask &task = tasks[task_index];
|
|
|
|
|
execute_realize_mesh_task(options,
|
|
|
|
|
execute_realize_mesh_task(keep_original_ids,
|
|
|
|
|
task,
|
|
|
|
|
ordered_attributes,
|
|
|
|
|
dst_attribute_writers,
|
|
|
|
@ -1249,6 +1606,7 @@ static void execute_realize_mesh_tasks(const RealizeInstancesOptions &options,
|
|
|
|
|
static OrderedAttributes gather_generic_curve_attributes_to_propagate(
|
|
|
|
|
const bke::GeometrySet &in_geometry_set,
|
|
|
|
|
const RealizeInstancesOptions &options,
|
|
|
|
|
const VariedDepthOption &varied_depth_option,
|
|
|
|
|
bool &r_create_id)
|
|
|
|
|
{
|
|
|
|
|
Vector<bke::GeometryComponent::Type> src_component_types;
|
|
|
|
@ -1258,9 +1616,11 @@ static OrderedAttributes gather_generic_curve_attributes_to_propagate(
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<AttributeIDRef, AttributeKind> attributes_to_propagate;
|
|
|
|
|
in_geometry_set.gather_attributes_for_propagation(src_component_types,
|
|
|
|
|
gather_attributes_for_propagation(in_geometry_set,
|
|
|
|
|
src_component_types,
|
|
|
|
|
bke::GeometryComponent::Type::Curve,
|
|
|
|
|
true,
|
|
|
|
|
varied_depth_option.depths,
|
|
|
|
|
varied_depth_option.selection,
|
|
|
|
|
options.propagation_info,
|
|
|
|
|
attributes_to_propagate);
|
|
|
|
|
attributes_to_propagate.remove("position");
|
|
|
|
@ -1295,11 +1655,12 @@ static void gather_curves_to_realize(const bke::GeometrySet &geometry_set,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static AllCurvesInfo preprocess_curves(const bke::GeometrySet &geometry_set,
|
|
|
|
|
const RealizeInstancesOptions &options)
|
|
|
|
|
const RealizeInstancesOptions &options,
|
|
|
|
|
const VariedDepthOption &varied_depth_option)
|
|
|
|
|
{
|
|
|
|
|
AllCurvesInfo info;
|
|
|
|
|
info.attributes = gather_generic_curve_attributes_to_propagate(
|
|
|
|
|
geometry_set, options, info.create_id_attribute);
|
|
|
|
|
geometry_set, options, varied_depth_option, info.create_id_attribute);
|
|
|
|
|
|
|
|
|
|
gather_curves_to_realize(geometry_set, info.order);
|
|
|
|
|
info.realize_info.reinitialize(info.order.size());
|
|
|
|
@ -1358,7 +1719,7 @@ static AllCurvesInfo preprocess_curves(const bke::GeometrySet &geometry_set,
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void execute_realize_curve_task(const RealizeInstancesOptions &options,
|
|
|
|
|
static void execute_realize_curve_task(bool keep_original_ids,
|
|
|
|
|
const AllCurvesInfo &all_curves_info,
|
|
|
|
|
const RealizeCurveTask &task,
|
|
|
|
|
const OrderedAttributes &ordered_attributes,
|
|
|
|
@ -1441,7 +1802,7 @@ static void execute_realize_curve_task(const RealizeInstancesOptions &options,
|
|
|
|
|
|
|
|
|
|
if (!all_dst_ids.is_empty()) {
|
|
|
|
|
create_result_ids(
|
|
|
|
|
options, curves_info.stored_ids, task.id, all_dst_ids.slice(dst_point_range));
|
|
|
|
|
keep_original_ids, curves_info.stored_ids, task.id, all_dst_ids.slice(dst_point_range));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
copy_generic_attributes_to_result(
|
|
|
|
@ -1462,7 +1823,7 @@ static void execute_realize_curve_task(const RealizeInstancesOptions &options,
|
|
|
|
|
dst_attribute_writers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void execute_realize_curve_tasks(const RealizeInstancesOptions &options,
|
|
|
|
|
static void execute_realize_curve_tasks(bool keep_original_ids,
|
|
|
|
|
const AllCurvesInfo &all_curves_info,
|
|
|
|
|
const Span<RealizeCurveTask> tasks,
|
|
|
|
|
const OrderedAttributes &ordered_attributes,
|
|
|
|
@ -1541,7 +1902,7 @@ static void execute_realize_curve_tasks(const RealizeInstancesOptions &options,
|
|
|
|
|
threading::parallel_for(tasks.index_range(), 100, [&](const IndexRange task_range) {
|
|
|
|
|
for (const int task_index : task_range) {
|
|
|
|
|
const RealizeCurveTask &task = tasks[task_index];
|
|
|
|
|
execute_realize_curve_task(options,
|
|
|
|
|
execute_realize_curve_task(keep_original_ids,
|
|
|
|
|
all_curves_info,
|
|
|
|
|
task,
|
|
|
|
|
ordered_attributes,
|
|
|
|
@ -1594,8 +1955,50 @@ static void remove_id_attribute_from_instances(bke::GeometrySet &geometry_set)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Propagate instances from the old geometry set to the new geometry set if they are not realized.
|
|
|
|
|
*/
|
|
|
|
|
static void propagate_instances_to_keep(
|
|
|
|
|
const bke::GeometrySet &geometry_set,
|
|
|
|
|
IndexMask selection,
|
|
|
|
|
bke::GeometrySet &new_geometry_set,
|
|
|
|
|
const bke::AnonymousAttributePropagationInfo &propagation_info)
|
|
|
|
|
{
|
|
|
|
|
const Instances &instances = *geometry_set.get_instances();
|
|
|
|
|
IndexMaskMemory inverse_selection_indices;
|
|
|
|
|
const IndexMask inverse_selection = selection.complement(IndexRange(instances.instances_num()),
|
|
|
|
|
inverse_selection_indices);
|
|
|
|
|
/* Check not all instances are being realized. */
|
|
|
|
|
if (inverse_selection.is_empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<Instances> new_instances = std::make_unique<Instances>(instances);
|
|
|
|
|
new_instances->remove(inverse_selection, propagation_info);
|
|
|
|
|
|
|
|
|
|
bke::InstancesComponent &new_instances_components =
|
|
|
|
|
new_geometry_set.get_component_for_write<bke::InstancesComponent>();
|
|
|
|
|
new_instances_components.replace(new_instances.release(), bke::GeometryOwnershipType::Owned);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bke::GeometrySet realize_instances(bke::GeometrySet geometry_set,
|
|
|
|
|
const RealizeInstancesOptions &options)
|
|
|
|
|
{
|
|
|
|
|
if (!geometry_set.has_instances()) {
|
|
|
|
|
return geometry_set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VariedDepthOption all_instances;
|
|
|
|
|
all_instances.depths = VArray<int>::ForSingle(VariedDepthOption::MAX_DEPTH,
|
|
|
|
|
geometry_set.get_instances()->instances_num());
|
|
|
|
|
IndexMaskMemory memory;
|
|
|
|
|
all_instances.selection = IndexMask::from_bools(
|
|
|
|
|
VArray<bool>::ForSingle(true, geometry_set.get_instances()->instances_num()), memory);
|
|
|
|
|
return realize_instances(geometry_set, options, all_instances);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bke::GeometrySet realize_instances(bke::GeometrySet geometry_set,
|
|
|
|
|
const RealizeInstancesOptions &options,
|
|
|
|
|
const VariedDepthOption &varied_depth_option)
|
|
|
|
|
{
|
|
|
|
|
/* The algorithm works in three steps:
|
|
|
|
|
* 1. Preprocess each unique geometry that is instanced (e.g. each `Mesh`).
|
|
|
|
@ -1608,52 +2011,80 @@ bke::GeometrySet realize_instances(bke::GeometrySet geometry_set,
|
|
|
|
|
return geometry_set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bke::GeometrySet not_to_realize_set;
|
|
|
|
|
propagate_instances_to_keep(
|
|
|
|
|
geometry_set, varied_depth_option.selection, not_to_realize_set, options.propagation_info);
|
|
|
|
|
|
|
|
|
|
if (options.keep_original_ids) {
|
|
|
|
|
remove_id_attribute_from_instances(geometry_set);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AllPointCloudsInfo all_pointclouds_info = preprocess_pointclouds(geometry_set, options);
|
|
|
|
|
AllMeshesInfo all_meshes_info = preprocess_meshes(geometry_set, options);
|
|
|
|
|
AllCurvesInfo all_curves_info = preprocess_curves(geometry_set, options);
|
|
|
|
|
AllPointCloudsInfo all_pointclouds_info = preprocess_pointclouds(
|
|
|
|
|
geometry_set, options, varied_depth_option);
|
|
|
|
|
AllMeshesInfo all_meshes_info = preprocess_meshes(geometry_set, options, varied_depth_option);
|
|
|
|
|
AllCurvesInfo all_curves_info = preprocess_curves(geometry_set, options, varied_depth_option);
|
|
|
|
|
OrderedAttributes all_instance_attributes = gather_generic_instance_attributes_to_propagate(
|
|
|
|
|
geometry_set, options, varied_depth_option);
|
|
|
|
|
|
|
|
|
|
Vector<std::unique_ptr<GArray<>>> temporary_arrays;
|
|
|
|
|
const bool create_id_attribute = all_pointclouds_info.create_id_attribute ||
|
|
|
|
|
all_meshes_info.create_id_attribute ||
|
|
|
|
|
all_curves_info.create_id_attribute;
|
|
|
|
|
Vector<std::unique_ptr<GArray<>>> temporary_arrays;
|
|
|
|
|
GatherTasksInfo gather_info = {all_pointclouds_info,
|
|
|
|
|
all_meshes_info,
|
|
|
|
|
all_curves_info,
|
|
|
|
|
all_instance_attributes,
|
|
|
|
|
create_id_attribute,
|
|
|
|
|
varied_depth_option.selection,
|
|
|
|
|
varied_depth_option.depths,
|
|
|
|
|
temporary_arrays};
|
|
|
|
|
|
|
|
|
|
if (not_to_realize_set.has_instances()) {
|
|
|
|
|
gather_info.instances.instances_components_to_merge.append(
|
|
|
|
|
(not_to_realize_set.get_component_for_write<bke::InstancesComponent>()).copy());
|
|
|
|
|
gather_info.instances.instances_components_transforms.append(float4x4::identity());
|
|
|
|
|
gather_info.instances.attribute_fallback.append((gather_info.instances_attriubutes.size()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const float4x4 transform = float4x4::identity();
|
|
|
|
|
InstanceContext attribute_fallbacks(gather_info);
|
|
|
|
|
gather_realize_tasks_recursive(gather_info, geometry_set, transform, attribute_fallbacks);
|
|
|
|
|
|
|
|
|
|
gather_realize_tasks_recursive(gather_info,
|
|
|
|
|
0,
|
|
|
|
|
VariedDepthOption::MAX_DEPTH,
|
|
|
|
|
geometry_set,
|
|
|
|
|
transform,
|
|
|
|
|
attribute_fallbacks);
|
|
|
|
|
|
|
|
|
|
bke::GeometrySet new_geometry_set;
|
|
|
|
|
execute_instances_tasks(gather_info.instances.instances_components_to_merge,
|
|
|
|
|
gather_info.instances.instances_components_transforms,
|
|
|
|
|
all_instance_attributes,
|
|
|
|
|
gather_info.instances.attribute_fallback,
|
|
|
|
|
new_geometry_set);
|
|
|
|
|
|
|
|
|
|
const int64_t total_points_num = get_final_points_num(gather_info.r_tasks);
|
|
|
|
|
/* This doesn't have to be exact at all, it's just a rough estimate ot make decisions about
|
|
|
|
|
* multi-threading (overhead). */
|
|
|
|
|
const int64_t approximate_used_bytes_num = total_points_num * 32;
|
|
|
|
|
threading::memory_bandwidth_bound_task(approximate_used_bytes_num, [&]() {
|
|
|
|
|
execute_realize_pointcloud_tasks(options,
|
|
|
|
|
execute_realize_pointcloud_tasks(options.keep_original_ids,
|
|
|
|
|
all_pointclouds_info,
|
|
|
|
|
gather_info.r_tasks.pointcloud_tasks,
|
|
|
|
|
all_pointclouds_info.attributes,
|
|
|
|
|
new_geometry_set);
|
|
|
|
|
execute_realize_mesh_tasks(options,
|
|
|
|
|
execute_realize_mesh_tasks(options.keep_original_ids,
|
|
|
|
|
all_meshes_info,
|
|
|
|
|
gather_info.r_tasks.mesh_tasks,
|
|
|
|
|
all_meshes_info.attributes,
|
|
|
|
|
all_meshes_info.materials,
|
|
|
|
|
new_geometry_set);
|
|
|
|
|
execute_realize_curve_tasks(options,
|
|
|
|
|
execute_realize_curve_tasks(options.keep_original_ids,
|
|
|
|
|
all_curves_info,
|
|
|
|
|
gather_info.r_tasks.curve_tasks,
|
|
|
|
|
all_curves_info.attributes,
|
|
|
|
|
new_geometry_set);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (gather_info.r_tasks.first_volume) {
|
|
|
|
|
new_geometry_set.add(*gather_info.r_tasks.first_volume);
|
|
|
|
|
}
|
|
|
|
|