Skip to content

Commit 158bf28

Browse files
committed
example improvements: better camera & comments
1 parent 579dfd5 commit 158bf28

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

examples/3d/load_gltf_animation.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ fn main() {
2626

2727
const ANIMATIONS: [(&str, Transform, f32); 3] = [
2828
(
29+
// Model being loaded
2930
"models/animated/AnimatedTriangle.gltf",
31+
// Position of the camera
3032
Transform {
3133
translation: const_vec3!([0.0, 0.0, 3.0]),
3234
rotation: const_quat!([0.0, 0.0, 0.0, 1.0]),
3335
scale: const_vec3!([1.0; 3]),
3436
},
37+
// Speed of the animation
3538
0.12,
3639
),
3740
(
@@ -46,8 +49,8 @@ const ANIMATIONS: [(&str, Transform, f32); 3] = [
4649
(
4750
"models/animated/animations.gltf",
4851
Transform {
49-
translation: const_vec3!([2.0, 10.0, 5.0]),
50-
rotation: const_quat!([-0.48, 0.16, 0.09, 0.85]),
52+
translation: const_vec3!([-10.0, 5.0, -3.0]),
53+
rotation: const_quat!([0.16, 0.69, 0.16, -0.69]),
5154
scale: const_vec3!([1.0; 3]),
5255
},
5356
2.5,
@@ -70,43 +73,54 @@ fn setup(
7073
asset_server: Res<AssetServer>,
7174
mut scene_spawner: ResMut<SceneSpawner>,
7275
) {
76+
// Insert a resource with the current scene information
7377
commands.insert_resource(CurrentScene {
78+
// Its instance id, to be able to check that it's loaded
7479
instance_id: scene_spawner.spawn(asset_server.load(&format!("{}#Scene0", ANIMATIONS[0].0))),
80+
// The handle to the first animation
7581
animation: asset_server.load(&format!("{}#Animation0", ANIMATIONS[0].0)),
82+
// The animation speed modifier
7683
speed: ANIMATIONS[0].2,
7784
});
7885

86+
// Add a camera
7987
commands.spawn_bundle(PerspectiveCameraBundle {
8088
transform: ANIMATIONS[0].1,
8189
..Default::default()
8290
});
8391

92+
// Add a directional light
8493
commands.spawn_bundle(DirectionalLightBundle::default());
8594
}
8695

96+
// Switch the scene to the next one every 10 seconds
8797
fn switch_scene(
8898
mut commands: Commands,
8999
scene_root: Query<Entity, (Without<Camera>, Without<DirectionalLight>, Without<Parent>)>,
90-
camera: Query<Entity, With<Camera>>,
100+
mut camera: Query<&mut Transform, With<Camera>>,
91101
mut current: Local<usize>,
92102
mut current_scene: ResMut<CurrentScene>,
93103
asset_server: Res<AssetServer>,
94104
mut scene_spawner: ResMut<SceneSpawner>,
95105
) {
96106
*current = (*current + 1) % ANIMATIONS.len();
107+
108+
// Despawn the existing scene, then start loading the next one
97109
commands.entity(scene_root.single()).despawn_recursive();
98110
current_scene.instance_id =
99111
scene_spawner.spawn(asset_server.load(&format!("{}#Scene0", ANIMATIONS[*current].0)));
100112
current_scene.animation = asset_server.load(&format!("{}#Animation0", ANIMATIONS[*current].0));
101113
current_scene.speed = ANIMATIONS[*current].2;
102-
commands.entity(camera.single()).despawn_recursive();
103-
commands.spawn_bundle(PerspectiveCameraBundle {
104-
transform: ANIMATIONS[*current].1,
105-
..Default::default()
106-
});
114+
115+
// Update the camera position
116+
*camera.single_mut() = ANIMATIONS[*current].1;
117+
118+
// Reset the current animation
107119
commands.remove_resource::<CurrentAnimation>();
108120
}
109121

122+
// Setup the scene for animation once it is loaded, by adding the animation to a resource with
123+
// the start time.
110124
fn setup_scene_once_loaded(
111125
mut commands: Commands,
112126
scene_spawner: Res<SceneSpawner>,
@@ -115,9 +129,11 @@ fn setup_scene_once_loaded(
115129
mut done: Local<bool>,
116130
animations: Res<Assets<GltfAnimation>>,
117131
) {
132+
// If the current scene resource has changed, start waiting for it to be loaded
118133
if current_scene.is_changed() {
119134
*done = false;
120135
}
136+
// Once the scene and the animation are loaded, start the animation
121137
if !*done && scene_spawner.instance_is_ready(current_scene.instance_id) {
122138
if let Some(animation) = animations.get(&current_scene.animation) {
123139
*done = true;
@@ -129,6 +145,8 @@ fn setup_scene_once_loaded(
129145
}
130146
}
131147

148+
// This animation driver is not made to work in the general case. It will work with only one
149+
// animation per scene, and will ignore the specified interpolation method to only do linear.
132150
fn gltf_animation_driver(
133151
mut animated: Query<(&mut Transform, &GltfAnimatedNode)>,
134152
current_animation: Option<Res<CurrentAnimation>>,

0 commit comments

Comments
 (0)