#![allow(clippy::type_complexity)] #[cfg(feature = "bevy_animation")] use bevy_animation::AnimationClip; use bevy_utils::HashMap; mod loader; mod vertex_attributes; pub use loader::*; use bevy_app::prelude::*; use bevy_asset::{Asset, AssetApp, Handle}; use bevy_ecs::{prelude::Component, reflect::ReflectComponent}; use bevy_pbr::StandardMaterial; use bevy_reflect::{Reflect, TypePath}; use bevy_render::{ mesh::{Mesh, MeshVertexAttribute}, renderer::RenderDevice, texture::CompressedImageFormats, }; use bevy_scene::Scene; /// Adds support for glTF file loading to the app. #[derive(Default)] pub struct GltfPlugin { custom_vertex_attributes: HashMap, } impl GltfPlugin { pub fn add_custom_vertex_attribute( mut self, name: &str, attribute: MeshVertexAttribute, ) -> Self { self.custom_vertex_attributes .insert(name.to_string(), attribute); self } } impl Plugin for GltfPlugin { fn build(&self, app: &mut App) { app.register_type::() .init_asset::() .init_asset::() .init_asset::() .init_asset::() .preregister_asset_loader::(&["gltf", "glb"]); } fn finish(&self, app: &mut App) { let supported_compressed_formats = match app.world.get_resource::() { Some(render_device) => CompressedImageFormats::from_features(render_device.features()), None => CompressedImageFormats::NONE, }; app.register_asset_loader(GltfLoader { supported_compressed_formats, custom_vertex_attributes: self.custom_vertex_attributes.clone(), }); } } /// Representation of a loaded glTF file. #[derive(Asset, Debug, TypePath)] pub struct Gltf { pub scenes: Vec>, pub named_scenes: HashMap>, pub meshes: Vec>, pub named_meshes: HashMap>, pub materials: Vec>, pub named_materials: HashMap>, pub nodes: Vec>, pub named_nodes: HashMap>, pub default_scene: Option>, #[cfg(feature = "bevy_animation")] pub animations: Vec>, #[cfg(feature = "bevy_animation")] pub named_animations: HashMap>, } /// A glTF node with all of its child nodes, its [`GltfMesh`], /// [`Transform`](bevy_transform::prelude::Transform) and an optional [`GltfExtras`]. #[derive(Asset, Debug, Clone, TypePath)] pub struct GltfNode { pub children: Vec, pub mesh: Option>, pub transform: bevy_transform::prelude::Transform, pub extras: Option, } /// A glTF mesh, which may consist of multiple [`GltfPrimitives`](GltfPrimitive) /// and an optional [`GltfExtras`]. #[derive(Asset, Debug, Clone, TypePath)] pub struct GltfMesh { pub primitives: Vec, pub extras: Option, } /// Part of a [`GltfMesh`] that consists of a [`Mesh`], an optional [`StandardMaterial`] and [`GltfExtras`]. #[derive(Asset, Debug, Clone, TypePath)] pub struct GltfPrimitive { pub mesh: Handle, pub material: Option>, pub extras: Option, pub material_extras: Option, } #[derive(Clone, Debug, Reflect, Default, Component)] #[reflect(Component)] pub struct GltfExtras { pub value: String, }