Skip to content

Commit

Permalink
bevy_render: Provide a way to opt-out of the built-in frustum culling (
Browse files Browse the repository at this point in the history
…#3711)

# Objective

- Allow opting-out of the built-in frustum culling for cases where its behaviour would be incorrect
- Make use of the this in the shader_instancing example that uses a custom instancing method. The built-in frustum culling breaks the custom instancing in the shader_instancing example if the camera is moved to:

```rust
    commands.spawn_bundle(PerspectiveCameraBundle {
        transform: Transform::from_xyz(12.0, 0.0, 15.0)
            .looking_at(Vec3::new(12.0, 0.0, 0.0), Vec3::Y),
        ..Default::default()
    });
```

...such that the Aabb of the cube Mesh that is at the origin goes completely out of view. This incorrectly (for the purpose of the custom instancing) culls the `Mesh` and so culls all instances even though some may be visible.


## Solution

- Add a `NoFrustumCulling` marker component
- Do not compute and add an `Aabb` to `Mesh` entities without an `Aabb` if they have a `NoFrustumCulling` marker component
- Do not apply frustum culling to entities with the `NoFrustumCulling` marker component
  • Loading branch information
superdump committed Jan 17, 2022
1 parent e88e394 commit 55da315
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 10 additions & 2 deletions crates/bevy_render/src/view/visibility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl Default for ComputedVisibility {
}
}

/// Use this component to opt-out of built-in frustum culling for Mesh entities
#[derive(Component)]
pub struct NoFrustumCulling;

#[derive(Clone, Component, Default, Debug, Reflect)]
#[reflect(Component)]
pub struct VisibleEntities {
Expand Down Expand Up @@ -106,7 +110,7 @@ impl Plugin for VisibilityPlugin {
pub fn calculate_bounds(
mut commands: Commands,
meshes: Res<Assets<Mesh>>,
without_aabb: Query<(Entity, &Handle<Mesh>), Without<Aabb>>,
without_aabb: Query<(Entity, &Handle<Mesh>), (Without<Aabb>, Without<NoFrustumCulling>)>,
) {
for (entity, mesh_handle) in without_aabb.iter() {
if let Some(mesh) = meshes.get(mesh_handle) {
Expand Down Expand Up @@ -142,6 +146,7 @@ pub fn check_visibility(
&mut ComputedVisibility,
Option<&RenderLayers>,
Option<&Aabb>,
Option<&NoFrustumCulling>,
Option<&GlobalTransform>,
)>,
)>,
Expand All @@ -161,6 +166,7 @@ pub fn check_visibility(
mut computed_visibility,
maybe_entity_mask,
maybe_aabb,
maybe_no_frustum_culling,
maybe_transform,
) in visible_entity_query.q1().iter_mut()
{
Expand All @@ -174,7 +180,9 @@ pub fn check_visibility(
}

// If we have an aabb and transform, do frustum culling
if let (Some(aabb), Some(transform)) = (maybe_aabb, maybe_transform) {
if let (Some(aabb), None, Some(transform)) =
(maybe_aabb, maybe_no_frustum_culling, maybe_transform)
{
if !frustum.intersects_obb(aabb, &transform.compute_matrix()) {
continue;
}
Expand Down
10 changes: 9 additions & 1 deletion examples/shader/shader_instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use bevy::{
},
render_resource::*,
renderer::RenderDevice,
view::{ComputedVisibility, ExtractedView, Msaa, Visibility},
view::{ComputedVisibility, ExtractedView, Msaa, NoFrustumCulling, Visibility},
RenderApp, RenderStage,
},
};
Expand Down Expand Up @@ -45,6 +45,14 @@ fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
),
Visibility::default(),
ComputedVisibility::default(),
// NOTE: Frustum culling is done based on the Aabb of the Mesh and the GlobalTransform.
// As the cube is at the origin, if its Aabb moves outside the view frustum, all the
// instanced cubes will be culled.
// The InstanceMaterialData contains the 'GlobalTransform' information for this custom
// instancing, and that is not taken into account with the built-in frustum culling.
// We must disable the built-in frustum culling by adding the `NoFrustumCulling` marker
// component to avoid incorrect culling.
NoFrustumCulling,
));

// camera
Expand Down

0 comments on commit 55da315

Please sign in to comment.