mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
rename rect to quad
This commit is contained in:
parent
5d40bddf6c
commit
e68ae995f8
15 changed files with 102 additions and 79 deletions
|
@ -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<Mesh>, // TODO: maybe abstract this out
|
||||
pub material: Handle<ColorMaterial>,
|
||||
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 {
|
||||
|
|
|
@ -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::<Assets<Mesh>>().unwrap();
|
||||
meshes.set(
|
||||
QUAD_HANDLE,
|
||||
Mesh::from(Quad {
|
||||
Mesh::from(shape::Quad {
|
||||
size: Vec2::new(1.0, 1.0),
|
||||
}),
|
||||
);
|
||||
|
|
20
crates/bevy_sprite/src/quad.rs
Normal file
20
crates/bevy_sprite/src/quad.rs
Normal file
|
@ -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<u8> {
|
||||
self.as_bytes().to_vec()
|
||||
}
|
||||
fn get_bytes_ref(&self) -> Option<&[u8]> {
|
||||
Some(self.as_bytes())
|
||||
}
|
||||
}
|
|
@ -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<u8> {
|
||||
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
|
||||
}
|
||||
}
|
|
@ -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<Shader>) -> 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::<Rect>::new(false));
|
||||
self.add_node_edge(node::RECT, base_render_graph::node::MAIN_PASS)
|
||||
self.add_system_node(node::QUAD, UniformNode::<Quad>::new(false));
|
||||
self.add_node_edge(node::QUAD, base_render_graph::node::MAIN_PASS)
|
||||
.unwrap();
|
||||
|
||||
let mut pipelines = resources.get_mut::<Assets<PipelineDescriptor>>().unwrap();
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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<dyn Schedulable> {
|
|||
.read_resource::<Assets<ColorMaterial>>()
|
||||
.read_resource::<Assets<Texture>>()
|
||||
.with_query(
|
||||
<(Read<Sprite>, Read<Handle<ColorMaterial>>, Write<Rect>)>::query().filter(
|
||||
changed::<Sprite>() | changed::<Rect>() | changed::<Handle<ColorMaterial>>(),
|
||||
<(Read<Sprite>, Read<Handle<ColorMaterial>>, Write<Quad>)>::query().filter(
|
||||
changed::<Sprite>() | changed::<Quad>() | changed::<Handle<ColorMaterial>>(),
|
||||
),
|
||||
)
|
||||
.build(|_, world, (materials, textures), query| {
|
||||
|
|
8
crates/bevy_sprite/src/sprite_sheet.rs
Normal file
8
crates/bevy_sprite/src/sprite_sheet.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
use crate::Quad;
|
||||
use bevy_asset::Handle;
|
||||
use bevy_render::texture::Texture;
|
||||
|
||||
pub struct SpriteSheet {
|
||||
pub texture: Handle<Texture>,
|
||||
pub sprites: Vec<Quad>,
|
||||
}
|
|
@ -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<Mesh>, // TODO: maybe abstract this out
|
||||
pub material: Handle<ColorMaterial>,
|
||||
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<Mesh>, // TODO: maybe abstract this out
|
||||
pub material: Handle<ColorMaterial>,
|
||||
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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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<dyn Schedulable> {
|
|||
.read_resource::<Windows>()
|
||||
.with_query(<Read<Node>>::query().filter(!component::<Parent>()))
|
||||
.write_component::<Node>()
|
||||
.write_component::<Rect>()
|
||||
.write_component::<Quad>()
|
||||
.read_component::<Children>()
|
||||
.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<dyn Schedulable> {
|
|||
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<Rect> {
|
||||
fn update_node_entity(world: &mut SubWorld, entity: Entity, parent_quad: Quad) -> Option<Quad> {
|
||||
// TODO: Somehow remove this unsafe
|
||||
unsafe {
|
||||
if let Some(mut node) = world.get_component_mut_unchecked::<Node>(entity) {
|
||||
if let Some(mut rect) = world.get_component_mut_unchecked::<Rect>(entity) {
|
||||
if let Some(mut quad) = world.get_component_mut_unchecked::<Quad>(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;
|
||||
|
|
|
@ -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<Assets<Texture>>,
|
||||
fonts: Res<Assets<Font>>,
|
||||
label: Com<Label>,
|
||||
rect: Com<Rect>,
|
||||
quad: Com<Quad>,
|
||||
color_material_handle: Com<Handle<ColorMaterial>>,
|
||||
) {
|
||||
// ensure the texture is at least 1x1
|
||||
let width = rect.size.x().max(1.0);
|
||||
let height = rect.size.y().max(1.0);
|
||||
let width = quad.size.x().max(1.0);
|
||||
let height = quad.size.y().max(1.0);
|
||||
|
||||
if let Some(font) = fonts.get(&label.font) {
|
||||
let texture =
|
||||
|
|
|
@ -17,8 +17,7 @@ fn setup(
|
|||
.build()
|
||||
.add_entity(OrthographicCameraEntity::default())
|
||||
.add_entity(SpriteEntity {
|
||||
rect: Rect {
|
||||
position: Vec2::new(0.0, 0.0),
|
||||
quad: Quad {
|
||||
z_index: 0.5,
|
||||
..Default::default()
|
||||
},
|
||||
|
|
|
@ -33,7 +33,7 @@ pub use crate::render::{
|
|||
#[cfg(feature = "scene")]
|
||||
pub use crate::scene::{Scene, SceneSpawner};
|
||||
#[cfg(feature = "sprite")]
|
||||
pub use crate::sprite::{ColorMaterial, Rect, Sprite, entity::SpriteEntity};
|
||||
pub use crate::sprite::{ColorMaterial, Quad, Sprite, entity::SpriteEntity};
|
||||
#[cfg(feature = "text")]
|
||||
pub use crate::text::Font;
|
||||
#[cfg(feature = "transform")]
|
||||
|
|
Loading…
Reference in a new issue