@@ -43,23 +43,25 @@ fn setup_mesh_and_animation(
4343 // Store the animation graph as an asset.
4444 let graph_handle = graphs. add ( graph) ;
4545
46- // Create a SceneRoot component that will spawn our mesh after it has loaded.
47- let mesh_scene = SceneRoot ( asset_server. load ( GltfAssetLabel :: Scene ( 0 ) . from_asset ( GLTF_PATH ) ) ) ;
48-
4946 // Create a component that stores a reference to our animation.
5047 let animation_to_play = AnimationToPlay {
5148 graph_handle,
5249 index,
5350 } ;
5451
55- // Spawn an entity with our components and connect it to our
56- // play_animation_once_loaded trigger.
52+ // Start loading the asset as a scene and store a reference to it in a
53+ // SceneRoot component. This component will automatically spawn a scene
54+ // containing our mesh once it has loaded.
55+ let mesh_scene = SceneRoot ( asset_server. load ( GltfAssetLabel :: Scene ( 0 ) . from_asset ( GLTF_PATH ) ) ) ;
56+
57+ // Spawn an entity with our components, and connect it to an observer that
58+ // will trigger when the scene is loaded and spawned.
5759 commands
58- . spawn ( ( mesh_scene , animation_to_play ) )
59- . observe ( play_animation_once_loaded ) ;
60+ . spawn ( ( animation_to_play , mesh_scene ) )
61+ . observe ( play_animation_when_ready ) ;
6062}
6163
62- fn play_animation_once_loaded (
64+ fn play_animation_when_ready (
6365 trigger : Trigger < SceneInstanceReady > ,
6466 mut commands : Commands ,
6567 children : Query < & Children > ,
@@ -69,9 +71,10 @@ fn play_animation_once_loaded(
6971 // The entity we spawned in `setup_mesh_and_animation` is the trigger's target.
7072 // Start by finding the AnimationToPlay component we added to that entity.
7173 if let Ok ( animation_to_play) = animations_to_play. get ( trigger. target ( ) ) {
72- // The SceneRoot component will have spawned the mesh and an animation
73- // player as a descendent of our entity. Search the descendents to find
74- // the animation player.
74+ // The SceneRoot component will have spawned the scene as a hierarchy
75+ // of entities parented to our entity. Since the asset contained a skinned
76+ // mesh and animations, it will also have spawned an animation player
77+ // component. Search our entity's descendents to find the animation player.
7578 for child in children. iter_descendants ( trigger. target ( ) ) {
7679 if let Ok ( mut player) = players. get_mut ( child) {
7780 // Tell the animation player to start the animation and keep
0 commit comments