Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,7 +1865,7 @@ pub fn queue_shadows(
mut shadow_render_phases: ResMut<ViewBinnedRenderPhases<Shadow>>,
gpu_preprocessing_support: Res<GpuPreprocessingSupport>,
mesh_allocator: Res<MeshAllocator>,
view_lights: Query<(Entity, &ViewLightEntities), With<ExtractedView>>,
view_lights: Query<(Entity, &ViewLightEntities, Option<&RenderLayers>), With<ExtractedView>>,
view_light_entities: Query<(&LightEntity, &ExtractedView)>,
point_light_entities: Query<&RenderCubemapVisibleEntities, With<ExtractedPointLight>>,
directional_light_entities: Query<
Expand All @@ -1875,7 +1875,7 @@ pub fn queue_shadows(
spot_light_entities: Query<&RenderVisibleMeshEntities, With<ExtractedPointLight>>,
specialized_material_pipeline_cache: Res<SpecializedShadowMaterialPipelineCache>,
) {
for (entity, view_lights) in &view_lights {
for (entity, view_lights, camera_layers) in &view_lights {
for view_light_entity in view_lights.lights.iter().copied() {
let Ok((light_entity, extracted_view_light)) =
view_light_entities.get(view_light_entity)
Expand Down Expand Up @@ -1925,11 +1925,6 @@ pub fn queue_shadows(
continue;
};

// Skip the entity if it's cached in a bin and up to date.
if shadow_phase.validate_cached_entity(main_entity, *current_change_tick) {
continue;
}

let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(main_entity)
else {
continue;
Expand All @@ -1941,6 +1936,23 @@ pub fn queue_shadows(
continue;
}

let mesh_layers = mesh_instance
.shared
.render_layers
.as_ref()
.unwrap_or_default();

let camera_layers = camera_layers.unwrap_or_default();

if !camera_layers.intersects(mesh_layers) {
continue;
}

// Skip the entity if it's cached in a bin and up to date.
if shadow_phase.validate_cached_entity(main_entity, *current_change_tick) {
continue;
}

let Some(material_instance) = render_material_instances.instances.get(&main_entity)
else {
continue;
Expand Down
14 changes: 14 additions & 0 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ use bevy_render::camera::TemporalJitter;
use bevy_render::prelude::Msaa;
use bevy_render::sync_world::{MainEntity, MainEntityHashMap};
use bevy_render::view::ExtractedView;
use bevy_render::view::RenderLayers;
use bevy_render::RenderSystems::PrepareAssets;

use bytemuck::{Pod, Zeroable};
use nonmax::{NonMaxU16, NonMaxU32};
use smallvec::{smallvec, SmallVec};
Expand Down Expand Up @@ -720,6 +722,8 @@ pub struct RenderMeshInstanceShared {
pub lightmap_slab_index: Option<LightmapSlabIndex>,
/// User supplied tag to identify this mesh instance.
pub tag: u32,
/// Render layers that this mesh instance belongs to.
pub render_layers: Option<RenderLayers>,
}

/// Information that is gathered during the parallel portion of mesh extraction
Expand Down Expand Up @@ -811,6 +815,7 @@ impl RenderMeshInstanceShared {
tag: Option<&MeshTag>,
not_shadow_caster: bool,
no_automatic_batching: bool,
render_layers: Option<&RenderLayers>,
) -> Self {
Self::for_cpu_building(
previous_transform,
Expand All @@ -819,6 +824,7 @@ impl RenderMeshInstanceShared {
default(),
not_shadow_caster,
no_automatic_batching,
render_layers,
)
}

Expand All @@ -830,6 +836,7 @@ impl RenderMeshInstanceShared {
material_bindings_index: MaterialBindingId,
not_shadow_caster: bool,
no_automatic_batching: bool,
render_layers: Option<&RenderLayers>,
) -> Self {
let mut mesh_instance_flags = RenderMeshInstanceFlags::empty();
mesh_instance_flags.set(RenderMeshInstanceFlags::SHADOW_CASTER, !not_shadow_caster);
Expand All @@ -848,6 +855,7 @@ impl RenderMeshInstanceShared {
material_bindings_index,
lightmap_slab_index: None,
tag: tag.map_or(0, |i| **i),
render_layers: render_layers.cloned(),
}
}

Expand Down Expand Up @@ -1313,6 +1321,7 @@ pub fn extract_meshes_for_cpu_building(
Has<NotShadowCaster>,
Has<NoAutomaticBatching>,
Has<VisibilityRange>,
Option<&RenderLayers>,
)>,
>,
) {
Expand All @@ -1332,6 +1341,7 @@ pub fn extract_meshes_for_cpu_building(
not_shadow_caster,
no_automatic_batching,
visibility_range,
render_layers,
)| {
if !view_visibility.get() {
return;
Expand Down Expand Up @@ -1364,6 +1374,7 @@ pub fn extract_meshes_for_cpu_building(
material_bindings_index,
not_shadow_caster,
no_automatic_batching,
render_layers,
);

let world_from_local = transform.affine();
Expand Down Expand Up @@ -1417,6 +1428,7 @@ type GpuMeshExtractionQuery = (
Has<NotShadowCaster>,
Has<NoAutomaticBatching>,
Has<VisibilityRange>,
Option<Read<RenderLayers>>,
);

/// Extracts meshes from the main world into the render world and queues
Expand Down Expand Up @@ -1536,6 +1548,7 @@ fn extract_mesh_for_gpu_building(
not_shadow_caster,
no_automatic_batching,
visibility_range,
render_layers,
): <GpuMeshExtractionQuery as QueryData>::Item<'_, '_>,
render_visibility_ranges: &RenderVisibilityRanges,
render_mesh_instances: &RenderMeshInstancesGpu,
Expand Down Expand Up @@ -1566,6 +1579,7 @@ fn extract_mesh_for_gpu_building(
tag,
not_shadow_caster,
no_automatic_batching,
render_layers,
);

let lightmap_uv_rect = pack_lightmap_uv_rect(lightmap.map(|lightmap| lightmap.uv_rect));
Expand Down
Loading