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

Render Layers #15

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Changes from all 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
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