diff --git a/crates/bevy_render/src/camera.rs b/crates/bevy_render/src/camera.rs index 744a64ea7a..bef70e45be 100644 --- a/crates/bevy_render/src/camera.rs +++ b/crates/bevy_render/src/camera.rs @@ -9,6 +9,7 @@ pub struct ActiveCamera; #[derive(Default)] pub struct ActiveCamera2d; +#[derive(Debug)] pub struct OrthographicCamera { pub left: f32, pub right: f32, @@ -45,6 +46,7 @@ impl Default for OrthographicCamera { } } +#[derive(Debug)] pub struct PerspectiveCamera { pub fov: f32, pub aspect_ratio: f32, @@ -70,6 +72,7 @@ impl Default for PerspectiveCamera { } } +#[derive(Debug)] pub enum CameraType { Perspective(PerspectiveCamera), Orthographic(OrthographicCamera), @@ -91,7 +94,7 @@ impl Default for CameraType { } } -#[derive(Default)] +#[derive(Default, Debug)] pub struct Camera { pub view_matrix: Mat4, pub camera_type: CameraType, diff --git a/crates/bevy_render/src/draw_target/draw_targets/assigned_meshes_draw_target.rs b/crates/bevy_render/src/draw_target/draw_targets/assigned_meshes_draw_target.rs index 97e2fc69ea..a704ce7adb 100644 --- a/crates/bevy_render/src/draw_target/draw_targets/assigned_meshes_draw_target.rs +++ b/crates/bevy_render/src/draw_target/draw_targets/assigned_meshes_draw_target.rs @@ -80,6 +80,7 @@ impl DrawTarget for AssignedMeshesDrawTarget { pipeline_descriptor, &renderable.render_resource_assignments, ); + render_pass.draw_indexed(0..current_mesh_index_len, 0, 0..1); } } diff --git a/crates/bevy_render/src/render_graph/nodes/camera2d_node.rs b/crates/bevy_render/src/render_graph/nodes/camera2d_node.rs index 14f883110f..c94332ba42 100644 --- a/crates/bevy_render/src/render_graph/nodes/camera2d_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/camera2d_node.rs @@ -8,7 +8,6 @@ use crate::{ renderer::{GlobalRenderResourceContext, RenderContext}, }; -use bevy_transform::components::LocalToWorld; use legion::prelude::*; use zerocopy::AsBytes; @@ -41,7 +40,7 @@ impl SystemNode for Camera2dNode { // TODO: this write on RenderResourceAssignments will prevent this system from running in parallel with other systems that do the same .write_resource::() .read_resource::>() - .with_query(<(Read, Read, Read)>::query()) + .with_query(<(Read, Read)>::query()) .build( move |_, world, @@ -66,9 +65,9 @@ impl SystemNode for Camera2dNode { .find_latest(&window_resized_events, |event| event.is_primary); if let Some(_) = primary_window_resized_event { let matrix_size = std::mem::size_of::<[[f32; 4]; 4]>(); - for (camera, local_to_world, _) in query.iter(world) { + for (camera, _) in query.iter(world) { let camera_matrix: [[f32; 4]; 4] = - (camera.view_matrix * local_to_world.0).to_cols_array_2d(); + camera.view_matrix.to_cols_array_2d(); if let Some(old_tmp_buffer) = tmp_buffer { render_resources.remove_buffer(old_tmp_buffer); diff --git a/crates/bevy_ui/src/entity.rs b/crates/bevy_ui/src/entity.rs index 8e4a14c9bc..bc268a29c4 100644 --- a/crates/bevy_ui/src/entity.rs +++ b/crates/bevy_ui/src/entity.rs @@ -10,7 +10,7 @@ pub struct UiEntity { pub node: Node, pub rect: Rect, pub mesh: Handle, // TODO: maybe abstract this out - // pub renderable: Renderable, + pub renderable: Renderable, } impl Default for UiEntity { @@ -19,12 +19,12 @@ impl Default for UiEntity { node: Default::default(), rect: Default::default(), mesh: QUAD_HANDLE, - // renderable: Renderable { - // pipelines: vec![ - // UI_PIPELINE_HANDLE - // ], - // ..Default::default() - // } + renderable: Renderable { + pipelines: vec![ + UI_PIPELINE_HANDLE + ], + ..Default::default() + } } } } \ No newline at end of file diff --git a/crates/bevy_ui/src/node.rs b/crates/bevy_ui/src/node.rs index b8ac8c3820..3d287ddcc7 100644 --- a/crates/bevy_ui/src/node.rs +++ b/crates/bevy_ui/src/node.rs @@ -1,7 +1,6 @@ use super::{Anchors, Margins}; -use bevy_render::Color; -use glam::{self, Vec2}; use crate::Rect; +use glam::Vec2; #[derive(Debug, Clone)] enum GrowDirection { @@ -12,36 +11,30 @@ enum GrowDirection { #[derive(Debug, Clone)] pub struct Node { pub position: Vec2, - pub size: Vec2, pub anchors: Anchors, pub margins: Margins, - pub color: Color, } impl Default for Node { fn default() -> Self { Node { position: Vec2::default(), - size: Vec2::default(), anchors: Anchors::default(), margins: Margins::default(), - color: Color::rgb(0.0, 0.0, 0.0), } } } impl Node { - pub fn new(position: Vec2, anchors: Anchors, margins: Margins, color: Color) -> Self { + pub fn new(position: Vec2, anchors: Anchors, margins: Margins) -> Self { Node { position, - size: Vec2::default(), anchors, margins, - color, } } - pub fn update(&mut self, rect: &mut Rect, parent_dimensions: Vec2, parent_position: Vec2) { + pub fn update(&mut self, rect: &mut Rect, parent_dimensions: Vec2, parent_position: Vec2, z_index: f32) { let (rect_x, rect_width) = Self::compute_dimension_properties( self.position.x(), self.margins.left, @@ -59,8 +52,9 @@ impl Node { parent_dimensions.y(), ); - self.size = glam::vec2(rect_width, rect_height); - rect.position = glam::vec2(rect_x, rect_y) + parent_position; + rect.size = Vec2::new(rect_width, rect_height); + rect.position = Vec2::new(rect_x, rect_y) + parent_position; + rect.z_index = z_index; } fn compute_dimension_properties( diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 4cd4244e2f..7dc9095824 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -25,8 +25,8 @@ pub fn build_ui_pipeline(shaders: &mut AssetStorage) -> PipelineDescript }), depth_stencil_state: Some(DepthStencilStateDescriptor { format: TextureFormat::Depth32Float, - depth_write_enabled: true, - depth_compare: CompareFunction::Less, + depth_write_enabled: false, + depth_compare: CompareFunction::Always, stencil_front: StencilStateFaceDescriptor::IGNORE, stencil_back: StencilStateFaceDescriptor::IGNORE, stencil_read_mask: 0, diff --git a/crates/bevy_ui/src/ui_update_system.rs b/crates/bevy_ui/src/ui_update_system.rs index 2a1cdaa37e..8960c6f720 100644 --- a/crates/bevy_ui/src/ui_update_system.rs +++ b/crates/bevy_ui/src/ui_update_system.rs @@ -9,7 +9,7 @@ use legion::{prelude::*, systems::SubWorld}; pub fn ui_update_system() -> Box { SystemBuilder::new("ui_update") .read_resource::() - .with_query(<(Write,)>::query().filter(!component::())) + .with_query(>::query().filter(!component::())) .write_component::() .write_component::() .read_component::() @@ -17,11 +17,15 @@ pub fn ui_update_system() -> Box { if let Some(window) = windows.get_primary() { let parent_size = glam::vec2(window.width as f32, window.height as f32); let parent_position = glam::vec2(0.0, 0.0); - for (entity, _) in node_query.iter_entities_mut(world) { + for entity in node_query + .iter_entities(world) + .map(|(e, _)| e) + .collect::>() + { run_on_hierarchy_subworld_mut( world, entity, - (parent_size, parent_position), + (parent_size, parent_position, 0.9999), &mut update_node_entity, ); } @@ -32,15 +36,15 @@ pub fn ui_update_system() -> Box { fn update_node_entity( world: &mut SubWorld, entity: Entity, - parent_properties: (Vec2, Vec2), -) -> Option<(Vec2, Vec2)> { - let (parent_size, parent_position) = parent_properties; + parent_properties: (Vec2, Vec2, f32), +) -> Option<(Vec2, Vec2, f32)> { + let (parent_size, parent_position, z_index) = parent_properties; // 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) { - node.update(&mut rect, parent_size, parent_position); - return Some((node.size, rect.position)); + node.update(&mut rect, parent_size, parent_position, z_index); + return Some((rect.size, rect.position, z_index - 0.0001)); } } } diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index a935484e21..a0936b5296 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +use bevy_ui::Rect; fn main() { App::build() @@ -51,8 +52,11 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(0.0, 0.0), Anchors::new(0.0, 0.0, 0.0, 1.0), Margins::new(10.0, 200.0, 10.0, 10.0), - Color::rgb(0.1, 0.1, 0.1), ), + rect: Rect { + color: Color::rgb(0.02, 0.02, 0.02), + ..Default::default() + }, ..Default::default() }) // top right anchor with vertical fill @@ -61,8 +65,11 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(0.0, 0.0), Anchors::new(1.0, 1.0, 0.0, 1.0), Margins::new(10.0, 100.0, 50.0, 100.0), - Color::rgb(0.1, 0.1, 0.1), ), + rect: Rect { + color: Color::rgb(0.02, 0.02, 0.02), + ..Default::default() + }, ..Default::default() }) // render order test: reddest in the back, whitest in the front @@ -71,8 +78,11 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(75.0, 75.0), Anchors::new(0.5, 0.5, 0.5, 0.5), Margins::new(0.0, 100.0, 0.0, 100.0), - Color::rgb(1.0, 0.1, 0.1), ), + rect: Rect { + color: Color::rgb(1.0, 0.00, 0.0), + ..Default::default() + }, ..Default::default() }) .add_entity(UiEntity { @@ -80,8 +90,11 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(50.0, 50.0), Anchors::new(0.5, 0.5, 0.5, 0.5), Margins::new(0.0, 100.0, 0.0, 100.0), - Color::rgb(1.0, 0.3, 0.3), ), + rect: Rect { + color: Color::rgb(1.0, 0.3, 0.3), + ..Default::default() + }, ..Default::default() }) .add_entity(UiEntity { @@ -89,8 +102,11 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(100.0, 100.0), Anchors::new(0.5, 0.5, 0.5, 0.5), Margins::new(0.0, 100.0, 0.0, 100.0), - Color::rgb(1.0, 0.5, 0.5), ), + rect: Rect { + color: Color::rgb(1.0, 0.5, 0.5), + ..Default::default() + }, ..Default::default() }) .add_entity(UiEntity { @@ -98,8 +114,11 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(150.0, 150.0), Anchors::new(0.5, 0.5, 0.5, 0.5), Margins::new(0.0, 100.0, 0.0, 100.0), - Color::rgb(1.0, 0.7, 0.7), ), + rect: Rect { + color: Color::rgb(1.0, 0.7, 0.7), + ..Default::default() + }, ..Default::default() }) // parenting @@ -108,8 +127,11 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(300.0, 300.0), Anchors::new(0.0, 0.0, 0.0, 0.0), Margins::new(0.0, 200.0, 0.0, 200.0), - Color::rgb(0.1, 0.1, 1.0), ), + rect: Rect { + color: Color::rgb(0.1, 0.1, 1.0), + ..Default::default() + }, ..Default::default() }) .add_children(|builder| { @@ -118,8 +140,11 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(0.0, 0.0), Anchors::new(0.0, 1.0, 0.0, 1.0), Margins::new(20.0, 20.0, 20.0, 20.0), - Color::rgb(0.6, 0.6, 1.0), ), + rect: Rect { + color: Color::rgb(0.6, 0.6, 1.0), + ..Default::default() + }, ..Default::default() }); }) @@ -129,8 +154,11 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(200.0, 200.0), Anchors::new(0.5, 0.5, 0.5, 0.5), Margins::new(0.0, 100.0, 0.0, 100.0), - Color::rgba(1.0, 0.9, 0.9, 0.4), ), + rect: Rect { + color: Color::rgba(1.0, 0.9, 0.9, 0.4), + ..Default::default() + }, ..Default::default() }); }