@@ -7,8 +7,8 @@ use bevy_render::{
77 camera:: Camera ,
88 color:: Color ,
99 extract_resource:: ExtractResource ,
10- prelude:: Projection ,
11- primitives:: { Aabb , CascadesFrusta , CubemapFrusta , Frustum , HalfSpace , Sphere } ,
10+ prelude:: { Mesh , Projection } ,
11+ primitives:: { Aabb , AabbSource , CascadesFrusta , CubemapFrusta , Frustum , HalfSpace , Sphere } ,
1212 render_resource:: BufferBindingType ,
1313 renderer:: RenderDevice ,
1414 view:: { ComputedVisibility , RenderLayers , VisibleEntities } ,
@@ -22,6 +22,7 @@ use crate::{
2222 CLUSTERED_FORWARD_STORAGE_BUFFER_COUNT , CUBE_MAP_FACES , MAX_UNIFORM_BUFFER_POINT_LIGHTS ,
2323 POINT_LIGHT_NEAR_Z ,
2424} ;
25+ use bevy_asset:: { Assets , Handle } ;
2526
2627/// A light that emits light in all directions from a central point.
2728///
@@ -1911,6 +1912,7 @@ pub fn update_spot_light_frusta(
19111912}
19121913
19131914pub fn check_light_mesh_visibility (
1915+ meshes : Res < Assets < Mesh > > ,
19141916 visible_point_lights : Query < & VisiblePointLights > ,
19151917 mut point_lights : Query < (
19161918 & PointLight ,
@@ -1941,8 +1943,9 @@ pub fn check_light_mesh_visibility(
19411943 Entity ,
19421944 & mut ComputedVisibility ,
19431945 Option < & RenderLayers > ,
1944- Option < & Aabb > ,
1946+ Option < & AabbSource > ,
19451947 Option < & GlobalTransform > ,
1948+ Option < & Handle < Mesh > > ,
19461949 ) ,
19471950 ( Without < NotShadowCaster > , Without < DirectionalLight > ) ,
19481951 > ,
@@ -2002,8 +2005,14 @@ pub fn check_light_mesh_visibility(
20022005
20032006 let view_mask = maybe_view_mask. copied ( ) . unwrap_or_default ( ) ;
20042007
2005- for ( entity, mut computed_visibility, maybe_entity_mask, maybe_aabb, maybe_transform) in
2006- & mut visible_entity_query
2008+ for (
2009+ entity,
2010+ mut computed_visibility,
2011+ maybe_entity_mask,
2012+ maybe_aabb_source,
2013+ maybe_transform,
2014+ maybe_mesh,
2015+ ) in & mut visible_entity_query
20072016 {
20082017 if !computed_visibility. is_visible_in_hierarchy ( ) {
20092018 continue ;
@@ -2014,6 +2023,8 @@ pub fn check_light_mesh_visibility(
20142023 continue ;
20152024 }
20162025
2026+ let maybe_aabb = maybe_aabb_source. and_then ( |s| s. get ( maybe_mesh, & meshes) ) ;
2027+
20172028 // If we have an aabb and transform, do frustum culling
20182029 if let ( Some ( aabb) , Some ( transform) ) = ( maybe_aabb, maybe_transform) {
20192030 for ( view, view_frusta) in frusta. frusta . iter ( ) {
@@ -2026,7 +2037,8 @@ pub fn check_light_mesh_visibility(
20262037 view_frusta. iter ( ) . zip ( view_visible_entities)
20272038 {
20282039 // Disable near-plane culling, as a shadow caster could lie before the near plane.
2029- if !frustum. intersects_obb ( aabb, & transform. compute_matrix ( ) , false , true ) {
2040+ if !frustum. intersects_obb ( & aabb, & transform. compute_matrix ( ) , false , true )
2041+ {
20302042 continue ;
20312043 }
20322044
@@ -2084,8 +2096,9 @@ pub fn check_light_mesh_visibility(
20842096 entity,
20852097 mut computed_visibility,
20862098 maybe_entity_mask,
2087- maybe_aabb ,
2099+ maybe_aabb_source ,
20882100 maybe_transform,
2101+ maybe_mesh,
20892102 ) in & mut visible_entity_query
20902103 {
20912104 if !computed_visibility. is_visible_in_hierarchy ( ) {
@@ -2097,19 +2110,20 @@ pub fn check_light_mesh_visibility(
20972110 continue ;
20982111 }
20992112
2113+ let maybe_aabb = maybe_aabb_source. and_then ( |s| s. get ( maybe_mesh, & meshes) ) ;
21002114 // If we have an aabb and transform, do frustum culling
21012115 if let ( Some ( aabb) , Some ( transform) ) = ( maybe_aabb, maybe_transform) {
21022116 let model_to_world = transform. compute_matrix ( ) ;
21032117 // Do a cheap sphere vs obb test to prune out most meshes outside the sphere of the light
2104- if !light_sphere. intersects_obb ( aabb, & model_to_world) {
2118+ if !light_sphere. intersects_obb ( & aabb, & model_to_world) {
21052119 continue ;
21062120 }
21072121
21082122 for ( frustum, visible_entities) in cubemap_frusta
21092123 . iter ( )
21102124 . zip ( cubemap_visible_entities. iter_mut ( ) )
21112125 {
2112- if frustum. intersects_obb ( aabb, & model_to_world, true , true ) {
2126+ if frustum. intersects_obb ( & aabb, & model_to_world, true , true ) {
21132127 computed_visibility. set_visible_in_view ( ) ;
21142128 visible_entities. entities . push ( entity) ;
21152129 }
@@ -2148,8 +2162,9 @@ pub fn check_light_mesh_visibility(
21482162 entity,
21492163 mut computed_visibility,
21502164 maybe_entity_mask,
2151- maybe_aabb ,
2165+ maybe_aabb_source ,
21522166 maybe_transform,
2167+ maybe_mesh,
21532168 ) in visible_entity_query. iter_mut ( )
21542169 {
21552170 if !computed_visibility. is_visible_in_hierarchy ( ) {
@@ -2161,15 +2176,16 @@ pub fn check_light_mesh_visibility(
21612176 continue ;
21622177 }
21632178
2179+ let maybe_aabb = maybe_aabb_source. and_then ( |s| s. get ( maybe_mesh, & meshes) ) ;
21642180 // If we have an aabb and transform, do frustum culling
21652181 if let ( Some ( aabb) , Some ( transform) ) = ( maybe_aabb, maybe_transform) {
21662182 let model_to_world = transform. compute_matrix ( ) ;
21672183 // Do a cheap sphere vs obb test to prune out most meshes outside the sphere of the light
2168- if !light_sphere. intersects_obb ( aabb, & model_to_world) {
2184+ if !light_sphere. intersects_obb ( & aabb, & model_to_world) {
21692185 continue ;
21702186 }
21712187
2172- if frustum. intersects_obb ( aabb, & model_to_world, true , true ) {
2188+ if frustum. intersects_obb ( & aabb, & model_to_world, true , true ) {
21732189 computed_visibility. set_visible_in_view ( ) ;
21742190 visible_entities. entities . push ( entity) ;
21752191 }
0 commit comments