Skip to content

Adding NoCpuCulling to a mesh at runtime hides the mesh #23473

@IceSentry

Description

@IceSentry

Bevy version and features

[Optional] Relevant system information

Windows 11, nvidia 4080

What you did

Here's a simple repro. When pressing space the meshes will disappear as soon as the NoCpuCulling component is added to them.

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

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .add_systems(Update, toggle_no_cpu_culling)
        .run();
}

/// set up a simple 3D scene
fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    // circular base
    commands.spawn((
        Mesh3d(meshes.add(Circle::new(4.0))),
        MeshMaterial3d(materials.add(Color::WHITE)),
        Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
    ));
    // cube
    commands.spawn((
        Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
        MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
        Transform::from_xyz(0.0, 0.5, 0.0),
    ));
    // light
    commands.spawn((
        PointLight {
            shadow_maps_enabled: true,
            ..default()
        },
        Transform::from_xyz(4.0, 8.0, 4.0),
    ));
    // camera
    commands.spawn((
        Camera3d::default(),
        Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
    ));
}

fn toggle_no_cpu_culling(
    mut commands: Commands,
    input: Res<ButtonInput<KeyCode>>,
    meshes: Query<(Entity, Has<NoCpuCulling>), With<Mesh3d>>,
) {
    if input.just_pressed(KeyCode::Space) {
        for (entity, has_no_cpu_culling) in &meshes {
            if has_no_cpu_culling {
                commands.entity(entity).remove::<NoCpuCulling>();
            } else {
                commands.entity(entity).insert(NoCpuCulling);
            }
        }
    }
}

What went wrong

Meshes should have remained visible but they disappeared

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-RenderingDrawing game state to the screenC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions