From c9f8f7915fd8f65d1c57cd971f8e8a31a662a912 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Thu, 17 Dec 2020 07:43:31 -0600 Subject: Geometry Nodes: Make random attribute node stable Currently, the random attribute node doesn't work well for most workflows because for any change in the input data it outputs completely different results. This patch adds an implicit seed attribute input to the node, referred to by "id". The attribute is hashed for each element using the CPPType system's hash method, meaning the attribute can have any data type. Supporting any data type is also important so any attribute can be copied into the "id" attribute and used as a seed. The "id" attribute is an example of a "reserved name" attribute, meaning attributes with this name can be used implicitly by nodes like the random attribute node. Although it makes it a bit more difficult to dig deeper, using the name implicitly rather than exposing it as an input should make the system more accessible and predictable. Differential Revision: https://developer.blender.org/D9832 --- source/blender/nodes/NOD_geometry_exec.hh | 2 + .../geometry/nodes/node_geo_attribute_randomize.cc | 84 +++++++++++++++------- .../geometry/nodes/node_geo_point_distribute.cc | 34 +++++++-- 3 files changed, 92 insertions(+), 28 deletions(-) (limited to 'source/blender/nodes') diff --git a/source/blender/nodes/NOD_geometry_exec.hh b/source/blender/nodes/NOD_geometry_exec.hh index 445e1ed6af2..cac04e18fc7 100644 --- a/source/blender/nodes/NOD_geometry_exec.hh +++ b/source/blender/nodes/NOD_geometry_exec.hh @@ -34,6 +34,8 @@ using bke::Float3ReadAttribute; using bke::Float3WriteAttribute; using bke::FloatReadAttribute; using bke::FloatWriteAttribute; +using bke::Int32ReadAttribute; +using bke::Int32WriteAttribute; using bke::PersistentDataHandleMap; using bke::PersistentObjectHandle; using bke::ReadAttribute; diff --git a/source/blender/nodes/geometry/nodes/node_geo_attribute_randomize.cc b/source/blender/nodes/geometry/nodes/node_geo_attribute_randomize.cc index 53df2e8c087..2c3acfc9735 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_attribute_randomize.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_attribute_randomize.cc @@ -16,6 +16,7 @@ #include "node_geometry_util.hh" +#include "BLI_hash.h" #include "BLI_rand.hh" #include "DNA_mesh_types.h" @@ -59,48 +60,85 @@ static void geo_node_attribute_randomize_update(bNodeTree *UNUSED(ntree), bNode namespace blender::nodes { -static void randomize_attribute(BooleanWriteAttribute &attribute, RandomNumberGenerator &rng) +/** Rehash to combine the seed with the "id" hash and a mutator for each dimension. */ +static float noise_from_index_and_mutator(const int seed, const int hash, const int mutator) +{ + const int combined_hash = BLI_hash_int_3d(seed, hash, mutator); + return BLI_hash_int_01(combined_hash); +} + +/** Rehash to combine the seed with the "id" hash. */ +static float noise_from_index(const int seed, const int hash) +{ + const int combined_hash = BLI_hash_int_2d(seed, hash); + return BLI_hash_int_01(combined_hash); +} + +static void randomize_attribute(BooleanWriteAttribute &attribute, Span hashes, const int seed) { MutableSpan attribute_span = attribute.get_span(); for (const int i : IndexRange(attribute.size())) { - const bool value = rng.get_float() > 0.5f; + const bool value = noise_from_index(seed, hashes[i]) > 0.5f; attribute_span[i] = value; } attribute.apply_span(); } -static void randomize_attribute(FloatWriteAttribute &attribute, - float min, - float max, - RandomNumberGenerator &rng) +static void randomize_attribute( + FloatWriteAttribute &attribute, float min, float max, Span hashes, const int seed) { MutableSpan attribute_span = attribute.get_span(); for (const int i : IndexRange(attribute.size())) { - const float value = rng.get_float() * (max - min) + min; + const float value = noise_from_index(seed, hashes[i]) * (max - min) + min; attribute_span[i] = value; } attribute.apply_span(); } -static void randomize_attribute(Float3WriteAttribute &attribute, - float3 min, - float3 max, - RandomNumberGenerator &rng) +static void randomize_attribute( + Float3WriteAttribute &attribute, float3 min, float3 max, Span hashes, const int seed) { MutableSpan attribute_span = attribute.get_span(); for (const int i : IndexRange(attribute.size())) { - const float x = rng.get_float(); - const float y = rng.get_float(); - const float z = rng.get_float(); + const float x = noise_from_index_and_mutator(seed, hashes[i], 47); + const float y = noise_from_index_and_mutator(seed, hashes[i], 8); + const float z = noise_from_index_and_mutator(seed, hashes[i], 64); const float3 value = float3(x, y, z) * (max - min) + min; attribute_span[i] = value; } attribute.apply_span(); } +static Array get_element_hashes(GeometryComponent &component, + const AttributeDomain domain, + const int attribute_size) +{ + /* Hash the reserved name attribute "id" as a (hopefully) stable seed for each point. */ + ReadAttributePtr hash_attribute = component.attribute_try_get_for_read("id", domain); + Array hashes(attribute_size); + if (hash_attribute) { + BLI_assert(hashes.size() == hash_attribute->size()); + const CPPType &cpp_type = hash_attribute->cpp_type(); + fn::GSpan items = hash_attribute->get_span(); + for (const int i : hashes.index_range()) { + hashes[i] = (int)cpp_type.hash(items[i]); + } + } + else { + /* If there is no "id" attribute for per-point variation, just create it here. */ + RandomNumberGenerator rng; + rng.seed(0); + for (const int i : hashes.index_range()) { + hashes[i] = rng.get_int32(); + } + } + + return hashes; +} + static void randomize_attribute(GeometryComponent &component, const GeoNodeExecParams ¶ms, - RandomNumberGenerator &rng) + const int seed) { const bNode &node = params.node(); const CustomDataType data_type = static_cast(node.custom1); @@ -116,24 +154,26 @@ static void randomize_attribute(GeometryComponent &component, return; } + Array hashes = get_element_hashes(component, domain, attribute->size()); + switch (data_type) { case CD_PROP_FLOAT: { FloatWriteAttribute float_attribute = std::move(attribute); const float min_value = params.get_input("Min_001"); const float max_value = params.get_input("Max_001"); - randomize_attribute(float_attribute, min_value, max_value, rng); + randomize_attribute(float_attribute, min_value, max_value, hashes, seed); break; } case CD_PROP_FLOAT3: { Float3WriteAttribute float3_attribute = std::move(attribute); const float3 min_value = params.get_input("Min"); const float3 max_value = params.get_input("Max"); - randomize_attribute(float3_attribute, min_value, max_value, rng); + randomize_attribute(float3_attribute, min_value, max_value, hashes, seed); break; } case CD_PROP_BOOL: { BooleanWriteAttribute boolean_attribute = std::move(attribute); - randomize_attribute(boolean_attribute, rng); + randomize_attribute(boolean_attribute, hashes, seed); break; } default: @@ -147,14 +187,10 @@ static void geo_node_random_attribute_exec(GeoNodeExecParams params) const int seed = params.get_input("Seed"); if (geometry_set.has()) { - RandomNumberGenerator rng; - rng.seed_random(seed); - randomize_attribute(geometry_set.get_component_for_write(), params, rng); + randomize_attribute(geometry_set.get_component_for_write(), params, seed); } if (geometry_set.has()) { - RandomNumberGenerator rng; - rng.seed_random(seed + 3245231); - randomize_attribute(geometry_set.get_component_for_write(), params, rng); + randomize_attribute(geometry_set.get_component_for_write(), params, seed); } params.set_output("Geometry", geometry_set); diff --git a/source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc b/source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc index 50c9246fc9b..1d3fbae5b2e 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc @@ -58,6 +58,7 @@ namespace blender::nodes { static Vector random_scatter_points_from_mesh(const Mesh *mesh, const float density, const FloatReadAttribute &density_factors, + Vector &r_ids, const int seed) { /* This only updates a cache and can be considered to be logically const. */ @@ -95,6 +96,9 @@ static Vector random_scatter_points_from_mesh(const Mesh *mesh, float3 point_pos; interp_v3_v3v3v3(point_pos, v0_pos, v1_pos, v2_pos, bary_coords); points.append(point_pos); + + /* Build a hash stable even when the mesh is deformed. */ + r_ids.append(((int)(bary_coords.hash()) + looptri_index)); } } @@ -106,10 +110,14 @@ struct RayCastAll_Data { BVHTree_RayCastCallback raycast_callback; + /** The original coordinate the result point was projected from. */ + float2 raystart; + const Mesh *mesh; float base_weight; FloatReadAttribute *density_factors; Vector *projected_points; + Vector *stable_ids; float cur_point_weight; }; @@ -148,6 +156,9 @@ static void project_2d_bvh_callback(void *userdata, if (point_weight >= FLT_EPSILON && data->cur_point_weight <= point_weight) { data->projected_points->append(hit->co); + + /* Build a hash stable even when the mesh is deformed. */ + data->stable_ids->append((int)data->raystart.hash()); } } } @@ -156,6 +167,7 @@ static Vector poisson_scatter_points_from_mesh(const Mesh *mesh, const float density, const float minimum_distance, const FloatReadAttribute &density_factors, + Vector &r_ids, const int seed) { Vector points; @@ -191,6 +203,7 @@ static Vector poisson_scatter_points_from_mesh(const Mesh *mesh, const float3 bounds_max = float3(point_scale_multiplier, point_scale_multiplier, 0); poisson_disk_point_elimination(&points, &output_points, 2.0f * minimum_distance, bounds_max); Vector final_points; + r_ids.reserve(output_points_target); final_points.reserve(output_points_target); /* Check if we have any points we should remove from the final possion distribition. */ @@ -205,6 +218,7 @@ static Vector poisson_scatter_points_from_mesh(const Mesh *mesh, data.raycast_callback = treedata.raycast_callback; data.mesh = mesh; data.projected_points = &final_points; + data.stable_ids = &r_ids; data.density_factors = const_cast(&density_factors); data.base_weight = std::min( 1.0f, density / (output_points.size() / (point_scale_multiplier * point_scale_multiplier))); @@ -229,6 +243,7 @@ static Vector poisson_scatter_points_from_mesh(const Mesh *mesh, raystart.y = output_points[idx].y + tile_curr_y_coord; data.cur_point_weight = (float)idx / (float)output_points.size(); + data.raystart = raystart; BLI_bvhtree_ray_cast_all( treedata.tree, raystart, dir, 0.0f, max_dist, project_2d_bvh_callback, &data); @@ -267,15 +282,17 @@ static void geo_node_point_distribute_exec(GeoNodeExecParams params) density_attribute, ATTR_DOMAIN_POINT, 1.0f); const int seed = params.get_input("Seed"); + Vector stable_ids; Vector points; - switch (distribute_method) { case GEO_NODE_POINT_DISTRIBUTE_RANDOM: - points = random_scatter_points_from_mesh(mesh_in, density, density_factors, seed); + points = random_scatter_points_from_mesh( + mesh_in, density, density_factors, stable_ids, seed); break; case GEO_NODE_POINT_DISTRIBUTE_POISSON: const float min_dist = params.extract_input("Distance Min"); - points = poisson_scatter_points_from_mesh(mesh_in, density, min_dist, density_factors, seed); + points = poisson_scatter_points_from_mesh( + mesh_in, density, min_dist, density_factors, stable_ids, seed); break; } @@ -286,7 +303,16 @@ static void geo_node_point_distribute_exec(GeoNodeExecParams params) pointcloud->radius[i] = 0.05f; } - geometry_set_out.replace_pointcloud(pointcloud); + PointCloudComponent &point_component = + geometry_set_out.get_component_for_write(); + point_component.replace(pointcloud); + + Int32WriteAttribute stable_id_attribute = point_component.attribute_try_ensure_for_write( + "id", ATTR_DOMAIN_POINT, CD_PROP_INT32); + MutableSpan stable_ids_span = stable_id_attribute.get_span(); + stable_ids_span.copy_from(stable_ids); + stable_id_attribute.apply_span(); + params.set_output("Geometry", std::move(geometry_set_out)); } } // namespace blender::nodes -- cgit v1.2.3