Fitting UV islands into a grid

calendar_today

22.11.2023

label

VEX, Texturing

mouse

Houdini 20

Description

Proportionally scaling and positioning each UV island into a unique grid cell based on UV connectivity and per-primitive bounding boxes.

The polygons of the grid can potentially be scaled and moved individually to assign varying cell sizes for the UV islands, enable facet1 and primitive1 nodes to see this.

1 Code

float pad = chf('padding');
int class = prim(0, 'class', i@primnum);

// GRID
string geo_tile = geounwrap(1, 'uv');
vector tile_min = getbbox_min(geo_tile, itoa(class));
vector tile_max = getbbox_max(geo_tile, itoa(class));
vector size_tile = getbbox_size(geo_tile, itoa(class));

// MESH
string geo_mesh = geounwrap(0, 'uv');
string grp_mesh = '@class==' + itoa(class);
vector size_mesh = getbbox_size(geo_mesh, '@class==' + itoa(class));

// ASPECT
vector aspect_tile = size_tile / min(size_tile.x, size_tile.y);
vector aspect_mesh = size_mesh / max(size_mesh.x, size_mesh.y);
vector aspect = aspect_mesh / aspect_tile;

// FIT
vector bb = relbbox(geo_mesh, grp_mesh, v@uv) * aspect;
bb = fit01(bb, pad, 1.0 - pad);
vector uv = fit01(bb, tile_min, tile_max);

v@uv = uv;
download

Downloads