| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use std::f32::consts::*; |
| 5 |
|
| 6 |
use bevy::{math::ops, mesh::skinning::SkinnedMesh, prelude::*}; |
| 7 |
|
| 8 |
fn main() { |
| 9 |
App::new() |
| 10 |
.add_plugins(DefaultPlugins) |
| 11 |
.insert_resource(GlobalAmbientLight { |
| 12 |
brightness: 750.0, |
| 13 |
..default() |
| 14 |
}) |
| 15 |
.add_systems(Startup, setup) |
| 16 |
.add_systems(Update, joint_animation) |
| 17 |
.run(); |
| 18 |
} |
| 19 |
|
| 20 |
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { |
| 21 |
|
| 22 |
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 |
|
| 28 |
commands.spawn(WorldAssetRoot(asset_server.load( |
| 29 |
GltfAssetLabel::Scene(0).from_asset("models/SimpleSkin/SimpleSkin.gltf"), |
| 30 |
))); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
fn 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 |
|
| 52 |
for child_of in &children { |
| 53 |
|
| 54 |
let mesh_node_entity = child_of.parent(); |
| 55 |
|
| 56 |
let mesh_node_parent = parents.get(mesh_node_entity).unwrap(); |
| 57 |
|
| 58 |
|
| 59 |
let first_joint_entity = mesh_node_parent[1]; |
| 60 |
|
| 61 |
let first_joint_children = parents.get(first_joint_entity).unwrap(); |
| 62 |
|
| 63 |
|
| 64 |
let second_joint_entity = first_joint_children[0]; |
| 65 |
|
| 66 |
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 |
} |