mbrea-c / bevy_animation_graph

Animation graphs in Bevy!
Apache License 2.0
97 stars 5 forks source link

Animated Scene Assets #5

Closed mbrea-c closed 9 months ago

mbrea-c commented 9 months ago

Objective

This plugin is currently not very convenient to use: the target scene needs to be added, a system must then find the bevy::animation::AnimationPlayer in the instantiated scene, replace it with an AnimationGraphPlayer and finally add the desired animation graph to it.

This would be better handled by having an asset specifying the scene to be loaded together with the path (using entity Names) of the AnimationPlayer to be replaced and the asset path of the animation graph that should be played.

Solution

Provide AnimatedScene assets and an appropriate asset loader. Animated scenes are defined in .animscn.ron asset files as follows:

(
    source: "models/character_rigged.gltf#Scene0",
    path_to_player: ["Main Controller"],
    animation_graph: "animation_graphs/locomotion.animgraph.ron",
)

where

We can now simply instantiate an AnimatedSceneBundle with the given AnimatedScene handle, just like we would do with a regular scene:

    //...
    commands.spawn(AnimatedSceneBundle {
        animated_scene: asset_server.load("animated_scenes/character.animscn.ron"),
        ..default()
    });
    //...

Once the animated scene is finished successfully spawning, an AnimatedSceneInstance component will be added to it. For convenience, this component contains the entity id of the child containing the AnimationGraphPlayer, in case the user decides to manually set some animation graph parameters. If the animated scene spawning fails, (e.g. because the given path_to_player is incorrect), an error will be printed and the AnimatedSceneFailed component will be added instead.