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

Open Beginner friendly
#26,689 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

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

Research direction

Start in bevy_pbr/src/volumetric_fog/volumetric_fog.wesl and compare how ray_length_view is used for ray marching and the ambient term. The report gives the proposed correction for step_size_world and notes the ambient term has the same depth-versus-ray-length issue. Check the fog rendering path and relevant tests; done means off-axis rays use their full ray length and transmittance varies as expected across the screen.

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 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.

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.