mirror of
https://github.com/bevyengine/bevy
synced 2024-12-30 06:53:13 +00:00
ffecb05a0a
This makes the [New Bevy Renderer](#2535) the default (and only) renderer. The new renderer isn't _quite_ ready for the final release yet, but I want as many people as possible to start testing it so we can identify bugs and address feedback prior to release. The examples are all ported over and operational with a few exceptions: * I removed a good portion of the examples in the `shader` folder. We still have some work to do in order to make these examples possible / ergonomic / worthwhile: #3120 and "high level shader material plugins" are the big ones. This is a temporary measure. * Temporarily removed the multiple_windows example: doing this properly in the new renderer will require the upcoming "render targets" changes. Same goes for the render_to_texture example. * Removed z_sort_debug: entity visibility sort info is no longer available in app logic. we could do this on the "render app" side, but i dont consider it a priority.
100 lines
3.6 KiB
Rust
100 lines
3.6 KiB
Rust
use crate::{Rect, TextureAtlas};
|
|
use bevy_asset::Assets;
|
|
use bevy_math::Vec2;
|
|
use bevy_render::texture::{Image, TextureFormatPixelInfo};
|
|
use guillotiere::{size2, Allocation, AtlasAllocator};
|
|
|
|
pub struct DynamicTextureAtlasBuilder {
|
|
pub atlas_allocator: AtlasAllocator,
|
|
pub padding: i32,
|
|
}
|
|
|
|
impl DynamicTextureAtlasBuilder {
|
|
pub fn new(size: Vec2, padding: i32) -> Self {
|
|
Self {
|
|
atlas_allocator: AtlasAllocator::new(to_size2(size)),
|
|
padding,
|
|
}
|
|
}
|
|
|
|
pub fn add_texture(
|
|
&mut self,
|
|
texture_atlas: &mut TextureAtlas,
|
|
textures: &mut Assets<Image>,
|
|
texture: &Image,
|
|
) -> Option<usize> {
|
|
let allocation = self.atlas_allocator.allocate(size2(
|
|
texture.texture_descriptor.size.width as i32 + self.padding,
|
|
texture.texture_descriptor.size.height as i32 + self.padding,
|
|
));
|
|
if let Some(allocation) = allocation {
|
|
let atlas_texture = textures.get_mut(&texture_atlas.texture).unwrap();
|
|
self.place_texture(atlas_texture, allocation, texture);
|
|
let mut rect: Rect = allocation.rectangle.into();
|
|
rect.max.x -= self.padding as f32;
|
|
rect.max.y -= self.padding as f32;
|
|
Some(texture_atlas.add_texture(rect))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
// fn resize(
|
|
// &mut self,
|
|
// texture_atlas: &mut TextureAtlas,
|
|
// textures: &mut Assets<Texture>,
|
|
// size: Vec2,
|
|
// ) {
|
|
// let new_size2 = to_size2(new_size);
|
|
// self.atlas_texture = Texture::new_fill(new_size, &[0,0,0,0]);
|
|
// let change_list = self.atlas_allocator.resize_and_rearrange(new_size2);
|
|
|
|
// for change in change_list.changes {
|
|
// if let Some(changed_texture_handle) = self.allocation_textures.remove(&change.old.id)
|
|
// { let changed_texture = textures.get(&changed_texture_handle).unwrap();
|
|
// self.place_texture(change.new, changed_texture_handle, changed_texture);
|
|
// }
|
|
// }
|
|
|
|
// for failure in change_list.failures {
|
|
// let failed_texture = self.allocation_textures.remove(&failure.id).unwrap();
|
|
// queued_textures.push(failed_texture);
|
|
// }
|
|
// }
|
|
|
|
fn place_texture(
|
|
&mut self,
|
|
atlas_texture: &mut Image,
|
|
allocation: Allocation,
|
|
texture: &Image,
|
|
) {
|
|
let mut rect = allocation.rectangle;
|
|
rect.max.x -= self.padding;
|
|
rect.max.y -= self.padding;
|
|
let atlas_width = atlas_texture.texture_descriptor.size.width as usize;
|
|
let rect_width = rect.width() as usize;
|
|
let format_size = atlas_texture.texture_descriptor.format.pixel_size();
|
|
|
|
for (texture_y, bound_y) in (rect.min.y..rect.max.y).map(|i| i as usize).enumerate() {
|
|
let begin = (bound_y * atlas_width + rect.min.x as usize) * format_size;
|
|
let end = begin + rect_width * format_size;
|
|
let texture_begin = texture_y * rect_width * format_size;
|
|
let texture_end = texture_begin + rect_width * format_size;
|
|
atlas_texture.data[begin..end]
|
|
.copy_from_slice(&texture.data[texture_begin..texture_end]);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<guillotiere::Rectangle> for Rect {
|
|
fn from(rectangle: guillotiere::Rectangle) -> Self {
|
|
Rect {
|
|
min: Vec2::new(rectangle.min.x as f32, rectangle.min.y as f32),
|
|
max: Vec2::new(rectangle.max.x as f32, rectangle.max.y as f32),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn to_size2(vec2: Vec2) -> guillotiere::Size {
|
|
guillotiere::Size::new(vec2.x as i32, vec2.y as i32)
|
|
}
|