Path: blob/main/examples/stress_tests/many_cameras_lights.rs
6592 views
//! Test rendering of many cameras and lights12use std::f32::consts::PI;34use bevy::{5camera::Viewport,6math::ops::{cos, sin},7prelude::*,8window::{PresentMode, WindowResolution},9};1011fn main() {12App::new()13.add_plugins(DefaultPlugins.set(WindowPlugin {14primary_window: Some(Window {15present_mode: PresentMode::AutoNoVsync,16resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),17..default()18}),19..default()20}))21.add_systems(Startup, setup)22.add_systems(Update, rotate_cameras)23.run();24}2526const CAMERA_ROWS: usize = 4;27const CAMERA_COLS: usize = 4;28const NUM_LIGHTS: usize = 5;2930/// set up a simple 3D scene31fn setup(32mut commands: Commands,33mut meshes: ResMut<Assets<Mesh>>,34mut materials: ResMut<Assets<StandardMaterial>>,35window: Query<&Window>,36) -> Result {37// circular base38commands.spawn((39Mesh3d(meshes.add(Circle::new(4.0))),40MeshMaterial3d(materials.add(Color::WHITE)),41Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),42));4344// cube45commands.spawn((46Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),47MeshMaterial3d(materials.add(Color::WHITE)),48Transform::from_xyz(0.0, 0.5, 0.0),49));5051// lights52for i in 0..NUM_LIGHTS {53let angle = (i as f32) / (NUM_LIGHTS as f32) * PI * 2.0;54commands.spawn((55PointLight {56color: Color::hsv(angle.to_degrees(), 1.0, 1.0),57intensity: 2_000_000.0 / NUM_LIGHTS as f32,58shadows_enabled: true,59..default()60},61Transform::from_xyz(sin(angle) * 4.0, 2.0, cos(angle) * 4.0),62));63}6465// cameras66let window = window.single()?;67let width = window.resolution.width() / CAMERA_COLS as f32 * window.resolution.scale_factor();68let height = window.resolution.height() / CAMERA_ROWS as f32 * window.resolution.scale_factor();69let mut i = 0;70for y in 0..CAMERA_COLS {71for x in 0..CAMERA_ROWS {72let angle = i as f32 / (CAMERA_ROWS * CAMERA_COLS) as f32 * PI * 2.0;73commands.spawn((74Camera3d::default(),75Camera {76viewport: Some(Viewport {77physical_position: UVec2::new(78(x as f32 * width) as u32,79(y as f32 * height) as u32,80),81physical_size: UVec2::new(width as u32, height as u32),82..default()83}),84order: i,85..default()86},87Transform::from_xyz(sin(angle) * 4.0, 2.5, cos(angle) * 4.0)88.looking_at(Vec3::ZERO, Vec3::Y),89));90i += 1;91}92}93Ok(())94}9596fn rotate_cameras(time: Res<Time>, mut query: Query<&mut Transform, With<Camera>>) {97for mut transform in query.iter_mut() {98transform.rotate_around(Vec3::ZERO, Quat::from_rotation_y(time.delta_secs()));99}100}101102103