Skip to content

Commit

Permalink
Fix panic with multiple fog volumes (#18119)
Browse files Browse the repository at this point in the history
# Objective

Fixes #17590.

## Solution

`prepare_volumetric_fog_uniforms` adds a uniform for each combination of
fog volume and view. But it only allocated enough uniforms for one fog
volume per view.

## Testing

Ran the `volumetric_fog` example with 1/2/3/4 fog volumes. Also checked
the `fog_volumes` and `scrolling_fog` examples (without multiple
volumes). Win10/Vulkan/Nvidia.

To test multiple views I tried adding fog volumes to the `split_screen`
example. This doesn't quite work - the fog should be centred on the fox,
but instead it's centred on the window. Same result with and without the
PR, so I'm assuming it's a separate bug.


![image](https://github.com/user-attachments/assets/4a74d6d7-8a73-4322-9dc6-c5ddd054ece2)
  • Loading branch information
greeble-dev authored Mar 3, 2025
1 parent bfdd018 commit f42ecc4
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions crates/bevy_pbr/src/volumetric_fog/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,20 +694,20 @@ pub fn prepare_volumetric_fog_uniforms(
render_queue: Res<RenderQueue>,
mut local_from_world_matrices: Local<Vec<Mat4>>,
) {
let Some(mut writer) = volumetric_lighting_uniform_buffer.get_writer(
view_targets.iter().len(),
&render_device,
&render_queue,
) else {
return;
};

// Do this up front to avoid O(n^2) matrix inversion.
local_from_world_matrices.clear();
for (_, _, fog_transform) in fog_volumes.iter() {
local_from_world_matrices.push(fog_transform.compute_matrix().inverse());
}

let uniform_count = view_targets.iter().len() * local_from_world_matrices.len();

let Some(mut writer) =
volumetric_lighting_uniform_buffer.get_writer(uniform_count, &render_device, &render_queue)
else {
return;
};

for (view_entity, extracted_view, volumetric_fog) in view_targets.iter() {
let world_from_view = extracted_view.world_from_view.compute_matrix();

Expand Down

0 comments on commit f42ecc4

Please sign in to comment.