1

Bevy v0.8 has several "displayable-object" types, Sprite/SpriteBundle, SpriteSheetBundle, and MaterialMeshBundle. I would like to change the transparency (Alpha-channel, stored on Color Components) of, well, all of them - if possible.

The Answer Changing Sprite Color, addresses changing the Color of an object (where Color has the Alpha-channel) , but it only works for things that have a material associated. In Bevy v0.8, SpriteBundles don't have a material (they did in 0.5).

So is it possible to change the transparency of other objects, that don't have materials? (And if so, how?)

John C
  • 6,285
  • 12
  • 45
  • 69

1 Answers1

1

Fortunately, it is doable (and thanks to people in the Bevy-Discord channel for suggestions). There are two elements to the solution - first, all of the items I named, do have a Color component (although finding it can be tricky). Even a SpriteSheet, which is an Atlas of bitmaps, has one - presumably just for this usage. Second, the AnyOf filter, which gets any of the requested components, as a tuple of Options, each of which is either an object, or None.

// Create some disparate objects
pub fn do_add_sprites(mut commands: Commands, asset_server: Res<AssetServer>,
    mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<ColorMaterial>>,
    mut texture_atlases: ResMut<Assets<TextureAtlas>>,
) {
    commands.spawn_bundle(SpriteSheetBundle {
            transform: Transform::from_xyz(-300.0, -200.0, 1.0),
            texture_atlas: texture_atlas_handle2, ..default()
        })
        .insert(IsMousing { is_dragging: false, is_hovering: false });
    commands.spawn_bundle(MaterialMesh2dBundle {
            transform: Transform::from_translation(Vec3::new(500.0, -200.0, 1.0)),
            mesh: meshes.add(shape::RegularPolygon::new(50., 6).into()).into(),
            material: materials.add(ColorMaterial::from(Color::BLUE)),
            ..default()})
        .insert(IsMousing { is_dragging: false, is_hovering: false });
}

// Code elsewhere that says: if cursor over object, set is_hovering flag
pub fn apply_any_hovers(mut materials: ResMut<Assets<ColorMaterial>>,
    mut any_hovercraft: Query<(Entity, &IsMousing,
    AnyOf<(&mut TextureAtlasSprite, &Handle<ColorMaterial>)>,)>,
) {
    for (_entity_id, mouser_data, some_object) in any_hovercraft.iter_mut() {
        if mouser_data.is_hovering {
            if some_object.0.is_some() {
                let some_color = &mut some_object.0.unwrap().color;
                some_color.set_a(0.5);
            }
            if some_object.1.is_some() {
                let some_handle = some_object.1.unwrap();
                let some_color = &mut materials.get_mut(some_handle).unwrap().color;
                some_color.set_a(0.5);
            }
        } else {
            if some_object.0.is_some() {
                let some_color = &mut some_object.0.unwrap().color;
                some_color.set_a(1.0);
            }
            if some_object.1.is_some() {
                let some_handle = some_object.1.unwrap();
                let some_color = &mut materials.get_mut(some_handle).unwrap().color;
                some_color.set_a(1.0);
            }
        }
    }
}
John C
  • 6,285
  • 12
  • 45
  • 69