Directional lights sort shadow-casting and volumetric lights last, contrary to the comment, so they're the ones dropped past MAX_DIRECTIONAL_LIGHTS

Open Beginner friendly
#26,642 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
70/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust, wasm

Research direction

The sort is in prepare_lights in crates/bevy_pbr/src/render/light.rs, where directional_light_entities.sort_by_cached_key keys on (light.volumetric, light.shadow_maps_enabled, *entity) and the result is cut with .take(MAX_DIRECTIONAL_LIGHTS). Read that block alongside the comment above it, then confirm that false sorts first. Done means shadow-casting and volumetric lights are kept ahead of shadowless ones, with the entity tie-break left in place. Check the ordering reasoning by hand, since the issue gives no test to run.

Written by the indexing model from the issue text.

Description

Bevy version and features

  • 0.20.1. The same code is on main (crates/bevy_pbr/src/render/light.rs, prepare_lights).
  • Seen on WebGL2 (webgl2, wasm32), where MAX_DIRECTIONAL_LIGHTS is 1.

[Optional] Relevant system information

  • Rust 1.97.1, Chromium 156 on Linux.
AdapterInfo { name: "ANGLE (AMD, AMD Radeon RX 6650 XT (radeonsi navi23 ACO), OpenGL ES 3.2)", vendor: 4098, device: 0, device_type: Other, driver_info: "WebGL 2.0 (OpenGL ES 3.0 Chromium)", backend: Gl, .. }

What you did

Lit a scene with two DirectionalLights on the same layers: a bright sun and a dim fill light from the opposite side with shadow_maps_enabled: false. Built it for WebGL2, which warns The amount of directional lights of 2 is exceeding the supported limit of 1.

What went wrong

The comment above the sort says lights with volumetrics, then shadows, come first:

// Sort lights by
// - those with volumetric (and shadows) enabled first, ...
// - then those with shadows enabled second, ...
// - then by entity as a stable key ...
directional_light_entities.sort_by_cached_key(|entity| {
    let light = directional_lights.get(*entity).unwrap().2;
    (light.volumetric, light.shadow_maps_enabled, *entity)
});

But false sorts before true, so lights without volumetrics or shadows come first. Each view then keeps .take(MAX_DIRECTIONAL_LIGHTS), so past the limit it's the shadow-casting (or volumetric) light that gets dropped.

What we saw: on WebGL2, our scene was lit only by the dim fill light. With the sun's shadows on, the fill wins outright. With both lights shadowless, the entity tie-break decides, and which light won flipped between two builds of our app when we added an unrelated plugin.

Expected: the order the comment describes, Reverse on the first two keys or !light.volumetric, !light.shadow_maps_enabled. Then a shadowed sun is kept over a shadowless fill.

Additional information

  • Natively this only matters past 10 directional lights (or the volumetric/shadow counts derived from .take(MAX_DIRECTIONAL_LIGHTS)), since per-view shadow maps are capped by a running cascade count, not by index.
  • Workaround: we don't spawn the fill light on WebGL2.
Dominant language
Rust
Stars
49.8k
Forks
5.1k
Avg merge
3d 4h
Merged PRs (30d)
136

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.