Skip to content

Overwriting an ExtendedMaterial with another ExtendedMaterial crashes bevy #20031

Open
@Affinator

Description

@Affinator

Bevy version

0.16.1

maybe related to: #19838

Relevant system information

2025-07-08T15:06:55.756508Z  INFO bevy_render::renderer: AdapterInfo { name: "NVIDIA RTX A2000 Laptop GPU", vendor: 4318, device: 9656, device_type: DiscreteGpu, driver: "NVIDIA", driver_info: "553.62", backend: Vulkan }
2025-07-08T15:06:55.924385Z  INFO bevy_render::batching::gpu_preprocessing: GPU preprocessing is fully supported on this device.

What you did

I am trying to create debug views for my procedural terrain generation.

I have:

  • one "normal" terrain shader: MeshMaterial3d<ExtendedMaterial<StandardMaterial, TerrainShader>>
  • one "debug" terrain shader: MeshMaterial3d<ExtendedMaterial<StandardMaterial, TerrainColoredTilesShader>>

What went wrong

I can use both shaders separately without problems. But when I try to change a terrain cluster (entity) from one material to the other, bevy crashes with the following errors:

thread 'Compute Task Pool (2)' panicked at C:\Users\___\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_pbr-0.16.1\src\prepass\mod.rs:952:79:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'Compute Task Pool (3)' panicked at C:\Users\___\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_pbr-0.16.1\src\material.rs:1023:79:
called `Option::unwrap()` on a `None` value
Encountered a panic in system `bevy_pbr::prepass::specialize_prepass_material_meshes<bevy_pbr::extended_material::ExtendedMaterial<bevy_pbr::pbr_material::StandardMaterial, project::assets::shaders::terrain_colored_tiles_shader::TerrainColoredTilesShader>>`!
Encountered a panic in system `bevy_pbr::material::specialize_material_meshes<bevy_pbr::extended_material::ExtendedMaterial<bevy_pbr::pbr_material::StandardMaterial, project::assets::shaders::terrain_colored_tiles_shader::TerrainColoredTilesShader>>`!

thread 'Compute Task Pool (0)' panicked at C:\Users\___\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_pbr-0.16.1\src\render\light.rs:1897:84:
called `Option::unwrap()` on a `None` value
Encountered a panic in system `bevy_pbr::render::light::specialize_shadows<bevy_pbr::extended_material::ExtendedMaterial<bevy_pbr::pbr_material::StandardMaterial, project::assets::shaders::terrain_colored_tiles_shader::TerrainColoredTilesShader>>`!
2025-07-08T15:07:01.764144Z  WARN bevy_render::diagnostic::internal: Failed to download render statistics buffer: Error occurred when trying to async map a buffer
2025-07-08T15:07:01.764259Z  WARN bevy_render::diagnostic::internal: Failed to download render statistics buffer: Error occurred when trying to async map a buffer
2025-07-08T15:07:01.764318Z  WARN bevy_render::diagnostic::internal: Failed to download render statistics buffer: Error occurred when trying to async map a buffer
2025-07-08T15:07:01.764388Z  WARN bevy_render::diagnostic::internal: Failed to download render statistics buffer: Error occurred when trying to async map a buffer

Additional information

Both are simple shaders with a small uniform:

#[derive(Asset, AsBindGroup, Reflect, Debug, Clone)]
pub struct TerrainShader {
    #[uniform(100)]
    pub erosion_start: f32,
    #[uniform(100)]
    pub erosion_max: f32,
}

#[derive(Asset, AsBindGroup, Reflect, Debug, Clone)]
pub struct TerrainColoredTilesShader {
    #[uniform(101)]
    size_of_tile: f32
}

I try to overwrite the material like:

for surface in surfaces.iter() {
    let mut entity_commands = commands.entity(surface);
    // materials is a resource with MeshMaterial3d handles
    entity_commands.insert(materials.terrain_colored_tiles.clone())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-BugAn unexpected or incorrect behaviorS-Needs-TriageThis issue needs to be labelled

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions