Skip to content

Commit

Permalink
Tweaks to TextureAtlasBuilder.finish() (#887)
Browse files Browse the repository at this point in the history
Tweaks to TextureAtlasBuilder.finish()
  • Loading branch information
4-rodrigo-salazar authored Nov 21, 2020
1 parent 106486b commit 85ecab8
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions crates/bevy_sprite/src/texture_atlas_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 85ecab8

Please sign in to comment.