Path: blob/main/examples/stress_tests/many_cameras_lights.rs
6849 views
//! Test rendering of many cameras and lights12use std::f32::consts::PI;34use bevy::{5camera::Viewport,6math::ops::{cos, sin},7prelude::*,8window::{PresentMode, WindowResolution},9winit::WinitSettings,10};1112fn main() {13App::new()14.add_plugins(DefaultPlugins.set(WindowPlugin {15primary_window: Some(Window {16present_mode: PresentMode::AutoNoVsync,17resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),18..default()19}),20..default()21}))22.insert_resource(WinitSettings::continuous())23.add_systems(Startup, setup)24.add_systems(Update, rotate_cameras)25.run();26}2728const CAMERA_ROWS: usize = 4;29const CAMERA_COLS: usize = 4;30const NUM_LIGHTS: usize = 5;3132/// set up a simple 3D scene33fn setup(34mut commands: Commands,35mut meshes: ResMut<Assets<Mesh>>,36mut materials: ResMut<Assets<StandardMaterial>>,37window: Query<&Window>,38) -> Result {39// circular base40commands.spawn((41Mesh3d(meshes.add(Circle::new(4.0))),42MeshMaterial3d(materials.add(Color::WHITE)),43Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),44));4546// cube47commands.spawn((48Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),49MeshMaterial3d(materials.add(Color::WHITE)),50Transform::from_xyz(0.0, 0.5, 0.0),51));5253// lights54for i in 0..NUM_LIGHTS {55let angle = (i as f32) / (NUM_LIGHTS as f32) * PI * 2.0;56commands.spawn((57PointLight {58color: Color::hsv(angle.to_degrees(), 1.0, 1.0),59intensity: 2_000_000.0 / NUM_LIGHTS as f32,60shadows_enabled: true,61..default()62},63Transform::from_xyz(sin(angle) * 4.0, 2.0, cos(angle) * 4.0),64));65}6667// cameras68let window = window.single()?;69let width = window.resolution.width() / CAMERA_COLS as f32 * window.resolution.scale_factor();70let height = window.resolution.height() / CAMERA_ROWS as f32 * window.resolution.scale_factor();71let mut i = 0;72for y in 0..CAMERA_COLS {73for x in 0..CAMERA_ROWS {74let angle = i as f32 / (CAMERA_ROWS * CAMERA_COLS) as f32 * PI * 2.0;75commands.spawn((76Camera3d::default(),77Camera {78viewport: Some(Viewport {79physical_position: UVec2::new(80(x as f32 * width) as u32,81(y as f32 * height) as u32,82),83physical_size: UVec2::new(width as u32, height as u32),84..default()85}),86order: i,87..default()88},89Transform::from_xyz(sin(angle) * 4.0, 2.5, cos(angle) * 4.0)90.looking_at(Vec3::ZERO, Vec3::Y),91));92i += 1;93}94}95Ok(())96}9798fn rotate_cameras(time: Res<Time>, mut query: Query<&mut Transform, With<Camera>>) {99for mut transform in query.iter_mut() {100transform.rotate_around(Vec3::ZERO, Quat::from_rotation_y(time.delta_secs()));101}102}103104105