Blender V5.3
node_geo_uv_pack_islands.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 "GEO_uv_pack.hh"
7
8#include "DNA_mesh_types.h"
9
10#include "node_geometry_util.hh"
11
13
15enum class ShapeMethod : int16_t {
16 Aabb = 0,
17 Convex = 1,
19};
20
23 "AABB",
24 0,
25 N_("Bounding Box"),
26 N_("Use axis-aligned bounding boxes for packing (fastest, least space efficient)")},
28 "CONVEX",
29 0,
30 N_("Convex Hull"),
31 N_("Use convex hull approximation of islands (good balance of speed and space efficiency)")},
33 "CONCAVE",
34 0,
35 N_("Exact Shape"),
36 N_("Use exact geometry for most efficient packing (slowest)")},
37 {0, nullptr, 0, nullptr, nullptr},
38};
39
53
55{
56 b.use_custom_socket_order();
57 b.allow_any_socket_order();
58 b.add_input<decl::Vector>("UV"_ustr).hide_value().structure_type(StructureType::Field);
59 b.add_output<decl::Vector>("UV"_ustr)
60 .structure_type(StructureType::Field)
61 .propagate_references()
62 .align_with_previous();
63 b.add_input<decl::Bool>("Selection"_ustr)
64 .default_value(true)
65 .hide_value()
66 .structure_type(StructureType::Field)
67 .description("Faces to consider when packing islands");
68 b.add_input<decl::Float>("Margin"_ustr)
69 .default_value(0.001f)
70 .min(0.0f)
71 .max(1.0f)
72 .description("Space between islands");
73 b.add_input<decl::Bool>("Rotate"_ustr)
74 .default_value(true)
75 .description("Rotate islands for best fit");
76 b.add_input<decl::Menu>("Method"_ustr)
77 .static_items(shape_method_items)
79 .optional_label()
80 .description("Method used for packing UV islands");
81 b.add_input<decl::Vector>("Bottom Left"_ustr)
82 .default_value({0.0f, 0.0f})
83 .dimensions(2)
85 .description("Bottom-left corner of packing bounds");
86 b.add_input<decl::Vector>("Top Right"_ustr)
87 .default_value({1.0f, 1.0f})
88 .dimensions(2)
90 .description("Top-right corner of packing bounds");
91}
92
94 const Field<bool> selection_field,
95 const Field<float3> uv_field,
96 const bool rotate,
97 const float margin,
98 const eUVPackIsland_ShapeMethod shape_method,
99 const float3 bottom,
100 const float3 top,
101 const AttrDomain domain)
102{
103 const Span<float3> positions = mesh.vert_positions();
104 const OffsetIndices faces = mesh.faces();
105 const Span<int> corner_verts = mesh.corner_verts();
106
107 const bke::MeshFieldContext face_context{mesh, AttrDomain::Face};
108 FieldEvaluator face_evaluator{face_context, faces.size()};
109 face_evaluator.add(selection_field);
110 face_evaluator.evaluate();
111 const IndexMask selection = face_evaluator.get_evaluated_as_mask(0);
112 if (selection.is_empty()) {
113 return {};
114 }
115
116 const bke::MeshFieldContext corner_context{mesh, AttrDomain::Corner};
117 FieldEvaluator evaluator{corner_context, mesh.corners_num};
118 Array<float3> uv(mesh.corners_num);
119 evaluator.add_with_destination(uv_field, uv.as_mutable_span());
120 evaluator.evaluate();
121
123 selection.foreach_index([&](const int face_index) {
124 const IndexRange face = faces[face_index];
125 Array<geometry::ParamKey, 16> mp_vkeys(face.size());
126 Array<bool, 16> mp_pin(face.size());
127 Array<bool, 16> mp_select(face.size());
128 Array<const float *, 16> mp_co(face.size());
129 Array<float *, 16> mp_uv(face.size());
130 for (const int i : IndexRange(face.size())) {
131 const int corner = face[i];
132 const int vert = corner_verts[corner];
133 mp_vkeys[i] = vert;
134 mp_co[i] = positions[vert];
135 mp_uv[i] = uv[corner];
136 mp_pin[i] = false;
137 mp_select[i] = false;
138 }
140 face_index,
141 face.size(),
142 mp_vkeys.data(),
143 mp_co.data(),
144 mp_uv.data(),
145 nullptr,
146 mp_pin.data(),
147 mp_select.data());
148 });
149 geometry::uv_parametrizer_construct_end(handle, true, true, nullptr);
150
152 params.shape_method = shape_method;
154 params.margin = margin;
155 if (top.x > bottom.x && top.y > bottom.y) {
156 params.udim_base_offset[0] = bottom.x;
157 params.udim_base_offset[1] = bottom.y;
158 params.target_extent = top.y - bottom.y;
159 params.target_aspect_y = (top.x - bottom.x) / (top.y - bottom.y);
160 }
161 geometry::uv_parametrizer_pack(handle, params);
163 delete (handle);
164
165 return mesh.attributes().adapt_domain<float3>(
166 VArray<float3>::from_container(std::move(uv)), AttrDomain::Corner, domain);
167}
168
170 private:
171 const Field<bool> selection_field_;
172 const Field<float3> uv_field_;
173 const bool rotate_;
174 const float margin_;
175 const eUVPackIsland_ShapeMethod shape_method_;
176 const float3 bottom_;
177 const float3 top_;
178
179 public:
180 PackIslandsFieldInput(const Field<bool> selection_field,
181 const Field<float3> uv_field,
182 const bool rotate,
183 const float margin,
184 const eUVPackIsland_ShapeMethod shape_method,
185 const float3 bottom,
186 const float3 top)
187 : bke::MeshFieldInput(CPPType::get<float3>(), "Pack UV Islands Field"),
188 selection_field_(selection_field),
189 uv_field_(uv_field),
190 rotate_(rotate),
191 margin_(margin),
192 shape_method_(shape_method),
193 bottom_(bottom),
194 top_(top)
195 {
196 }
197
199 const AttrDomain domain,
200 const IndexMask & /*mask*/) const final
201 {
203 mesh, selection_field_, uv_field_, rotate_, margin_, shape_method_, bottom_, top_, domain);
204 }
205
206 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const override
207 {
208 fn(selection_field_);
209 fn(uv_field_);
210 }
211
212 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
213 {
214 return AttrDomain::Corner;
215 }
216};
217
219{
220 const ShapeMethod local_shape_method = params.get_input<ShapeMethod>("Method"_ustr);
221 const eUVPackIsland_ShapeMethod shape_method = convert_shape_method(local_shape_method);
222
223 const Field<bool> selection_field = params.extract_input<Field<bool>>("Selection"_ustr);
224 const Field<float3> uv_field = params.extract_input<Field<float3>>("UV"_ustr);
225 const bool rotate = params.extract_input<bool>("Rotate"_ustr);
226 const float margin = params.extract_input<float>("Margin"_ustr);
227 const float3 bottom = params.extract_input<float3>("Bottom Left"_ustr);
228 const float3 top = params.extract_input<float3>("Top Right"_ustr);
229 params.set_output("UV"_ustr,
231 selection_field, uv_field, rotate, margin, shape_method, bottom, top));
232}
233
234static void node_register()
235{
236 static bke::bNodeType ntype;
237
238 geo_node_type_base(&ntype, "GeometryNodeUVPackIslands"_ustr, GEO_NODE_UV_PACK_ISLANDS);
239 ntype.ui_name = "Pack UV Islands";
240 ntype.ui_description =
241 "Scale islands of a UV map and move them so they fill the UV space as much as possible";
242 ntype.enum_name_legacy = "UV_PACK_ISLANDS";
244 ntype.declare = node_declare;
247}
249
250} // namespace blender::nodes::node_geo_uv_pack_islands_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define GEO_NODE_UV_PACK_ISLANDS
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define BLI_assert_unreachable()
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
const T * data() const
Definition BLI_array.hh:323
constexpr int64_t size() const
constexpr IndexRange()=default
static VArray from_container(ContainerT container)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_as_mask(int field_index)
int add_with_destination(GField field, GVMutableArray dst)
eUVPackIsland_RotationMethod rotate_method
eUVPackIsland_ShapeMethod shape_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)
void foreach_recursive_field(FunctionRef< void(const GField &)> fn) const override
PackIslandsFieldInput(const Field< bool > selection_field, const Field< float3 > uv_field, const bool rotate, const float margin, const eUVPackIsland_ShapeMethod shape_method, const float3 bottom, const float3 top)
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
std::optional< AttrDomain > preferred_domain(const Mesh &) const override
float3x3 rotate(float3x3 mat, AxisAngle rotation)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void uv_parametrizer_construct_end(ParamHandle *phandle, bool fill_holes, bool topology_from_uvs, int *r_count_failed=nullptr)
void uv_parametrizer_flush(ParamHandle *handle)
void uv_parametrizer_pack(ParamHandle *handle, const UVPackIsland_Params &params)
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_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
static eUVPackIsland_ShapeMethod convert_shape_method(const ShapeMethod method)
static VArray< float3 > construct_uv_gvarray(const Mesh &mesh, const Field< bool > selection_field, const Field< float3 > uv_field, const bool rotate, const float margin, const eUVPackIsland_ShapeMethod shape_method, const float3 bottom, const float3 top, const AttrDomain domain)
@ ED_UVPACK_ROTATION_NONE
@ ED_UVPACK_ROTATION_ANY
VecBase< float, 3 > float3
eUVPackIsland_ShapeMethod
@ ED_UVPACK_SHAPE_CONVEX
@ ED_UVPACK_SHAPE_CONCAVE
@ ED_UVPACK_SHAPE_AABB
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed short int16_t
Definition BKE_text.h:77
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
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)