bevyengine / bevyengine/bevy

Some Sprites don't render when World contains entity with Sprite and Mesh2d

Open
#19,459 14 comments 1 reaction 0 assignees View on GitHub
A-Rendering C-Bug S-Needs-Investigation
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## Introduction

First off, this is my first issue here, so I hope I'm not doing something obviously wrong and silly. I greatly appreciate the work done on bevy and love working with it.

This issue is a bit niche and hard to describe, so I'll do my best to make it reproducible and clear.

## Bevy version

0.16.1 and 0.16.0

## Relevant system information

**Cargo Version**
```
cargo --version
cargo 1.87.0 (99624be96 2025-05-06)
```

**Operating System**
`MacOS Seq15.5 (24F74)`

**Adaptor Info**
```
`AdapterInfo { name: "Apple M1", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Metal }`
```

## What you did

This is the smallest code sample I've come up with that reproduces the problem.
The code Spawns a camera, and 3 entities.
A Marker entity, containing a sprite that is used to mark the last hovered entity.
An entity with a sprite that can be hovered (by the mouse).
An entity with a sprite AND a Mesh2d that can be hovered.

```rs
use bevy::color::palettes::basic::RED;
use bevy::prelude::*;

/// Component added to the entity used to show
#[derive(Component)]
struct HoverIndicator;

fn main() {
let mut app = App::new();
app.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()));
app.add_systems(Startup, setup);
app.run();
}

fn setup(
mut commands: Commands,
mut meshes: ResMut>,
mut materials: ResMut>,
asset_server: Res,
) {
commands.spawn((
Camera2d,
Projection::Orthographic(OrthographicProjection {
scale: 0.5,
..OrthographicProjection::default_2d()
}),
Transform::from_xyz(0.0, 0.0, 0.0),
));

// 1. just sprite
commands
.spawn((
Transform::from_xyz(64.0, 0.0, 0.),
Name::new("Sprite"),
Sprite {
image: asset_server.load("Button_Blue.png"),
..default()
},
Pickable::default(),
))
.observe(on_pointer_over)
.with_children(|parent| {
// This entity gets moved to the entity last hovered by the mouse
parent.spawn((
HoverIndicator,
Sprite {
image: asset_server.load("02.png"),
..Default::default()
},
Transform::from_xyz(0.0, 0.0, 1.0),
));
});

// 2. mesh and sprite
commands
.spawn((
Transform::from_xyz(128.0, 0.0, 0.),
Name::new("Mesh and Sprite"),
Sprite {
image: asset_server.load("Button_Blue.png"),
..default()
},
Mesh2d(meshes.add(Rectangle::new(64., 64.))),
MeshMaterial2d(materials.add(ColorMaterial::from_color(RED))),
Pickable::default(),
))
.observe(on_pointer_over);
}

fn on_pointer_over(
trigger: Trigger>,
mut commands: Commands,
hover_entity: Single>,
) {
trace!("Pointer over selectable");
commands.entity(trigger.target).add_child(*hover_entity);
}
```

## What went wrong

When I hover the entity with a sprite, the `HoverIndicator` entity disappears and is no longer rendered. If I then hover the entity with both mesh and sprite, the `HoverIndicator` is rendered and the hover functionality works fully (I can hover over the entity with the sprite and it renders now).

If I don't spawn any entities with both `Mesh2d` and `Sprite`, the `HoverIndicator` works as intended.

## Additional information

Gif showing the `HoverIndicator` disappearing.

![Image](https://github.com/user-attachments/assets/c5926eb7-8d95-4dbf-97ae-6e8741a1214c)
### Other notable effects

- If you set the `transform.translate.z` of the `HoverIndicator` entity to `0`, it will continue to render sometimes.
- if you spawn more sprites, one will not render (like I describe in this issue) and all the others will render normally (this is a potential workaround).
- If the entities with both `Sprite` + `Mesh2d` are spawned later (during an `Update` schedule for example), then the issue does not arise.

### My Theory

The Mesh2d + Sprite + system is interfering with rendering of the `HoverIndicator` sprite. I'm guessing some kind of conflict in the rendering order is what is causing sprites not to render.

## Asset folder

[assets.zip](https://github.com/user-attachments/files/20542325/assets.zip)

In case you are wondering, the assets come from [https://pixelfrog-assets.itch.io/tiny-swords](https://pixelfrog-assets.itch.io/tiny-swords).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.