Resize mode for Sprite component (#430)

Adds a 'resize_mode' field for 'Sprite'.
This allows different resize handling based on 'SpriteResizeMode' enum value.
This commit is contained in:
Sergey Minakov 2020-09-08 22:04:22 +03:00 committed by GitHub
parent 69aa9bf9fd
commit 52ae217b16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 37 deletions

View File

@ -20,7 +20,7 @@ pub use texture_atlas_builder::*;
pub mod prelude { pub mod prelude {
pub use crate::{ pub use crate::{
entity::{SpriteComponents, SpriteSheetComponents}, entity::{SpriteComponents, SpriteSheetComponents},
ColorMaterial, Sprite, TextureAtlas, TextureAtlasSprite, ColorMaterial, Sprite, SpriteResizeMode, TextureAtlas, TextureAtlasSprite,
}; };
} }

View File

@ -13,12 +13,12 @@ layout(set = 0, binding = 0) uniform Camera {
layout(set = 2, binding = 0) uniform Transform { layout(set = 2, binding = 0) uniform Transform {
mat4 Model; mat4 Model;
}; };
layout(set = 2, binding = 1) uniform Sprite { layout(set = 2, binding = 1) uniform Sprite_size {
vec2 Sprite_size; vec2 size;
}; };
void main() { void main() {
v_Uv = Vertex_Uv; 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); gl_Position = ViewProj * Model * vec4(position, 1.0);
} }

View File

@ -1,22 +1,38 @@
use crate::ColorMaterial; use crate::ColorMaterial;
use bevy_asset::{Assets, Handle}; use bevy_asset::{Assets, Handle};
use bevy_core::Byteable;
use bevy_ecs::{Query, Res}; use bevy_ecs::{Query, Res};
use bevy_math::Vec2; use bevy_math::Vec2;
use bevy_render::{ use bevy_render::{renderer::RenderResources, texture::Texture};
renderer::{RenderResource, RenderResources},
texture::Texture,
};
#[repr(C)] #[derive(Default, RenderResources)]
#[derive(Default, RenderResources, RenderResource)]
#[render_resources(from_self)]
pub struct Sprite { pub struct Sprite {
pub size: Vec2, pub size: Vec2,
#[render_resources(ignore)]
pub resize_mode: SpriteResizeMode,
} }
// SAFE: sprite is repr(C) and only consists of byteables /// Determines how `Sprite` resize should be handled
unsafe impl Byteable for Sprite {} #[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( pub fn sprite_system(
materials: Res<Assets<ColorMaterial>>, materials: Res<Assets<ColorMaterial>>,
@ -24,10 +40,15 @@ pub fn sprite_system(
mut query: Query<(&mut Sprite, &Handle<ColorMaterial>)>, mut query: Query<(&mut Sprite, &Handle<ColorMaterial>)>,
) { ) {
for (mut sprite, handle) in &mut query.iter() { for (mut sprite, handle) in &mut query.iter() {
let material = materials.get(&handle).unwrap(); match sprite.resize_mode {
if let Some(texture_handle) = material.texture { SpriteResizeMode::Manual => continue,
if let Some(texture) = textures.get(&texture_handle) { SpriteResizeMode::Automatic => {
sprite.size = texture.size; 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;
}
}
} }
} }
} }

View File

@ -49,9 +49,7 @@ fn setup(
.spawn(SpriteComponents { .spawn(SpriteComponents {
material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()), material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()),
translation: Translation(Vec3::new(0.0, -215.0, 0.0)), translation: Translation(Vec3::new(0.0, -215.0, 0.0)),
sprite: Sprite { sprite: Sprite::new(Vec2::new(120.0, 30.0)),
size: Vec2::new(120.0, 30.0),
},
..Default::default() ..Default::default()
}) })
.with(Paddle { speed: 500.0 }) .with(Paddle { speed: 500.0 })
@ -60,9 +58,7 @@ fn setup(
.spawn(SpriteComponents { .spawn(SpriteComponents {
material: materials.add(Color::rgb(0.8, 0.2, 0.2).into()), material: materials.add(Color::rgb(0.8, 0.2, 0.2).into()),
translation: Translation(Vec3::new(0.0, -50.0, 1.0)), translation: Translation(Vec3::new(0.0, -50.0, 1.0)),
sprite: Sprite { sprite: Sprite::new(Vec2::new(30.0, 30.0)),
size: Vec2::new(30.0, 30.0),
},
..Default::default() ..Default::default()
}) })
.with(Ball { .with(Ball {
@ -100,9 +96,7 @@ fn setup(
.spawn(SpriteComponents { .spawn(SpriteComponents {
material: wall_material, material: wall_material,
translation: Translation(Vec3::new(-bounds.x() / 2.0, 0.0, 0.0)), translation: Translation(Vec3::new(-bounds.x() / 2.0, 0.0, 0.0)),
sprite: Sprite { sprite: Sprite::new(Vec2::new(wall_thickness, bounds.y() + wall_thickness)),
size: Vec2::new(wall_thickness, bounds.y() + wall_thickness),
},
..Default::default() ..Default::default()
}) })
.with(Collider::Solid) .with(Collider::Solid)
@ -110,9 +104,7 @@ fn setup(
.spawn(SpriteComponents { .spawn(SpriteComponents {
material: wall_material, material: wall_material,
translation: Translation(Vec3::new(bounds.x() / 2.0, 0.0, 0.0)), translation: Translation(Vec3::new(bounds.x() / 2.0, 0.0, 0.0)),
sprite: Sprite { sprite: Sprite::new(Vec2::new(wall_thickness, bounds.y() + wall_thickness)),
size: Vec2::new(wall_thickness, bounds.y() + wall_thickness),
},
..Default::default() ..Default::default()
}) })
.with(Collider::Solid) .with(Collider::Solid)
@ -120,9 +112,7 @@ fn setup(
.spawn(SpriteComponents { .spawn(SpriteComponents {
material: wall_material, material: wall_material,
translation: Translation(Vec3::new(0.0, -bounds.y() / 2.0, 0.0)), translation: Translation(Vec3::new(0.0, -bounds.y() / 2.0, 0.0)),
sprite: Sprite { sprite: Sprite::new(Vec2::new(bounds.x() + wall_thickness, wall_thickness)),
size: Vec2::new(bounds.x() + wall_thickness, wall_thickness),
},
..Default::default() ..Default::default()
}) })
.with(Collider::Solid) .with(Collider::Solid)
@ -130,9 +120,7 @@ fn setup(
.spawn(SpriteComponents { .spawn(SpriteComponents {
material: wall_material, material: wall_material,
translation: Translation(Vec3::new(0.0, bounds.y() / 2.0, 0.0)), translation: Translation(Vec3::new(0.0, bounds.y() / 2.0, 0.0)),
sprite: Sprite { sprite: Sprite::new(Vec2::new(bounds.x() + wall_thickness, wall_thickness)),
size: Vec2::new(bounds.x() + wall_thickness, wall_thickness),
},
..Default::default() ..Default::default()
}) })
.with(Collider::Solid); .with(Collider::Solid);
@ -158,7 +146,7 @@ fn setup(
// brick // brick
.spawn(SpriteComponents { .spawn(SpriteComponents {
material: materials.add(Color::rgb(0.2, 0.2, 0.8).into()), 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), translation: Translation(brick_position),
..Default::default() ..Default::default()
}) })