From e610badf6cdad6354173f32664dcfd22cc52ed22 Mon Sep 17 00:00:00 2001 From: JMS55 <47158642+JMS55@users.noreply.github.com> Date: Sun, 15 Jun 2025 21:54:26 -0700 Subject: [PATCH] Add more PreviousViewData (#19605) Add some more matrices to PreviousViewData for future use with bevy_solari. --- crates/bevy_core_pipeline/src/prepass/mod.rs | 5 +++++ crates/bevy_pbr/src/prepass/mod.rs | 14 ++++++++++++-- crates/bevy_pbr/src/prepass/prepass_bindings.wgsl | 2 ++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/bevy_core_pipeline/src/prepass/mod.rs b/crates/bevy_core_pipeline/src/prepass/mod.rs index deea2a5fa8..880e2b6892 100644 --- a/crates/bevy_core_pipeline/src/prepass/mod.rs +++ b/crates/bevy_core_pipeline/src/prepass/mod.rs @@ -74,11 +74,16 @@ pub struct MotionVectorPrepass; #[reflect(Component, Default)] pub struct DeferredPrepass; +/// View matrices from the previous frame. +/// +/// Useful for temporal rendering techniques that need access to last frame's camera data. #[derive(Component, ShaderType, Clone)] pub struct PreviousViewData { pub view_from_world: Mat4, pub clip_from_world: Mat4, pub clip_from_view: Mat4, + pub world_from_clip: Mat4, + pub view_from_clip: Mat4, } #[derive(Resource, Default)] diff --git a/crates/bevy_pbr/src/prepass/mod.rs b/crates/bevy_pbr/src/prepass/mod.rs index 35182910a9..1ae0c7fa84 100644 --- a/crates/bevy_pbr/src/prepass/mod.rs +++ b/crates/bevy_pbr/src/prepass/mod.rs @@ -216,11 +216,16 @@ pub fn update_previous_view_data( query: Query<(Entity, &Camera, &GlobalTransform), Or<(With, With)>>, ) { for (entity, camera, camera_transform) in &query { - let view_from_world = camera_transform.compute_matrix().inverse(); + let world_from_view = camera_transform.compute_matrix(); + let view_from_world = world_from_view.inverse(); + let view_from_clip = camera.clip_from_view().inverse(); + commands.entity(entity).try_insert(PreviousViewData { view_from_world, clip_from_world: camera.clip_from_view() * view_from_world, clip_from_view: camera.clip_from_view(), + world_from_clip: world_from_view * view_from_clip, + view_from_clip, }); } } @@ -698,11 +703,16 @@ pub fn prepare_previous_view_uniforms( let prev_view_data = match maybe_previous_view_uniforms { Some(previous_view) => previous_view.clone(), None => { - let view_from_world = camera.world_from_view.compute_matrix().inverse(); + let world_from_view = camera.world_from_view.compute_matrix(); + let view_from_world = world_from_view.inverse(); + let view_from_clip = camera.clip_from_view.inverse(); + PreviousViewData { view_from_world, clip_from_world: camera.clip_from_view * view_from_world, clip_from_view: camera.clip_from_view, + world_from_clip: world_from_view * view_from_clip, + view_from_clip, } } }; diff --git a/crates/bevy_pbr/src/prepass/prepass_bindings.wgsl b/crates/bevy_pbr/src/prepass/prepass_bindings.wgsl index 3bd27b2e03..141f7d7b0d 100644 --- a/crates/bevy_pbr/src/prepass/prepass_bindings.wgsl +++ b/crates/bevy_pbr/src/prepass/prepass_bindings.wgsl @@ -4,6 +4,8 @@ struct PreviousViewUniforms { view_from_world: mat4x4, clip_from_world: mat4x4, clip_from_view: mat4x4, + world_from_clip: mat4x4, + view_from_clip: mat4x4, } @group(0) @binding(2) var previous_view_uniforms: PreviousViewUniforms;