Skip to content

Docs: SceneSpawner #50

@ChristopherBiscardi

Description

@ChristopherBiscardi

while we wait for bsn (maybe 0.18?) its useful to document the behavior of SceneRoot and SceneSpawner, specifically around the behavior of SceneRoot causing an additional parent node.

Using the example program below with a default cube scene (no camera, no light, but that's not particularly critical to the example).

use bevy::{
    input::common_conditions::input_toggle_active,
    prelude::*, scene::SceneInstanceReady,
};
use bevy_inspector_egui::{
    bevy_egui::EguiPlugin, quick::WorldInspectorPlugin,
};
use bevy_skein::SkeinPlugin;

fn main() {
    App::new()
        .register_type::<Character>()
        .add_plugins((
            DefaultPlugins,
            SkeinPlugin::default(),
            EguiPlugin::default(),
            WorldInspectorPlugin::default().run_if(
                input_toggle_active(true, KeyCode::Escape),
            ),
        ))
        .add_systems(Startup, startup)
        .add_systems(Update, on_load_gltf)
        .add_observer(
            |trigger: Trigger<SceneInstanceReady>| {
                info!(event = ?trigger.event());
            },
        )
        .run();
}

#[derive(Component, Reflect, Debug)]
#[reflect(Component)]
struct Character {
    name: String,
}

#[derive(Resource)]
struct Hold(Handle<Gltf>);

fn startup(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
) {
    commands.spawn((
        Camera3d::default(),
        Transform::from_xyz(0., 4., 10.)
            .looking_at(Vec3::ZERO, Vec3::Y),
    ));

    // commands.spawn((
    //     Name::new("MySceneContainer"),
    //     SceneRoot(
    //         asset_server.load(
    //             // Change this to your exported gltf file
    //             GltfAssetLabel::Scene(0)
    //                 .from_asset("Untitled.gltf"),
    //         ),
    //     ),
    // ));

    let gltf = asset_server.load("Untitled.gltf");
    commands.insert_resource(Hold(gltf));
}

fn on_load_gltf(
    mut events: EventReader<AssetEvent<Gltf>>,
    gltfs: Res<Assets<Gltf>>,
    mut scene_spawner: ResMut<SceneSpawner>,
) {
    for event in events.read() {
        match event {
            AssetEvent::LoadedWithDependencies { id } => {
                let Some(gltf) = gltfs.get(*id) else {
                    continue;
                };

                let Some(scene_handle) =
                    gltf.default_scene.as_ref()
                else {
                    continue;
                };

                scene_spawner.spawn(scene_handle.clone());
            }
            _ => {}
        }
    }
}

The "SceneRoot Entity" is inserted as the parent, or not, depending on the approach chosen.

Image Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions