bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.22k stars 3.57k forks source link

Changes to textures does not propagate to materials #15595

Open JoNil opened 1 month ago

JoNil commented 1 month ago

Bevy version

0.14.2

What you did

I have a system that updates a texture from the cpu:

pub fn update_visibility_texture(
    visibility_grid: Res<VisibilityGrid>,
    visiblity_texture: Res<VisibilityTexture>,
    mut images: ResMut<Assets<Image>>,
) {
    let handle = visiblity_texture.handle.clone();

    if let Some(image) = images.get_mut(&handle) {
        image.data.copy_from_slice(&visibility_grid.grid[..]);
    }
}

What went wrong

I expect the new texture data to be used when rendering. By any material that uses the same texture handle.

Instead the old texture is used when rendering.

Additional information

I have a workaround where i manually cause the materials that use the texture to be marked as changed in the asset system.

pub fn update_materials(
    mut ground: ResMut<Assets<ExtendedMaterial<StandardMaterial, GroundMaterialExtension>>>,
    mut trees: ResMut<Assets<ExtendedMaterial<StandardMaterial, TreeMaterialExtension>>>,
    mut grass: ResMut<Assets<GrassMaterial>>,
) {
    for _ in ground.iter_mut() {}
    for _ in grass.iter_mut() {}
    for _ in trees.iter_mut() {}
}

My guess is that there is noting that connects the material asset to the texture assets that it depends on. If this is by design then feel free to close this !

kristoff3r commented 1 month ago

This is a known / general issue described in #5069 (and other linked issues). It would be awesome to have a general solution for it.