Multirious / bevy_tween

Flexible tweening plugin library for Bevy.
Apache License 2.0
87 stars 2 forks source link

color_material and color_material_to don't work in 0.5 #38

Closed guyguy2001 closed 1 month ago

guyguy2001 commented 1 month ago

I'm trying to use bevy_tween 0.5 with the color_material function, and it doesn't seem to animate the color. This also happened to me with color_material_to, although it isn't in the repro.

Reproduction

https://github.com/Multirious/bevy_tween/assets/6932278/c39847b3-137f-4d28-a1cf-9ad9e7711335 https://github.com/guyguy2001/bevy-tween-reproduction (The first commit is a working version copied from your follow.rs example, with 2 egui lines commented out; the second commit is me changing it from sprite to mesh2d and color materials)

The difference from the example: image image (I don't know what's up with the weird interpolate::color_material import; moving it to the correct place doesn't fix it)

Information

Windows 11, nightly-x86_64-pc-windows-msvc, rustc 1.79.0-nightly

> cargo tree
bevy_tween_repro v0.1.0 (C:\Users\User\Desktop\projects\reproductions\bevy_tween_repro)
├── bevy v0.13.2
...
└── bevy_tween v0.5.0
    ├── bevy v0.13.2 (*)
    └── bevy_time_runner v0.1.4
        ├── bevy_app v0.13.2 (*)
        ├── bevy_ecs v0.13.2 (*)
        ├── bevy_eventlistener v0.7.0
        │   ├── bevy_app v0.13.2 (*)
        │   ├── bevy_ecs v0.13.2 (*)
        │   ├── bevy_eventlistener_derive v0.7.0 (proc-macro)
        │   │   ├── proc-macro2 v1.0.86 (*)
        │   │   ├── quote v1.0.36 (*)
        │   │   └── syn v2.0.68 (*)
        │   ├── bevy_hierarchy v0.13.2 (*)
        │   └── bevy_utils v0.13.2 (*)
        ├── bevy_hierarchy v0.13.2 (*)
        ├── bevy_reflect v0.13.2 (*)
        └── bevy_time v0.13.2 (*)
        [build-dependencies]
        └── rustc_version v0.4.0
            └── semver v1.0.23
    [build-dependencies]
    └── rustc_version v0.4.0 (*)
guyguy2001 commented 1 month ago

If I'm already here, I'll add that I was confused by the color_material_to name.

I started with sprite_color_to based on the example, and when I tried to use the color material one, I just searched for material_color_to, and didn't realize I was supposed to bring the "color" to the front.

I understand now that the role of the "color" in each name is different (in sprite it's the sprite's color, and in material it's the colors material), but that is still confusing if you don't come from this specific angle.

Multirious commented 1 month ago
jeb.with(color_material(...));

jeb is TargetComponent ColorMaterial interpolator is currently made to only interpolate using the target TargetAsset. It seems like you're trying to interpolate by the Handle<ColorMaterial> component which is not implemented (Unfortunately there is no compiler error for that now). You can create custom interpolator for that currently (Might also need custom system, @ me if you need any help). This is an interesting case I've never thought of before, it could be built-in.

For the naming I agree that it can be confusing 😅, I will add doc alias for discovery. When the function is created, I thought the name material_color would be conflicting with other material types which might've the same property so that awkward name has been chosen. If you think otherwise, please tell me so!

guyguy2001 commented 1 month ago

I can't really say I understand the name collision on material_color - I think I don't understand materials enough now to see what you mean.

As for my issue - I have this code now, which doesn't work for me: (Since this is probably a simple problem with my code, I dropped the reproduction code in favour of my real code)

pub fn spawn_sword(
    parent: Entity,
    // TODO: Role, take from laser's code
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
    mut commands: Commands,
) {
    let starting_color = Color::WHITE.with_a(0.);

    let color_material = materials.add(starting_color);
    let material_backup = color_material.clone();

    let mut color_animation = color_material.into_target().state(starting_color);

    let size = 15.;

    commands.entity(parent).with_children(|builder| {
        // TODO: Improve the animation, make the sword only active for part of the animation
        let sword = builder
            .spawn((
                SwordAttack::default(),
                MaterialMesh2dBundle {
                    mesh: meshes.add(Circle::new(size)).into(),
                    material: material_backup,
                    ..Default::default()
                },
                AnimationTarget,
                Name::new("Sword animation"),
            ))
            .animation()
            .insert(sequence((
                tween(
                    secs(0.1),
                    EaseFunction::CircularOut,
                    color_animation.with(interpolate::sprite_color_to(starting_color.with_a(1.0))),
                ),
                tween(
                    secs(0.1),
                    EaseFunction::CircularIn,
                    color_animation.with(interpolate::sprite_color_to(starting_color.with_a(0.0))),
                ),
            )))
            .id();
    });
}

color_animation is TargetState<TargetAsset<ColorMaterial>, Color>, which sounds promising to me.

The 2 suspicious things are:

Multirious commented 1 month ago

material_color collision naming is probably my overthinking. Though it'd be kept in this way until the overhaul.

Now for your issue.

guyguy2001 commented 1 month ago

This seems to work, thanks!

I'm surprised that color_material_to doesn't give a compilation error, but other than that I think it makes sense to me

Multirious commented 1 month ago

Closing this as resolved