mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 12:43:34 +00:00
Re-implement Automatic Sprite Sizing (#2613)
# Objective - Prevent the need to specify a sprite size when using the pipelined sprite renderer ## Solution - Re-introduce the sprite auto resize system from the old renderer
This commit is contained in:
parent
b5b9a95981
commit
c8330037e7
3 changed files with 32 additions and 8 deletions
|
@ -1,14 +1,14 @@
|
||||||
use bevy::{
|
use bevy::{
|
||||||
asset::LoadState,
|
asset::LoadState,
|
||||||
math::{Vec2, Vec3},
|
math::Vec3,
|
||||||
prelude::{
|
prelude::{
|
||||||
App, AssetServer, Assets, Commands, HandleUntyped, IntoSystem, Res, ResMut, State,
|
App, AssetServer, Assets, Commands, HandleUntyped, IntoSystem, Res, ResMut, State,
|
||||||
SystemSet, Transform,
|
SystemSet, Transform,
|
||||||
},
|
},
|
||||||
render2::{camera::OrthographicCameraBundle, texture::Image},
|
render2::{camera::OrthographicCameraBundle, texture::Image},
|
||||||
sprite2::{
|
sprite2::{
|
||||||
PipelinedSpriteBundle, PipelinedSpriteSheetBundle, Sprite, TextureAtlas,
|
PipelinedSpriteBundle, PipelinedSpriteSheetBundle, TextureAtlas, TextureAtlasBuilder,
|
||||||
TextureAtlasBuilder, TextureAtlasSprite,
|
TextureAtlasSprite,
|
||||||
},
|
},
|
||||||
PipelinedDefaultPlugins,
|
PipelinedDefaultPlugins,
|
||||||
};
|
};
|
||||||
|
@ -87,10 +87,6 @@ fn setup(
|
||||||
});
|
});
|
||||||
// draw the atlas itself
|
// draw the atlas itself
|
||||||
commands.spawn_bundle(PipelinedSpriteBundle {
|
commands.spawn_bundle(PipelinedSpriteBundle {
|
||||||
sprite: Sprite {
|
|
||||||
size: Vec2::new(512.0, 512.0),
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
texture: texture_atlas_texture,
|
texture: texture_atlas_texture,
|
||||||
transform: Transform::from_xyz(-300.0, 0.0, 0.0),
|
transform: Transform::from_xyz(-300.0, 0.0, 0.0),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|
|
@ -23,7 +23,9 @@ pub struct SpritePlugin;
|
||||||
|
|
||||||
impl Plugin for SpritePlugin {
|
impl Plugin for SpritePlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_asset::<TextureAtlas>().register_type::<Sprite>();
|
app.add_asset::<TextureAtlas>()
|
||||||
|
.register_type::<Sprite>()
|
||||||
|
.add_system_to_stage(CoreStage::PostUpdate, sprite_auto_resize_system);
|
||||||
let render_app = app.sub_app_mut(0);
|
let render_app = app.sub_app_mut(0);
|
||||||
render_app
|
render_app
|
||||||
.init_resource::<ExtractedSprites>()
|
.init_resource::<ExtractedSprites>()
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
|
use bevy_asset::{Assets, Handle};
|
||||||
|
use bevy_ecs::prelude::{Query, Res};
|
||||||
use bevy_math::Vec2;
|
use bevy_math::Vec2;
|
||||||
use bevy_reflect::{Reflect, ReflectDeserialize, TypeUuid};
|
use bevy_reflect::{Reflect, ReflectDeserialize, TypeUuid};
|
||||||
|
use bevy_render2::texture::Image;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, TypeUuid, Reflect)]
|
#[derive(Debug, Default, Clone, TypeUuid, Reflect)]
|
||||||
|
@ -37,3 +40,26 @@ impl Sprite {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// System that resizes sprites that have their resize mode set to automatic
|
||||||
|
pub fn sprite_auto_resize_system(
|
||||||
|
textures: Res<Assets<Image>>,
|
||||||
|
mut query: Query<(&mut Sprite, &Handle<Image>)>,
|
||||||
|
) {
|
||||||
|
for (mut sprite, image_handle) in query.iter_mut() {
|
||||||
|
match sprite.resize_mode {
|
||||||
|
SpriteResizeMode::Manual => continue,
|
||||||
|
SpriteResizeMode::Automatic => {
|
||||||
|
if let Some(image) = textures.get(image_handle) {
|
||||||
|
let extent = image.texture_descriptor.size;
|
||||||
|
let texture_size = Vec2::new(extent.width as f32, extent.height as f32);
|
||||||
|
// only set sprite size if it has changed (this check prevents change
|
||||||
|
// detection from triggering)
|
||||||
|
if sprite.size != texture_size {
|
||||||
|
sprite.size = texture_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue