Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
IceSentry committed Jan 21, 2023
1 parent c22f051 commit 3fc441e
Showing 1 changed file with 55 additions and 12 deletions.
67 changes: 55 additions & 12 deletions examples/shader/post_process_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ use bevy::{
clear_color::ClearColorConfig, core_3d,
fullscreen_vertex_shader::fullscreen_shader_vertex_state,
},
ecs::query::QueryItem,
prelude::*,
render::{
extract_component::{ExtractComponent, ExtractComponentPlugin},
render_graph::{Node, NodeRunError, RenderGraph, RenderGraphContext, SlotInfo, SlotType},
render_resource::{
BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutDescriptor,
BindGroupLayoutEntry, BindingResource, BindingType, CachedRenderPipelineId,
ColorTargetState, ColorWrites, FragmentState, MultisampleState, Operations,
PipelineCache, PrimitiveState, RenderPassColorAttachment, RenderPassDescriptor,
RenderPipelineDescriptor, Sampler, SamplerBindingType, SamplerDescriptor, ShaderStages,
TextureFormat, TextureSampleType, TextureViewDimension,
ShaderType, TextureFormat, TextureSampleType, TextureViewDimension,
},
renderer::{RenderContext, RenderDevice},
texture::BevyDefault,
view::{ExtractedView, ViewTarget},
RenderApp,
Extract, RenderApp, RenderStage,
},
};

Expand All @@ -45,13 +47,17 @@ fn main() {
struct PostProcessPlugin;
impl Plugin for PostProcessPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(ExtractComponentPlugin::<PostProcessSettings>::default());

let Ok(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};

// The renderer has multiple stages. For more details see the docs for RenderStage
// The renderer has multiple stages. For more details of each stage see the docs for RenderStage
// Extract -> Prepare -> Queue -> PhaseSort -> Render -> CleanUp
render_app.init_resource::<PostProcessPipeline>();
render_app
.init_resource::<PostProcessPipeline>()
.add_system_to_stage(RenderStage::Extract, extract_post_process_settings);

// Create our node with the render world
let node = PostProcessNode::new(&mut render_app.world);
Expand Down Expand Up @@ -88,7 +94,7 @@ impl Plugin for PostProcessPlugin {
/// The post process node used for the render graph
struct PostProcessNode {
// The node needs a query to know how to render,
// but it's not a normal system so we need to define it here.
// but it's not a normal system so we need to define it manually.
query: QueryState<&'static ViewTarget, With<ExtractedView>>,
}

Expand Down Expand Up @@ -275,6 +281,40 @@ impl FromWorld for PostProcessPipeline {
}
}

#[derive(Component, Default, Clone)]
struct PostProcessSettings {
intensity: f32,
}

// TODO explain extract stage
fn extract_post_process_settings(
mut commands: Commands,
cameras_2d: Extract<Query<(Entity, &Camera, &PostProcessSettings), With<Camera3d>>>,
) {
for (entity, camera, settings) in &cameras_2d {
if camera.is_active {
commands.get_or_spawn(entity).insert(PostProcessUniform {
intensity: settings.intensity,
});
}
}
}

impl ExtractComponent for PostProcessSettings {
type Query = &'static Self;
type Filter = ();
type Out = Self;

fn extract_component(item: QueryItem<'_, Self::Query>) -> Option<Self> {
Some(item.clone())
}
}

#[derive(Component, ShaderType, Clone)]
struct PostProcessUniform {
intensity: f32,
}

/// set up a simple 3D scene
fn setup(
mut commands: Commands,
Expand All @@ -297,15 +337,18 @@ fn setup(
..default()
});
// camera
commands.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 5.0))
.looking_at(Vec3::default(), Vec3::Y),
camera_3d: Camera3d {
clear_color: ClearColorConfig::Custom(Color::WHITE),
commands.spawn((
Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 5.0))
.looking_at(Vec3::default(), Vec3::Y),
camera_3d: Camera3d {
clear_color: ClearColorConfig::Custom(Color::WHITE),
..default()
},
..default()
},
..default()
});
PostProcessSettings { intensity: 0.02 },
));
}

#[derive(Component)]
Expand Down

0 comments on commit 3fc441e

Please sign in to comment.