gltf_skinned_mesh/
gltf_skinned_mesh.rs1use std::f32::consts::*;
5
6use bevy::{math::ops, mesh::skinning::SkinnedMesh, prelude::*};
7
8fn main() {
9 App::new()
10 .add_plugins(DefaultPlugins)
11 .insert_resource(AmbientLight {
12 brightness: 750.0,
13 ..default()
14 })
15 .add_systems(Startup, setup)
16 .add_systems(Update, joint_animation)
17 .run();
18}
19
20fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
21 commands.spawn((
23 Camera3d::default(),
24 Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::new(0.0, 1.0, 0.0), Vec3::Y),
25 ));
26
27 commands.spawn(SceneRoot(asset_server.load(
29 GltfAssetLabel::Scene(0).from_asset("models/SimpleSkin/SimpleSkin.gltf"),
30 )));
31}
32
33fn joint_animation(
46 time: Res<Time>,
47 children: Query<&ChildOf, With<SkinnedMesh>>,
48 parents: Query<&Children>,
49 mut transform_query: Query<&mut Transform>,
50) {
51 for child_of in &children {
53 let mesh_node_entity = child_of.parent();
55 let mesh_node_parent = parents.get(mesh_node_entity).unwrap();
57
58 let first_joint_entity = mesh_node_parent[1];
60 let first_joint_children = parents.get(first_joint_entity).unwrap();
62
63 let second_joint_entity = first_joint_children[0];
65 let mut second_joint_transform = transform_query.get_mut(second_joint_entity).unwrap();
67
68 second_joint_transform.rotation =
69 Quat::from_rotation_z(FRAC_PI_2 * ops::sin(time.elapsed_secs()));
70 }
71}