@@ -26,12 +26,15 @@ fn main() {
26
26
27
27
const ANIMATIONS : [ ( & str , Transform , f32 ) ; 3 ] = [
28
28
(
29
+ // Model being loaded
29
30
"models/animated/AnimatedTriangle.gltf" ,
31
+ // Position of the camera
30
32
Transform {
31
33
translation : const_vec3 ! ( [ 0.0 , 0.0 , 3.0 ] ) ,
32
34
rotation : const_quat ! ( [ 0.0 , 0.0 , 0.0 , 1.0 ] ) ,
33
35
scale : const_vec3 ! ( [ 1.0 ; 3 ] ) ,
34
36
} ,
37
+ // Speed of the animation
35
38
0.12 ,
36
39
) ,
37
40
(
@@ -46,8 +49,8 @@ const ANIMATIONS: [(&str, Transform, f32); 3] = [
46
49
(
47
50
"models/animated/animations.gltf" ,
48
51
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 ] ) ,
51
54
scale : const_vec3 ! ( [ 1.0 ; 3 ] ) ,
52
55
} ,
53
56
2.5 ,
@@ -70,43 +73,54 @@ fn setup(
70
73
asset_server : Res < AssetServer > ,
71
74
mut scene_spawner : ResMut < SceneSpawner > ,
72
75
) {
76
+ // Insert a resource with the current scene information
73
77
commands. insert_resource ( CurrentScene {
78
+ // Its instance id, to be able to check that it's loaded
74
79
instance_id : scene_spawner. spawn ( asset_server. load ( & format ! ( "{}#Scene0" , ANIMATIONS [ 0 ] . 0 ) ) ) ,
80
+ // The handle to the first animation
75
81
animation : asset_server. load ( & format ! ( "{}#Animation0" , ANIMATIONS [ 0 ] . 0 ) ) ,
82
+ // The animation speed modifier
76
83
speed : ANIMATIONS [ 0 ] . 2 ,
77
84
} ) ;
78
85
86
+ // Add a camera
79
87
commands. spawn_bundle ( PerspectiveCameraBundle {
80
88
transform : ANIMATIONS [ 0 ] . 1 ,
81
89
..Default :: default ( )
82
90
} ) ;
83
91
92
+ // Add a directional light
84
93
commands. spawn_bundle ( DirectionalLightBundle :: default ( ) ) ;
85
94
}
86
95
96
+ // Switch the scene to the next one every 10 seconds
87
97
fn switch_scene (
88
98
mut commands : Commands ,
89
99
scene_root : Query < Entity , ( Without < Camera > , Without < DirectionalLight > , Without < Parent > ) > ,
90
- camera : Query < Entity , With < Camera > > ,
100
+ mut camera : Query < & mut Transform , With < Camera > > ,
91
101
mut current : Local < usize > ,
92
102
mut current_scene : ResMut < CurrentScene > ,
93
103
asset_server : Res < AssetServer > ,
94
104
mut scene_spawner : ResMut < SceneSpawner > ,
95
105
) {
96
106
* current = ( * current + 1 ) % ANIMATIONS . len ( ) ;
107
+
108
+ // Despawn the existing scene, then start loading the next one
97
109
commands. entity ( scene_root. single ( ) ) . despawn_recursive ( ) ;
98
110
current_scene. instance_id =
99
111
scene_spawner. spawn ( asset_server. load ( & format ! ( "{}#Scene0" , ANIMATIONS [ * current] . 0 ) ) ) ;
100
112
current_scene. animation = asset_server. load ( & format ! ( "{}#Animation0" , ANIMATIONS [ * current] . 0 ) ) ;
101
113
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
107
119
commands. remove_resource :: < CurrentAnimation > ( ) ;
108
120
}
109
121
122
+ // Setup the scene for animation once it is loaded, by adding the animation to a resource with
123
+ // the start time.
110
124
fn setup_scene_once_loaded (
111
125
mut commands : Commands ,
112
126
scene_spawner : Res < SceneSpawner > ,
@@ -115,9 +129,11 @@ fn setup_scene_once_loaded(
115
129
mut done : Local < bool > ,
116
130
animations : Res < Assets < GltfAnimation > > ,
117
131
) {
132
+ // If the current scene resource has changed, start waiting for it to be loaded
118
133
if current_scene. is_changed ( ) {
119
134
* done = false ;
120
135
}
136
+ // Once the scene and the animation are loaded, start the animation
121
137
if !* done && scene_spawner. instance_is_ready ( current_scene. instance_id ) {
122
138
if let Some ( animation) = animations. get ( & current_scene. animation ) {
123
139
* done = true ;
@@ -129,6 +145,8 @@ fn setup_scene_once_loaded(
129
145
}
130
146
}
131
147
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.
132
150
fn gltf_animation_driver (
133
151
mut animated : Query < ( & mut Transform , & GltfAnimatedNode ) > ,
134
152
current_animation : Option < Res < CurrentAnimation > > ,
0 commit comments