Support Warning
WebGPU is currently only supported on Chrome starting with version 113, and only on desktop. If they don't work on your configuration, you can check the WebGL2 examples
here.
use std::f32::consts::*;
use bevy::{pbr::AmbientLight, prelude::*, render::mesh::skinning::SkinnedMesh};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(AmbientLight {
brightness: 1.0,
..default()
})
.add_systems(Startup, setup)
.add_systems(Update, joint_animation)
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(SceneBundle {
scene: asset_server.load("models/SimpleSkin/SimpleSkin.gltf#Scene0"),
..default()
});
}
fn joint_animation(
time: Res<Time>,
parent_query: Query<&Parent, With<SkinnedMesh>>,
children_query: Query<&Children>,
mut transform_query: Query<&mut Transform>,
) {
for skinned_mesh_parent in &parent_query {
let mesh_node_entity = skinned_mesh_parent.get();
let mesh_node_children = children_query.get(mesh_node_entity).unwrap();
let first_joint_entity = mesh_node_children[1];
let first_joint_children = children_query.get(first_joint_entity).unwrap();
let second_joint_entity = first_joint_children[0];
let mut second_joint_transform = transform_query.get_mut(second_joint_entity).unwrap();
second_joint_transform.rotation =
Quat::from_rotation_z(FRAC_PI_2 * time.elapsed_seconds().sin());
}
}