Blender V4.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
6
7#include "BKE_mesh.hh"
8
9#include "UI_interface.hh"
10#include "UI_resources.hh"
11
12#include "NOD_rna_define.hh"
13
14#include "node_geometry_util.hh"
15
17
19
21{
22 b.add_input<decl::Bool>("Selection")
23 .default_value(true)
24 .hide_value()
25 .supports_field()
26 .description("Faces to participate in the unwrap operation");
27 b.add_input<decl::Bool>("Seam").hide_value().supports_field().description(
28 "Edges to mark where the mesh is \"cut\" for the purposes of unwrapping");
29 b.add_input<decl::Float>("Margin").default_value(0.001f).min(0.0f).max(1.0f).description(
30 "Space between islands");
31 b.add_input<decl::Bool>("Fill Holes")
32 .default_value(true)
34 "Virtually fill holes in mesh before unwrapping, to better avoid overlaps "
35 "and preserve symmetry");
36 b.add_output<decl::Vector>("UV").field_source_reference_all().description(
37 "UV coordinates between 0 and 1 for each face corner in the selected faces");
38}
39
40static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
41{
42 uiLayoutSetPropSep(layout, true);
43 uiLayoutSetPropDecorate(layout, false);
44 uiItemR(layout, ptr, "method", UI_ITEM_NONE, "", ICON_NONE);
45}
46
47static void node_init(bNodeTree * /*tree*/, bNode *node)
48{
49 NodeGeometryUVUnwrap *data = MEM_cnew<NodeGeometryUVUnwrap>(__func__);
51 node->storage = data;
52}
53
55 const Field<bool> selection_field,
56 const Field<bool> seam_field,
57 const bool fill_holes,
58 const float margin,
59 const GeometryNodeUVUnwrapMethod method,
60 const AttrDomain domain)
61{
62 const Span<float3> positions = mesh.vert_positions();
63 const Span<int2> edges = mesh.edges();
64 const OffsetIndices faces = mesh.faces();
65 const Span<int> corner_verts = mesh.corner_verts();
66
67 const bke::MeshFieldContext face_context{mesh, AttrDomain::Face};
68 FieldEvaluator face_evaluator{face_context, faces.size()};
69 face_evaluator.add(selection_field);
70 face_evaluator.evaluate();
71 const IndexMask selection = face_evaluator.get_evaluated_as_mask(0);
72 if (selection.is_empty()) {
73 return {};
74 }
75
76 const bke::MeshFieldContext edge_context{mesh, AttrDomain::Edge};
77 FieldEvaluator edge_evaluator{edge_context, edges.size()};
78 edge_evaluator.add(seam_field);
79 edge_evaluator.evaluate();
80 const IndexMask seam = edge_evaluator.get_evaluated_as_mask(0);
81
82 Array<float3> uv(corner_verts.size(), float3(0));
83
85 selection.foreach_index([&](const int face_index) {
86 const IndexRange face = faces[face_index];
87 Array<geometry::ParamKey, 16> mp_vkeys(face.size());
88 Array<bool, 16> mp_pin(face.size());
89 Array<bool, 16> mp_select(face.size());
90 Array<const float *, 16> mp_co(face.size());
91 Array<float *, 16> mp_uv(face.size());
92 for (const int i : IndexRange(face.size())) {
93 const int corner = face[i];
94 const int vert = corner_verts[corner];
95 mp_vkeys[i] = vert;
96 mp_co[i] = positions[vert];
97 mp_uv[i] = uv[corner];
98 mp_pin[i] = false;
99 mp_select[i] = false;
100 }
102 face_index,
103 face.size(),
104 mp_vkeys.data(),
105 mp_co.data(),
106 mp_uv.data(),
107 nullptr,
108 mp_pin.data(),
109 mp_select.data());
110 });
111
112 seam.foreach_index([&](const int i) {
113 geometry::ParamKey vkeys[2]{uint(edges[i][0]), uint(edges[i][1])};
115 });
116
117 /* TODO: once field input nodes are able to emit warnings (#94039), emit a
118 * warning if we fail to solve an island. */
119 geometry::uv_parametrizer_construct_end(handle, fill_holes, false, nullptr);
120
122 handle, false, method == GEO_NODE_UV_UNWRAP_METHOD_ANGLE_BASED);
123 geometry::uv_parametrizer_lscm_solve(handle, nullptr, nullptr);
125 geometry::uv_parametrizer_average(handle, true, false, false);
126 geometry::uv_parametrizer_pack(handle, margin, true, true);
128 delete (handle);
129
130 return mesh.attributes().adapt_domain<float3>(
131 VArray<float3>::ForContainer(std::move(uv)), AttrDomain::Corner, domain);
132}
133
135 private:
136 const Field<bool> selection_;
137 const Field<bool> seam_;
138 const bool fill_holes_;
139 const float margin_;
140 const GeometryNodeUVUnwrapMethod method_;
141
142 public:
144 const Field<bool> seam,
145 const bool fill_holes,
146 const float margin,
147 const GeometryNodeUVUnwrapMethod method)
148 : bke::MeshFieldInput(CPPType::get<float3>(), "UV Unwrap Field"),
149 selection_(selection),
150 seam_(seam),
151 fill_holes_(fill_holes),
152 margin_(margin),
153 method_(method)
154 {
156 }
157
159 const AttrDomain domain,
160 const IndexMask & /*mask*/) const final
161 {
162 return construct_uv_gvarray(mesh, selection_, seam_, fill_holes_, margin_, method_, domain);
163 }
164
165 void for_each_field_input_recursive(FunctionRef<void(const FieldInput &)> fn) const override
166 {
167 selection_.node().for_each_field_input_recursive(fn);
169 }
170
171 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
172 {
173 return AttrDomain::Corner;
174 }
175};
176
178{
179 const NodeGeometryUVUnwrap &storage = node_storage(params.node());
181 const Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
182 const Field<bool> seam_field = params.extract_input<Field<bool>>("Seam");
183 const bool fill_holes = params.extract_input<bool>("Fill Holes");
184 const float margin = params.extract_input<float>("Margin");
185 params.set_output("UV",
186 Field<float3>(std::make_shared<UnwrapFieldInput>(
187 selection_field, seam_field, fill_holes, margin, method)));
188}
189
190static void node_rna(StructRNA *srna)
191{
192 static EnumPropertyItem method_items[] = {
194 "ANGLE_BASED",
195 0,
196 "Angle Based",
197 "This method gives a good 2D representation of a mesh"},
199 "CONFORMAL",
200 0,
201 "Conformal",
202 "Uses LSCM (Least Squares Conformal Mapping). This usually gives a less accurate UV "
203 "mapping than Angle Based, but works better for simpler objects"},
204 {0, nullptr, 0, nullptr, nullptr},
205 };
206
208 srna, "method", "Method", "", method_items, NOD_storage_enum_accessors(method));
209}
210
211static void node_register()
212{
213 static blender::bke::bNodeType ntype;
214
215 geo_node_type_base(&ntype, GEO_NODE_UV_UNWRAP, "UV Unwrap", NODE_CLASS_CONVERTER);
216 ntype.initfunc = node_init;
218 &ntype, "NodeGeometryUVUnwrap", node_free_standard_storage, node_copy_standard_storage);
219 ntype.declare = node_declare;
223
224 node_rna(ntype.rna_ext.srna);
225}
227
228} // namespace blender::nodes::node_geo_uv_unwrap_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:410
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1799
unsigned int uint
GeometryNodeUVUnwrapMethod
@ GEO_NODE_UV_UNWRAP_METHOD_CONFORMAL
@ GEO_NODE_UV_UNWRAP_METHOD_ANGLE_BASED
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
#define UI_ITEM_NONE
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
const T * data() const
Definition BLI_array.hh:301
constexpr int64_t size() const
Definition BLI_span.hh:253
static VArray ForContainer(ContainerT container)
int add(GField field, GVArray *varray_ptr)
Definition field.cc:756
virtual void for_each_field_input_recursive(FunctionRef< void(const FieldInput &)> fn) const
Definition field.cc:587
const FieldNode & node() const
Definition FN_field.hh:137
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
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_type_storage(bNodeType *ntype, const char *storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:4632
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
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_lscm_begin(ParamHandle *handle, bool live, bool abf)
void uv_parametrizer_lscm_solve(ParamHandle *handle, int *count_changed, int *count_failed)
void uv_parametrizer_pack(ParamHandle *handle, float margin, bool do_rotate, bool ignore_pinned)
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_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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_rna(StructRNA *srna)
static void node_init(bNodeTree *, bNode *node)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:46
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:58
StructRNA * srna
Definition RNA_types.hh:780
Defines a node type.
Definition BKE_node.hh:218
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeDeclareFunction declare
Definition BKE_node.hh:347
PointerRNA * ptr
Definition wm_files.cc:4126