|
9 | 9 | //! for loading glTF 2.0 (a standard 3D scene definition format) files in Bevy.
|
10 | 10 | //!
|
11 | 11 | //! The [glTF 2.0 specification](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html) defines the format of the glTF files.
|
| 12 | +//! |
| 13 | +//! # Quick Start |
| 14 | +//! |
| 15 | +//! Here's how to spawn a simple glTF scene |
| 16 | +//! |
| 17 | +//! ``` |
| 18 | +//! # use bevy_ecs::prelude::*; |
| 19 | +//! # use bevy_asset::prelude::*; |
| 20 | +//! # use bevy_scene::prelude::*; |
| 21 | +//! # use bevy_transform::prelude::*; |
| 22 | +//! |
| 23 | +//! fn spawn_gltf(mut commands: Commands, asset_server: Res<AssetServer>) { |
| 24 | +//! commands.spawn(SceneBundle { |
| 25 | +//! // The `#Scene0` label here is very important because it tells bevy to load the first scene in the glTF file. |
| 26 | +//! // If this isn't specified bevy doesn't know which part of the glTF file to load. |
| 27 | +//! scene: asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"), |
| 28 | +//! // You can use the transform to give it a position |
| 29 | +//! transform: Transform::from_xyz(2.0, 0.0, -5.0), |
| 30 | +//! ..Default::default() |
| 31 | +//! }); |
| 32 | +//! } |
| 33 | +//! ``` |
| 34 | +//! # Loading parts of a glTF asset |
| 35 | +//! |
| 36 | +//! ## Using `Gltf` |
| 37 | +//! |
| 38 | +//! If you want to access part of the asset, you can load the entire `Gltf` using the `AssetServer`. |
| 39 | +//! Once the `Handle<Gltf>` is loaded you can then use it to access named parts of it. |
| 40 | +//! |
| 41 | +//! ``` |
| 42 | +//! # use bevy_ecs::prelude::*; |
| 43 | +//! # use bevy_asset::prelude::*; |
| 44 | +//! # use bevy_scene::prelude::*; |
| 45 | +//! # use bevy_transform::prelude::*; |
| 46 | +//! # use bevy_gltf::Gltf; |
| 47 | +//! |
| 48 | +//! // Holds the scene handle |
| 49 | +//! #[derive(Resource)] |
| 50 | +//! struct HelmetScene(Handle<Gltf>); |
| 51 | +//! |
| 52 | +//! fn load_gltf(mut commands: Commands, asset_server: Res<AssetServer>) { |
| 53 | +//! let gltf = asset_server.load("models/FlightHelmet/FlightHelmet.gltf"); |
| 54 | +//! commands.insert_resource(HelmetScene(gltf)); |
| 55 | +//! } |
| 56 | +//! |
| 57 | +//! fn spawn_gltf_objects( |
| 58 | +//! mut commands: Commands, |
| 59 | +//! helmet_scene: Res<HelmetScene>, |
| 60 | +//! gltf_assets: Res<Assets<Gltf>>, |
| 61 | +//! mut loaded: Local<bool>, |
| 62 | +//! ) { |
| 63 | +//! // Only do this once |
| 64 | +//! if *loaded { |
| 65 | +//! return; |
| 66 | +//! } |
| 67 | +//! // Wait until the scene is loaded |
| 68 | +//! let Some(gltf) = gltf_assets.get(&helmet_scene.0) else { |
| 69 | +//! return; |
| 70 | +//! }; |
| 71 | +//! *loaded = true; |
| 72 | +//! |
| 73 | +//! commands.spawn(SceneBundle { |
| 74 | +//! // Gets the first scene in the file |
| 75 | +//! scene: gltf.scenes[0].clone(), |
| 76 | +//! ..Default::default() |
| 77 | +//! }); |
| 78 | +//! |
| 79 | +//! commands.spawn(SceneBundle { |
| 80 | +//! // Gets the scene named "Lenses_low" |
| 81 | +//! scene: gltf.named_scenes["Lenses_low"].clone(), |
| 82 | +//! transform: Transform::from_xyz(1.0, 2.0, 3.0), |
| 83 | +//! ..Default::default() |
| 84 | +//! }); |
| 85 | +//! } |
| 86 | +//! ``` |
| 87 | +//! |
| 88 | +//! ## Asset Labels |
| 89 | +//! |
| 90 | +//! The glTF loader let's you specify labels that let you target specific parts of the glTF. |
| 91 | +//! |
| 92 | +//! Be careful when using this feature, if you misspell a label it will simply ignore it without warning. |
| 93 | +//! |
| 94 | +//! Here's the list of supported labels (`{}` is the index in the file): |
| 95 | +//! |
| 96 | +//! - `Scene{}`: glTF Scene as a Bevy `Scene` |
| 97 | +//! - `Node{}`: glTF Node as a `GltfNode` |
| 98 | +//! - `Mesh{}`: glTF Mesh as a `GltfMesh` |
| 99 | +//! - `Mesh{}/Primitive{}`: glTF Primitive as a Bevy `Mesh` |
| 100 | +//! - `Mesh{}/Primitive{}/MorphTargets`: Morph target animation data for a glTF Primitive |
| 101 | +//! - `Texture{}`: glTF Texture as a Bevy `Image` |
| 102 | +//! - `Material{}`: glTF Material as a Bevy `StandardMaterial` |
| 103 | +//! - `DefaultMaterial`: as above, if the glTF file contains a default material with no index |
| 104 | +//! - `Animation{}`: glTF Animation as Bevy `AnimationClip` |
| 105 | +//! - `Skin{}`: glTF mesh skin as Bevy `SkinnedMeshInverseBindposes` |
12 | 106 |
|
13 | 107 | #[cfg(feature = "bevy_animation")]
|
14 | 108 | use bevy_animation::AnimationClip;
|
|
0 commit comments