Skip to content

Commit e4dc7ef

Browse files
committed
bevy_pbr: Leverage batch spawning of ExtractedPointLights
1 parent c730c3d commit e4dc7ef

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

crates/bevy_pbr/src/render/light.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ pub fn extract_lights(
448448
point_light_shadow_map: Res<PointLightShadowMap>,
449449
directional_light_shadow_map: Res<DirectionalLightShadowMap>,
450450
global_point_lights: Res<VisiblePointLights>,
451-
// visible_point_lights: Query<&VisiblePointLights>,
451+
mut previous_point_lights_len: Local<usize>,
452452
mut point_lights: Query<(&PointLight, &mut CubemapVisibleEntities, &GlobalTransform)>,
453453
mut directional_lights: Query<(
454454
Entity,
@@ -474,32 +474,38 @@ pub fn extract_lights(
474474
// https://catlikecoding.com/unity/tutorials/custom-srp/point-and-spot-shadows/
475475
let point_light_texel_size = 2.0 / point_light_shadow_map.size as f32;
476476

477+
let mut point_lights_values = Vec::with_capacity(*previous_point_lights_len);
477478
for entity in global_point_lights.iter().copied() {
478479
if let Ok((point_light, cubemap_visible_entities, transform)) = point_lights.get_mut(entity)
479480
{
480481
let render_cubemap_visible_entities =
481482
std::mem::take(cubemap_visible_entities.into_inner());
482-
commands.get_or_spawn(entity).insert_bundle((
483-
ExtractedPointLight {
484-
color: point_light.color,
485-
// NOTE: Map from luminous power in lumens to luminous intensity in lumens per steradian
486-
// for a point light. See https://google.github.io/filament/Filament.html#mjx-eqn-pointLightLuminousPower
487-
// for details.
488-
intensity: point_light.intensity / (4.0 * std::f32::consts::PI),
489-
range: point_light.range,
490-
radius: point_light.radius,
491-
transform: *transform,
492-
shadows_enabled: point_light.shadows_enabled,
493-
shadow_depth_bias: point_light.shadow_depth_bias,
494-
// The factor of SQRT_2 is for the worst-case diagonal offset
495-
shadow_normal_bias: point_light.shadow_normal_bias
496-
* point_light_texel_size
497-
* std::f32::consts::SQRT_2,
498-
},
499-
render_cubemap_visible_entities,
483+
point_lights_values.push((
484+
entity,
485+
(
486+
ExtractedPointLight {
487+
color: point_light.color,
488+
// NOTE: Map from luminous power in lumens to luminous intensity in lumens per steradian
489+
// for a point light. See https://google.github.io/filament/Filament.html#mjx-eqn-pointLightLuminousPower
490+
// for details.
491+
intensity: point_light.intensity / (4.0 * std::f32::consts::PI),
492+
range: point_light.range,
493+
radius: point_light.radius,
494+
transform: *transform,
495+
shadows_enabled: point_light.shadows_enabled,
496+
shadow_depth_bias: point_light.shadow_depth_bias,
497+
// The factor of SQRT_2 is for the worst-case diagonal offset
498+
shadow_normal_bias: point_light.shadow_normal_bias
499+
* point_light_texel_size
500+
* std::f32::consts::SQRT_2,
501+
},
502+
render_cubemap_visible_entities,
503+
),
500504
));
501505
}
502506
}
507+
*previous_point_lights_len = point_lights_values.len();
508+
commands.insert_or_spawn_batch(point_lights_values);
503509

504510
for (entity, directional_light, visible_entities, transform) in directional_lights.iter_mut() {
505511
// Calulate the directional light shadow map texel size using the largest x,y dimension of

0 commit comments

Comments
 (0)