Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lightmaps don't work with shadow-enabled directional lights. #11898

Closed
doonv opened this issue Feb 16, 2024 · 1 comment · Fixed by #11910
Closed

Lightmaps don't work with shadow-enabled directional lights. #11898

doonv opened this issue Feb 16, 2024 · 1 comment · Fixed by #11910
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior
Milestone

Comments

@doonv
Copy link
Contributor

doonv commented Feb 16, 2024

Bevy version

main (fe777d5)

What you did

I used lightmaps with the directional lights where shadows_enabled is set to true, here's an example:

//! Rendering a scene with baked lightmaps.

use bevy::pbr::Lightmap;
use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .insert_resource(AmbientLight::NONE)
        .add_systems(Startup, setup)
        .add_systems(Update, add_lightmaps_to_meshes)
        .run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(SceneBundle {
        scene: asset_server.load("models/CornellBox/CornellBox.glb#Scene0"),
        ..default()
    });

    commands.spawn(Camera3dBundle {
        transform: Transform::from_xyz(-278.0, 273.0, 800.0),
        ..default()
    });
    commands.spawn(DirectionalLightBundle {
        directional_light: DirectionalLight {
            shadows_enabled: true, // This must be enabled for it to panic!
            ..default()
        },
        ..default()
    });
}

fn add_lightmaps_to_meshes(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    meshes: Query<
        (Entity, &Name, &Handle<StandardMaterial>),
        (With<Handle<Mesh>>, Without<Lightmap>),
    >,
) {
    let exposure = 250.0;
    for (entity, name, material) in meshes.iter() {
        if &**name == "Light" {
            materials.get_mut(material).unwrap().emissive = Color::WHITE * exposure;
            continue;
        }

        if &**name == "large_box" {
            materials.get_mut(material).unwrap().lightmap_exposure = exposure;
            commands.entity(entity).insert(Lightmap {
                image: asset_server.load("lightmaps/CornellBox-Large.zstd.ktx2"),
                ..default()
            });
            continue;
        }

        if &**name == "small_box" {
            materials.get_mut(material).unwrap().lightmap_exposure = exposure;
            commands.entity(entity).insert(Lightmap {
                image: asset_server.load("lightmaps/CornellBox-Small.zstd.ktx2"),
                ..default()
            });
            continue;
        }

        if name.starts_with("cornell_box") {
            materials.get_mut(material).unwrap().lightmap_exposure = exposure;
            commands.entity(entity).insert(Lightmap {
                image: asset_server.load("lightmaps/CornellBox-Box.zstd.ktx2"),
                ..default()
            });
            continue;
        }
    }
}

What went wrong

I got this panic:

2024-02-16T13:06:01.153062Z ERROR log: Handling wgpu errors as fatal by default    
thread '<unnamed>' panicked at /home/doonv/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-0.19.1/src/backend/wgpu_core.rs:3009:5:
wgpu error: Validation Error

Caused by:
    In a RenderPass
      note: encoder = `shadow_pass_command_encoder`
    In a draw command, indexed:true indirect:false
      note: render pipeline = `pbr_prepass_pipeline`
    Incompatible bind group at index 1 in the current render pipeline
      note: Should be compatible an with an explicit bind group layout with label = `mesh_layout`
      note: Assigned explicit bind group layout with label = `lightmapped_mesh_layout`
      note: Entry 4 not found in expected bind group layout
      note: Entry 5 not found in expected bind group layout


note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic in exclusive system `bevy_render::renderer::render_system`!
thread 'Compute Task Pool (6)' panicked at crates/bevy_render/src/pipelined_rendering.rs:49:67:
called `Result::unwrap()` on an `Err` value: RecvError

Additional information

The lightmaps PR: #10231

This issue has been present since lightmaps were introduced.

@doonv doonv added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Feb 16, 2024
@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen and removed S-Needs-Triage This issue needs to be labelled labels Feb 16, 2024
@cart cart added this to the 0.13.1 milestone Feb 16, 2024
@cart
Copy link
Member

cart commented Feb 16, 2024

@pcwalton

@alice-i-cecile alice-i-cecile modified the milestones: 0.13.1, 0.13 Feb 16, 2024
pcwalton added a commit to pcwalton/bevy that referenced this issue Feb 16, 2024
pass.

I did this during the prepass, but I neglected to do it during the
shadow map pass, causing a panic when directional lights with shadows
were enabled with lightmapped meshes present. This patch fixes the
issue.

Closes bevyengine#11898.
github-merge-queue bot pushed a commit that referenced this issue Feb 17, 2024
…p pass. (#11910)

I did this during the prepass, but I neglected to do it during the
shadow map pass, causing a panic when directional lights with shadows
were enabled with lightmapped meshes present. This patch fixes the
issue.

Closes #11898.
github-merge-queue bot pushed a commit that referenced this issue Feb 17, 2024
…p pass. (#11910)

I did this during the prepass, but I neglected to do it during the
shadow map pass, causing a panic when directional lights with shadows
were enabled with lightmapped meshes present. This patch fixes the
issue.

Closes #11898.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants