Volumetric fog: rays are marched over the depth difference, not the ray length — off-axis rays are too short

Aperta Adatta ai principianti
#26,689 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à
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust

Direzione di ricerca

Inizia da bevy_pbr/src/volumetric_fog/volumetric_fog.wesl e confronta come viene usato ray_length_view per il ray marching e il termine ambientale. Il rapporto propone la correzione per step_size_world e segnala che il termine ambientale presenta lo stesso problema tra profondità e lunghezza del raggio. Controlla il percorso di rendering della nebbia e i test pertinenti; il lavoro è completato quando i raggi fuori asse usano tutta la loro lunghezza e la trasmittanza varia sullo schermo come previsto.

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 wide-angle camera inside a large uniform FogVolume, a white unlit wall 50 m in front of it,
perpendicular to the view axis. The fog only absorbs (light_intensity: 0.0, no ambient), so the
wall shows the fog's transmittance exp(-σ · ray length) directly:

use bevy::light::{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(),
        Projection::Perspective(PerspectiveProjection { fov: 100f32.to_radians(), ..default() }),
        Transform::default(),
        VolumetricFog { ambient_intensity: 0.0, ..default() },
    ));
    // The fog needs a volumetric light to be drawn at all; the fog itself ignores it (light_intensity 0).
    commands.spawn((
        DirectionalLight::default(),
        VolumetricLight,
        Transform::from_xyz(0.0, 1.0, 1.0).looking_at(Vec3::ZERO, Vec3::Y),
    ));
    // A white unlit wall 50 m in front of the camera, perpendicular to the view axis.
    commands.spawn((
        Mesh3d(meshes.add(Plane3d::new(Vec3::Z, Vec2::splat(500.0)))),
        MeshMaterial3d(materials.add(StandardMaterial { base_color: Color::WHITE, unlit: true, ..default() })),
        Transform::from_xyz(0.0, 0.0, -50.0),
    ));
    // Uniform fog around the camera: absorption only, so the wall shows the transmittance exp(-σ·length).
    commands.spawn((
        FogVolume { density_factor: 0.02, light_intensity: 0.0, ..default() },
        Transform::from_scale(Vec3::splat(400.0)),
    ));
}

What went wrong

  • Expected: the wall gets darker towards the edges of the screen — the ray to the centre is 50 m
    long, the ray to a corner of this frame about 130 m (50 / cos 68°).
  • Actual: the wall is one flat grey, as if every ray were 50 m long. Off-axis rays are shortened by
    cos(angle to the view axis), so fog is always thinner at the sides of the screen than in the
    middle. In our game (ultrawide, about 110° horizontally) this showed up as a bright blob in the
    middle of the screen that followed the camera.

Left: the example above. Right: the same example with the one-line fix below.

fog_7

Additional information

bevy_pbr/src/volumetric_fog/volumetric_fog.wesl:

let ray_length_view = max(0.0, end_depth_view - start_depth_view);   // difference of view-space depths
...
let step_size_world = ray_length_view * inv_step_count;
...
let P_view = start_pos_view + Rd_view * f32(step) * step_size_world;  // but steps go along the normalized ray

The length along the ray is (end_depth_view - start_depth_view) / -Rd_view.z. Suggested fix
(one line):

let step_size_world = ray_length_view * inv_step_count / max(-Rd_view.z, 1e-3);

The ambient term before the loop, exp(-ray_length_view * (absorption + scattering)), has the
same mistake.

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.