Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine for now (and i know this is the approach we currently use), but its worth calling out that we can probably do better than polling every texture for every sprite every frame. But I'm fine eating the perf cost (~0.9 milliseconds per frame for 67,000 sprites in bevymark_pipelined or ~3 fps).

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