29 releases

0.14.0 Jul 4, 2024
0.13.2 Apr 4, 2024
0.13.1 Mar 18, 2024
0.12.1 Nov 30, 2023
0.3.0 Nov 3, 2020

#2108 in Game dev

Download history 13654/week @ 2024-04-03 11585/week @ 2024-04-10 13429/week @ 2024-04-17 12213/week @ 2024-04-24 11621/week @ 2024-05-01 11188/week @ 2024-05-08 11365/week @ 2024-05-15 11791/week @ 2024-05-22 12948/week @ 2024-05-29 12451/week @ 2024-06-05 10657/week @ 2024-06-12 11087/week @ 2024-06-19 11562/week @ 2024-06-26 11270/week @ 2024-07-03 15460/week @ 2024-07-10 13595/week @ 2024-07-17

53,263 downloads per month
Used in 115 crates (via bevy_internal)

MIT/Apache

7.5MB
136K SLoC

Bevy glTF

License Crates.io Downloads Docs Discord


lib.rs:

Plugin providing an AssetLoader and type definitions for loading glTF 2.0 (a standard 3D scene definition format) files in Bevy.

The glTF 2.0 specification defines the format of the glTF files.

Quick Start

Here's how to spawn a simple glTF scene


fn spawn_gltf(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(SceneBundle {
        // This is equivalent to "models/FlightHelmet/FlightHelmet.gltf#Scene0"
        // The `#Scene0` label here is very important because it tells bevy to load the first scene in the glTF file.
        // If this isn't specified bevy doesn't know which part of the glTF file to load.
        scene: asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/FlightHelmet/FlightHelmet.gltf")),
        // You can use the transform to give it a position
        transform: Transform::from_xyz(2.0, 0.0, -5.0),
        ..Default::default()
    });
}

Loading parts of a glTF asset

Using Gltf

If you want to access part of the asset, you can load the entire Gltf using the AssetServer. Once the Handle<Gltf> is loaded you can then use it to access named parts of it.


// Holds the scene handle
#[derive(Resource)]
struct HelmetScene(Handle<Gltf>);

fn load_gltf(mut commands: Commands, asset_server: Res<AssetServer>) {
    let gltf = asset_server.load("models/FlightHelmet/FlightHelmet.gltf");
    commands.insert_resource(HelmetScene(gltf));
}

fn spawn_gltf_objects(
    mut commands: Commands,
    helmet_scene: Res<HelmetScene>,
    gltf_assets: Res<Assets<Gltf>>,
    mut loaded: Local<bool>,
) {
    // Only do this once
    if *loaded {
        return;
    }
    // Wait until the scene is loaded
    let Some(gltf) = gltf_assets.get(&helmet_scene.0) else {
        return;
    };
    *loaded = true;

    commands.spawn(SceneBundle {
        // Gets the first scene in the file
        scene: gltf.scenes[0].clone(),
        ..Default::default()
    });

    commands.spawn(SceneBundle {
        // Gets the scene named "Lenses_low"
        scene: gltf.named_scenes["Lenses_low"].clone(),
        transform: Transform::from_xyz(1.0, 2.0, 3.0),
        ..Default::default()
    });
}

Asset Labels

The glTF loader let's you specify labels that let you target specific parts of the glTF.

Be careful when using this feature, if you misspell a label it will simply ignore it without warning.

You can use GltfAssetLabel to ensure you are using the correct label.

Dependencies

~24–60MB
~1M SLoC