Skip to content

Conversation

grace125
Copy link
Contributor

Objective

Implements #12461.

Solution

Makes RenderLayers no longer a component, adds components VisibleLayers and InheritedVisibleLayers that work as you'd expect, and VisibleLayers is now required by Visibility.

Testing

These changes work on all of the examples that used to use RenderLayers as a component: pixel_grid_snap, first_person_view_model, order_independent_transparency, and render_to_texture. I have yet to do any benchmarking. It also seems to work in the more hierarchical example I put in showcase.


Showcase

Users can now add VisibleLayers to a root component of a mesh, and it will inherit down the tree!

Click to view showcase
use std::f32::consts::PI;

use bevy::prelude::*;
use bevy_render::view::VisibleLayers;

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

#[derive(Component)]
struct Box;

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn((Transform::default(), Visibility::default(), VisibleLayers::layer(1), Name::new("Root"))).with_children(|parent| {

        parent.spawn((
            Name::new("Camera3d"),
            Camera3d::default(),
            Transform::from_xyz(4.0, 4.0, 12.0).looking_at(Vec3::new(0.0, 0.0, 0.5), Vec3::Y),
        ));
    
        parent.spawn((
            Name::new("DirectionalLight"),
            Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 1.0, -PI / 4.)),
            DirectionalLight::default(),
        ));

        parent.spawn((
            Name::new("Box"),
            Transform::default(), 
            Visibility::default(),
            Box
        )).with_child((
            Name::new("BoxScene"),
            SceneRoot(asset_server.load(
                GltfAssetLabel::Scene(0).from_asset("models/GltfPrimitives/gltf_primitives.glb"),
                
            ))
        ));
    });
}

fn input(mut query: Single<&mut VisibleLayers, With<Box>>, input: Res<ButtonInput<KeyCode>>) {
    if input.just_pressed(KeyCode::KeyA) {
        **query = VisibleLayers::Inherited;
    }
    if input.just_pressed(KeyCode::KeyS) {
        **query = VisibleLayers::layer(0);
    }
    if input.just_pressed(KeyCode::KeyD) {
        **query = VisibleLayers::layer(1);
    }
}

Migration Guide

  • Replace uses of RenderLayers as a component with VisibleLayers.

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Rendering Drawing game state to the screen X-Contentious There are nontrivial implications that should be thought through M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 23, 2025
@tomara-x
Copy link
Contributor

tomara-x commented May 27, 2025

if i really really want this, what can i do to help?
i fixed the conflicts and i'm testing it. it has everything i need!
can i help in some way?

edit: never mind, i was given the generic component propagation pr :D (#17575)

@grace125
Copy link
Contributor Author

I forgot I left this PR open! I realised everything I was doing could be done in userspace, so I did that. A generic solution would be better, it doesn't really make sense to have ad-hoc propagation for everything that might need it.

@grace125 grace125 closed this May 27, 2025
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-Feature A new feature, making something new possible M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Contentious There are nontrivial implications that should be thought through
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants