|
|
|
|
@@ -1,12 +1,12 @@
|
|
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
|
|
|
|
|
|
#include "BLI_array_utils.hh"
|
|
|
|
|
#include "BLI_task.hh"
|
|
|
|
|
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
|
#include "DNA_modifier_types.h"
|
|
|
|
|
|
|
|
|
|
#include "BKE_attribute.hh"
|
|
|
|
|
#include "BKE_lib_id.h"
|
|
|
|
|
#include "BKE_mesh.hh"
|
|
|
|
|
#include "BKE_subdiv.h"
|
|
|
|
|
#include "BKE_subdiv_mesh.hh"
|
|
|
|
|
@@ -54,136 +54,136 @@ static void node_init(bNodeTree * /*tree*/, bNode *node)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
|
static void materialize_and_clamp_creases(const VArray<float> &crease_varray,
|
|
|
|
|
MutableSpan<float> creases)
|
|
|
|
|
|
|
|
|
|
static void write_vert_creases(Mesh &mesh, const VArray<float> &creases)
|
|
|
|
|
{
|
|
|
|
|
threading::parallel_for(creases.index_range(), 1024, [&](IndexRange range) {
|
|
|
|
|
crease_varray.materialize(range, creases);
|
|
|
|
|
for (const int i : range) {
|
|
|
|
|
creases[i] = std::clamp(creases[i], 0.0f, 1.0f);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
CustomData_free_layers(&mesh.vdata, CD_CREASE, mesh.totvert);
|
|
|
|
|
float *layer = static_cast<float *>(
|
|
|
|
|
CustomData_add_layer(&mesh.vdata, CD_CREASE, CD_CONSTRUCT, mesh.totvert));
|
|
|
|
|
array_utils::copy(creases, {layer, mesh.totvert});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void write_vertex_creases(Mesh &mesh, const VArray<float> &crease_varray)
|
|
|
|
|
static void write_edge_creases(Mesh &mesh, const VArray<float> &creases)
|
|
|
|
|
{
|
|
|
|
|
float *crease;
|
|
|
|
|
if (CustomData_has_layer(&mesh.vdata, CD_CREASE)) {
|
|
|
|
|
crease = static_cast<float *>(
|
|
|
|
|
CustomData_get_layer_for_write(&mesh.vdata, CD_CREASE, mesh.totvert));
|
|
|
|
|
bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
|
|
|
|
|
attributes.remove("crease");
|
|
|
|
|
attributes.add<float>("crease", ATTR_DOMAIN_EDGE, bke::AttributeInitVArray(creases));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool varray_is_single_zero(const VArray<float> &varray)
|
|
|
|
|
{
|
|
|
|
|
if (const std::optional<float> value = varray.get_if_single()) {
|
|
|
|
|
return *value == 0.0f;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
crease = static_cast<float *>(
|
|
|
|
|
CustomData_add_layer(&mesh.vdata, CD_CREASE, CD_CONSTRUCT, mesh.totvert));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static fn::Field<float> clamp_crease(fn::Field<float> crease_field)
|
|
|
|
|
{
|
|
|
|
|
static auto clamp_fn = mf::build::SI1_SO<float, float>(
|
|
|
|
|
"Clamp",
|
|
|
|
|
[](float value) { return std::clamp(value, 0.0f, 1.0f); },
|
|
|
|
|
mf::build::exec_presets::AllSpanOrSingle());
|
|
|
|
|
return fn::Field<float>(fn::FieldOperation::Create(clamp_fn, {std::move(crease_field)}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Mesh *mesh_subsurf_calc(const Mesh *mesh,
|
|
|
|
|
const int level,
|
|
|
|
|
const Field<float> &vert_crease_field,
|
|
|
|
|
const Field<float> &edge_crease_field,
|
|
|
|
|
const int boundary_smooth,
|
|
|
|
|
const int uv_smooth)
|
|
|
|
|
{
|
|
|
|
|
const bke::MeshFieldContext point_context{*mesh, ATTR_DOMAIN_POINT};
|
|
|
|
|
FieldEvaluator point_evaluator(point_context, mesh->totvert);
|
|
|
|
|
point_evaluator.add(clamp_crease(vert_crease_field));
|
|
|
|
|
point_evaluator.evaluate();
|
|
|
|
|
|
|
|
|
|
const bke::MeshFieldContext edge_context{*mesh, ATTR_DOMAIN_EDGE};
|
|
|
|
|
FieldEvaluator edge_evaluator(edge_context, mesh->totedge);
|
|
|
|
|
edge_evaluator.add(clamp_crease(edge_crease_field));
|
|
|
|
|
edge_evaluator.evaluate();
|
|
|
|
|
|
|
|
|
|
const VArray<float> vert_creases = point_evaluator.get_evaluated<float>(0);
|
|
|
|
|
const VArray<float> edge_creases = edge_evaluator.get_evaluated<float>(0);
|
|
|
|
|
const bool use_creases = !varray_is_single_zero(vert_creases) ||
|
|
|
|
|
!varray_is_single_zero(edge_creases);
|
|
|
|
|
|
|
|
|
|
Mesh *mesh_copy = nullptr;
|
|
|
|
|
if (use_creases) {
|
|
|
|
|
/* Due to the "BKE_subdiv" API, the crease layers must be on the input mesh. But in this node
|
|
|
|
|
* they are provided as separate inputs, not as custom data layers. When needed, retrieve the
|
|
|
|
|
* mesh with write access and store the new crease values there. */
|
|
|
|
|
mesh_copy = BKE_mesh_copy_for_eval(mesh);
|
|
|
|
|
write_vert_creases(*mesh_copy, vert_creases);
|
|
|
|
|
write_edge_creases(*mesh_copy, edge_creases);
|
|
|
|
|
mesh = mesh_copy;
|
|
|
|
|
}
|
|
|
|
|
materialize_and_clamp_creases(crease_varray, {crease, mesh.totvert});
|
|
|
|
|
|
|
|
|
|
SubdivToMeshSettings mesh_settings;
|
|
|
|
|
mesh_settings.resolution = (1 << level) + 1;
|
|
|
|
|
mesh_settings.use_optimal_display = false;
|
|
|
|
|
|
|
|
|
|
SubdivSettings subdiv_settings;
|
|
|
|
|
subdiv_settings.is_simple = false;
|
|
|
|
|
subdiv_settings.is_adaptive = false;
|
|
|
|
|
subdiv_settings.use_creases = use_creases;
|
|
|
|
|
subdiv_settings.level = level;
|
|
|
|
|
subdiv_settings.vtx_boundary_interpolation = BKE_subdiv_vtx_boundary_interpolation_from_subsurf(
|
|
|
|
|
boundary_smooth);
|
|
|
|
|
subdiv_settings.fvar_linear_interpolation = BKE_subdiv_fvar_interpolation_from_uv_smooth(
|
|
|
|
|
uv_smooth);
|
|
|
|
|
|
|
|
|
|
Subdiv *subdiv = BKE_subdiv_new_from_mesh(&subdiv_settings, mesh);
|
|
|
|
|
if (!subdiv) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Mesh *result = BKE_subdiv_to_mesh(subdiv, &mesh_settings, mesh);
|
|
|
|
|
BKE_subdiv_free(subdiv);
|
|
|
|
|
|
|
|
|
|
if (use_creases) {
|
|
|
|
|
/* Remove the layer in case it was created by the node from the field input. The fact
|
|
|
|
|
* that this node uses #CD_CREASE to input creases to the subdivision code is meant to be
|
|
|
|
|
* an implementation detail ideally. */
|
|
|
|
|
CustomData_free_layers(&result->edata, CD_CREASE, result->totedge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mesh_copy) {
|
|
|
|
|
BKE_id_free(nullptr, mesh_copy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void write_edge_creases(Mesh &mesh, const VArray<float> &crease_varray)
|
|
|
|
|
{
|
|
|
|
|
bke::SpanAttributeWriter<float> attribute =
|
|
|
|
|
mesh.attributes_for_write().lookup_or_add_for_write_only_span<float>("crease",
|
|
|
|
|
ATTR_DOMAIN_EDGE);
|
|
|
|
|
materialize_and_clamp_creases(crease_varray, attribute.span);
|
|
|
|
|
attribute.finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool varray_is_nonzero(const VArray<float> &varray)
|
|
|
|
|
{
|
|
|
|
|
return !(varray.is_single() && varray.get_internal_single() == 0.0f);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static void node_geo_exec(GeoNodeExecParams params)
|
|
|
|
|
{
|
|
|
|
|
GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");
|
|
|
|
|
#ifndef WITH_OPENSUBDIV
|
|
|
|
|
params.error_message_add(NodeWarningType::Error,
|
|
|
|
|
TIP_("Disabled, Blender was compiled without OpenSubdiv"));
|
|
|
|
|
#else
|
|
|
|
|
Field<float> edge_crease_field = params.extract_input<Field<float>>("Edge Crease");
|
|
|
|
|
Field<float> vertex_crease_field = params.extract_input<Field<float>>("Vertex Crease");
|
|
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
|
const Field<float> vert_crease = params.extract_input<Field<float>>("Vertex Crease");
|
|
|
|
|
const Field<float> edge_crease = params.extract_input<Field<float>>("Edge Crease");
|
|
|
|
|
|
|
|
|
|
const NodeGeometrySubdivisionSurface &storage = node_storage(params.node());
|
|
|
|
|
const int uv_smooth = storage.uv_smooth;
|
|
|
|
|
const int boundary_smooth = storage.boundary_smooth;
|
|
|
|
|
const int subdiv_level = clamp_i(params.extract_input<int>("Level"), 0, 30);
|
|
|
|
|
|
|
|
|
|
/* Only process subdivision if level is greater than 0. */
|
|
|
|
|
if (subdiv_level == 0) {
|
|
|
|
|
const int level = std::clamp(params.extract_input<int>("Level"), 0, 11);
|
|
|
|
|
if (level == 0) {
|
|
|
|
|
params.set_output("Mesh", std::move(geometry_set));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
|
|
|
|
|
const Mesh *mesh = geometry_set.get_mesh_for_read();
|
|
|
|
|
if (!mesh) {
|
|
|
|
|
return;
|
|
|
|
|
if (const Mesh *mesh = geometry_set.get_mesh_for_read()) {
|
|
|
|
|
geometry_set.replace_mesh(
|
|
|
|
|
mesh_subsurf_calc(mesh, level, vert_crease, edge_crease, boundary_smooth, uv_smooth));
|
|
|
|
|
}
|
|
|
|
|
if (mesh->totvert == 0 || mesh->totedge == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bke::MeshFieldContext point_context{*mesh, ATTR_DOMAIN_POINT};
|
|
|
|
|
FieldEvaluator point_evaluator(point_context, mesh->totvert);
|
|
|
|
|
point_evaluator.add(vertex_crease_field);
|
|
|
|
|
point_evaluator.evaluate();
|
|
|
|
|
const VArray<float> vertex_creases = point_evaluator.get_evaluated<float>(0);
|
|
|
|
|
|
|
|
|
|
bke::MeshFieldContext edge_context{*mesh, ATTR_DOMAIN_EDGE};
|
|
|
|
|
FieldEvaluator edge_evaluator(edge_context, mesh->totedge);
|
|
|
|
|
edge_evaluator.add(edge_crease_field);
|
|
|
|
|
edge_evaluator.evaluate();
|
|
|
|
|
const VArray<float> edge_creases = edge_evaluator.get_evaluated<float>(0);
|
|
|
|
|
|
|
|
|
|
const bool use_creases = varray_is_nonzero(vertex_creases) || varray_is_nonzero(edge_creases);
|
|
|
|
|
|
|
|
|
|
if (use_creases) {
|
|
|
|
|
/* Due to the "BKE_subdiv" API, the crease layers must be on the input mesh. But in this node
|
|
|
|
|
* they are provided as separate inputs, not as custom data layers. When needed, retrieve the
|
|
|
|
|
* mesh with write access and store the new crease values there. */
|
|
|
|
|
Mesh &mesh_for_write = *geometry_set.get_mesh_for_write();
|
|
|
|
|
write_vertex_creases(mesh_for_write, vertex_creases);
|
|
|
|
|
write_edge_creases(mesh_for_write, edge_creases);
|
|
|
|
|
mesh = &mesh_for_write;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Initialize mesh settings. */
|
|
|
|
|
SubdivToMeshSettings mesh_settings;
|
|
|
|
|
mesh_settings.resolution = (1 << subdiv_level) + 1;
|
|
|
|
|
mesh_settings.use_optimal_display = false;
|
|
|
|
|
|
|
|
|
|
/* Initialize subdivision settings. */
|
|
|
|
|
SubdivSettings subdiv_settings;
|
|
|
|
|
subdiv_settings.is_simple = false;
|
|
|
|
|
subdiv_settings.is_adaptive = false;
|
|
|
|
|
subdiv_settings.use_creases = use_creases;
|
|
|
|
|
subdiv_settings.level = subdiv_level;
|
|
|
|
|
|
|
|
|
|
subdiv_settings.vtx_boundary_interpolation =
|
|
|
|
|
BKE_subdiv_vtx_boundary_interpolation_from_subsurf(boundary_smooth);
|
|
|
|
|
subdiv_settings.fvar_linear_interpolation = BKE_subdiv_fvar_interpolation_from_uv_smooth(
|
|
|
|
|
uv_smooth);
|
|
|
|
|
|
|
|
|
|
/* Apply subdivision to mesh. */
|
|
|
|
|
Subdiv *subdiv = BKE_subdiv_update_from_mesh(nullptr, &subdiv_settings, mesh);
|
|
|
|
|
|
|
|
|
|
/* In case of bad topology, skip to input mesh. */
|
|
|
|
|
if (subdiv == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Mesh *result = BKE_subdiv_to_mesh(subdiv, &mesh_settings, mesh);
|
|
|
|
|
BKE_subdiv_free(subdiv);
|
|
|
|
|
if (use_creases) {
|
|
|
|
|
/* Remove the layer in case it was created by the node from the field input. The fact
|
|
|
|
|
* that this node uses #CD_CREASE to input creases to the subdivision code is meant to be
|
|
|
|
|
* an implementation detail ideally. */
|
|
|
|
|
CustomData_free_layers(&result->edata, CD_CREASE, result->totedge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
geometry_set.replace_mesh(result);
|
|
|
|
|
});
|
|
|
|
|
#else
|
|
|
|
|
params.error_message_add(NodeWarningType::Error,
|
|
|
|
|
TIP_("Disabled, Blender was compiled without OpenSubdiv"));
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
params.set_output("Mesh", std::move(geometry_set));
|
|
|
|
|
}
|
|
|
|
|
|