bevy/examples/animation
Greeble b2f3248432
Make the animated_mesh example more intuitive (#17421)
# Objective

Make the `animated_mesh` example more intuitive and easier for the user
to extend.

# Solution

The `animated_mesh` example shows how to spawn a single mesh and play a
single animation. The original code is roughly:

1. In `setup_mesh_and_animation`, spawn an entity with a SceneRoot that
will load and spawn the mesh. Also record the animation to play as a
resource.
2. Use `play_animation_once_loaded` to detect when any animation players
are spawned, then play the animation from the resource.

When I used this example as a starting point for my own app, I hit a
wall when trying to spawn multiple meshes with different animations.
`play_animation_once_loaded` tells me an animation player spawned
somewhere, but how do I get from there to the right animation? The
entity it runs on is spawned by the scene so I can't attach any data to
it?

The new code takes a different approach. Instead of a global resource,
the animation is recorded as a component on the entity with the
SceneRoot. Instead of detecting animation players spawning wherever, an
observer is attached to that specific entity.

This feels more intuitive and localised, and I think most users will
work out how to get from there to different animations and meshes. The
downside is more lines of code, and the "find the animation players"
part still feels a bit magical and inefficient.

# Side Notes

- The solution was mostly stolen from
https://github.com/bevyengine/bevy/issues/14852#issuecomment-2481401769.
- The example still feels too complicated.
    - "Why do I have to make this graph to play one animation?"
- "Why can't I choose and play the animation in one step and avoid this
temporary component?"
    - I think this requires engine changes.
- I originally started on a separate example of multiple meshes
([branch](https://github.com/bevyengine/bevy/compare/main...greeble-dev:bevy:animated-mesh-multiple)).
- I decided that the user could probably work this out themselves from
the single animation example.
    - But maybe still worth following through.

# Testing

`cargo run --example animated_mesh`

---------

Co-authored-by: Rob Parrett <robparrett@gmail.com>
2025-01-20 21:12:06 +00:00
..
animated_mesh_control.rs Rename animated fox examples to better communicate their purpose (#17239) 2025-01-08 18:59:17 +00:00
animated_mesh_events.rs Rename animated fox examples to better communicate their purpose (#17239) 2025-01-08 18:59:17 +00:00
animated_mesh.rs Make the animated_mesh example more intuitive (#17421) 2025-01-20 21:12:06 +00:00
animated_transform.rs Introduce support for mixed lighting by allowing lights to opt out of contributing diffuse light to lightmapped objects. (#16761) 2024-12-16 23:48:33 +00:00
animated_ui.rs Relationships (non-fragmenting, one-to-many) (#17398) 2025-01-18 22:20:30 +00:00
animation_events.rs AnimationEvent -> Event and other improvements (#16440) 2024-11-22 00:16:04 +00:00
animation_graph.rs Introduce support for mixed lighting by allowing lights to opt out of contributing diffuse light to lightmapped objects. (#16761) 2024-12-16 23:48:33 +00:00
animation_masks.rs Relationships (non-fragmenting, one-to-many) (#17398) 2025-01-18 22:20:30 +00:00
color_animation.rs ✏️ Fix typos across bevy (#16702) 2024-12-08 01:18:39 +00:00
custom_skinned_mesh.rs Fix a few typos (#17292) 2025-01-10 22:48:30 +00:00
eased_motion.rs AnimatedField and Rework Evaluators (#16484) 2024-11-27 22:19:55 +00:00
easing_functions.rs Use normal constructors for EasingCurve, FunctionCurve, ConstantCurve (#16367) 2024-11-13 15:30:05 +00:00
gltf_skinned_mesh.rs aligning public apis of Time,Timer and Stopwatch (#15962) 2024-10-16 21:09:32 +00:00
morph_targets.rs Fix println in morph_targets example (#15851) 2024-10-11 15:35:22 +00:00