Skip to content

Commit

Permalink
no frustum culling for shadows
Browse files Browse the repository at this point in the history
  • Loading branch information
robtfm committed Sep 9, 2024
1 parent ca48b8d commit dfa5563
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions crates/bevy_pbr/src/light/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use bevy_render::{
mesh::Mesh,
primitives::{Aabb, CascadesFrusta, CubemapFrusta, Frustum, Sphere},
view::{
InheritedVisibility, RenderLayers, ViewVisibility, VisibilityRange, VisibleEntities,
VisibleEntityRanges, WithMesh,
InheritedVisibility, NoFrustumCulling, RenderLayers, ViewVisibility, VisibilityRange,
VisibleEntities, VisibleEntityRanges, WithMesh,
},
};
use bevy_transform::components::{GlobalTransform, Transform};
Expand Down Expand Up @@ -673,6 +673,7 @@ pub fn check_light_mesh_visibility(
Option<&Aabb>,
Option<&GlobalTransform>,
Has<VisibilityRange>,
Has<NoFrustumCulling>,
),
(
Without<NotShadowCaster>,
Expand Down Expand Up @@ -742,6 +743,7 @@ pub fn check_light_mesh_visibility(
maybe_aabb,
maybe_transform,
has_visibility_range,
has_no_frustum_culling,
) in &mut visible_entity_query
{
if !inherited_visibility.get() {
Expand Down Expand Up @@ -774,7 +776,9 @@ pub fn check_light_mesh_visibility(
view_frusta.iter().zip(view_visible_entities)
{
// Disable near-plane culling, as a shadow caster could lie before the near plane.
if !frustum.intersects_obb(aabb, &transform.affine(), false, true) {
if !has_no_frustum_culling
&& !frustum.intersects_obb(aabb, &transform.affine(), false, true)
{
continue;
}

Expand Down Expand Up @@ -836,6 +840,7 @@ pub fn check_light_mesh_visibility(
maybe_aabb,
maybe_transform,
has_visibility_range,
has_no_frustum_culling,
) in &mut visible_entity_query
{
if !inherited_visibility.get() {
Expand All @@ -860,15 +865,19 @@ pub fn check_light_mesh_visibility(
if let (Some(aabb), Some(transform)) = (maybe_aabb, maybe_transform) {
let model_to_world = transform.affine();
// Do a cheap sphere vs obb test to prune out most meshes outside the sphere of the light
if !light_sphere.intersects_obb(aabb, &model_to_world) {
if !has_no_frustum_culling
&& !light_sphere.intersects_obb(aabb, &model_to_world)
{
continue;
}

for (frustum, visible_entities) in cubemap_frusta
.iter()
.zip(cubemap_visible_entities.iter_mut())
{
if frustum.intersects_obb(aabb, &model_to_world, true, true) {
if has_no_frustum_culling
|| frustum.intersects_obb(aabb, &model_to_world, true, true)
{
view_visibility.set();
visible_entities.push::<WithMesh>(entity);
}
Expand Down Expand Up @@ -911,6 +920,7 @@ pub fn check_light_mesh_visibility(
maybe_aabb,
maybe_transform,
has_visibility_range,
has_no_frustum_culling,
) in &mut visible_entity_query
{
if !inherited_visibility.get() {
Expand All @@ -935,11 +945,15 @@ pub fn check_light_mesh_visibility(
if let (Some(aabb), Some(transform)) = (maybe_aabb, maybe_transform) {
let model_to_world = transform.affine();
// Do a cheap sphere vs obb test to prune out most meshes outside the sphere of the light
if !light_sphere.intersects_obb(aabb, &model_to_world) {
if !has_no_frustum_culling
&& !light_sphere.intersects_obb(aabb, &model_to_world)
{
continue;
}

if frustum.intersects_obb(aabb, &model_to_world, true, true) {
if has_no_frustum_culling
|| frustum.intersects_obb(aabb, &model_to_world, true, true)
{
view_visibility.set();
visible_entities.push::<WithMesh>(entity);
}
Expand Down

0 comments on commit dfa5563

Please sign in to comment.