From 8e86cb4450db9e9de3cd9d1f6cb0d2d46bd6adf5 Mon Sep 17 00:00:00 2001 From: Sebastian Hamel Date: Sat, 16 Sep 2023 01:29:29 -0400 Subject: [PATCH] hide canvas when no render items are in scene --- Cargo.toml | 2 +- src/lib.rs | 8 +++++++- src/renderer/extract.rs | 36 ++++++++++++------------------------ src/renderer/plugin.rs | 3 +-- src/rendertarget.rs | 16 +++++++++++++++- 5 files changed, 36 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 491924b..e30883a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = ["demo"] [workspace.package] edition = "2021" -version = "0.3.1" +version = "0.3.2" license = "MIT OR Apache-2.0" repository = "https://github.com/vectorgameexperts/bevy-vello" diff --git a/src/lib.rs b/src/lib.rs index c002417..bed9ea0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,7 +50,13 @@ impl Plugin for BevyVelloPlugin { DebugVisualizationsPlugin, )); app.add_systems(Startup, rendertarget::setup_ss_rendertarget) - .add_systems(Update, rendertarget::resize_rendertargets); + .add_systems( + Update, + ( + rendertarget::resize_rendertargets, + rendertarget::clear_when_empty, + ), + ); } } diff --git a/src/renderer/extract.rs b/src/renderer/extract.rs index b818c7b..5d494af 100644 --- a/src/renderer/extract.rs +++ b/src/renderer/extract.rs @@ -6,21 +6,6 @@ use bevy::{ use crate::{font::VelloFont, ColorPaletteSwap, Layer, VelloText, VelloVector}; -#[derive(Component)] -pub struct RenderReadyTag; - -pub fn tag_vectors_for_render( - mut commands: Commands, - vector_assets: ResMut>, - vectors: Query<(Entity, &Handle), Without>, -) { - for (entity, handle) in vectors.iter() { - if vector_assets.get(handle).is_some() { - commands.entity(entity).insert(RenderReadyTag); - } - } -} - #[derive(Component, Clone)] pub struct ExtractedRenderVector { pub vector_handle: Handle, @@ -40,22 +25,25 @@ pub fn vector_instances( &GlobalTransform, Option<&ColorPaletteSwap>, Option<&Node>, + &ComputedVisibility, )>, >, assets: Extract>>, ) { - for (vello_vector_handle, layer, transform, color_pallette_swap, ui_node) in + for (vello_vector_handle, layer, transform, color_pallette_swap, ui_node, visibility) in query_vectors.iter() { if let Some(asset_data) = assets.get(vello_vector_handle) { - commands.spawn(ExtractedRenderVector { - vector_handle: vello_vector_handle.clone(), - render_data: asset_data.to_owned(), - transform: *transform, - layer: *layer, - color_pallette_swap: color_pallette_swap.cloned(), - ui_node: ui_node.cloned(), - }); + if visibility.is_visible() { + commands.spawn(ExtractedRenderVector { + vector_handle: vello_vector_handle.clone(), + render_data: asset_data.to_owned(), + transform: *transform, + layer: *layer, + color_pallette_swap: color_pallette_swap.cloned(), + ui_node: ui_node.cloned(), + }); + } } } } diff --git a/src/renderer/plugin.rs b/src/renderer/plugin.rs index 55dc8d5..1f9850c 100644 --- a/src/renderer/plugin.rs +++ b/src/renderer/plugin.rs @@ -49,8 +49,7 @@ impl Plugin for VelloRenderPlugin { ExtractComponentPlugin::::default(), RenderAssetPlugin::::default(), RenderAssetPlugin::::default(), - )) - .add_systems(Update, extract::tag_vectors_for_render); + )); } fn finish(&self, app: &mut App) { diff --git a/src/rendertarget.rs b/src/rendertarget.rs index 699a755..1854759 100644 --- a/src/rendertarget.rs +++ b/src/rendertarget.rs @@ -16,7 +16,7 @@ use bevy::{ window::{WindowResized, WindowResolution}, }; -use crate::{renderer::SSRenderTarget, SSRT_SHADER_HANDLE}; +use crate::{renderer::SSRenderTarget, Layer, SSRT_SHADER_HANDLE}; #[derive(Component)] struct MainCamera; @@ -195,3 +195,17 @@ impl Material2d for VelloCanvasMaterial { Ok(()) } } + +/// Hide the RenderTarget canvas if there is nothing to render +pub fn clear_when_empty( + mut query_render_target: Query<&mut Visibility, With>, + render_items: Query<(&mut Layer, &ComputedVisibility)>, +) { + if let Ok(mut visibility) = query_render_target.get_single_mut() { + if render_items.is_empty() { + *visibility = Visibility::Hidden; + } else { + *visibility = Visibility::Inherited; + } + } +}