Skip to content

Commit

Permalink
Break out Visible component from Draw (#1034)
Browse files Browse the repository at this point in the history
Break out Visible component from Draw
  • Loading branch information
cart authored Dec 9, 2020
1 parent 66f972c commit 7ab0eee
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 47 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
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
Loading

0 comments on commit 7ab0eee

Please sign in to comment.