From 7a596f1910c97c41f195b12d33025999e81dc0e5 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 18 May 2022 20:55:26 +0200 Subject: [PATCH 01/14] add a post-processing example --- Cargo.toml | 4 + .../custom_material_chromatic_aberration.wgsl | 31 ++ examples/shader/post_processing.rs | 340 ++++++++++++++++++ 3 files changed, 375 insertions(+) create mode 100644 assets/shaders/custom_material_chromatic_aberration.wgsl create mode 100644 examples/shader/post_processing.rs diff --git a/Cargo.toml b/Cargo.toml index 4dfe5fc8c7119..a0ba771371183 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -172,6 +172,10 @@ path = "examples/2d/texture_atlas.rs" name = "3d_scene" path = "examples/3d/3d_scene.rs" +[[example]] +name = "post_processing" +path = "examples/shader/post_processing.rs" + [[example]] name = "lighting" path = "examples/3d/lighting.rs" diff --git a/assets/shaders/custom_material_chromatic_aberration.wgsl b/assets/shaders/custom_material_chromatic_aberration.wgsl new file mode 100644 index 0000000000000..e7e8a0c5028cf --- /dev/null +++ b/assets/shaders/custom_material_chromatic_aberration.wgsl @@ -0,0 +1,31 @@ +#import bevy_pbr::mesh_view_bind_group + +struct VertexOutput { + [[builtin(position)]] clip_position: vec4; +}; + +struct CustomMaterial { + mouse_position: f32; +}; + +[[group(1), binding(0)]] +var texture: texture_2d; + +[[group(1), binding(1)]] +var our_sampler: sampler; + + +[[stage(fragment)]] +fn fragment(input: VertexOutput) -> [[location(0)]] vec4 { + let uv = input.clip_position.xy / vec2(view.width, view.height); + var output_color = vec4( + textureSample(texture, our_sampler, uv + vec2(0.01, -0.01)).r, + textureSample(texture, our_sampler, uv + vec2(-0.02, 0.0)).g, + textureSample(texture, our_sampler, uv + vec2(-0.01, 0.03)).b, + 1.0 + ); + + //var output_color = textureSample(texture, our_sampler, uv.xy); + + return output_color; +} \ No newline at end of file diff --git a/examples/shader/post_processing.rs b/examples/shader/post_processing.rs new file mode 100644 index 0000000000000..46085899fc030 --- /dev/null +++ b/examples/shader/post_processing.rs @@ -0,0 +1,340 @@ +//! Shows how to render to a texture. Useful for mirrors, UI, or exporting images. + +use bevy::{ + core_pipeline::{ + draw_3d_graph, node, AlphaMask3d, Opaque3d, RenderTargetClearColors, Transparent3d, + }, + ecs::system::{lifetimeless::SRes, SystemParamItem}, + prelude::*, + reflect::TypeUuid, + render::{ + camera::{ActiveCamera, Camera, CameraTypePlugin, RenderTarget}, + render_asset::{PrepareAssetError, RenderAsset, RenderAssets}, + render_graph::{Node, NodeRunError, RenderGraph, RenderGraphContext, SlotValue}, + render_phase::RenderPhase, + render_resource::{ + BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, + BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingResource, BindingType, + Extent3d, SamplerBindingType, ShaderStages, TextureDescriptor, TextureDimension, + TextureFormat, TextureSampleType, TextureUsages, TextureViewDimension, + }, + renderer::{RenderContext, RenderDevice}, + view::RenderLayers, + RenderApp, RenderStage, + }, + sprite::{Material2d, Material2dPipeline, Material2dPlugin, MaterialMesh2dBundle}, +}; + +#[derive(Component, Default)] +pub struct FirstPassCamera; + +// The name of the final node of the first pass. +pub const FIRST_PASS_DRIVER: &str = "first_pass_driver"; + +fn main() { + let mut app = App::new(); + app.insert_resource(Msaa { samples: 4 }) // Use 4x MSAA + .add_plugins(DefaultPlugins) + .add_plugin(Material2dPlugin::::default()) + .add_plugin(CameraTypePlugin::::default()) + .add_startup_system(setup) + .add_system(first_pass_cube_rotator_system); + + let render_app = app.sub_app_mut(RenderApp); + let driver = FirstPassCameraDriver::new(&mut render_app.world); + // This will add 3D render phases for the new camera. + render_app.add_system_to_stage(RenderStage::Extract, extract_first_pass_camera_phases); + + let mut graph = render_app.world.resource_mut::(); + + // Add a node for the first pass. + graph.add_node(FIRST_PASS_DRIVER, driver); + + // The first pass's dependencies include those of the main pass. + graph + .add_node_edge(node::MAIN_PASS_DEPENDENCIES, FIRST_PASS_DRIVER) + .unwrap(); + + // Insert the first pass node: CLEAR_PASS_DRIVER -> FIRST_PASS_DRIVER -> MAIN_PASS_DRIVER + graph + .add_node_edge(node::CLEAR_PASS_DRIVER, FIRST_PASS_DRIVER) + .unwrap(); + graph + .add_node_edge(FIRST_PASS_DRIVER, node::MAIN_PASS_DRIVER) + .unwrap(); + app.run(); +} + +// Add 3D render phases for FIRST_PASS_CAMERA. +fn extract_first_pass_camera_phases( + mut commands: Commands, + active: Res>, +) { + if let Some(entity) = active.get() { + commands.get_or_spawn(entity).insert_bundle(( + RenderPhase::::default(), + RenderPhase::::default(), + RenderPhase::::default(), + )); + } +} + +// A node for the first pass camera that runs draw_3d_graph with this camera. +struct FirstPassCameraDriver { + query: QueryState>, +} + +impl FirstPassCameraDriver { + pub fn new(render_world: &mut World) -> Self { + Self { + query: QueryState::new(render_world), + } + } +} +impl Node for FirstPassCameraDriver { + fn update(&mut self, world: &mut World) { + self.query.update_archetypes(world); + } + + fn run( + &self, + graph: &mut RenderGraphContext, + _render_context: &mut RenderContext, + world: &World, + ) -> Result<(), NodeRunError> { + for camera in self.query.iter_manual(world) { + graph.run_sub_graph(draw_3d_graph::NAME, vec![SlotValue::Entity(camera)])?; + } + Ok(()) + } +} + +// Marks the first pass cube (rendered to a texture.) +#[derive(Component)] +struct FirstPassCube; + +// Marks the main pass cube, to which the texture is applied. +#[derive(Component)] +struct MainPassCube; + +fn setup( + mut commands: Commands, + mut meshes: ResMut>, + mut post_processing_materials: ResMut>, + mut materials: ResMut>, + mut images: ResMut>, + mut clear_colors: ResMut, +) { + let size = Extent3d { + width: 1280, + height: 720, + ..default() + }; + + // This is the texture that will be rendered to. + let mut image = Image { + texture_descriptor: TextureDescriptor { + label: None, + size, + dimension: TextureDimension::D2, + format: TextureFormat::Bgra8UnormSrgb, + mip_level_count: 1, + sample_count: 1, + usage: TextureUsages::TEXTURE_BINDING + | TextureUsages::COPY_DST + | TextureUsages::RENDER_ATTACHMENT, + }, + ..default() + }; + + // fill image.data with zeroes + image.resize(size); + + let image_handle = images.add(image); + + let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 4.0 })); + let cube_material_handle = materials.add(StandardMaterial { + base_color: Color::rgb(0.8, 0.7, 0.6), + reflectance: 0.02, + unlit: false, + ..default() + }); + + // This specifies the layer used for the first pass, which will be attached to the first pass camera and cube. + let first_pass_layer = RenderLayers::layer(1); + + // The cube that will be rendered to the texture. + commands + .spawn_bundle(PbrBundle { + mesh: cube_handle, + material: cube_material_handle, + transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)), + ..default() + }) + .insert(FirstPassCube) + .insert(first_pass_layer); + + // Light + // NOTE: Currently lights are shared between passes - see https://github.com/bevyengine/bevy/issues/3462 + commands.spawn_bundle(PointLightBundle { + transform: Transform::from_translation(Vec3::new(0.0, 0.0, 10.0)), + ..default() + }); + + // First pass camera + let render_target = RenderTarget::Image(image_handle.clone()); + clear_colors.insert(render_target.clone(), Color::WHITE); + commands + .spawn_bundle(PerspectiveCameraBundle:: { + camera: Camera { + target: render_target, + ..default() + }, + transform: Transform::from_translation(Vec3::new(0.0, 0.0, 15.0)) + .looking_at(Vec3::default(), Vec3::Y), + ..PerspectiveCameraBundle::new() + }) + .insert(first_pass_layer); + // NOTE: omitting the RenderLayers component for this camera may cause a validation error: + // + // thread 'main' panicked at 'wgpu error: Validation Error + // + // Caused by: + // In a RenderPass + // note: encoder = `` + // In a pass parameter + // note: command buffer = `` + // Attempted to use texture (5, 1, Metal) mips 0..1 layers 0..1 as a combination of COLOR_TARGET within a usage scope. + // + // This happens because the texture would be written and read in the same frame, which is not allowed. + // So either render layers must be used to avoid this, or the texture must be double buffered. + + let quad_size = 400.0; + let quad_handle = meshes.add(Mesh::from(shape::Quad::new(Vec2::splat(quad_size)))); + + // This material has the texture that has been rendered. + let material_handle = post_processing_materials.add(PostProcessingMaterial { + source_image: image_handle, + }); + + // Main pass 2d quad, with material containing the rendered first pass texture, with a custom shader. + commands.spawn_bundle(MaterialMesh2dBundle { + mesh: quad_handle.into(), + material: material_handle, + transform: Transform { + translation: Vec3::new(0.0, 0.0, 1.5), + rotation: Quat::from_rotation_x(-std::f32::consts::PI / 5.0), + ..default() + }, + ..default() + }); + + // The main pass camera. + let mut camera = OrthographicCameraBundle::new_2d(); + camera.orthographic_projection.scale = 0.10f32; + camera.transform.translation = Vec3::new(0f32, 100f32, -1f32); + commands.spawn_bundle(camera); +} + +/// Rotates the inner cube (first pass) +fn first_pass_cube_rotator_system( + time: Res