diff --git a/src/renderer.rs b/src/renderer.rs index 3d19503..2703762 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,7 +1,7 @@ use std::marker::PhantomData; use crate::{prelude::*, typst_element::prelude::*}; -use bevy::{prelude::*, utils::HashMap}; +use bevy::{prelude::*, render::view::RenderLayers, utils::HashMap}; use bevy_vello::prelude::*; use typst_vello::TypstScene; @@ -149,6 +149,7 @@ fn render_velyst_scene( mut commands: Commands, mut q_scenes: Query<(&mut VelloScene, &mut Style)>, mut scene: ResMut>, + func: Res, ) { let scene = scene.bypass_change_detection(); @@ -166,7 +167,7 @@ fn render_velyst_scene( coordinate_space: CoordinateSpace::ScreenSpace, ..default() }) - .insert(NodeBundle::default()) + .insert((NodeBundle::default(), func.render_layers())) .id(), ); } @@ -175,7 +176,7 @@ fn render_velyst_scene( /// Construct the interaction tree using bevy ui nodes. fn construct_interaction_tree( mut commands: Commands, - mut q_nodes: Query<(&mut Style, &mut Transform, &mut ZIndex)>, + mut q_nodes: Query<(&mut Style, &mut Transform, &mut ZIndex, &mut Visibility)>, mut scene: ResMut>, ) { let scene = scene.bypass_change_detection(); @@ -214,7 +215,8 @@ fn construct_interaction_tree( let height = Val::Px(group.size().y as f32); let scale = Vec3::new(coeffs[0] as f32, coeffs[3] as f32, 0.0); - if let Some((mut style, mut transform, mut z_index)) = scene + // Reuse cached nodes when available, otherwise, spawn a new one. + if let Some((mut style, mut transform, mut z_index, mut viz)) = scene .cached_entities .get_mut(&label) .and_then(|entities| entities.iter_mut().find(|(_, used)| *used == false)) @@ -232,6 +234,8 @@ fn construct_interaction_tree( transform.scale = scale; // ZIndex *z_index = ZIndex::Local(i as i32); + // Visibility + *viz = Visibility::Inherited; } else { let new_entity = commands .spawn(( @@ -266,6 +270,18 @@ fn construct_interaction_tree( } } } + + // Hide unused cached nodes + for entities in scene.cached_entities.values() { + for (entity, used) in entities { + match *used { + true => continue, + false => { + commands.entity(*entity).insert(Visibility::Hidden); + } + } + } + } } #[derive(Resource, Deref, DerefMut)] @@ -359,6 +375,11 @@ pub struct TypstLabel(TypLabel); pub trait TypstFunc: Resource { fn func_name(&self) -> &str; + // TODO: Create macro to automatically generate the render layers function. + fn render_layers(&self) -> RenderLayers { + RenderLayers::layer(0) + } + // TODO: Create macro to automatically generate the content function. fn content(&self, func: foundations::Func) -> Content;