From eba129b3b09575a30432f0cca58bc3afea63511b Mon Sep 17 00:00:00 2001 From: Sergey Minakov Date: Thu, 3 Sep 2020 12:05:37 +0300 Subject: [PATCH] Resize mode for Sprite component MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a 'resize_mode' field for 'Sprite'. This allows different resize handling based on 'SpriteResizeMode' enum value. Co-authored-by: Marcel Müller --- crates/bevy_sprite/src/lib.rs | 2 +- crates/bevy_sprite/src/render/sprite.vert | 6 +-- crates/bevy_sprite/src/sprite.rs | 46 ++++++++++++++++++----- examples/game/breakout.rs | 26 ++++--------- 4 files changed, 47 insertions(+), 33 deletions(-) diff --git a/crates/bevy_sprite/src/lib.rs b/crates/bevy_sprite/src/lib.rs index ddd84abbcd1aef..e2abe5a36f1352 100644 --- a/crates/bevy_sprite/src/lib.rs +++ b/crates/bevy_sprite/src/lib.rs @@ -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, }; } diff --git a/crates/bevy_sprite/src/render/sprite.vert b/crates/bevy_sprite/src/render/sprite.vert index c4ecaed96e9bb6..fe1619d14b7fc7 100644 --- a/crates/bevy_sprite/src/render/sprite.vert +++ b/crates/bevy_sprite/src/render/sprite.vert @@ -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); } \ No newline at end of file diff --git a/crates/bevy_sprite/src/sprite.rs b/crates/bevy_sprite/src/sprite.rs index 2997d419a172db..9938a0420f170e 100644 --- a/crates/bevy_sprite/src/sprite.rs +++ b/crates/bevy_sprite/src/sprite.rs @@ -3,16 +3,37 @@ 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, +} + +/// 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, + } + } } // SAFE: sprite is repr(C) and only consists of byteables @@ -24,10 +45,15 @@ pub fn sprite_system( mut query: Query<(&mut Sprite, &Handle)>, ) { 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; + } + } } } } diff --git a/examples/game/breakout.rs b/examples/game/breakout.rs index 6690f99446c1f9..7a7f1827abe6f0 100644 --- a/examples/game/breakout.rs +++ b/examples/game/breakout.rs @@ -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 }) @@ -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 { @@ -100,9 +96,7 @@ 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) @@ -110,9 +104,7 @@ 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) @@ -120,9 +112,7 @@ fn setup( .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) @@ -130,9 +120,7 @@ fn setup( .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); @@ -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() })