Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Remove Need for Sprite Size Sync System #2632

Closed
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
8 changes: 2 additions & 6 deletions examples/tools/bevymark_pipelined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use bevy::{
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
ecs::prelude::*,
input::Input,
math::{Vec2, Vec3},
math::Vec3,
prelude::{App, AssetServer, Handle, MouseButton, Transform},
render2::{camera::OrthographicCameraBundle, color::Color, texture::Image},
sprite2::{PipelinedSpriteBundle, Sprite},
sprite2::PipelinedSpriteBundle,
window::WindowDescriptor,
PipelinedDefaultPlugins,
};
Expand Down Expand Up @@ -177,10 +177,6 @@ fn spawn_birds(
.spawn_bundle(PipelinedSpriteBundle {
// material: bird_material.0.clone(),
texture: texture.clone(),
sprite: Sprite {
size: Vec2::new(250.0, 250.0),
..Default::default()
},
transform: Transform {
translation: Vec3::new(bird_x, bird_y, bird_z),
scale: Vec3::splat(BIRD_SCALE),
Expand Down
4 changes: 1 addition & 3 deletions pipelined/bevy_sprite2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ pub struct SpritePlugin;

impl Plugin for SpritePlugin {
fn build(&self, app: &mut App) {
app.add_asset::<TextureAtlas>()
.register_type::<Sprite>()
.add_system_to_stage(CoreStage::PostUpdate, sprite_auto_resize_system);
app.add_asset::<TextureAtlas>().register_type::<Sprite>();
let render_app = app.sub_app(RenderApp);
render_app
.init_resource::<ExtractedSprites>()
Expand Down
11 changes: 8 additions & 3 deletions pipelined/bevy_sprite2/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,21 @@ pub fn extract_sprites(
) {
let mut extracted_sprites = Vec::new();
for (sprite, transform, handle) in sprite_query.iter() {
if !images.contains(handle) {
let image = if let Some(image) = images.get(handle) {
image
} else {
continue;
}
};
let size = image.texture_descriptor.size;

extracted_sprites.push(ExtractedSprite {
atlas_size: None,
transform: transform.compute_matrix(),
rect: Rect {
min: Vec2::ZERO,
max: sprite.size,
max: sprite
.custom_size
.unwrap_or_else(|| Vec2::new(size.width as f32, size.height as f32)),
},
handle: handle.clone_weak(),
});
Expand Down
62 changes: 6 additions & 56 deletions pipelined/bevy_sprite2/src/sprite.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,15 @@
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};
use bevy_reflect::{Reflect, TypeUuid};

#[derive(Debug, Default, Clone, TypeUuid, Reflect)]
#[uuid = "7233c597-ccfa-411f-bd59-9af349432ada"]
#[repr(C)]
pub struct Sprite {
pub size: Vec2,
/// Flip the sprite along the X axis
pub flip_x: bool,
/// Flip the sprite along the Y axis
pub flip_y: bool,
pub resize_mode: SpriteResizeMode,
}

/// Determines how `Sprite` resize should be handled
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)]
pub enum SpriteResizeMode {
Manual,
Automatic,
}

impl Default for SpriteResizeMode {
fn default() -> Self {
SpriteResizeMode::Automatic
}
}

impl Sprite {
/// Creates new `Sprite` with `SpriteResizeMode::Manual` value for `resize_mode`
pub fn new(size: Vec2) -> Self {
Self {
size,
resize_mode: SpriteResizeMode::Manual,
flip_x: false,
flip_y: false,
}
}
}

/// 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;
}
}
}
}
}
/// An optional custom size for the sprite that will be used when rendering, instead of the size
/// of the sprite's image
pub custom_size: Option<Vec2>,
}