Skip to content

Commit 48049f7

Browse files
authored
Don't mark a previous mesh transform as changed if it didn't actually change. (#17688)
This patch fixes a bug whereby we're re-extracting every mesh every frame. It's a regression from PR #17413. The code in question has actually been in the tree with this bug for quite a while; it's that just the code didn't actually run unless the renderer considered the previous view transforms necessary. Occlusion culling expanded the set of circumstances under which Bevy computes the previous view transforms, causing this bug to appear more often. This patch fixes the issue by checking to see if the previous transform of a mesh actually differs from the current transform before copying the current transform to the previous transform.
1 parent 642e016 commit 48049f7

File tree

1 file changed

+10
-6
lines changed
  • crates/bevy_pbr/src/prepass

1 file changed

+10
-6
lines changed

crates/bevy_pbr/src/prepass/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ pub fn update_previous_view_data(
226226
}
227227
}
228228

229-
#[derive(Component, Default)]
229+
#[derive(Component, PartialEq, Default)]
230230
pub struct PreviousGlobalTransform(pub Affine3A);
231231

232232
#[cfg(not(feature = "meshlet"))]
@@ -237,15 +237,19 @@ type PreviousMeshFilter = Or<(With<Mesh3d>, With<MeshletMesh3d>)>;
237237
pub fn update_mesh_previous_global_transforms(
238238
mut commands: Commands,
239239
views: Query<&Camera, Or<(With<Camera3d>, With<ShadowView>)>>,
240-
meshes: Query<(Entity, &GlobalTransform), PreviousMeshFilter>,
240+
meshes: Query<(Entity, &GlobalTransform, Option<&PreviousGlobalTransform>), PreviousMeshFilter>,
241241
) {
242242
let should_run = views.iter().any(|camera| camera.is_active);
243243

244244
if should_run {
245-
for (entity, transform) in &meshes {
246-
commands
247-
.entity(entity)
248-
.try_insert(PreviousGlobalTransform(transform.affine()));
245+
for (entity, transform, old_previous_transform) in &meshes {
246+
let new_previous_transform = PreviousGlobalTransform(transform.affine());
247+
// Make sure not to trigger change detection on
248+
// `PreviousGlobalTransform` if the previous transform hasn't
249+
// changed.
250+
if old_previous_transform != Some(&new_previous_transform) {
251+
commands.entity(entity).try_insert(new_previous_transform);
252+
}
249253
}
250254
}
251255
}

0 commit comments

Comments
 (0)