Skip to content

Commit

Permalink
Re-implement Automatic Sprite Sizing (#2613)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
zicklag committed Aug 10, 2021
1 parent b5b9a95 commit c833003
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
10 changes: 3 additions & 7 deletions examples/2d/pipelined_texture_atlas.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use bevy::{
asset::LoadState,
math::{Vec2, Vec3},
math::Vec3,
prelude::{
App, AssetServer, Assets, Commands, HandleUntyped, IntoSystem, Res, ResMut, State,
SystemSet, Transform,
},
render2::{camera::OrthographicCameraBundle, texture::Image},
sprite2::{
PipelinedSpriteBundle, PipelinedSpriteSheetBundle, Sprite, TextureAtlas,
TextureAtlasBuilder, TextureAtlasSprite,
PipelinedSpriteBundle, PipelinedSpriteSheetBundle, TextureAtlas, TextureAtlasBuilder,
TextureAtlasSprite,
},
PipelinedDefaultPlugins,
};
Expand Down Expand Up @@ -87,10 +87,6 @@ fn setup(
});
// draw the atlas itself
commands.spawn_bundle(PipelinedSpriteBundle {
sprite: Sprite {
size: Vec2::new(512.0, 512.0),
..Default::default()
},
texture: texture_atlas_texture,
transform: Transform::from_xyz(-300.0, 0.0, 0.0),
..Default::default()
Expand Down
4 changes: 3 additions & 1 deletion pipelined/bevy_sprite2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ pub struct SpritePlugin;

impl Plugin for SpritePlugin {
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);
render_app
.init_resource::<ExtractedSprites>()
Expand Down
26 changes: 26 additions & 0 deletions pipelined/bevy_sprite2/src/sprite.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use bevy_asset::{Assets, Handle};
use bevy_ecs::prelude::{Query, Res};
use bevy_math::Vec2;
use bevy_reflect::{Reflect, ReflectDeserialize, TypeUuid};
use bevy_render2::texture::Image;
use serde::{Deserialize, Serialize};

#[derive(Debug, Default, Clone, TypeUuid, Reflect)]
Expand Down Expand Up @@ -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;
}
}
}
}
}
}

0 comments on commit c833003

Please sign in to comment.