Volumetric fog: no sunlight in fog beyond the last shadow cascade (cascade index not checked against `num_cascades`)

Open
#26,690 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust

Research direction

Start with bevy_pbr/src/volumetric_fog/volumetric_fog.wesl and compare its cascade handling with shadows.wesl in fetch_directional_shadow. Check how get_cascade_index behaves beyond num_cascades and how the fog shader accesses the cascades array. Done when fog beyond the last shadow cascade receives sunlight without accessing an out-of-range cascade.

Written by the indexing model from the issue text.

Description

C-Bug S-Needs-Triage

Bevy version and features

  • 0.20.1 (the code is the same on main)
  • Features: default + dlss

[Optional] Relevant system information

  • cargo 1.99.1 (5f94df478 2026-08-27)
  • Windows 11
AdapterInfo { name: "NVIDIA GeForce RTX 5080", vendor: 4318, device: 11266, device_type: DiscreteGpu, driver: "NVIDIA", driver_info: "616.92", backend: Vulkan }

What you did

A DirectionalLight with shadows and VolumetricLight, one shadow cascade up to 20 m, and a row
of small fog cubes 5…50 m from the camera:

use bevy::light::{CascadeShadowConfigBuilder, FogVolume, VolumetricFog, VolumetricLight};
use bevy::prelude::*;

fn main() {
    App::new().add_plugins(DefaultPlugins).add_systems(Startup, setup).run();
}

fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<StandardMaterial>>) {
    commands.spawn((
        Camera3d::default(),
        Transform::from_xyz(4.0, 4.0, 5.0).looking_at(Vec3::new(0.0, 1.0, -20.0), Vec3::Y),
        VolumetricFog { ambient_intensity: 0.0, ..default() },
    ));
    // One shadow cascade, 20 m.
    commands.spawn((
        DirectionalLight { shadow_maps_enabled: true, illuminance: 32000.0, ..default() },
        VolumetricLight,
        CascadeShadowConfigBuilder { num_cascades: 1, maximum_distance: 20.0, ..default() }.build(),
        Transform::from_xyz(1.0, 1.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
    ));
    commands.spawn((
        Mesh3d(meshes.add(Plane3d::default().mesh().size(400.0, 400.0))),
        MeshMaterial3d(materials.add(Color::srgb(0.05, 0.05, 0.05))),
    ));
    // A row of small fog cubes, 5 m apart, 5…50 m from the camera.
    for i in 0..10 {
        commands.spawn((
            FogVolume { density_factor: 1.0, ..default() },
            Transform::from_xyz(0.0, 1.0, -5.0 * i as f32).with_scale(Vec3::splat(2.0)),
        ));
    }
}

What went wrong

  • Expected: all cubes are lit by the sun. Beyond the last cascade there is no shadow information,
    so the fog should be lit, the same as surfaces there.
  • Actual: every cube farther than 20 m from the camera gets no sunlight at all and only darkens
    what is behind it. With 4 cascades the index goes past the end of the cascades array
    (MAX_CASCADES_PER_LIGHT = 4), so the result depends on how the backend clamps it.

Left: the example above. Right: the same example with the fix below.

fog_8

Additional information

bevy_pbr/src/volumetric_fog/volumetric_fog.wesl:

let cascade_index = get_cascade_index(light_index, P_view.z);   // == num_cascades beyond the last cascade
let light_local = world_to_directional_light_local(light_index, cascade_index, ...);
var local_light_attenuation = f32(light_local.w != 0.0);         // unused cascade → w == 0 → no light
if (local_light_attenuation != 0.0) {
    let cascade = &(*light).cascades[cascade_index];

Regular shadows check this case (shadows.wesl, fetch_directional_shadow:
if (cascade_index >= (*light).num_cascades) { return 1.0; }), the fog doesn't. Suggested fix:

let cascade_found = get_cascade_index(light_index, P_view.z);
let in_cascades = cascade_found < (*light).num_cascades;
let cascade_index = min(cascade_found, max((*light).num_cascades, 1u) - 1u);
...
var local_light_attenuation = select(1.0, f32(light_local.w != 0.0), in_cascades);
if (in_cascades && local_light_attenuation != 0.0) {
Dominant language
Rust
Stars
50k
Forks
5.1k
Avg merge
2d 20h
Merged PRs (30d)
129

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bevyengine/bevy

All issues in bevyengine/bevy

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.