//! Demonstrates the ability to manually override which instance of PanOrbitCamera receives input //! events, which is necessary when rendering to a texture/image instead of a window/viewport. //! //! In this example, input controls the camera that is rendering the texture applied to the cube, //! rather than the main window camera. //! //! This example is based off Bevy's render_to_texture example. use std::f32::consts::PI; use bevy::window::PrimaryWindow; use bevy::{ prelude::*, render::{ camera::RenderTarget, render_resource::{ Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, }, view::RenderLayers, }, }; use bevy_panorbit_camera::{ActiveCameraData, PanOrbitCamera, PanOrbitCameraPlugin}; fn main() { App::new() .add_plugins(DefaultPlugins) .add_plugins(PanOrbitCameraPlugin) .add_systems(Startup, setup) .add_systems(Update, cube_rotator_system) .run(); } // 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 materials: ResMut>, mut images: ResMut>, mut active_cam: ResMut, windows: Query<&Window, With>, ) { let size = Extent3d { width: 512, height: 512, ..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, view_formats: &[], }, ..default() }; // fill image.data with zeroes image.resize(size); let image_handle = images.add(image); let cube_handle = meshes.add(Cuboid::new(4.0, 4.0, 4.0)); let cube_material_handle = materials.add(StandardMaterial { base_color: Color::srgb(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(( PbrBundle { mesh: cube_handle, material: cube_material_handle, transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)), ..default() }, FirstPassCube, first_pass_layer.clone(), )); // Light // NOTE: Currently lights are shared between passes - see https://github.com/bevyengine/bevy/issues/3462 commands.spawn(PointLightBundle { transform: Transform::from_translation(Vec3::new(0.0, 0.0, 10.0)), ..default() }); // The camera for the first pass cube that will be rendered to the texture. This is the camera // that is controlled by PanOrbitCamera. let pan_orbit_id = commands .spawn(( Camera3dBundle { camera: Camera { // render before the "main pass" camera clear_color: ClearColorConfig::Custom(Color::WHITE), order: -1, target: RenderTarget::Image(image_handle.clone()), ..default() }, transform: Transform::from_translation(Vec3::new(0.0, 0.0, 15.0)) .looking_at(Vec3::ZERO, Vec3::Y), ..default() }, PanOrbitCamera::default(), first_pass_layer, )) .id(); let cube_size = 4.0; let cube_handle = meshes.add(Cuboid::new(cube_size, cube_size, cube_size)); // This material has the texture that has been rendered. let material_handle = materials.add(StandardMaterial { base_color_texture: Some(image_handle), reflectance: 0.02, unlit: false, ..default() }); // Main pass cube, with material containing the rendered first pass texture. commands.spawn(( PbrBundle { mesh: cube_handle, material: material_handle, transform: Transform::from_xyz(0.0, 0.0, 1.5) .with_rotation(Quat::from_rotation_x(-PI / 5.0)), ..default() }, MainPassCube, )); // The main pass camera. commands.spawn(Camera3dBundle { transform: Transform::from_xyz(0.0, 0.0, 15.0).looking_at(Vec3::ZERO, Vec3::Y), ..default() }); // Set up manual override of PanOrbitCamera. Note that this must run after PanOrbitCameraPlugin // is added, otherwise ActiveCameraData will be overwritten. // Note: you probably want to update the `viewport_size` and `window_size` whenever they change, // I haven't done this here for simplicity. let primary_window = windows .get_single() .expect("There is only ever one primary window"); active_cam.set_if_neq(ActiveCameraData { // Set the entity to the entity ID of the camera you want to control. In this case, it's // the inner (first pass) cube that is rendered to the texture/image. entity: Some(pan_orbit_id), // What you set these values to will depend on your use case, but generally you want the // viewport size to match the size of the render target (image, viewport), and the window // size to match the size of the window that you are interacting with. viewport_size: Some(Vec2::new(size.width as f32, size.height as f32)), window_size: Some(Vec2::new(primary_window.width(), primary_window.height())), // Setting manual to true ensures PanOrbitCameraPlugin will not overwrite this resource manual: true, }); } /// Rotates the outer cube (main pass) fn cube_rotator_system(time: Res