@@ -28,6 +28,7 @@ use bevy_render::{
2828 view:: {
2929 ExtractedView , ViewUniform , ViewUniformOffset , ViewUniforms , Visibility , VisibleEntities ,
3030 } ,
31+ Extract ,
3132} ;
3233use bevy_transform:: components:: GlobalTransform ;
3334use bevy_utils:: {
@@ -331,8 +332,11 @@ pub struct ExtractedClustersPointLights {
331332 data : Vec < VisiblePointLights > ,
332333}
333334
334- pub fn extract_clusters ( mut commands : Commands , views : Query < ( Entity , & Clusters ) , With < Camera > > ) {
335- for ( entity, clusters) in views. iter ( ) {
335+ pub fn extract_clusters (
336+ mut commands : Commands ,
337+ mut views : Extract < Query < ( Entity , & Clusters ) , With < Camera > > > ,
338+ ) {
339+ for ( entity, clusters) in views. value ( ) . iter ( ) {
336340 commands. get_or_spawn ( entity) . insert_bundle ( (
337341 ExtractedClustersPointLights {
338342 data : clusters. lights . clone ( ) ,
@@ -348,20 +352,25 @@ pub fn extract_clusters(mut commands: Commands, views: Query<(Entity, &Clusters)
348352
349353pub fn extract_lights (
350354 mut commands : Commands ,
351- ambient_light : Res < AmbientLight > ,
352- point_light_shadow_map : Res < PointLightShadowMap > ,
353- directional_light_shadow_map : Res < DirectionalLightShadowMap > ,
354- global_point_lights : Res < GlobalVisiblePointLights > ,
355+ mut ambient_light : Extract < Res < AmbientLight > > ,
356+ mut point_light_shadow_map : Extract < Res < PointLightShadowMap > > ,
357+ mut directional_light_shadow_map : Extract < Res < DirectionalLightShadowMap > > ,
358+ mut global_point_lights : Extract < Res < GlobalVisiblePointLights > > ,
355359 // visible_point_lights: Query<&VisiblePointLights>,
356- mut point_lights : Query < ( & PointLight , & mut CubemapVisibleEntities , & GlobalTransform ) > ,
357- mut directional_lights : Query < (
358- Entity ,
359- & DirectionalLight ,
360- & mut VisibleEntities ,
361- & GlobalTransform ,
362- & Visibility ,
363- ) > ,
360+ mut point_lights : Extract < Query < ( & PointLight , & CubemapVisibleEntities , & GlobalTransform ) > > ,
361+ mut directional_lights : Extract <
362+ Query < (
363+ Entity ,
364+ & DirectionalLight ,
365+ & VisibleEntities ,
366+ & GlobalTransform ,
367+ & Visibility ,
368+ ) > ,
369+ > ,
364370) {
371+ let ambient_light = ambient_light. value ( ) ;
372+ let point_light_shadow_map = point_light_shadow_map. value ( ) ;
373+ let directional_light_shadow_map = directional_light_shadow_map. value ( ) ;
365374 commands. insert_resource ( ExtractedAmbientLight {
366375 color : ambient_light. color ,
367376 brightness : ambient_light. brightness ,
@@ -379,11 +388,15 @@ pub fn extract_lights(
379388 // https://catlikecoding.com/unity/tutorials/custom-srp/point-and-spot-shadows/
380389 let point_light_texel_size = 2.0 / point_light_shadow_map. size as f32 ;
381390
382- for entity in global_point_lights. iter ( ) . copied ( ) {
383- if let Ok ( ( point_light, cubemap_visible_entities, transform) ) = point_lights. get_mut ( entity)
384- {
385- let render_cubemap_visible_entities =
386- std:: mem:: take ( cubemap_visible_entities. into_inner ( ) ) ;
391+ let point_lights = point_lights. value ( ) ;
392+ for entity in global_point_lights. value ( ) . iter ( ) . copied ( ) {
393+ if let Ok ( ( point_light, cubemap_visible_entities, transform) ) = point_lights. get ( entity) {
394+ // PERF: Ideally, this would avoid creating new allocations each frame.
395+ // The nicest way to do that would be to get last frame's render_cubemap_visible_entities,
396+ // and clone into that buffer.
397+ // TODO: Evaluate whether moving the buffers into a resource at the end of frame
398+ // is more performant.
399+ let render_cubemap_visible_entities = cubemap_visible_entities. clone ( ) ;
387400 commands. get_or_spawn ( entity) . insert_bundle ( (
388401 ExtractedPointLight {
389402 color : point_light. color ,
@@ -407,7 +420,7 @@ pub fn extract_lights(
407420 }
408421
409422 for ( entity, directional_light, visible_entities, transform, visibility) in
410- directional_lights. iter_mut ( )
423+ directional_lights. value ( ) . iter ( )
411424 {
412425 if !visibility. is_visible {
413426 continue ;
@@ -425,7 +438,8 @@ pub fn extract_lights(
425438 ) ;
426439 let directional_light_texel_size =
427440 largest_dimension / directional_light_shadow_map. size as f32 ;
428- let render_visible_entities = std:: mem:: take ( visible_entities. into_inner ( ) ) ;
441+ // See above
442+ let render_visible_entities = visible_entities. clone ( ) ;
429443 commands. get_or_spawn ( entity) . insert_bundle ( (
430444 ExtractedDirectionalLight {
431445 color : directional_light. color ,
0 commit comments