diff --git a/crates/bevy_sprite/src/entity.rs b/crates/bevy_sprite/src/entity.rs index 36ce79c9a7..d51649c021 100644 --- a/crates/bevy_sprite/src/entity.rs +++ b/crates/bevy_sprite/src/entity.rs @@ -1,5 +1,5 @@ use crate::{ - render::SPRITE_PIPELINE_HANDLE, sprite::Sprite, ColorMaterial, Rect, QUAD_HANDLE, + render::SPRITE_PIPELINE_HANDLE, sprite::Sprite, ColorMaterial, Quad, QUAD_HANDLE, }; use bevy_asset::Handle; use bevy_derive::EntityArchetype; @@ -8,7 +8,7 @@ use bevy_render::{mesh::Mesh, Renderable}; #[derive(EntityArchetype)] pub struct SpriteEntity { pub sprite: Sprite, - pub rect: Rect, + pub quad: Quad, pub mesh: Handle, // TODO: maybe abstract this out pub material: Handle, pub renderable: Renderable, @@ -18,7 +18,7 @@ impl Default for SpriteEntity { fn default() -> Self { SpriteEntity { sprite: Default::default(), - rect: Default::default(), + quad: Default::default(), mesh: QUAD_HANDLE, material: Default::default(), renderable: Renderable { diff --git a/crates/bevy_sprite/src/lib.rs b/crates/bevy_sprite/src/lib.rs index 840fd443b0..5f615ee0bc 100644 --- a/crates/bevy_sprite/src/lib.rs +++ b/crates/bevy_sprite/src/lib.rs @@ -1,18 +1,20 @@ mod color_material; pub mod entity; -mod rect; +mod quad; mod render; mod sprite; +mod sprite_sheet; pub use color_material::*; -pub use rect::*; +pub use quad::*; pub use render::*; pub use sprite::*; +pub use sprite_sheet::*; use bevy_app::{stage, AppBuilder, AppPlugin}; use bevy_asset::{AddAsset, Assets, Handle}; use bevy_render::{ - mesh::{shape::Quad, Mesh}, + mesh::{shape, Mesh}, render_graph::RenderGraph, shader::asset_shader_def_system, }; @@ -41,7 +43,7 @@ impl AppPlugin for SpritePlugin { let mut meshes = resources.get_mut::>().unwrap(); meshes.set( QUAD_HANDLE, - Mesh::from(Quad { + Mesh::from(shape::Quad { size: Vec2::new(1.0, 1.0), }), ); diff --git a/crates/bevy_sprite/src/quad.rs b/crates/bevy_sprite/src/quad.rs new file mode 100644 index 0000000000..3e094e6e92 --- /dev/null +++ b/crates/bevy_sprite/src/quad.rs @@ -0,0 +1,20 @@ +use bevy_core::bytes::GetBytes; +use bevy_derive::Uniform; +use glam::Vec2; +use zerocopy::AsBytes; +#[repr(C)] +#[derive(Default, Clone, Copy, Debug, Uniform, AsBytes)] +pub struct Quad { + pub position: Vec2, + pub size: Vec2, + pub z_index: f32, +} + +impl GetBytes for Quad { + fn get_bytes(&self) -> Vec { + self.as_bytes().to_vec() + } + fn get_bytes_ref(&self) -> Option<&[u8]> { + Some(self.as_bytes()) + } +} diff --git a/crates/bevy_sprite/src/rect.rs b/crates/bevy_sprite/src/rect.rs index a716a0b538..71edc17d12 100644 --- a/crates/bevy_sprite/src/rect.rs +++ b/crates/bevy_sprite/src/rect.rs @@ -1,20 +1,14 @@ -use bevy_core::bytes::GetBytes; -use bevy_derive::Uniform; -use glam::Vec2; -use zerocopy::AsBytes; -#[repr(C)] -#[derive(Default, Clone, Copy, Debug, Uniform, AsBytes)] pub struct Rect { - pub position: Vec2, - pub size: Vec2, - pub z_index: f32, + pub min: Vec2, + pub max: Vec2, } -impl GetBytes for Rect { - fn get_bytes(&self) -> Vec { - self.as_bytes().to_vec() +impl Rect { + pub fn width(&self) -> f32 { + self.max.x - self.min.x } - fn get_bytes_ref(&self) -> Option<&[u8]> { - Some(self.as_bytes()) + + pub fn height(&self) -> f32 { + self.max.y - self.min.y } -} +} \ No newline at end of file diff --git a/crates/bevy_sprite/src/render/mod.rs b/crates/bevy_sprite/src/render/mod.rs index 05d71f98a6..7a906487fd 100644 --- a/crates/bevy_sprite/src/render/mod.rs +++ b/crates/bevy_sprite/src/render/mod.rs @@ -1,4 +1,4 @@ -use crate::{ColorMaterial, Rect}; +use crate::{ColorMaterial, Quad}; use bevy_asset::{Assets, Handle}; use bevy_render::{ base_render_graph, @@ -63,7 +63,7 @@ pub fn build_sprite_pipeline(shaders: &mut Assets) -> PipelineDescriptor pub mod node { pub const COLOR_MATERIAL: &'static str = "color_material"; - pub const RECT: &'static str = "rect"; + pub const QUAD: &'static str = "quad"; } pub trait SpriteRenderGraphBuilder { @@ -79,8 +79,8 @@ impl SpriteRenderGraphBuilder for RenderGraph { self.add_node_edge(node::COLOR_MATERIAL, base_render_graph::node::MAIN_PASS) .unwrap(); - self.add_system_node(node::RECT, UniformNode::::new(false)); - self.add_node_edge(node::RECT, base_render_graph::node::MAIN_PASS) + self.add_system_node(node::QUAD, UniformNode::::new(false)); + self.add_node_edge(node::QUAD, base_render_graph::node::MAIN_PASS) .unwrap(); let mut pipelines = resources.get_mut::>().unwrap(); diff --git a/crates/bevy_sprite/src/render/sprite.vert b/crates/bevy_sprite/src/render/sprite.vert index b4a3b334a7..e77712518b 100644 --- a/crates/bevy_sprite/src/render/sprite.vert +++ b/crates/bevy_sprite/src/render/sprite.vert @@ -10,15 +10,15 @@ layout(set = 0, binding = 0) uniform Camera2d { mat4 ViewProj; }; -layout(set = 1, binding = 0) uniform Rect { - vec2 Rect_Position; - vec2 Rect_Size; - float Rect_ZIndex; +layout(set = 1, binding = 0) uniform Quad { + vec2 Quad_Position; + vec2 Quad_Size; + float Quad_ZIndex; }; void main() { v_Uv = Vertex_Uv; - vec3 position = Vertex_Position * vec3(Rect_Size, 0.0); - position = position + vec3(Rect_Position, -Rect_ZIndex); + vec3 position = Vertex_Position * vec3(Quad_Size, 0.0); + position = position + vec3(Quad_Position, -Quad_ZIndex); gl_Position = ViewProj * 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 9a98094e30..559e99142f 100644 --- a/crates/bevy_sprite/src/sprite.rs +++ b/crates/bevy_sprite/src/sprite.rs @@ -1,4 +1,4 @@ -use crate::{ColorMaterial, Rect}; +use crate::{ColorMaterial, Quad}; use bevy_asset::{Assets, Handle}; use bevy_render::texture::Texture; pub use legion::prelude::*; @@ -17,8 +17,8 @@ pub fn sprite_system() -> Box { .read_resource::>() .read_resource::>() .with_query( - <(Read, Read>, Write)>::query().filter( - changed::() | changed::() | changed::>(), + <(Read, Read>, Write)>::query().filter( + changed::() | changed::() | changed::>(), ), ) .build(|_, world, (materials, textures), query| { diff --git a/crates/bevy_sprite/src/sprite_sheet.rs b/crates/bevy_sprite/src/sprite_sheet.rs new file mode 100644 index 0000000000..7ff28c0117 --- /dev/null +++ b/crates/bevy_sprite/src/sprite_sheet.rs @@ -0,0 +1,8 @@ +use crate::Quad; +use bevy_asset::Handle; +use bevy_render::texture::Texture; + +pub struct SpriteSheet { + pub texture: Handle, + pub sprites: Vec, +} diff --git a/crates/bevy_ui/src/entity.rs b/crates/bevy_ui/src/entity.rs index 42d784ad0e..3de70d32ff 100644 --- a/crates/bevy_ui/src/entity.rs +++ b/crates/bevy_ui/src/entity.rs @@ -3,12 +3,12 @@ use crate::{render::UI_PIPELINE_HANDLE, widget::Label}; use bevy_asset::Handle; use bevy_derive::EntityArchetype; use bevy_render::{mesh::Mesh, Renderable}; -use bevy_sprite::{ColorMaterial, Rect, QUAD_HANDLE}; +use bevy_sprite::{ColorMaterial, Quad, QUAD_HANDLE}; #[derive(EntityArchetype)] pub struct UiEntity { pub node: Node, - pub rect: Rect, + pub quad: Quad, pub mesh: Handle, // TODO: maybe abstract this out pub material: Handle, pub renderable: Renderable, @@ -18,7 +18,7 @@ impl Default for UiEntity { fn default() -> Self { UiEntity { node: Default::default(), - rect: Default::default(), + quad: Default::default(), mesh: QUAD_HANDLE, material: Default::default(), renderable: Renderable { @@ -32,7 +32,7 @@ impl Default for UiEntity { #[derive(EntityArchetype)] pub struct LabelEntity { pub node: Node, - pub rect: Rect, + pub quad: Quad, pub mesh: Handle, // TODO: maybe abstract this out pub material: Handle, pub renderable: Renderable, @@ -43,7 +43,7 @@ impl Default for LabelEntity { fn default() -> Self { LabelEntity { node: Default::default(), - rect: Default::default(), + quad: Default::default(), mesh: QUAD_HANDLE, // NOTE: labels each get their own material. material: Handle::new(), // TODO: maybe abstract this out diff --git a/crates/bevy_ui/src/node.rs b/crates/bevy_ui/src/node.rs index cf0732269e..2b1c17f57a 100644 --- a/crates/bevy_ui/src/node.rs +++ b/crates/bevy_ui/src/node.rs @@ -1,5 +1,5 @@ use super::{Anchors, Margins}; -use bevy_sprite::Rect; +use bevy_sprite::Quad; use glam::Vec2; #[derive(Debug, Clone)] @@ -36,12 +36,12 @@ impl Node { pub fn update( &mut self, - rect: &mut Rect, + quad: &mut Quad, parent_size: Vec2, parent_position: Vec2, z_index: f32, ) { - let (rect_x, rect_width) = Self::compute_dimension_properties( + let (quad_x, quad_width) = Self::compute_dimension_properties( self.position.x(), self.margins.left, self.margins.right, @@ -49,7 +49,7 @@ impl Node { self.anchors.right, parent_size.x(), ); - let (rect_y, rect_height) = Self::compute_dimension_properties( + let (quad_y, quad_height) = Self::compute_dimension_properties( self.position.y(), self.margins.bottom, self.margins.top, @@ -58,9 +58,9 @@ impl Node { parent_size.y(), ); - rect.size = Vec2::new(rect_width, rect_height); - rect.position = Vec2::new(rect_x, rect_y) + parent_position; - rect.z_index = z_index; + quad.size = Vec2::new(quad_width, quad_height); + quad.position = Vec2::new(quad_x, quad_y) + parent_position; + quad.z_index = z_index; } fn compute_dimension_properties( @@ -85,15 +85,15 @@ impl Node { MarginGrowDirection::Negative }; - let p0 = Self::compute_rect_position(offset, margin0, anchor_p0, p0_grow_direction); - let p1 = Self::compute_rect_position(offset, margin1, anchor_p1, p1_grow_direction); + let p0 = Self::compute_quad_position(offset, margin0, anchor_p0, p0_grow_direction); + let p1 = Self::compute_quad_position(offset, margin1, anchor_p1, p1_grow_direction); let final_width = p1 - p0; let p = (p0 + p1) / 2.0; (p, final_width.abs()) } - fn compute_rect_position( + fn compute_quad_position( position: f32, margin: f32, anchor_position: f32, diff --git a/crates/bevy_ui/src/render/ui.vert b/crates/bevy_ui/src/render/ui.vert index a2e94f9d94..c2c269cbb6 100644 --- a/crates/bevy_ui/src/render/ui.vert +++ b/crates/bevy_ui/src/render/ui.vert @@ -10,15 +10,15 @@ layout(set = 0, binding = 0) uniform UiCamera { mat4 ViewProj; }; -layout(set = 1, binding = 0) uniform Rect { - vec2 Rect_Position; - vec2 Rect_Size; - float Rect_ZIndex; +layout(set = 1, binding = 0) uniform Quad { + vec2 Quad_Position; + vec2 Quad_Size; + float Quad_ZIndex; }; void main() { v_Uv = Vertex_Uv; - vec3 position = Vertex_Position * vec3(Rect_Size, 0.0); - position = position + vec3(Rect_Position, -Rect_ZIndex); + vec3 position = Vertex_Position * vec3(Quad_Size, 0.0); + position = position + vec3(Quad_Position, -Quad_ZIndex); gl_Position = ViewProj * vec4(position, 1.0); } \ No newline at end of file diff --git a/crates/bevy_ui/src/ui_update_system.rs b/crates/bevy_ui/src/ui_update_system.rs index 5f771214c0..7d914cc258 100644 --- a/crates/bevy_ui/src/ui_update_system.rs +++ b/crates/bevy_ui/src/ui_update_system.rs @@ -4,7 +4,7 @@ use bevy_transform::prelude::{Children, Parent}; use bevy_window::Windows; use glam::Vec2; use legion::{prelude::*, systems::SubWorld}; -use bevy_sprite::Rect; +use bevy_sprite::Quad; pub const UI_Z_STEP: f32 = 0.0001; @@ -13,11 +13,11 @@ pub fn ui_update_system() -> Box { .read_resource::() .with_query(>::query().filter(!component::())) .write_component::() - .write_component::() + .write_component::() .read_component::() .build(move |_, world, windows, node_query| { if let Some(window) = windows.get_primary() { - let mut window_rect = Rect { + let mut window_quad = Quad { size: Vec2::new(window.width as f32, window.height as f32), position: Vec2::new(0.0, 0.0), z_index: 0.9999, @@ -30,34 +30,34 @@ pub fn ui_update_system() -> Box { let result = run_on_hierarchy_subworld_mut( world, entity, - window_rect.clone(), + window_quad.clone(), &mut update_node_entity, &mut process_child_result, ); if let Some(result) = result { - window_rect.z_index = result.z_index; + window_quad.z_index = result.z_index; } } } }) } -fn update_node_entity(world: &mut SubWorld, entity: Entity, parent_rect: Rect) -> Option { +fn update_node_entity(world: &mut SubWorld, entity: Entity, parent_quad: Quad) -> Option { // TODO: Somehow remove this unsafe unsafe { if let Some(mut node) = world.get_component_mut_unchecked::(entity) { - if let Some(mut rect) = world.get_component_mut_unchecked::(entity) { + if let Some(mut quad) = world.get_component_mut_unchecked::(entity) { node.update( - &mut rect, - parent_rect.size, - parent_rect.position, - parent_rect.z_index, + &mut quad, + parent_quad.size, + parent_quad.position, + parent_quad.z_index, ); - return Some(Rect { - size: rect.size, - position: rect.position - rect.size / 2.0, - z_index: rect.z_index - UI_Z_STEP, + return Some(Quad { + size: quad.size, + position: quad.position - quad.size / 2.0, + z_index: quad.z_index - UI_Z_STEP, }); } } @@ -66,7 +66,7 @@ fn update_node_entity(world: &mut SubWorld, entity: Entity, parent_rect: Rect) - None } -fn process_child_result(_parent_result: Rect, child_result: Rect) -> Rect { +fn process_child_result(_parent_result: Quad, child_result: Quad) -> Quad { // "earlier" children are sorted behind "later" children let mut result = child_result.clone(); result.z_index -= UI_Z_STEP; diff --git a/crates/bevy_ui/src/widget/label.rs b/crates/bevy_ui/src/widget/label.rs index 5e703b88fb..8fa5302823 100644 --- a/crates/bevy_ui/src/widget/label.rs +++ b/crates/bevy_ui/src/widget/label.rs @@ -1,6 +1,6 @@ use bevy_asset::{Assets, Handle}; use bevy_render::{texture::Texture, Color}; -use bevy_sprite::{ColorMaterial, Rect}; +use bevy_sprite::{ColorMaterial, Quad}; use bevy_text::Font; use legion::prelude::{Com, Res, ResMut}; @@ -29,12 +29,12 @@ impl Label { mut textures: ResMut>, fonts: Res>, label: Com