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

Resize mode for sprite #430

Merged
merged 1 commit into from
Sep 8, 2020
Merged
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
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use texture_atlas_builder::*;
pub mod prelude {
pub use crate::{
entity::{SpriteComponents, SpriteSheetComponents},
ColorMaterial, Sprite, TextureAtlas, TextureAtlasSprite,
ColorMaterial, Sprite, SpriteResizeMode, TextureAtlas, TextureAtlasSprite,
};
}

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_sprite/src/render/sprite.vert
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ layout(set = 0, binding = 0) uniform Camera {
layout(set = 2, binding = 0) uniform Transform {
mat4 Model;
};
layout(set = 2, binding = 1) uniform Sprite {
vec2 Sprite_size;
layout(set = 2, binding = 1) uniform Sprite_size {
vec2 size;
};

void main() {
v_Uv = Vertex_Uv;
vec3 position = Vertex_Position * vec3(Sprite_size, 1.0);
vec3 position = Vertex_Position * vec3(size, 1.0);
gl_Position = ViewProj * Model * vec4(position, 1.0);
}
49 changes: 35 additions & 14 deletions crates/bevy_sprite/src/sprite.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
use crate::ColorMaterial;
use bevy_asset::{Assets, Handle};
use bevy_core::Byteable;
use bevy_ecs::{Query, Res};
use bevy_math::Vec2;
use bevy_render::{
renderer::{RenderResource, RenderResources},
texture::Texture,
};
use bevy_render::{renderer::RenderResources, texture::Texture};

#[repr(C)]
#[derive(Default, RenderResources, RenderResource)]
#[render_resources(from_self)]
#[derive(Default, RenderResources)]
pub struct Sprite {
pub size: Vec2,
#[render_resources(ignore)]
pub resize_mode: SpriteResizeMode,
}

// SAFE: sprite is repr(C) and only consists of byteables
unsafe impl Byteable for Sprite {}
/// Determines how `Sprite` resize should be handled
#[derive(Debug)]
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,
}
}
}

pub fn sprite_system(
materials: Res<Assets<ColorMaterial>>,
textures: Res<Assets<Texture>>,
mut query: Query<(&mut Sprite, &Handle<ColorMaterial>)>,
) {
for (mut sprite, handle) in &mut query.iter() {
let material = materials.get(&handle).unwrap();
if let Some(texture_handle) = material.texture {
if let Some(texture) = textures.get(&texture_handle) {
sprite.size = texture.size;
match sprite.resize_mode {
SpriteResizeMode::Manual => continue,
SpriteResizeMode::Automatic => {
let material = materials.get(&handle).unwrap();
if let Some(texture_handle) = material.texture {
if let Some(texture) = textures.get(&texture_handle) {
sprite.size = texture.size;
}
}
}
}
}
Expand Down
26 changes: 7 additions & 19 deletions examples/game/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ fn setup(
.spawn(SpriteComponents {
material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()),
translation: Translation(Vec3::new(0.0, -215.0, 0.0)),
sprite: Sprite {
size: Vec2::new(120.0, 30.0),
},
sprite: Sprite::new(Vec2::new(120.0, 30.0)),
..Default::default()
})
.with(Paddle { speed: 500.0 })
Expand All @@ -60,9 +58,7 @@ fn setup(
.spawn(SpriteComponents {
material: materials.add(Color::rgb(0.8, 0.2, 0.2).into()),
translation: Translation(Vec3::new(0.0, -50.0, 1.0)),
sprite: Sprite {
size: Vec2::new(30.0, 30.0),
},
sprite: Sprite::new(Vec2::new(30.0, 30.0)),
..Default::default()
})
.with(Ball {
Expand Down Expand Up @@ -100,39 +96,31 @@ fn setup(
.spawn(SpriteComponents {
material: wall_material,
translation: Translation(Vec3::new(-bounds.x() / 2.0, 0.0, 0.0)),
sprite: Sprite {
size: Vec2::new(wall_thickness, bounds.y() + wall_thickness),
},
sprite: Sprite::new(Vec2::new(wall_thickness, bounds.y() + wall_thickness)),
..Default::default()
})
.with(Collider::Solid)
// right
.spawn(SpriteComponents {
material: wall_material,
translation: Translation(Vec3::new(bounds.x() / 2.0, 0.0, 0.0)),
sprite: Sprite {
size: Vec2::new(wall_thickness, bounds.y() + wall_thickness),
},
sprite: Sprite::new(Vec2::new(wall_thickness, bounds.y() + wall_thickness)),
..Default::default()
})
.with(Collider::Solid)
// bottom
.spawn(SpriteComponents {
material: wall_material,
translation: Translation(Vec3::new(0.0, -bounds.y() / 2.0, 0.0)),
sprite: Sprite {
size: Vec2::new(bounds.x() + wall_thickness, wall_thickness),
},
sprite: Sprite::new(Vec2::new(bounds.x() + wall_thickness, wall_thickness)),
..Default::default()
})
.with(Collider::Solid)
// top
.spawn(SpriteComponents {
material: wall_material,
translation: Translation(Vec3::new(0.0, bounds.y() / 2.0, 0.0)),
sprite: Sprite {
size: Vec2::new(bounds.x() + wall_thickness, wall_thickness),
},
sprite: Sprite::new(Vec2::new(bounds.x() + wall_thickness, wall_thickness)),
..Default::default()
})
.with(Collider::Solid);
Expand All @@ -158,7 +146,7 @@ fn setup(
// brick
.spawn(SpriteComponents {
material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()),
sprite: Sprite { size: brick_size },
sprite: Sprite::new(brick_size),
translation: Translation(brick_position),
..Default::default()
})
Expand Down