From 85ecab8bb9dd1f890cabc3faa11c080864db1621 Mon Sep 17 00:00:00 2001 From: rod-salazar <51817379+rod-salazar@users.noreply.github.com> Date: Sat, 21 Nov 2020 12:55:25 -0800 Subject: [PATCH] Tweaks to TextureAtlasBuilder.finish() (#887) Tweaks to TextureAtlasBuilder.finish() --- .../bevy_sprite/src/texture_atlas_builder.rs | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/crates/bevy_sprite/src/texture_atlas_builder.rs b/crates/bevy_sprite/src/texture_atlas_builder.rs index c50553e7723ab..262667a8d65ed 100644 --- a/crates/bevy_sprite/src/texture_atlas_builder.rs +++ b/crates/bevy_sprite/src/texture_atlas_builder.rs @@ -86,28 +86,37 @@ impl TextureAtlasBuilder { while rect_placements.is_none() { if current_width > max_width || current_height > max_height { - rect_placements = None; break; } + + let last_attempt = current_height == max_height && current_width == max_width; + let mut target_bins = std::collections::BTreeMap::new(); target_bins.insert(0, TargetBin::new(current_width, current_height, 1)); - atlas_texture = Texture::new_fill( - Vec2::new(current_width as f32, current_height as f32), - &[0, 0, 0, 0], - TextureFormat::Rgba8UnormSrgb, - ); + rect_placements = match pack_rects( &self.rects_to_place, target_bins, &volume_heuristic, &contains_smallest_box, ) { - Ok(rect_placements) => Some(rect_placements), + Ok(rect_placements) => { + atlas_texture = Texture::new_fill( + Vec2::new(current_width as f32, current_height as f32), + &[0, 0, 0, 0], + TextureFormat::Rgba8UnormSrgb, + ); + Some(rect_placements) + } Err(rectangle_pack::RectanglePackError::NotEnoughBinSpace) => { - current_width *= 2; - current_height *= 2; + current_height = bevy_math::clamp(current_height * 2, 0, max_height); + current_width = bevy_math::clamp(current_width * 2, 0, max_width); None } + }; + + if last_attempt { + break; } }