Skip to content

Commit

Permalink
Render Layers (#15)
Browse files Browse the repository at this point in the history
* feat: add render layers support

* fix: hide cached NodeBundle when not in used

* chore: add comments
  • Loading branch information
nixon-voxell authored Sep 17, 2024
1 parent 8a4a073 commit 3d44f16
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/renderer.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -149,6 +149,7 @@ fn render_velyst_scene<F: TypstFunc>(
mut commands: Commands,
mut q_scenes: Query<(&mut VelloScene, &mut Style)>,
mut scene: ResMut<VelystScene<F>>,
func: Res<F>,
) {
let scene = scene.bypass_change_detection();

Expand All @@ -166,7 +167,7 @@ fn render_velyst_scene<F: TypstFunc>(
coordinate_space: CoordinateSpace::ScreenSpace,
..default()
})
.insert(NodeBundle::default())
.insert((NodeBundle::default(), func.render_layers()))
.id(),
);
}
Expand All @@ -175,7 +176,7 @@ fn render_velyst_scene<F: TypstFunc>(
/// Construct the interaction tree using bevy ui nodes.
fn construct_interaction_tree<F: TypstFunc>(
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<VelystScene<F>>,
) {
let scene = scene.bypass_change_detection();
Expand Down Expand Up @@ -214,7 +215,8 @@ fn construct_interaction_tree<F: TypstFunc>(
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))
Expand All @@ -232,6 +234,8 @@ fn construct_interaction_tree<F: TypstFunc>(
transform.scale = scale;
// ZIndex
*z_index = ZIndex::Local(i as i32);
// Visibility
*viz = Visibility::Inherited;
} else {
let new_entity = commands
.spawn((
Expand Down Expand Up @@ -266,6 +270,18 @@ fn construct_interaction_tree<F: TypstFunc>(
}
}
}

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

Expand Down

0 comments on commit 3d44f16

Please sign in to comment.