Blender V5.0
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")},
28 {0, nullptr, 0, nullptr, nullptr},
29};
30
32{
33 b.add_input<decl::Bool>("Selection")
34 .default_value(true)
35 .hide_value()
36 .supports_field()
37 .description("Faces to participate in the unwrap operation");
38 b.add_input<decl::Bool>("Seam").hide_value().supports_field().description(
39 "Edges to mark where the mesh is \"cut\" for the purposes of unwrapping");
40 b.add_input<decl::Float>("Margin").default_value(0.001f).min(0.0f).max(1.0f).description(
41 "Space between islands");
42 b.add_input<decl::Bool>("Fill Holes")
43 .default_value(true)
45 "Virtually fill holes in mesh before unwrapping, to better avoid overlaps "
46 "and preserve symmetry");
47 b.add_input<decl::Menu>("Method").static_items(method_items).optional_label();
48 b.add_output<decl::Vector>("UV").field_source_reference_all().description(
49 "UV coordinates between 0 and 1 for each face corner in the selected faces");
50}
51
52static void node_init(bNodeTree * /*tree*/, bNode *node)
53{
54 /* Still used for forward compatibility. */
56}
57
59 const Field<bool> selection_field,
60 const Field<bool> seam_field,
61 const bool fill_holes,
62 const float margin,
63 const GeometryNodeUVUnwrapMethod method,
64 const AttrDomain domain)
65{
66 const Span<float3> positions = mesh.vert_positions();
67 const Span<int2> edges = mesh.edges();
68 const OffsetIndices faces = mesh.faces();
69 const Span<int> corner_verts = mesh.corner_verts();
70
71 const bke::MeshFieldContext face_context{mesh, AttrDomain::Face};
72 FieldEvaluator face_evaluator{face_context, faces.size()};
73 face_evaluator.add(selection_field);
74 face_evaluator.evaluate();
75 const IndexMask selection = face_evaluator.get_evaluated_as_mask(0);
76 if (selection.is_empty()) {
77 return {};
78 }
79
80 const bke::MeshFieldContext edge_context{mesh, AttrDomain::Edge};
81 FieldEvaluator edge_evaluator{edge_context, edges.size()};
82 edge_evaluator.add(seam_field);
83 edge_evaluator.evaluate();
84 const IndexMask seam = edge_evaluator.get_evaluated_as_mask(0);
85
86 Array<float3> uv(corner_verts.size(), float3(0));
87
89 selection.foreach_index([&](const int face_index) {
90 const IndexRange face = faces[face_index];
91 Array<geometry::ParamKey, 16> mp_vkeys(face.size());
92 Array<bool, 16> mp_pin(face.size());
93 Array<bool, 16> mp_select(face.size());
94 Array<const float *, 16> mp_co(face.size());
95 Array<float *, 16> mp_uv(face.size());
96 for (const int i : IndexRange(face.size())) {
97 const int corner = face[i];
98 const int vert = corner_verts[corner];
99 mp_vkeys[i] = vert;
100 mp_co[i] = positions[vert];
101 mp_uv[i] = uv[corner];
102 mp_pin[i] = false;
103 mp_select[i] = false;
104 }
106 face_index,
107 face.size(),
108 mp_vkeys.data(),
109 mp_co.data(),
110 mp_uv.data(),
111 nullptr,
112 mp_pin.data(),
113 mp_select.data());
114 });
115
116 seam.foreach_index([&](const int i) {
117 geometry::ParamKey vkeys[2]{uint(edges[i][0]), uint(edges[i][1])};
119 });
120
122 params.margin = margin;
123 params.rotate_method = ED_UVPACK_ROTATION_ANY;
124
125 /* TODO: once field input nodes are able to emit warnings (#94039), emit a
126 * warning if we fail to solve an island. */
127 geometry::uv_parametrizer_construct_end(handle, fill_holes, false, nullptr);
128
130 handle, false, method == GEO_NODE_UV_UNWRAP_METHOD_ANGLE_BASED);
131 geometry::uv_parametrizer_lscm_solve(handle, nullptr, nullptr);
133 geometry::uv_parametrizer_average(handle, true, false, false);
136 delete (handle);
137
138 return mesh.attributes().adapt_domain<float3>(
139 VArray<float3>::from_container(std::move(uv)), AttrDomain::Corner, domain);
140}
141
143 private:
144 const Field<bool> selection_;
145 const Field<bool> seam_;
146 const bool fill_holes_;
147 const float margin_;
148 const GeometryNodeUVUnwrapMethod method_;
149
150 public:
152 const Field<bool> seam,
153 const bool fill_holes,
154 const float margin,
155 const GeometryNodeUVUnwrapMethod method)
156 : bke::MeshFieldInput(CPPType::get<float3>(), "UV Unwrap Field"),
157 selection_(selection),
158 seam_(seam),
159 fill_holes_(fill_holes),
160 margin_(margin),
161 method_(method)
162 {
164 }
165
167 const AttrDomain domain,
168 const IndexMask & /*mask*/) const final
169 {
170 return construct_uv_gvarray(mesh, selection_, seam_, fill_holes_, margin_, method_, domain);
171 }
172
173 void for_each_field_input_recursive(FunctionRef<void(const FieldInput &)> fn) const override
174 {
175 selection_.node().for_each_field_input_recursive(fn);
176 seam_.node().for_each_field_input_recursive(fn);
177 }
178
179 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
180 {
181 return AttrDomain::Corner;
182 }
183};
184
186{
187 const auto method = params.get_input<GeometryNodeUVUnwrapMethod>("Method");
188 const Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
189 const Field<bool> seam_field = params.extract_input<Field<bool>>("Seam");
190 const bool fill_holes = params.extract_input<bool>("Fill Holes");
191 const float margin = params.extract_input<float>("Margin");
192 params.set_output("UV",
193 Field<float3>(std::make_shared<UnwrapFieldInput>(
194 selection_field, seam_field, fill_holes, margin, method)));
195}
196
197static void node_register()
198{
199 static blender::bke::bNodeType ntype;
200
201 geo_node_type_base(&ntype, "GeometryNodeUVUnwrap", GEO_NODE_UV_UNWRAP);
202 ntype.ui_name = "UV Unwrap";
203 ntype.ui_description = "Generate a UV map based on seam edges";
204 ntype.enum_name_legacy = "UV_UNWRAP";
206 ntype.initfunc = node_init;
208 ntype, "NodeGeometryUVUnwrap", node_free_standard_storage, node_copy_standard_storage);
209 ntype.declare = node_declare;
212}
214
215} // namespace blender::nodes::node_geo_uv_unwrap_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1240
#define GEO_NODE_UV_UNWRAP
#define final(a, b, c)
Definition BLI_hash.h:19
unsigned int uint
GeometryNodeUVUnwrapMethod
@ GEO_NODE_UV_UNWRAP_METHOD_CONFORMAL
@ GEO_NODE_UV_UNWRAP_METHOD_ANGLE_BASED
@ ED_UVPACK_ROTATION_ANY
#define NOD_REGISTER_NODE(REGISTER_FUNC)
AttributeSet attributes
const T * data() const
Definition BLI_array.hh:312
constexpr int64_t size() const
constexpr int64_t size() const
Definition BLI_span.hh:252
static VArray from_container(ContainerT container)
FieldInput(const CPPType &type, std::string debug_name="")
Definition field.cc:677
int add(GField field, GVArray *varray_ptr)
Definition field.cc:751
IndexMask get_evaluated_as_mask(int field_index)
Definition field.cc:804
void foreach_index(Fn &&fn) const
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
std::optional< AttrDomain > preferred_domain(const Mesh &) const override
UnwrapFieldInput(const Field< bool > selection, const Field< bool > seam, const bool fill_holes, const float margin, const GeometryNodeUVUnwrapMethod method)
void for_each_field_input_recursive(FunctionRef< void(const FieldInput &)> fn) const override
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
static char faces[256]
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
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:5414
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_end(ParamHandle *handle)
void uv_parametrizer_pack(ParamHandle *handle, const UVPackIsland_Params &params)
void uv_parametrizer_lscm_begin(ParamHandle *handle, bool live, bool abf)
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 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 AttrDomain domain)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_init(bNodeTree *, bNode *node)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
void * storage
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
i
Definition text_draw.cc:230
#define N_(msgid)