Skip to content

Fix: changing render layers on View with NoCpuCulling meshes does not work - #25764

Open
CodingDaniel1 wants to merge 3 commits into
bevyengine:mainfrom
CodingDaniel1:ncl-camera-change-layers
Open

CodingDaniel1 wants to merge 3 commits into
bevyengine:mainfrom
CodingDaniel1:ncl-camera-change-layers

Conversation

@CodingDaniel1

Copy link
Copy Markdown
Contributor

Objective

This is follow up work against #25690

This PR fixes RenderLayers change on View not working when meshes are tagged with NoCpuCulling.

Solution

Extract RenderLayers change on views into ExtractedRenderLayersMeta. This happens for every views with RenderLayers.
Look for view RenderLayers changes in collect_gpu_culled_meshes inside bevy_pbr, check for every meshes thats relevant to the new layers or not, add and remove as we go.

Testing

I tested with code changing view's RenderLayers at runtime and all Meshes tagged with NoCpuCulling. It successfully hide the meshes when the layer does not intersect, just like in the cpu culling path.

Showcase

I use the following example code to test this issue

Click to view showcase
//! A simple 3D scene with light shining over a cube sitting on a plane.

use bevy::{
    camera::visibility::{NoCpuCulling, RenderLayers},
    prelude::*,
};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, scene.spawn())
        .add_systems(Update, (change_layers, change_light_layers))
        .add_observer(add_ncl)
        .run();
}

/// set up a simple 3D scene
fn scene() -> impl SceneList {
    bsn! {
        #CircularBase
        Mesh3d(asset_value(Circle::new(4.0)))
        MeshMaterial3d::<StandardMaterial>(asset_value(Color::WHITE))
        Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2))
        --
        #Cube
        Mesh3d(asset_value(Cuboid::new(1.0, 1.0, 1.0)))
        MeshMaterial3d::<StandardMaterial>(asset_value(Color::srgb_u8(124, 144, 255)))
        Transform::from_xyz(0.0, 0.5, 0.0)
        --
        PointLight {
            shadow_maps_enabled: true,
        }
        Transform::from_xyz(4.0, 8.0, 4.0)
        --
        Camera3d
        Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y)
    }
}

fn add_ncl(add: On<Add<Mesh3d>>, mut commands: Commands) {
    commands.entity(add.entity).insert(NoCpuCulling);
}

fn change_layers(
    keyboard: Res<ButtonInput<KeyCode>>,
    query: Query<Entity, With<Camera3d>>,
    mut commands: Commands,
    mut state: Local<bool>,
) {
    if keyboard.just_pressed(KeyCode::KeyP) {
        let layers = if *state {
            RenderLayers::layer(0)
        } else {
            RenderLayers::layer(1)
        };
        *state = !*state;
        for entity in query {
            commands.entity(entity).insert(layers.clone());
        }
    }
}

fn change_light_layers(
    keyboard: Res<ButtonInput<KeyCode>>,
    query: Query<Entity, Or<(With<PointLight>, With<SpotLight>, With<DirectionalLight>)>>,
    mut commands: Commands,
    mut state: Local<bool>,
) {
    if keyboard.just_pressed(KeyCode::KeyL) {
        let layers = if *state {
            RenderLayers::layer(0)
        } else {
            RenderLayers::layer(1)
        };
        *state = !*state;
        for entity in query {
            commands.entity(entity).insert(layers.clone());
        }
    }
}

@github-actions

Copy link
Copy Markdown
Contributor

Your PR increases Bevy Minimum Supported Rust Version. Please update the rust-version field in the root Cargo.toml file.

@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-Rendering Drawing game state to the screen X-Uncontroversial This work is generally agreed upon D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 13, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in Rendering Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Uncontroversial This work is generally agreed upon

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

2 participants