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

Aperta
#26,690 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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) {
Lingua principale
Rust
Stelle
50k
Fork
5.1k
Merge medio
2g 20h
PR unite (30g)
129

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di bevyengine/bevy

Tutte le issue di bevyengine/bevy

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.