forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorph_targets.rs
More file actions
98 lines (86 loc) · 2.76 KB
/
morph_targets.rs
File metadata and controls
98 lines (86 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! Play an animation with morph targets.
//!
//! Also illustrates how to read morph target names in `name_morphs`.
use bevy::{prelude::*, scene::SceneInstanceReady};
use std::f32::consts::PI;
const GLTF_PATH: &str = "models/animated/MorphStressTest.gltf";
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(AmbientLight {
brightness: 150.0,
..default()
})
.add_systems(Startup, setup)
.add_systems(Update, name_morphs)
.run();
}
#[derive(Component)]
struct AnimationToPlay {
graph_handle: Handle<AnimationGraph>,
index: AnimationNodeIndex,
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut graphs: ResMut<Assets<AnimationGraph>>,
) {
let (graph, index) = AnimationGraph::from_clip(
asset_server.load(GltfAssetLabel::Animation(2).from_asset(GLTF_PATH)),
);
commands
.spawn((
AnimationToPlay {
graph_handle: graphs.add(graph),
index,
},
SceneRoot(asset_server.load(GltfAssetLabel::Scene(0).from_asset(GLTF_PATH))),
))
.observe(play_animation_when_ready);
commands.spawn((
DirectionalLight::default(),
Transform::from_rotation(Quat::from_rotation_z(PI / 2.0)),
));
commands.spawn((
Camera3d::default(),
Transform::from_xyz(3.0, 2.1, 10.2).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn play_animation_when_ready(
scene_ready: On<SceneInstanceReady>,
mut commands: Commands,
children: Query<&Children>,
animations_to_play: Query<&AnimationToPlay>,
mut players: Query<&mut AnimationPlayer>,
) {
if let Ok(animation_to_play) = animations_to_play.get(scene_ready.entity) {
for child in children.iter_descendants(scene_ready.entity) {
if let Ok(mut player) = players.get_mut(child) {
player.play(animation_to_play.index).repeat();
commands
.entity(child)
.insert(AnimationGraphHandle(animation_to_play.graph_handle.clone()));
}
}
}
}
/// Whenever a mesh asset is loaded, print the name of the asset and the names
/// of its morph targets.
fn name_morphs(
asset_server: Res<AssetServer>,
mut events: MessageReader<AssetEvent<Mesh>>,
meshes: Res<Assets<Mesh>>,
) {
for event in events.read() {
if let AssetEvent::<Mesh>::Added { id } = event
&& let Some(path) = asset_server.get_path(*id)
&& let Some(mesh) = meshes.get(*id)
&& let Some(names) = mesh.morph_target_names()
{
info!("Morph target names for {path:?}:");
for name in names {
info!(" {name}");
}
}
}
}