Skip to content

Commit 60729c5

Browse files
committed
avoid unnecessary allocation for visible_lights
1 parent e14043f commit 60729c5

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

crates/bevy_pbr/src/light.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -926,16 +926,7 @@ pub(crate) fn assign_lights_to_clusters(
926926
z_planes.push(Plane::new(normal.extend(d)));
927927
}
928928

929-
let mut visible_lights_scratch = Vec::new();
930-
931-
{
932-
// reuse existing visible lights Vec, if it exists
933-
let visible_lights = if let Some(visible_lights) = visible_lights.as_mut() {
934-
visible_lights.entities.clear();
935-
&mut visible_lights.entities
936-
} else {
937-
&mut visible_lights_scratch
938-
};
929+
let mut update_from_light_intersections = |visible_lights: &mut Vec<Entity>| {
939930
for light in lights.iter() {
940931
let light_sphere = Sphere {
941932
center: Vec3A::from(light.translation),
@@ -1088,12 +1079,18 @@ pub(crate) fn assign_lights_to_clusters(
10881079
}
10891080
}
10901081
}
1091-
}
1082+
};
10921083

1093-
if visible_lights.is_none() {
1094-
commands.entity(view_entity).insert(VisiblePointLights {
1095-
entities: visible_lights_scratch,
1096-
});
1084+
// reuse existing visible lights Vec, if it exists
1085+
if let Some(visible_lights) = visible_lights.as_mut() {
1086+
visible_lights.entities.clear();
1087+
update_from_light_intersections(&mut visible_lights.entities);
1088+
} else {
1089+
let mut entities = Vec::new();
1090+
update_from_light_intersections(&mut entities);
1091+
commands
1092+
.entity(view_entity)
1093+
.insert(VisiblePointLights { entities });
10971094
}
10981095
}
10991096
}

0 commit comments

Comments
 (0)