Text2dShadow sometimes renders in front of its text

Open
#26,706 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
game-dev

Research direction

Start by reproducing the provided example in the Bevy 2D text rendering pipeline, focusing on Text2dShadow, AlphaMode2d, and the RenderSystems::Queue ordering described in the workaround. Locate where shadow and text draw commands are queued and compare their ordering when an unrelated mesh changes visibility. Done means the reproduction consistently renders the shadow behind the text across the affected platform or build configurations without relying on the workaround.

Written by the indexing model from the issue text.

Description

C-Bug S-Needs-Triage

Bevy version and features

  • Bevy 0.20.1
  • Bevy 5c485c424 (latest main)

Default features

What you did

Repeatedly include/exclude another (unrelated) mesh from rendering. Visibility::Hidden works, but for some reason moving it out of view produces more noticeable effects.

use bevy::{prelude::*, sprite::Text2dShadow, sprite_render::AlphaMode2d};

fn main() {
    App::new()
        .insert_resource(ClearColor(Color::WHITE))
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, startup)
        .add_systems(Update, update)
        .run();
}

fn startup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    commands.spawn(Camera2d);

    commands.spawn((
        Text2d::new("Red text with blue shadow"),
        TextColor(Color::srgba(1.0, 0.0, 0.0, 1.0)),
        TextFont::from_font_size(64.0),
        Text2dShadow {
            color: Color::srgba(0.0, 0.0, 1.0, 1.0),
            offset: Vec2::ZERO,
        },
    ));

    commands.spawn((
        Mesh2d(meshes.add(Rectangle::new(64.0, 64.0))),
        MeshMaterial2d(materials.add(ColorMaterial {
            color: Color::srgba(0.9, 0.9, 0.9, 1.0),
            alpha_mode: AlphaMode2d::Blend, // this matters
            ..Default::default()
        })),
        Transform::from_xyz(0.0, 96.0, 0.0),
    ));
}

fn update(time: Res<Time>, mut mesh_transform: Single<&mut Transform, With<Mesh2d>>) {
    **mesh_transform = if (time.elapsed().as_millis() / 250) % 2 == 0 {
        // Move the rectangle out of frame.
        // `Visibility::Hidden` also works but seems to only flicker briefly.
        Transform::from_xyz(0.0, 99999.0, 0.0)
    } else {
        Transform::from_xyz(0.0, 96.0, 0.0)
    };
}

This seems to be systems-ordering dependent, so it may not always be easy to reproduce. If the above works as expected, perhaps try a web build.

What went wrong

The blue text shadow sometimes renders in front of the red text, meaning its drawn later.

Additional information

image (still image, in reality text alternates between red and blue)

Workaround:

impl Plugin for RenderingPlugin {
    fn build(&self, app: &mut App) {
        if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
            render_app.add_systems(
                Render,
                (|| {})
                    .in_set(RenderSystems::Queue)
                    .after(queue_material2d_meshes)
                    .before(queue_sprites),
            );
        }
    }
}
Dominant language
Rust
Stars
50k
Forks
5.1k
Avg merge
2d 20h
Merged PRs (30d)
129

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bevyengine/bevy

All issues in bevyengine/bevy

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.