Blender V5.3
node_geo_uv_unwrap.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "DNA_mesh_types.h"
6
7#include "GEO_uv_pack.hh"
9
10#include "node_geometry_util.hh"
11
13
15
18 "ANGLE_BASED",
19 0,
20 N_("Angle Based"),
21 N_("This method gives a good 2D representation of a mesh")},
23 "CONFORMAL",
24 0,
25 N_("Conformal"),
26 N_("Uses LSCM (Least Squares Conformal Mapping). This usually gives a less accurate UV "
27 "mapping than Angle Based, but works better for simpler objects")},
29 "MINIMUM_STRETCH",
30 0,
31 N_("Minimum Stretch"),
32 N_("Uses SLIM (Scalable Locally Injective Mapping). Minimizes distortion in areas and "
33 "angles.")},
34 {0, nullptr, 0, nullptr, nullptr},
35};
36
38{
39 b.add_input<decl::Bool>("Selection"_ustr)
40 .default_value(true)
41 .hide_value()
42 .structure_type(StructureType::Field)
43 .description("Faces to participate in the unwrap operation");
44 b.add_input<decl::Bool>("Seam"_ustr)
45 .hide_value()
46 .structure_type(StructureType::Field)
47 .description("Edges to mark where the mesh is \"cut\" for the purposes of unwrapping");
48 b.add_input<decl::Float>("Margin"_ustr)
49 .default_value(0.001f)
50 .min(0.0f)
51 .max(1.0f)
52 .description("Space between islands");
53 b.add_input<decl::Bool>("Fill Holes"_ustr)
54 .default_value(true)
56 "Virtually fill holes in mesh before unwrapping, to better avoid overlaps "
57 "and preserve symmetry");
58 b.add_input<decl::Menu>("Method"_ustr).static_items(method_items).optional_label();
59 b.add_input<decl::Int>("Iterations"_ustr)
60 .default_value(10)
61 .min(1)
63 .description("Number of iterations to run the SLIM solver for");
64 b.add_input<decl::Bool>("No Flip"_ustr)
66 .description("Prevents flipping UVs");
67 b.add_output<decl::Vector>("UV"_ustr)
68 .structure_type(StructureType::Field)
69 .propagate_references()
70 .description("UV coordinates between 0 and 1 for each face corner in the selected faces");
71}
72
73static void node_init(bNodeTree * /*tree*/, bNode *node)
74{
75 /* Still used for forward compatibility. */
76 node->storage = MEM_new<NodeGeometryUVUnwrap>(__func__);
77}
78
80 const Field<bool> selection_field,
81 const Field<bool> seam_field,
82 const bool fill_holes,
83 const float margin,
84 const GeometryNodeUVUnwrapMethod method,
85 const int iterations_,
86 const bool no_flip_,
87 const AttrDomain domain)
88{
89 const Span<float3> positions = mesh.vert_positions();
90 const Span<int2> edges = mesh.edges();
91 const OffsetIndices faces = mesh.faces();
92 const Span<int> corner_verts = mesh.corner_verts();
93
94 const bke::MeshFieldContext face_context{mesh, AttrDomain::Face};
95 FieldEvaluator face_evaluator{face_context, faces.size()};
96 face_evaluator.add(selection_field);
97 face_evaluator.evaluate();
98 const IndexMask selection = face_evaluator.get_evaluated_as_mask(0);
99 if (selection.is_empty()) {
100 return {};
101 }
102
103 const bke::MeshFieldContext edge_context{mesh, AttrDomain::Edge};
104 FieldEvaluator edge_evaluator{edge_context, edges.size()};
105 edge_evaluator.add(seam_field);
106 edge_evaluator.evaluate();
107 const IndexMask seam = edge_evaluator.get_evaluated_as_mask(0);
108
109 Array<float3> uv(corner_verts.size(), float3(0));
110
112 selection.foreach_index([&](const int face_index) {
113 const IndexRange face = faces[face_index];
114 Array<geometry::ParamKey, 16> mp_vkeys(face.size());
115 Array<bool, 16> mp_pin(face.size());
116 Array<bool, 16> mp_select(face.size());
117 Array<const float *, 16> mp_co(face.size());
118 Array<float *, 16> mp_uv(face.size());
119 for (const int i : IndexRange(face.size())) {
120 const int corner = face[i];
121 const int vert = corner_verts[corner];
122 mp_vkeys[i] = vert;
123 mp_co[i] = positions[vert];
124 mp_uv[i] = uv[corner];
125 mp_pin[i] = false;
126 mp_select[i] = false;
127 }
129 face_index,
130 face.size(),
131 mp_vkeys.data(),
132 mp_co.data(),
133 mp_uv.data(),
134 nullptr,
135 mp_pin.data(),
136 mp_select.data());
137 });
138
139 seam.foreach_index([&](const int i) {
140 geometry::ParamKey vkeys[2]{uint(edges[i][0]), uint(edges[i][1])};
142 });
143
145 params.margin = margin;
147
148 /* TODO: once field input nodes are able to emit warnings (#94039), emit a
149 * warning if we fail to solve an island. */
150 geometry::uv_parametrizer_construct_end(handle, fill_holes, false, nullptr);
151
153 geometry::ParamSlimOptions slim_options{};
154 slim_options.iterations = iterations_;
155 slim_options.no_flip = no_flip_;
156 slim_options.skip_init = false;
157 uv_parametrizer_slim_solve(handle, &slim_options, false, nullptr, nullptr);
158 }
159 else {
161 handle, false, method == GEO_NODE_UV_UNWRAP_METHOD_ANGLE_BASED, false);
162 geometry::uv_parametrizer_lscm_solve(handle, nullptr, nullptr);
164 }
165 geometry::uv_parametrizer_average(handle, true, false, false);
166 geometry::uv_parametrizer_pack(handle, params);
168 delete (handle);
169
170 return mesh.attributes().adapt_domain<float3>(
171 VArray<float3>::from_container(std::move(uv)), AttrDomain::Corner, domain);
172}
173
175 private:
176 const Field<bool> selection_;
177 const Field<bool> seam_;
178 const bool fill_holes_;
179 const float margin_;
180 const GeometryNodeUVUnwrapMethod method_;
181 const int iterations_;
182 const bool no_flip_;
183
184 public:
186 const Field<bool> seam,
187 const bool fill_holes,
188 const float margin,
189 const GeometryNodeUVUnwrapMethod method,
190 const int iterations,
191 const bool no_flip)
192 : bke::MeshFieldInput(CPPType::get<float3>(), "UV Unwrap Field"),
193 selection_(selection),
194 seam_(seam),
195 fill_holes_(fill_holes),
196 margin_(margin),
197 method_(method),
198 iterations_(iterations),
199 no_flip_(no_flip)
200 {
201 }
202
204 const AttrDomain domain,
205 const IndexMask & /*mask*/) const final
206 {
208 mesh, selection_, seam_, fill_holes_, margin_, method_, iterations_, no_flip_, domain);
209 }
210
211 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const override
212 {
213 fn(selection_);
214 fn(seam_);
215 }
216
217 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
218 {
219 return AttrDomain::Corner;
220 }
221};
222
224{
225 const auto method = params.get_input<GeometryNodeUVUnwrapMethod>("Method"_ustr);
226 const Field<bool> selection_field = params.extract_input<Field<bool>>("Selection"_ustr);
227 const Field<bool> seam_field = params.extract_input<Field<bool>>("Seam"_ustr);
228 const bool fill_holes = params.extract_input<bool>("Fill Holes"_ustr);
229 const float margin = params.extract_input<float>("Margin"_ustr);
230 int iterations = 0;
231 bool no_flip = false;
233 iterations = params.extract_input<int>("Iterations"_ustr);
234 no_flip = params.extract_input<bool>("No Flip"_ustr);
235 }
236 params.set_output(
237 "UV"_ustr,
239 selection_field, seam_field, fill_holes, margin, method, iterations, no_flip));
240}
241
242static void node_register()
243{
244 static bke::bNodeType ntype;
245
246 geo_node_type_base(&ntype, "GeometryNodeUVUnwrap"_ustr, GEO_NODE_UV_UNWRAP);
247 ntype.ui_name = "UV Unwrap";
248 ntype.ui_description = "Generate a UV map based on seam edges";
249 ntype.enum_name_legacy = "UV_UNWRAP";
251 ntype.initfunc = node_init;
253 ntype, "NodeGeometryUVUnwrap", node_free_standard_storage, node_copy_standard_storage);
254 ntype.declare = node_declare;
257}
259
260} // namespace blender::nodes::node_geo_uv_unwrap_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define GEO_NODE_UV_UNWRAP
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
const T * data() const
Definition BLI_array.hh:323
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
static VArray from_container(ContainerT container)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_as_mask(int field_index)
eUVPackIsland_RotationMethod rotate_method
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
static Field from_input(Args &&...args)
T extract_input(const UString identifier)
T get_input(const UString identifier) const
void set_output(const UString identifier, T &&value)
UnwrapFieldInput(const Field< bool > selection, const Field< bool > seam, const bool fill_holes, const float margin, const GeometryNodeUVUnwrapMethod method, const int iterations, const bool no_flip)
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
std::optional< AttrDomain > preferred_domain(const Mesh &) const override
void foreach_recursive_field(FunctionRef< void(const GField &)> fn) const override
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
void uv_parametrizer_construct_end(ParamHandle *phandle, bool fill_holes, bool topology_from_uvs, int *r_count_failed=nullptr)
void uv_parametrizer_edge_set_seam(ParamHandle *phandle, const ParamKey *vkeys)
void uv_parametrizer_average(ParamHandle *handle, bool ignore_pinned, bool scale_uv, bool shear)
void uv_parametrizer_flush(ParamHandle *handle)
void uv_parametrizer_lscm_begin(ParamHandle *handle, bool live, bool abf, bool use_original_bounds)
void uv_parametrizer_lscm_end(ParamHandle *handle)
void uv_parametrizer_pack(ParamHandle *handle, const UVPackIsland_Params &params)
void uv_parametrizer_lscm_solve(ParamHandle *handle, int *count_changed, int *count_failed)
void uv_parametrizer_face_add(ParamHandle *handle, const ParamKey key, const int nverts, const ParamKey *vkeys, const float **co, float **uv, const float *weight, const bool *pin, const bool *select)
static void node_declare(NodeDeclarationBuilder &b)
static VArray< float3 > construct_uv_gvarray(const Mesh &mesh, const Field< bool > selection_field, const Field< bool > seam_field, const bool fill_holes, const float margin, const GeometryNodeUVUnwrapMethod method, const int iterations_, const bool no_flip_, const AttrDomain domain)
static void node_geo_exec(GeoNodeExecParams params)
static void node_init(bNodeTree *, bNode *node)
@ GEO_NODE_UV_UNWRAP_METHOD_CONFORMAL
@ GEO_NODE_UV_UNWRAP_METHOD_ANGLE_BASED
@ GEO_NODE_UV_UNWRAP_METHOD_MINIMUM_STRETCH
unsigned int uint
Definition BKE_text.h:55
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
@ ED_UVPACK_ROTATION_ANY
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)