Sprite keeps drawing an image's old texture after the image is replaced with one of a different size
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
Research direction
Start by reading bevy_render/src/texture/gpu_image.rs around GpuImage::prepare_asset, then inspect bevy_sprite_render/src/sprite_mesh/mod.rs and add_material to see how the sprite material cache uses the image AssetId. The fix should ensure that replacing an image with a differently sized texture refreshes the sprite bind group and stops it using the old texture view. Verify that size changes and later same-size pixel updates are rendered correctly, using the issue's reproduction as the starting point.
Written by the indexing model from the issue text.
Description
Bevy version and features
- 0.20.1
- Default features
[Optional] Relevant system information
cargo 1.102.0-nightly (ede6cefea 2026-11-11)- Fedora Linux 44 (Workstation Edition), kernel 7.3.4
AdapterInfo { name: "NVIDIA GeForce RTX 2080", vendor: 4318, device: 7810, device_type: DiscreteGpu, device_pci_bus_id: "0000:01:00.0", driver: "NVIDIA", driver_info: "620.70.06", backend: Vulkan, subgroup_min_size: 32, subgroup_max_size: 32, transient_saves_memory: Some(false), limit_bucket: None }
What you did
Changed a sprite's image in place with Assets<Image>::get_mut, replacing it with an image of a different size, without changing the Sprite itself:
use bevy::asset::RenderAssetUsages;
use bevy::prelude::*;
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, replace_image)
.run();
}
#[derive(Resource)]
struct SpriteImage(Handle<Image>);
fn solid_image(size: u32, color: [u8; 4]) -> Image {
Image::new_fill(
Extent3d {
width: size,
height: size,
depth_or_array_layers: 1,
},
TextureDimension::D2,
&color,
TextureFormat::Rgba8UnormSrgb,
RenderAssetUsages::default(),
)
}
fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
commands.spawn(Camera2d);
let image = images.add(solid_image(100, [255, 0, 0, 255]));
commands.spawn(Sprite::from_image(image.clone()));
commands.insert_resource(SpriteImage(image));
}
/// Once a second, replaces the image: red 100×100, then green 200×200,
/// then blue 200×200, then red 100×100 again, and so on.
fn replace_image(
time: Res<Time>,
mut timer: Local<Option<Timer>>,
mut step: Local<u32>,
image: Res<SpriteImage>,
mut images: ResMut<Assets<Image>>,
) {
let timer = timer.get_or_insert_with(|| Timer::from_seconds(1.0, TimerMode::Repeating));
if !timer.tick(time.delta()).just_finished() {
return;
}
*step += 1;
let (size, color) = match *step % 3 {
0 => (100, [255, 0, 0, 255]),
1 => (200, [0, 255, 0, 255]),
_ => (200, [0, 0, 255, 255]),
};
*images.get_mut(&image.0).unwrap() = solid_image(size, color);
}
What went wrong
- Expected: the sprite cycles through a red 100×100 square, a green 200×200 square and a blue 200×200 square, one second each.
- Actual: the sprite stays a red 100×100 square.
The same happens with any change that gives the image a new GPU texture. A change that keeps the size (and the rest of the texture descriptor) shows up, because the new pixels are written into the existing texture. Once the size has changed, though, the sprite never shows the image again, not even later same-size changes, because those are written into the new texture.
Additional information
Where it goes wrong
GpuImage::prepare_asset(bevy_render/src/texture/gpu_image.rs) writes the new pixels into the previous texture and keeps its view when the texture descriptor is unchanged. Otherwise it creates a new texture and view. It also creates a new sampler on every prepare when the image has anImageSampler::Descriptor.- In 0.20, a
Spriteis drawn with aMesh2dand aSpriteMeshMaterial.add_material(bevy_sprite_render/src/sprite_mesh/mod.rs) takes that material from a cache keyed by theSprite's fields, including the image'sAssetId, and only runs when theSpriteorAnchorchanges. The material's bind group holds the image's texture view and sampler from when the material was prepared. - Nothing prepares the material again when its image's
GpuImagegets a new texture. The bind group keeps the old texture view, so the sprite keeps drawing the old texture.
The legacy sprite path did handle this: prepare_sprite_image_bind_groups (bevy_sprite_render/src/render/mod.rs) drops its cached bind group for an image on AssetEvent::Modified.
Workarounds
- Add the changed image as a new asset (
Assets::add) instead of replacing it in place, when its size changes. The sprite then gets a newAssetId, and so a new material. - Changing a
Spritefield that is part of the material cache key, such ascolor, also gives the sprite a new material.
Related issues
- #26398: the
TextureAtlasLayoutis copied into theSpriteMeshMaterialwhen the material is made, so later changes to the layout asset aren't shown either. TheNOTEonadd_materialmentions this for atlas layouts. - #17854 (Bevy 0.15): sprites disappear after
Assets<Image>::get_mut. That was the legacy sprite path, with a different symptom.
- Dominant language
- Rust
- Stars
- 50k
- Forks
- 5.1k
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 129
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from bevyengine/bevy
-
C-Bug S-Needs-Triage
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
bevyengine/bevy#26718 ·
Maintainers usually reply within 2 days
-
C-Bug S-Needs-Triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
bevyengine/bevy#26689 ·
Maintainers usually reply within 2 days
-
Volumetric fog: a `FogVolume` with `Visibility::Hidden` is still renderedPossibly taken @adityawrk claimed this 2 days ago. OpenC-Bug S-Needs-Triage
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
bevyengine/bevy#26688 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
bevyengine/bevy#26684 ·
Maintainers usually reply within 2 days
-
Create a bevy_fuzzy examplePossibly taken @Tatsuya0330 claimed this 1 day ago. OpenA-UI C-Examples C-Feature D-Straightforward S-Needs-Triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
bevyengine/bevy#26676 ·
Maintainers usually reply within 2 days
Similar issues
-
area:ui effort:medium enhancement P7 source:agent
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
marketing
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
documentation priority: low tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
nledford/Ladderback#1823 ·
Maintainers usually reply within 1 day
-
psmux-resurrect: restoring a session with a numeric name puts its windows into the wrong session (`new-window -t 4` is read as window 4)Possibly taken @ealwanlee claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
psmux/psmux-plugins#47 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 87/100
Maintainers usually reply within 1 day