|
1 |
| -//! Controls morph targets in a loaded scene. |
| 1 | +//! Play an animation with morph targets. |
2 | 2 | //!
|
3 |
| -//! Illustrates: |
4 |
| -//! |
5 |
| -//! - How to access and modify individual morph target weights. |
6 |
| -//! See the `update_weights` system for details. |
7 |
| -//! - How to read morph target names in `name_morphs`. |
8 |
| -//! - How to play morph target animations in `setup_animations`. |
| 3 | +//! Also illustrates how to read morph target names in `name_morphs`. |
9 | 4 |
|
10 |
| -use bevy::prelude::*; |
| 5 | +use bevy::{prelude::*, scene::SceneInstanceReady}; |
11 | 6 | use std::f32::consts::PI;
|
12 | 7 |
|
| 8 | +const GLTF_PATH: &str = "models/animated/MorphStressTest.gltf"; |
| 9 | + |
13 | 10 | fn main() {
|
14 | 11 | App::new()
|
15 |
| - .add_plugins(DefaultPlugins.set(WindowPlugin { |
16 |
| - primary_window: Some(Window { |
17 |
| - title: "morph targets".to_string(), |
18 |
| - ..default() |
19 |
| - }), |
20 |
| - ..default() |
21 |
| - })) |
| 12 | + .add_plugins(DefaultPlugins) |
22 | 13 | .insert_resource(AmbientLight {
|
23 | 14 | brightness: 150.0,
|
24 | 15 | ..default()
|
25 | 16 | })
|
26 | 17 | .add_systems(Startup, setup)
|
27 |
| - .add_systems(Update, (name_morphs, setup_animations)) |
| 18 | + .add_systems(Update, name_morphs) |
28 | 19 | .run();
|
29 | 20 | }
|
30 | 21 |
|
31 |
| -#[derive(Resource)] |
32 |
| -struct MorphData { |
33 |
| - the_wave: Handle<AnimationClip>, |
34 |
| - mesh: Handle<Mesh>, |
| 22 | +#[derive(Component)] |
| 23 | +struct AnimationToPlay { |
| 24 | + graph_handle: Handle<AnimationGraph>, |
| 25 | + index: AnimationNodeIndex, |
35 | 26 | }
|
36 | 27 |
|
37 |
| -fn setup(asset_server: Res<AssetServer>, mut commands: Commands) { |
38 |
| - commands.insert_resource(MorphData { |
39 |
| - the_wave: asset_server |
40 |
| - .load(GltfAssetLabel::Animation(2).from_asset("models/animated/MorphStressTest.gltf")), |
41 |
| - mesh: asset_server.load( |
42 |
| - GltfAssetLabel::Primitive { |
43 |
| - mesh: 0, |
44 |
| - primitive: 0, |
45 |
| - } |
46 |
| - .from_asset("models/animated/MorphStressTest.gltf"), |
47 |
| - ), |
48 |
| - }); |
49 |
| - commands.spawn(SceneRoot(asset_server.load( |
50 |
| - GltfAssetLabel::Scene(0).from_asset("models/animated/MorphStressTest.gltf"), |
51 |
| - ))); |
| 28 | +fn setup( |
| 29 | + mut commands: Commands, |
| 30 | + asset_server: Res<AssetServer>, |
| 31 | + mut graphs: ResMut<Assets<AnimationGraph>>, |
| 32 | +) { |
| 33 | + let (graph, index) = AnimationGraph::from_clip( |
| 34 | + asset_server.load(GltfAssetLabel::Animation(2).from_asset(GLTF_PATH)), |
| 35 | + ); |
| 36 | + |
| 37 | + commands |
| 38 | + .spawn(( |
| 39 | + AnimationToPlay { |
| 40 | + graph_handle: graphs.add(graph), |
| 41 | + index, |
| 42 | + }, |
| 43 | + SceneRoot(asset_server.load(GltfAssetLabel::Scene(0).from_asset(GLTF_PATH))), |
| 44 | + )) |
| 45 | + .observe(play_animation_when_ready); |
| 46 | + |
52 | 47 | commands.spawn((
|
53 | 48 | DirectionalLight::default(),
|
54 | 49 | Transform::from_rotation(Quat::from_rotation_z(PI / 2.0)),
|
55 | 50 | ));
|
| 51 | + |
56 | 52 | commands.spawn((
|
57 | 53 | Camera3d::default(),
|
58 | 54 | Transform::from_xyz(3.0, 2.1, 10.2).looking_at(Vec3::ZERO, Vec3::Y),
|
59 | 55 | ));
|
60 | 56 | }
|
61 | 57 |
|
62 |
| -/// Plays an [`AnimationClip`] from the loaded [`Gltf`] on the [`AnimationPlayer`] created by the spawned scene. |
63 |
| -fn setup_animations( |
64 |
| - mut has_setup: Local<bool>, |
| 58 | +fn play_animation_when_ready( |
| 59 | + trigger: On<SceneInstanceReady>, |
65 | 60 | mut commands: Commands,
|
66 |
| - mut players: Query<(Entity, &Name, &mut AnimationPlayer)>, |
67 |
| - morph_data: Res<MorphData>, |
68 |
| - mut graphs: ResMut<Assets<AnimationGraph>>, |
| 61 | + children: Query<&Children>, |
| 62 | + animations_to_play: Query<&AnimationToPlay>, |
| 63 | + mut players: Query<&mut AnimationPlayer>, |
69 | 64 | ) {
|
70 |
| - if *has_setup { |
71 |
| - return; |
72 |
| - } |
73 |
| - for (entity, name, mut player) in &mut players { |
74 |
| - // The name of the entity in the GLTF scene containing the AnimationPlayer for our morph targets is "Main" |
75 |
| - if name.as_str() != "Main" { |
76 |
| - continue; |
77 |
| - } |
| 65 | + if let Ok(animation_to_play) = animations_to_play.get(trigger.target()) { |
| 66 | + for child in children.iter_descendants(trigger.target()) { |
| 67 | + if let Ok(mut player) = players.get_mut(child) { |
| 68 | + player.play(animation_to_play.index).repeat(); |
78 | 69 |
|
79 |
| - let (graph, animation) = AnimationGraph::from_clip(morph_data.the_wave.clone()); |
80 |
| - commands |
81 |
| - .entity(entity) |
82 |
| - .insert(AnimationGraphHandle(graphs.add(graph))); |
83 |
| - |
84 |
| - player.play(animation).repeat(); |
85 |
| - *has_setup = true; |
| 70 | + commands |
| 71 | + .entity(child) |
| 72 | + .insert(AnimationGraphHandle(animation_to_play.graph_handle.clone())); |
| 73 | + } |
| 74 | + } |
86 | 75 | }
|
87 | 76 | }
|
88 | 77 |
|
89 |
| -/// You can get the target names in their corresponding [`Mesh`]. |
90 |
| -/// They are in the order of the weights. |
| 78 | +/// Whenever a mesh asset is loaded, print the name of the asset and the names |
| 79 | +/// of its morph targets. |
91 | 80 | fn name_morphs(
|
92 |
| - mut has_printed: Local<bool>, |
93 |
| - morph_data: Res<MorphData>, |
| 81 | + asset_server: Res<AssetServer>, |
| 82 | + mut events: EventReader<AssetEvent<Mesh>>, |
94 | 83 | meshes: Res<Assets<Mesh>>,
|
95 | 84 | ) {
|
96 |
| - if *has_printed { |
97 |
| - return; |
98 |
| - } |
99 |
| - |
100 |
| - let Some(mesh) = meshes.get(&morph_data.mesh) else { |
101 |
| - return; |
102 |
| - }; |
103 |
| - let Some(names) = mesh.morph_target_names() else { |
104 |
| - return; |
105 |
| - }; |
| 85 | + for event in events.read() { |
| 86 | + if let AssetEvent::<Mesh>::Added { id } = event |
| 87 | + && let Some(path) = asset_server.get_path(*id) |
| 88 | + && let Some(mesh) = meshes.get(*id) |
| 89 | + && let Some(names) = mesh.morph_target_names() |
| 90 | + { |
| 91 | + info!("Morph target names for {path:?}:"); |
106 | 92 |
|
107 |
| - info!("Target names:"); |
108 |
| - for name in names { |
109 |
| - info!(" {name}"); |
| 93 | + for name in names { |
| 94 | + info!(" {name}"); |
| 95 | + } |
| 96 | + } |
110 | 97 | }
|
111 |
| - *has_printed = true; |
112 | 98 | }
|
0 commit comments