Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break out Visible component from Draw #1034

Merged
merged 3 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/bevy_pbr/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -16,6 +17,7 @@ pub struct PbrBundle {
pub material: Handle<StandardMaterial>,
pub main_pass: MainPass,
pub draw: Draw,
pub visible: Visible,
pub render_pipelines: RenderPipelines,
pub transform: Transform,
pub global_transform: GlobalTransform,
Expand All @@ -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(),
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_render/src/camera/visible_entities.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -26,21 +26,21 @@ 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<Draw>>,
visible_query: Query<(Entity, &Visible)>,
visible_transform_query: Query<&GlobalTransform, With<Visible>>,
) {
for (camera, camera_global_transform, mut visible_entities) in camera_query.iter_mut() {
visible_entities.value.clear();
let camera_position = camera_global_transform.translation;

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 {
Expand All @@ -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 })
Expand Down
21 changes: 17 additions & 4 deletions crates/bevy_render/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,34 @@ 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<RenderCommand>,
}

impl Default for Draw {
fn default() -> Self {
Self {
is_visible: true,
is_transparent: false,
render_commands: Default::default(),
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_render/src/entity.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
camera::{Camera, OrthographicProjection, PerspectiveProjection, VisibleEntities},
pipeline::RenderPipelines,
prelude::Visible,
render_graph::base,
Draw, Mesh,
};
Expand All @@ -15,6 +16,7 @@ use bevy_transform::components::{GlobalTransform, Transform};
pub struct MeshBundle {
pub mesh: Handle<Mesh>,
pub draw: Draw,
pub visible: Visible,
pub render_pipelines: RenderPipelines,
pub main_pass: MainPass,
pub transform: Transform,
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -105,6 +106,7 @@ impl Plugin for RenderPlugin {
.add_asset::<PipelineDescriptor>()
.register_type::<Camera>()
.register_type::<Draw>()
.register_type::<Visible>()
.register_type::<RenderPipelines>()
.register_type::<OrthographicProjection>()
.register_type::<PerspectiveProjection>()
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_render/src/pipeline/render_pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -82,10 +82,10 @@ pub fn draw_render_pipelines_system(
mut render_resource_bindings: ResMut<RenderResourceBindings>,
msaa: Res<Msaa>,
meshes: Res<Assets<Mesh>>,
mut query: Query<(&mut Draw, &mut RenderPipelines, &Handle<Mesh>)>,
mut query: Query<(&mut Draw, &mut RenderPipelines, &Handle<Mesh>, &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;
}

Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_render/src/render_graph/nodes/pass_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -236,8 +237,10 @@ where
continue;
};

if !draw.is_visible {
continue;
if let Ok(visible) = world.get::<Visible>(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
Expand Down
24 changes: 12 additions & 12 deletions crates/bevy_render/src/render_graph/nodes/render_resources_node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
draw::Draw,
pipeline::RenderPipelines,
prelude::Visible,
render_graph::{CommandQueue, Node, ResourceSlots, SystemNode},
renderer::{
self, BufferInfo, BufferUsage, RenderContext, RenderResourceBinding,
Expand All @@ -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};
Expand Down Expand Up @@ -437,8 +437,8 @@ fn render_resources_node_system<T: RenderResources>(
mut entities_waiting_for_textures: Local<Vec<Entity>>,
render_resource_context: Res<Box<dyn RenderResourceContext>>,
mut queries: QuerySet<(
Query<(Entity, &T, &Draw, &mut RenderPipelines), Changed<T>>,
Query<(Entity, &T, &Draw, &mut RenderPipelines)>,
Query<(Entity, &T, &Visible, &mut RenderPipelines), Or<(Changed<T>, Changed<Visible>)>>,
Query<(Entity, &T, &Visible, &mut RenderPipelines)>,
)>,
) {
let state = state.deref_mut();
Expand All @@ -456,7 +456,7 @@ fn render_resources_node_system<T: RenderResources>(

// 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::<T>(
Expand All @@ -469,8 +469,8 @@ fn render_resources_node_system<T: RenderResources>(
}
}

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;
}

Expand Down Expand Up @@ -498,10 +498,10 @@ fn render_resources_node_system<T: RenderResources>(
&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;
}

Expand All @@ -515,10 +515,10 @@ fn render_resources_node_system<T: RenderResources>(
);
}
} 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;
}

Expand Down
14 changes: 9 additions & 5 deletions crates/bevy_sprite/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -19,6 +19,7 @@ pub struct SpriteBundle {
pub material: Handle<ColorMaterial>,
pub main_pass: MainPass,
pub draw: Draw,
pub visible: Visible,
pub render_pipelines: RenderPipelines,
pub transform: Transform,
pub global_transform: GlobalTransform,
Expand All @@ -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(),
Expand All @@ -54,6 +56,7 @@ pub struct SpriteSheetBundle {
pub texture_atlas: Handle<TextureAtlas>,
/// 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<Mesh>, // TODO: maybe abstract this out
Expand All @@ -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(),
Expand Down
11 changes: 11 additions & 0 deletions crates/bevy_ui/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -23,6 +24,7 @@ pub struct NodeBundle {
pub mesh: Handle<Mesh>, // TODO: maybe abstract this out
pub material: Handle<ColorMaterial>,
pub draw: Draw,
pub visible: Visible,
pub render_pipelines: RenderPipelines,
pub transform: Transform,
pub global_transform: GlobalTransform,
Expand All @@ -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(),
Expand All @@ -54,6 +57,7 @@ pub struct ImageBundle {
pub mesh: Handle<Mesh>, // TODO: maybe abstract this out
pub material: Handle<ColorMaterial>,
pub draw: Draw,
pub visible: Visible,
pub render_pipelines: RenderPipelines,
pub transform: Transform,
pub global_transform: GlobalTransform,
Expand All @@ -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(),
}
Expand All @@ -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,
Expand All @@ -95,6 +101,9 @@ impl Default for TextBundle {
TextBundle {
focus_policy: FocusPolicy::Pass,
draw: Draw {
..Default::default()
},
visible: Visible {
is_transparent: true,
..Default::default()
},
Expand All @@ -118,6 +127,7 @@ pub struct ButtonBundle {
pub mesh: Handle<Mesh>, // TODO: maybe abstract this out
pub material: Handle<ColorMaterial>,
pub draw: Draw,
pub visible: Visible,
pub render_pipelines: RenderPipelines,
pub transform: Transform,
pub global_transform: GlobalTransform,
Expand All @@ -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(),
}
Expand Down
Loading