Iter added camera to update their projection (#488)
This commit is contained in:
		
							parent
							
								
									1bdb9d3b00
								
							
						
					
					
						commit
						219527ed7d
					
				@ -1,6 +1,6 @@
 | 
				
			|||||||
use super::CameraProjection;
 | 
					use super::CameraProjection;
 | 
				
			||||||
use bevy_app::prelude::{EventReader, Events};
 | 
					use bevy_app::prelude::{EventReader, Events};
 | 
				
			||||||
use bevy_ecs::{Component, Local, Query, Res};
 | 
					use bevy_ecs::{Added, Component, Entity, Local, Query, Res};
 | 
				
			||||||
use bevy_math::Mat4;
 | 
					use bevy_math::Mat4;
 | 
				
			||||||
use bevy_property::Properties;
 | 
					use bevy_property::Properties;
 | 
				
			||||||
use bevy_window::{WindowCreated, WindowId, WindowResized, Windows};
 | 
					use bevy_window::{WindowCreated, WindowId, WindowResized, Windows};
 | 
				
			||||||
@ -38,7 +38,8 @@ pub fn camera_system<T: CameraProjection + Component>(
 | 
				
			|||||||
    window_resized_events: Res<Events<WindowResized>>,
 | 
					    window_resized_events: Res<Events<WindowResized>>,
 | 
				
			||||||
    window_created_events: Res<Events<WindowCreated>>,
 | 
					    window_created_events: Res<Events<WindowCreated>>,
 | 
				
			||||||
    windows: Res<Windows>,
 | 
					    windows: Res<Windows>,
 | 
				
			||||||
    mut query: Query<(&mut Camera, &mut T)>,
 | 
					    mut query: Query<(Entity, &mut Camera, &mut T)>,
 | 
				
			||||||
 | 
					    mut query_added: Query<(Entity, Added<Camera>)>,
 | 
				
			||||||
) {
 | 
					) {
 | 
				
			||||||
    let mut changed_window_ids = Vec::new();
 | 
					    let mut changed_window_ids = Vec::new();
 | 
				
			||||||
    // handle resize events. latest events are handled first because we only want to resize each window once
 | 
					    // handle resize events. latest events are handled first because we only want to resize each window once
 | 
				
			||||||
@ -67,9 +68,13 @@ pub fn camera_system<T: CameraProjection + Component>(
 | 
				
			|||||||
        changed_window_ids.push(event.id);
 | 
					        changed_window_ids.push(event.id);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (mut camera, mut camera_projection) in &mut query.iter() {
 | 
					    let mut added_cameras = vec![];
 | 
				
			||||||
 | 
					    for (entity, _camera) in &mut query_added.iter() {
 | 
				
			||||||
 | 
					        added_cameras.push(entity);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    for (entity, mut camera, mut camera_projection) in &mut query.iter() {
 | 
				
			||||||
        if let Some(window) = windows.get(camera.window) {
 | 
					        if let Some(window) = windows.get(camera.window) {
 | 
				
			||||||
            if changed_window_ids.contains(&window.id) {
 | 
					            if changed_window_ids.contains(&window.id) || added_cameras.contains(&entity) {
 | 
				
			||||||
                camera_projection.update(window.width as usize, window.height as usize);
 | 
					                camera_projection.update(window.width as usize, window.height as usize);
 | 
				
			||||||
                camera.projection_matrix = camera_projection.get_projection_matrix();
 | 
					                camera.projection_matrix = camera_projection.get_projection_matrix();
 | 
				
			||||||
                camera.depth_calculation = camera_projection.depth_calculation();
 | 
					                camera.depth_calculation = camera_projection.depth_calculation();
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user