diff options
author | Siddhartha Jejurkar <sidd017> | 2021-05-12 17:08:16 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2021-05-12 17:12:56 +0300 |
commit | c1f7f18a8e6c82fddaa6ec06b7b78e3d2c64a1c8 (patch) | |
tree | e08d4df32c81b9278ef63a4aecc69d9eee3ed6f2 /source/blender/blenkernel/intern | |
parent | 79b7c46eecdf87dafd5ccb9ceafb174be7dd233e (diff) |
Fix T81247: Constrain selected UVs to correct UDIM
With Constrain to Image Bounds selected, UVs will be constrained to the
correct/closest UDIM if the image is tiled.
UVs will be constrained to the 0-1 UV space if the image is not tiled.
This will override the present behavior of always constraining selected
UVs to the 0-1 UV space (UDIM 1001).
Reviewed By: campbellbarton
Ref D11202
Diffstat (limited to 'source/blender/blenkernel/intern')
-rw-r--r-- | source/blender/blenkernel/intern/image.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/source/blender/blenkernel/intern/image.c b/source/blender/blenkernel/intern/image.c index 368b1c2e66b..2f7e2b41a73 100644 --- a/source/blender/blenkernel/intern/image.c +++ b/source/blender/blenkernel/intern/image.c @@ -735,6 +735,37 @@ int BKE_image_get_tile_from_pos(struct Image *ima, return tile_number; } +/** + * Return the tile_number for the closest UDIM tile. + */ +int BKE_image_find_nearest_tile(const Image *image, const float co[2]) +{ + const float co_floor[2] = {floorf(co[0]), floorf(co[1])}; + /* Distance to the closest UDIM tile. */ + float dist_best_sq = FLT_MAX; + int tile_number_best = -1; + + LISTBASE_FOREACH (const ImageTile *, tile, &image->tiles) { + const int tile_index = tile->tile_number - 1001; + /* Coordinates of the current tile. */ + const float tile_index_co[2] = {tile_index % 10, tile_index / 10}; + + if (equals_v2v2(co_floor, tile_index_co)) { + return tile->tile_number; + } + + /* Distance between co[2] and UDIM tile. */ + const float dist_sq = len_squared_v2v2(tile_index_co, co); + + if (dist_sq < dist_best_sq) { + dist_best_sq = dist_sq; + tile_number_best = tile->tile_number; + } + } + + return tile_number_best; +} + static void image_init_color_management(Image *ima) { ImBuf *ibuf; |