diff --git a/CHANGELOG.md b/CHANGELOG.md index a051f8e68a533..58c3b42d53eac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,8 @@ current changes on git with [previous release tags][git_tag_comparison]. - Created getters to get `Time` state and made members private. - Modifying `Time`'s values directly is no longer possible outside of bevy. - [Use `mailbox` instead of `fifo` for vsync on supported systems][920] +- [Break out Visible component from Draw][1034] + - Users setting `Draw::is_visible` or `Draw::is_transparent` should now set `Visible::is_visible` and `Visible::is_transparent` ### Fixed @@ -68,6 +70,7 @@ current changes on git with [previous release tags][git_tag_comparison]. [934]: https://github.com/bevyengine/bevy/pull/934 [945]: https://github.com/bevyengine/bevy/pull/945 [955]: https://github.com/bevyengine/bevy/pull/955 +[1034]: https://github.com/bevyengine/bevy/pull/1034 ## Version 0.3.0 (2020-11-03) diff --git a/crates/bevy_pbr/src/entity.rs b/crates/bevy_pbr/src/entity.rs index c3dde08d8bf44..5cf11e89844e4 100644 --- a/crates/bevy_pbr/src/entity.rs +++ b/crates/bevy_pbr/src/entity.rs @@ -5,6 +5,7 @@ use bevy_render::{ draw::Draw, mesh::Mesh, pipeline::{RenderPipeline, RenderPipelines}, + prelude::Visible, render_graph::base::MainPass, }; use bevy_transform::prelude::{GlobalTransform, Transform}; @@ -16,6 +17,7 @@ pub struct PbrBundle { pub material: Handle, pub main_pass: MainPass, pub draw: Draw, + pub visible: Visible, pub render_pipelines: RenderPipelines, pub transform: Transform, pub global_transform: GlobalTransform, @@ -28,6 +30,7 @@ impl Default for PbrBundle { FORWARD_PIPELINE_HANDLE.typed(), )]), mesh: Default::default(), + visible: Default::default(), material: Default::default(), main_pass: Default::default(), draw: Default::default(), diff --git a/crates/bevy_render/src/camera/visible_entities.rs b/crates/bevy_render/src/camera/visible_entities.rs index 132e0fd9d7f8b..e1d54bc1c40ad 100644 --- a/crates/bevy_render/src/camera/visible_entities.rs +++ b/crates/bevy_render/src/camera/visible_entities.rs @@ -1,5 +1,5 @@ use super::{Camera, DepthCalculation}; -use crate::Draw; +use crate::prelude::Visible; use bevy_core::FloatOrd; use bevy_ecs::{Entity, Query, With}; use bevy_reflect::{Reflect, ReflectComponent}; @@ -26,8 +26,8 @@ impl VisibleEntities { pub fn visible_entities_system( mut camera_query: Query<(&Camera, &GlobalTransform, &mut VisibleEntities)>, - draw_query: Query<(Entity, &Draw)>, - draw_transform_query: Query<&GlobalTransform, With>, + visible_query: Query<(Entity, &Visible)>, + visible_transform_query: Query<&GlobalTransform, With>, ) { for (camera, camera_global_transform, mut visible_entities) in camera_query.iter_mut() { visible_entities.value.clear(); @@ -35,12 +35,12 @@ pub fn visible_entities_system( let mut no_transform_order = 0.0; let mut transparent_entities = Vec::new(); - for (entity, draw) in draw_query.iter() { - if !draw.is_visible { + for (entity, visible) in visible_query.iter() { + if !visible.is_visible { continue; } - let order = if let Ok(global_transform) = draw_transform_query.get(entity) { + let order = if let Ok(global_transform) = visible_transform_query.get(entity) { let position = global_transform.translation; // smaller distances are sorted to lower indices by using the distance from the camera FloatOrd(match camera.depth_calculation { @@ -53,7 +53,7 @@ pub fn visible_entities_system( order }; - if draw.is_transparent { + if visible.is_transparent { transparent_entities.push(VisibleEntity { entity, order }) } else { visible_entities.value.push(VisibleEntity { entity, order }) diff --git a/crates/bevy_render/src/draw.rs b/crates/bevy_render/src/draw.rs index c9396dafdd41a..25d6be663d1ef 100644 --- a/crates/bevy_render/src/draw.rs +++ b/crates/bevy_render/src/draw.rs @@ -43,12 +43,27 @@ pub enum RenderCommand { }, } -/// A component that indicates how to draw an entity. #[derive(Debug, Clone, Reflect)] #[reflect(Component)] -pub struct Draw { +pub struct Visible { pub is_visible: bool, + // TODO: consider moving this to materials pub is_transparent: bool, +} + +impl Default for Visible { + fn default() -> Self { + Visible { + is_visible: true, + is_transparent: false, + } + } +} + +/// A component that indicates how to draw an entity. +#[derive(Debug, Clone, Reflect)] +#[reflect(Component)] +pub struct Draw { #[reflect(ignore)] pub render_commands: Vec, } @@ -56,8 +71,6 @@ pub struct Draw { impl Default for Draw { fn default() -> Self { Self { - is_visible: true, - is_transparent: false, render_commands: Default::default(), } } diff --git a/crates/bevy_render/src/entity.rs b/crates/bevy_render/src/entity.rs index 1af4beba62776..c8837d39c36c0 100644 --- a/crates/bevy_render/src/entity.rs +++ b/crates/bevy_render/src/entity.rs @@ -1,6 +1,7 @@ use crate::{ camera::{Camera, OrthographicProjection, PerspectiveProjection, VisibleEntities}, pipeline::RenderPipelines, + prelude::Visible, render_graph::base, Draw, Mesh, }; @@ -15,6 +16,7 @@ use bevy_transform::components::{GlobalTransform, Transform}; pub struct MeshBundle { pub mesh: Handle, pub draw: Draw, + pub visible: Visible, pub render_pipelines: RenderPipelines, pub main_pass: MainPass, pub transform: Transform, diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index a2699ad8d7a96..506b597c67fae 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -12,13 +12,14 @@ pub mod shader; pub mod texture; use bevy_reflect::RegisterTypeBuilder; +use draw::Visible; pub use once_cell; pub mod prelude { pub use crate::{ base::Msaa, color::Color, - draw::Draw, + draw::{Draw, Visible}, entity::*, mesh::{shape, Mesh}, pass::ClearColor, @@ -105,6 +106,7 @@ impl Plugin for RenderPlugin { .add_asset::() .register_type::() .register_type::() + .register_type::() .register_type::() .register_type::() .register_type::() diff --git a/crates/bevy_render/src/pipeline/render_pipelines.rs b/crates/bevy_render/src/pipeline/render_pipelines.rs index d9d4be1fc5eca..0831e93cd0ad8 100644 --- a/crates/bevy_render/src/pipeline/render_pipelines.rs +++ b/crates/bevy_render/src/pipeline/render_pipelines.rs @@ -2,7 +2,7 @@ use super::{PipelineDescriptor, PipelineSpecialization}; use crate::{ draw::{Draw, DrawContext}, mesh::{Indices, Mesh}, - prelude::Msaa, + prelude::{Msaa, Visible}, renderer::RenderResourceBindings, }; use bevy_asset::{Assets, Handle}; @@ -82,10 +82,10 @@ pub fn draw_render_pipelines_system( mut render_resource_bindings: ResMut, msaa: Res, meshes: Res>, - mut query: Query<(&mut Draw, &mut RenderPipelines, &Handle)>, + mut query: Query<(&mut Draw, &mut RenderPipelines, &Handle, &Visible)>, ) { - for (mut draw, mut render_pipelines, mesh_handle) in query.iter_mut() { - if !draw.is_visible { + for (mut draw, mut render_pipelines, mesh_handle, visible) in query.iter_mut() { + if !visible.is_visible { continue; } diff --git a/crates/bevy_render/src/render_graph/nodes/pass_node.rs b/crates/bevy_render/src/render_graph/nodes/pass_node.rs index 8067997ba848f..72f0dfb860be7 100644 --- a/crates/bevy_render/src/render_graph/nodes/pass_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/pass_node.rs @@ -6,6 +6,7 @@ use crate::{ BindGroupDescriptor, BindType, BindingDescriptor, BindingShaderStage, PipelineDescriptor, UniformProperty, }, + prelude::Visible, render_graph::{Node, ResourceSlotInfo, ResourceSlots}, renderer::{ BindGroup, BindGroupId, BufferId, RenderContext, RenderResourceBindings, RenderResourceType, @@ -236,8 +237,10 @@ where continue; }; - if !draw.is_visible { - continue; + if let Ok(visible) = world.get::(visible_entity.entity) { + if !visible.is_visible { + continue; + } } // each Draw component contains an ordered list of render commands. we turn those into actual render commands here diff --git a/crates/bevy_render/src/render_graph/nodes/render_resources_node.rs b/crates/bevy_render/src/render_graph/nodes/render_resources_node.rs index eef183d9850bb..df1a592f3c9e4 100644 --- a/crates/bevy_render/src/render_graph/nodes/render_resources_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/render_resources_node.rs @@ -1,6 +1,6 @@ use crate::{ - draw::Draw, pipeline::RenderPipelines, + prelude::Visible, render_graph::{CommandQueue, Node, ResourceSlots, SystemNode}, renderer::{ self, BufferInfo, BufferUsage, RenderContext, RenderResourceBinding, @@ -12,8 +12,8 @@ use crate::{ use bevy_app::{EventReader, Events}; use bevy_asset::{Asset, AssetEvent, Assets, Handle, HandleId}; use bevy_ecs::{ - Changed, Commands, Entity, IntoSystem, Local, Query, QuerySet, Res, ResMut, Resources, System, - With, World, + Changed, Commands, Entity, IntoSystem, Local, Or, Query, QuerySet, Res, ResMut, Resources, + System, With, World, }; use bevy_utils::HashMap; use renderer::{AssetRenderResourceBindings, BufferId, RenderResourceType, RenderResources}; @@ -437,8 +437,8 @@ fn render_resources_node_system( mut entities_waiting_for_textures: Local>, render_resource_context: Res>, mut queries: QuerySet<( - Query<(Entity, &T, &Draw, &mut RenderPipelines), Changed>, - Query<(Entity, &T, &Draw, &mut RenderPipelines)>, + Query<(Entity, &T, &Visible, &mut RenderPipelines), Or<(Changed, Changed)>>, + Query<(Entity, &T, &Visible, &mut RenderPipelines)>, )>, ) { let state = state.deref_mut(); @@ -456,7 +456,7 @@ fn render_resources_node_system( // handle entities that were waiting for texture loads on the last update for entity in std::mem::take(&mut *entities_waiting_for_textures) { - if let Ok((entity, uniforms, _draw, mut render_pipelines)) = + if let Ok((entity, uniforms, _visible, mut render_pipelines)) = queries.q1_mut().get_mut(entity) { if !setup_uniform_texture_resources::( @@ -469,8 +469,8 @@ fn render_resources_node_system( } } - for (entity, uniforms, draw, mut render_pipelines) in queries.q0_mut().iter_mut() { - if !draw.is_visible { + for (entity, uniforms, visible, mut render_pipelines) in queries.q0_mut().iter_mut() { + if !visible.is_visible { continue; } @@ -498,10 +498,10 @@ fn render_resources_node_system( &mut |mut staging_buffer, _render_resource_context| { // if the buffer array was resized, write all entities to the new buffer, otherwise only write changes if resized { - for (entity, uniforms, draw, mut render_pipelines) in + for (entity, uniforms, visible, mut render_pipelines) in queries.q1_mut().iter_mut() { - if !draw.is_visible { + if !visible.is_visible { continue; } @@ -515,10 +515,10 @@ fn render_resources_node_system( ); } } else { - for (entity, uniforms, draw, mut render_pipelines) in + for (entity, uniforms, visible, mut render_pipelines) in queries.q0_mut().iter_mut() { - if !draw.is_visible { + if !visible.is_visible { continue; } diff --git a/crates/bevy_sprite/src/entity.rs b/crates/bevy_sprite/src/entity.rs index 0a6ce971ebfd8..9136cce5ed598 100644 --- a/crates/bevy_sprite/src/entity.rs +++ b/crates/bevy_sprite/src/entity.rs @@ -7,7 +7,7 @@ use bevy_ecs::Bundle; use bevy_render::{ mesh::Mesh, pipeline::{RenderPipeline, RenderPipelines}, - prelude::Draw, + prelude::{Draw, Visible}, render_graph::base::MainPass, }; use bevy_transform::prelude::{GlobalTransform, Transform}; @@ -19,6 +19,7 @@ pub struct SpriteBundle { pub material: Handle, pub main_pass: MainPass, pub draw: Draw, + pub visible: Visible, pub render_pipelines: RenderPipelines, pub transform: Transform, pub global_transform: GlobalTransform, @@ -31,12 +32,13 @@ impl Default for SpriteBundle { render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new( SPRITE_PIPELINE_HANDLE.typed(), )]), - draw: Draw { + visible: Visible { is_transparent: true, ..Default::default() }, - sprite: Default::default(), main_pass: MainPass, + draw: Default::default(), + sprite: Default::default(), material: Default::default(), transform: Default::default(), global_transform: Default::default(), @@ -54,6 +56,7 @@ pub struct SpriteSheetBundle { pub texture_atlas: Handle, /// Data pertaining to how the sprite is drawn on the screen pub draw: Draw, + pub visible: Visible, pub render_pipelines: RenderPipelines, pub main_pass: MainPass, pub mesh: Handle, // TODO: maybe abstract this out @@ -67,12 +70,13 @@ impl Default for SpriteSheetBundle { render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new( SPRITE_SHEET_PIPELINE_HANDLE.typed(), )]), - draw: Draw { + visible: Visible { is_transparent: true, ..Default::default() }, - mesh: QUAD_HANDLE.typed(), main_pass: MainPass, + mesh: QUAD_HANDLE.typed(), + draw: Default::default(), sprite: Default::default(), texture_atlas: Default::default(), transform: Default::default(), diff --git a/crates/bevy_ui/src/entity.rs b/crates/bevy_ui/src/entity.rs index b4b9516a0cc2b..b60d4c34a8bab 100644 --- a/crates/bevy_ui/src/entity.rs +++ b/crates/bevy_ui/src/entity.rs @@ -12,6 +12,7 @@ use bevy_render::{ draw::Draw, mesh::Mesh, pipeline::{RenderPipeline, RenderPipelines}, + prelude::Visible, }; use bevy_sprite::{ColorMaterial, QUAD_HANDLE}; use bevy_transform::prelude::{GlobalTransform, Transform}; @@ -23,6 +24,7 @@ pub struct NodeBundle { pub mesh: Handle, // TODO: maybe abstract this out pub material: Handle, pub draw: Draw, + pub visible: Visible, pub render_pipelines: RenderPipelines, pub transform: Transform, pub global_transform: GlobalTransform, @@ -35,6 +37,7 @@ impl Default for NodeBundle { render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new( UI_PIPELINE_HANDLE.typed(), )]), + visible: Default::default(), node: Default::default(), style: Default::default(), material: Default::default(), @@ -54,6 +57,7 @@ pub struct ImageBundle { pub mesh: Handle, // TODO: maybe abstract this out pub material: Handle, pub draw: Draw, + pub visible: Visible, pub render_pipelines: RenderPipelines, pub transform: Transform, pub global_transform: GlobalTransform, @@ -72,6 +76,7 @@ impl Default for ImageBundle { style: Default::default(), material: Default::default(), draw: Default::default(), + visible: Default::default(), transform: Default::default(), global_transform: Default::default(), } @@ -83,6 +88,7 @@ pub struct TextBundle { pub node: Node, pub style: Style, pub draw: Draw, + pub visible: Visible, pub text: Text, pub calculated_size: CalculatedSize, pub focus_policy: FocusPolicy, @@ -95,6 +101,9 @@ impl Default for TextBundle { TextBundle { focus_policy: FocusPolicy::Pass, draw: Draw { + ..Default::default() + }, + visible: Visible { is_transparent: true, ..Default::default() }, @@ -118,6 +127,7 @@ pub struct ButtonBundle { pub mesh: Handle, // TODO: maybe abstract this out pub material: Handle, pub draw: Draw, + pub visible: Visible, pub render_pipelines: RenderPipelines, pub transform: Transform, pub global_transform: GlobalTransform, @@ -137,6 +147,7 @@ impl Default for ButtonBundle { style: Default::default(), material: Default::default(), draw: Default::default(), + visible: Default::default(), transform: Default::default(), global_transform: Default::default(), } diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index d159299efe1c4..c5dd8d389d606 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -5,7 +5,7 @@ use bevy_math::Size; use bevy_render::{ draw::{Draw, DrawContext, Drawable}, mesh::Mesh, - prelude::Msaa, + prelude::{Msaa, Visible}, renderer::RenderResourceBindings, texture::Texture, }; @@ -146,13 +146,13 @@ pub fn draw_text_system( meshes: Res>, mut render_resource_bindings: ResMut, text_pipeline: Res, - mut query: Query<(Entity, &mut Draw, &Text, &Node, &GlobalTransform)>, + mut query: Query<(Entity, &mut Draw, &Visible, &Text, &Node, &GlobalTransform)>, ) { let font_quad = meshes.get(&QUAD_HANDLE).unwrap(); let vertex_buffer_descriptor = font_quad.get_vertex_buffer_descriptor(); - for (entity, mut draw, text, node, global_transform) in query.iter_mut() { - if !draw.is_visible { + for (entity, mut draw, visible, text, node, global_transform) in query.iter_mut() { + if !visible.is_visible { continue; } diff --git a/examples/3d/texture.rs b/examples/3d/texture.rs index 8d853eace397a..119cf7ec4afe3 100644 --- a/examples/3d/texture.rs +++ b/examples/3d/texture.rs @@ -58,7 +58,7 @@ fn setup( rotation: Quat::from_rotation_x(-std::f32::consts::PI / 5.0), ..Default::default() }, - draw: Draw { + visible: Visible { is_transparent: true, ..Default::default() }, @@ -73,7 +73,7 @@ fn setup( rotation: Quat::from_rotation_x(-std::f32::consts::PI / 5.0), ..Default::default() }, - draw: Draw { + visible: Visible { is_transparent: true, ..Default::default() }, @@ -88,7 +88,7 @@ fn setup( rotation: Quat::from_rotation_x(-std::f32::consts::PI / 5.0), ..Default::default() }, - draw: Draw { + visible: Visible { is_transparent: true, ..Default::default() }, diff --git a/examples/tools/bevymark.rs b/examples/tools/bevymark.rs index c3e27e4310e06..475d7224b9be8 100644 --- a/examples/tools/bevymark.rs +++ b/examples/tools/bevymark.rs @@ -97,7 +97,7 @@ fn mouse_handler( .spawn(SpriteBundle { material: bird_material.0.clone(), transform, - draw: Draw { + visible: Visible { is_transparent: true, ..Default::default() }, diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index 479954d085efe..fef5595574404 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -115,7 +115,7 @@ fn setup( ..Default::default() }, material: materials.add(Color::NONE.into()), - draw: Draw { + visible: Visible { is_transparent: true, ..Default::default() }, @@ -188,7 +188,7 @@ fn setup( ..Default::default() }, material: materials.add(Color::rgba(1.0, 0.9, 0.9, 0.4).into()), - draw: Draw { + visible: Visible { is_transparent: true, ..Default::default() }, @@ -206,7 +206,7 @@ fn setup( ..Default::default() }, material: materials.add(Color::NONE.into()), - draw: Draw { + visible: Visible { is_transparent: true, ..Default::default() }, @@ -221,7 +221,7 @@ fn setup( }, material: materials .add(asset_server.load("branding/bevy_logo_dark_big.png").into()), - draw: Draw { + visible: Visible { is_transparent: true, ..Default::default() },