-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a
SceneBundle
to spawn a scene (#2424)
# Objective - Spawning a scene is handled as a special case with a command `spawn_scene` that takes an handle but doesn't let you specify anything else. This is the only handle that works that way. - Workaround for this have been to add the `spawn_scene` on `ChildBuilder` to be able to specify transform of parent, or to make the `SceneSpawner` available to be able to select entities from a scene by their instance id ## Solution Add a bundle ```rust pub struct SceneBundle { pub scene: Handle<Scene>, pub transform: Transform, pub global_transform: GlobalTransform, pub instance_id: Option<InstanceId>, } ``` and instead of ```rust commands.spawn_scene(asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0")); ``` you can do ```rust commands.spawn_bundle(SceneBundle { scene: asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"), ..Default::default() }); ``` The scene will be spawned as a child of the entity with the `SceneBundle` ~I would like to remove the command `spawn_scene` in favor of this bundle but didn't do it yet to get feedback first~ Co-authored-by: François <[email protected]> Co-authored-by: Carter Anderson <[email protected]>
- Loading branch information
1 parent
cdb62af
commit c6958b3
Showing
18 changed files
with
262 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use bevy_asset::Handle; | ||
use bevy_derive::{Deref, DerefMut}; | ||
use bevy_ecs::{ | ||
bundle::Bundle, | ||
change_detection::ResMut, | ||
entity::Entity, | ||
prelude::{Changed, Component, Without}, | ||
system::{Commands, Query}, | ||
}; | ||
use bevy_transform::components::{GlobalTransform, Transform}; | ||
|
||
use crate::{DynamicScene, InstanceId, Scene, SceneSpawner}; | ||
|
||
/// [`InstanceId`] of a spawned scene. It can be used with the [`SceneSpawner`] to | ||
/// interact with the spawned scene. | ||
#[derive(Component, Deref, DerefMut)] | ||
pub struct SceneInstance(InstanceId); | ||
|
||
/// A component bundle for a [`Scene`] root. | ||
/// | ||
/// The scene from `scene` will be spawn as a child of the entity with this component. | ||
/// Once it's spawned, the entity will have a [`SceneInstance`] component. | ||
#[derive(Default, Bundle)] | ||
pub struct SceneBundle { | ||
/// Handle to the scene to spawn | ||
pub scene: Handle<Scene>, | ||
pub transform: Transform, | ||
pub global_transform: GlobalTransform, | ||
} | ||
|
||
/// A component bundle for a [`DynamicScene`] root. | ||
/// | ||
/// The dynamic scene from `scene` will be spawn as a child of the entity with this component. | ||
/// Once it's spawned, the entity will have a [`SceneInstance`] component. | ||
#[derive(Default, Bundle)] | ||
pub struct DynamicSceneBundle { | ||
/// Handle to the scene to spawn | ||
pub scene: Handle<DynamicScene>, | ||
pub transform: Transform, | ||
pub global_transform: GlobalTransform, | ||
} | ||
|
||
/// System that will spawn scenes from [`SceneBundle`]. | ||
pub fn scene_spawner( | ||
mut commands: Commands, | ||
mut scene_to_spawn: Query< | ||
(Entity, &Handle<Scene>, Option<&mut SceneInstance>), | ||
(Changed<Handle<Scene>>, Without<Handle<DynamicScene>>), | ||
>, | ||
mut dynamic_scene_to_spawn: Query< | ||
(Entity, &Handle<DynamicScene>, Option<&mut SceneInstance>), | ||
(Changed<Handle<DynamicScene>>, Without<Handle<Scene>>), | ||
>, | ||
mut scene_spawner: ResMut<SceneSpawner>, | ||
) { | ||
for (entity, scene, instance) in scene_to_spawn.iter_mut() { | ||
let new_instance = scene_spawner.spawn_as_child(scene.clone(), entity); | ||
if let Some(mut old_instance) = instance { | ||
scene_spawner.despawn_instance(**old_instance); | ||
*old_instance = SceneInstance(new_instance); | ||
} else { | ||
commands.entity(entity).insert(SceneInstance(new_instance)); | ||
} | ||
} | ||
for (entity, dynamic_scene, instance) in dynamic_scene_to_spawn.iter_mut() { | ||
let new_instance = scene_spawner.spawn_dynamic_as_child(dynamic_scene.clone(), entity); | ||
if let Some(mut old_instance) = instance { | ||
scene_spawner.despawn_instance(**old_instance); | ||
*old_instance = SceneInstance(new_instance); | ||
} else { | ||
commands.entity(entity).insert(SceneInstance(new_instance)); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.