Tweaks to TextureAtlasBuilder.finish() (#887)

Tweaks to TextureAtlasBuilder.finish()
This commit is contained in:
rod-salazar 2020-11-21 12:55:25 -08:00 committed by GitHub
parent 106486bd99
commit 85ecab8bb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
}
}