|
| 1 | +use bevy::{ |
| 2 | + core_pipeline::{draw_3d_graph, node, AlphaMask3d, Opaque3d, Transparent3d}, |
| 3 | + prelude::*, |
| 4 | + render::{ |
| 5 | + camera::{ActiveCamera, Camera, CameraTypePlugin, RenderTarget}, |
| 6 | + render_graph::{Node, NodeRunError, RenderGraph, RenderGraphContext, SlotValue}, |
| 7 | + render_phase::RenderPhase, |
| 8 | + renderer::RenderContext, |
| 9 | + view::RenderLayers, |
| 10 | + RenderApp, RenderStage, |
| 11 | + }, |
| 12 | + window::WindowId, |
| 13 | +}; |
| 14 | + |
| 15 | +// The name of the final node of the first pass. |
| 16 | +pub const FIRST_PASS_DRIVER: &str = "first_pass_driver"; |
| 17 | + |
| 18 | +// Marks the camera that determines the view rendered in the first pass. |
| 19 | +#[derive(Component, Default)] |
| 20 | +struct FirstPassCamera; |
| 21 | + |
| 22 | +fn main() { |
| 23 | + let mut app = App::new(); |
| 24 | + app.insert_resource(Msaa { samples: 4 }) |
| 25 | + .add_plugins(DefaultPlugins) |
| 26 | + .add_plugin(CameraTypePlugin::<FirstPassCamera>::default()) |
| 27 | + .add_startup_system(setup) |
| 28 | + .add_system(cube_rotator_system) |
| 29 | + .add_system(rotator_system) |
| 30 | + .add_system(toggle_msaa); |
| 31 | + |
| 32 | + let render_app = app.sub_app_mut(RenderApp); |
| 33 | + let driver = FirstPassCameraDriver::new(&mut render_app.world); |
| 34 | + |
| 35 | + // This will add 3D render phases for the new camera. |
| 36 | + render_app.add_system_to_stage(RenderStage::Extract, extract_first_pass_camera_phases); |
| 37 | + |
| 38 | + let mut graph = render_app.world.get_resource_mut::<RenderGraph>().unwrap(); |
| 39 | + |
| 40 | + // Add a node for the first pass. |
| 41 | + graph.add_node(FIRST_PASS_DRIVER, driver); |
| 42 | + |
| 43 | + // The first pass's dependencies include those of the main pass. |
| 44 | + graph |
| 45 | + .add_node_edge(node::MAIN_PASS_DEPENDENCIES, FIRST_PASS_DRIVER) |
| 46 | + .unwrap(); |
| 47 | + |
| 48 | + // Insert the first pass node: CLEAR_PASS_DRIVER -> FIRST_PASS_DRIVER -> MAIN_PASS_DRIVER |
| 49 | + graph |
| 50 | + .add_node_edge(node::CLEAR_PASS_DRIVER, FIRST_PASS_DRIVER) |
| 51 | + .unwrap(); |
| 52 | + graph |
| 53 | + .add_node_edge(FIRST_PASS_DRIVER, node::MAIN_PASS_DRIVER) |
| 54 | + .unwrap(); |
| 55 | + app.run(); |
| 56 | +} |
| 57 | + |
| 58 | +// Add 3D render phases for FirstPassCamera. |
| 59 | +fn extract_first_pass_camera_phases( |
| 60 | + mut commands: Commands, |
| 61 | + active: Res<ActiveCamera<FirstPassCamera>>, |
| 62 | +) { |
| 63 | + if let Some(entity) = active.get() { |
| 64 | + commands.get_or_spawn(entity).insert_bundle(( |
| 65 | + RenderPhase::<Opaque3d>::default(), |
| 66 | + RenderPhase::<AlphaMask3d>::default(), |
| 67 | + RenderPhase::<Transparent3d>::default(), |
| 68 | + )); |
| 69 | + } |
| 70 | +} |
| 71 | +// A node for the first pass camera that runs draw_3d_graph with this camera. |
| 72 | +struct FirstPassCameraDriver { |
| 73 | + query: QueryState<Entity, With<FirstPassCamera>>, |
| 74 | +} |
| 75 | + |
| 76 | +impl FirstPassCameraDriver { |
| 77 | + pub fn new(render_world: &mut World) -> Self { |
| 78 | + Self { |
| 79 | + query: QueryState::new(render_world), |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl Node for FirstPassCameraDriver { |
| 85 | + fn update(&mut self, world: &mut World) { |
| 86 | + self.query.update_archetypes(world); |
| 87 | + } |
| 88 | + |
| 89 | + fn run( |
| 90 | + &self, |
| 91 | + graph: &mut RenderGraphContext, |
| 92 | + _render_context: &mut RenderContext, |
| 93 | + world: &World, |
| 94 | + ) -> Result<(), NodeRunError> { |
| 95 | + for camera in self.query.iter_manual(world) { |
| 96 | + graph.run_sub_graph(draw_3d_graph::NAME, vec![SlotValue::Entity(camera)])?; |
| 97 | + } |
| 98 | + Ok(()) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Marks the first pass cube. |
| 103 | +#[derive(Component)] |
| 104 | +struct FirstPassCube; |
| 105 | + |
| 106 | +// Marks the main pass cube. |
| 107 | +#[derive(Component)] |
| 108 | +struct MainPassCube; |
| 109 | + |
| 110 | +fn setup( |
| 111 | + mut commands: Commands, |
| 112 | + mut meshes: ResMut<Assets<Mesh>>, |
| 113 | + mut materials: ResMut<Assets<StandardMaterial>>, |
| 114 | +) { |
| 115 | + let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 4.0 })); |
| 116 | + let cube_material_handle = materials.add(StandardMaterial { |
| 117 | + base_color: Color::GREEN, |
| 118 | + reflectance: 0.02, |
| 119 | + unlit: false, |
| 120 | + ..Default::default() |
| 121 | + }); |
| 122 | + |
| 123 | + let split = 2.0; |
| 124 | + |
| 125 | + // This specifies the layer used for the first pass, which will be attached to the first pass camera and cube. |
| 126 | + let first_pass_layer = RenderLayers::layer(1); |
| 127 | + |
| 128 | + // The first pass cube. |
| 129 | + commands |
| 130 | + .spawn_bundle(PbrBundle { |
| 131 | + mesh: cube_handle, |
| 132 | + material: cube_material_handle, |
| 133 | + transform: Transform::from_translation(Vec3::new(-split, 0.0, 1.0)), |
| 134 | + ..Default::default() |
| 135 | + }) |
| 136 | + .insert(FirstPassCube) |
| 137 | + .insert(first_pass_layer); |
| 138 | + |
| 139 | + // Light |
| 140 | + // NOTE: Currently lights are shared between passes - see https://github.com/bevyengine/bevy/issues/3462 |
| 141 | + commands.spawn_bundle(PointLightBundle { |
| 142 | + transform: Transform::from_translation(Vec3::new(0.0, 0.0, 10.0)), |
| 143 | + ..Default::default() |
| 144 | + }); |
| 145 | + |
| 146 | + // First pass camera |
| 147 | + commands |
| 148 | + .spawn_bundle(PerspectiveCameraBundle::<FirstPassCamera> { |
| 149 | + camera: Camera { |
| 150 | + target: RenderTarget::Window(WindowId::primary()), |
| 151 | + ..Default::default() |
| 152 | + }, |
| 153 | + transform: Transform::from_translation(Vec3::new(0.0, 0.0, 15.0)) |
| 154 | + .looking_at(Vec3::default(), Vec3::Y), |
| 155 | + ..PerspectiveCameraBundle::new() |
| 156 | + }) |
| 157 | + .insert(first_pass_layer); |
| 158 | + |
| 159 | + let cube_size = 4.0; |
| 160 | + let cube_handle = meshes.add(Mesh::from(shape::Box::new(cube_size, cube_size, cube_size))); |
| 161 | + |
| 162 | + let material_handle = materials.add(StandardMaterial { |
| 163 | + base_color: Color::RED, |
| 164 | + reflectance: 0.02, |
| 165 | + unlit: false, |
| 166 | + ..Default::default() |
| 167 | + }); |
| 168 | + |
| 169 | + // Main pass cube. |
| 170 | + commands |
| 171 | + .spawn_bundle(PbrBundle { |
| 172 | + mesh: cube_handle, |
| 173 | + material: material_handle, |
| 174 | + transform: Transform { |
| 175 | + translation: Vec3::new(split, 0.0, -4.5), |
| 176 | + rotation: Quat::from_rotation_x(-std::f32::consts::PI / 5.0), |
| 177 | + ..Default::default() |
| 178 | + }, |
| 179 | + ..Default::default() |
| 180 | + }) |
| 181 | + .insert(MainPassCube); |
| 182 | + |
| 183 | + // The main pass camera. |
| 184 | + commands.spawn_bundle(PerspectiveCameraBundle { |
| 185 | + transform: Transform::from_translation(Vec3::new(0.0, 0.0, 15.0)) |
| 186 | + .looking_at(Vec3::default(), Vec3::Y), |
| 187 | + ..Default::default() |
| 188 | + }); |
| 189 | +} |
| 190 | + |
| 191 | +/// Rotates the inner cube (first pass) |
| 192 | +fn rotator_system(time: Res<Time>, mut query: Query<&mut Transform, With<FirstPassCube>>) { |
| 193 | + for mut transform in query.iter_mut() { |
| 194 | + transform.rotation *= Quat::from_rotation_x(1.5 * time.delta_seconds()); |
| 195 | + transform.rotation *= Quat::from_rotation_z(1.3 * time.delta_seconds()); |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +/// Rotates the outer cube (main pass) |
| 200 | +fn cube_rotator_system(time: Res<Time>, mut query: Query<&mut Transform, With<MainPassCube>>) { |
| 201 | + for mut transform in query.iter_mut() { |
| 202 | + transform.rotation *= Quat::from_rotation_x(1.0 * time.delta_seconds()); |
| 203 | + transform.rotation *= Quat::from_rotation_y(0.7 * time.delta_seconds()); |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +fn toggle_msaa(input: Res<Input<KeyCode>>, mut msaa: ResMut<Msaa>) { |
| 208 | + if input.just_pressed(KeyCode::M) { |
| 209 | + if msaa.samples == 4 { |
| 210 | + info!("Not using MSAA"); |
| 211 | + msaa.samples = 1; |
| 212 | + } else { |
| 213 | + info!("Using 4x MSAA"); |
| 214 | + msaa.samples = 4; |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments