bevy_gltf_kun

Crates.iobevy_gltf_kun
lib.rsbevy_gltf_kun
version0.0.13
sourcesrc
created_at2023-11-09 23:25:57.481417
updated_at2024-07-18 00:53:52.084583
descriptionBevy glTF plugin using gltf_kun.
homepage
repositoryhttps://github.com/unavi-xyz/gltf_kun
max_upload_size
id1030516
size119,366
kayh (kayhhh)

documentation

README

bevy_gltf_kun

Bevy gltf_kun plugin.

Features

  • glTF extension processing via hooks
  • glTF import / export
  • glXF import / export

Usage

Add [GltfKunPlugin] to your app:

use bevy::prelude::*;
use bevy_gltf_kun::GltfKunPlugin;

App::new().add_plugins((DefaultPlugins, GltfKunPlugin::default()));

Export

Export scenes to glTF using GltfExportEvent.

The resulting GltfExportResult will contain a flexible GltfDocument that can be exported to various file types. See [gltf_kun] for more information on how to do so.

use bevy::prelude::*;
use bevy_gltf_kun::{
    extensions::DefaultExtensions,
    export::gltf::{GltfExportEvent, GltfExportResult}
};
use gltf_kun::io::format::glb::GlbExport;

fn export_scene(
    scenes: Query<&Handle<Scene>>,
    mut export: EventWriter<GltfExportEvent<DefaultExtensions>>,
    mut results: ResMut<Events<GltfExportResult>>,
    mut did_export: Local<bool>,
) {
    // Send an export event once.
    if !*did_export {
        if let Some(handle) = scenes.iter().next() {
            export.send(GltfExportEvent::new(handle.clone()));
            *did_export = true;
        }
    }

    // Listen for the result.
    for mut event in results.drain() {
        let doc = event.result.unwrap();
        let bytes = GlbExport::<DefaultExtensions>::export(&mut event.graph, &doc);
    }
}

App::new().add_systems(Update, export_scene);

Import

Import glTFs using the GltfKun asset.

use bevy::prelude::*;
use bevy_gltf_kun::import::gltf::{GltfKun, scene::GltfScene};

fn import_gltf(
    asset_server: Res<AssetServer>,
    gltf_kun_assets: Res<Assets<GltfKun>>,
    gltf_scene_assets: Res<Assets<GltfScene>>,
    mut commands: Commands,
    mut handle: Local<Option<Handle<GltfKun>>>,
    mut did_import: Local<bool>,
) {
    if *did_import {
        return;
    }

    // Load the asset.
    if handle.is_none() {
        *handle = Some(asset_server.load::<GltfKun>("model.gltf"));
    }

    let handle = handle.as_ref().unwrap();

    let gltf = match gltf_kun_assets.get(handle) {
        Some(a) => a,
        None => return,
    };

    // Spawn the first scene.
    let gltf_scene = gltf_scene_assets.get(&gltf.scenes[0]).unwrap();
    commands.spawn(SceneBundle { scene: gltf_scene.scene.clone(), ..default() });

    *did_import = true;
}

App::new().add_systems(Update, import_gltf);
Commit count: 520

cargo fmt