mirror of
https://github.com/bevyengine/bevy
synced 2025-01-08 19:29:04 +00:00
694c06f3d0
# Objective Currently the `missing_docs` lint is allowed-by-default and enabled at crate level when their documentations is complete (see #3492). This PR proposes to inverse this logic by making `missing_docs` warn-by-default and mark crates with imcomplete docs allowed. ## Solution Makes `missing_docs` warn at workspace level and allowed at crate level when the docs is imcomplete.
154 lines
5.8 KiB
Rust
154 lines
5.8 KiB
Rust
//! Plugin providing an [`AssetLoader`](bevy_asset::AssetLoader) and type definitions
|
|
//! for loading glTF 2.0 (a standard 3D scene definition format) files in Bevy.
|
|
//!
|
|
//! The [glTF 2.0 specification](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html) defines the format of the glTF files.
|
|
|
|
#[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<String, MeshVertexAttribute>,
|
|
}
|
|
|
|
impl GltfPlugin {
|
|
/// Register a custom vertex attribute so that it is recognized when loading a glTF file with the [`GltfLoader`].
|
|
///
|
|
/// `name` must be the attribute name as found in the glTF data, which must start with an underscore.
|
|
/// See [this section of the glTF specification](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview)
|
|
/// for additional details on custom attributes.
|
|
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::<GltfExtras>()
|
|
.init_asset::<Gltf>()
|
|
.init_asset::<GltfNode>()
|
|
.init_asset::<GltfPrimitive>()
|
|
.init_asset::<GltfMesh>()
|
|
.preregister_asset_loader::<GltfLoader>(&["gltf", "glb"]);
|
|
}
|
|
|
|
fn finish(&self, app: &mut App) {
|
|
let supported_compressed_formats = match app.world.get_resource::<RenderDevice>() {
|
|
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 {
|
|
/// All scenes loaded from the glTF file.
|
|
pub scenes: Vec<Handle<Scene>>,
|
|
/// Named scenes loaded from the glTF file.
|
|
pub named_scenes: HashMap<String, Handle<Scene>>,
|
|
/// All meshes loaded from the glTF file.
|
|
pub meshes: Vec<Handle<GltfMesh>>,
|
|
/// Named meshes loaded from the glTF file.
|
|
pub named_meshes: HashMap<String, Handle<GltfMesh>>,
|
|
/// All materials loaded from the glTF file.
|
|
pub materials: Vec<Handle<StandardMaterial>>,
|
|
/// Named materials loaded from the glTF file.
|
|
pub named_materials: HashMap<String, Handle<StandardMaterial>>,
|
|
/// All nodes loaded from the glTF file.
|
|
pub nodes: Vec<Handle<GltfNode>>,
|
|
/// Named nodes loaded from the glTF file.
|
|
pub named_nodes: HashMap<String, Handle<GltfNode>>,
|
|
/// Default scene to be displayed.
|
|
pub default_scene: Option<Handle<Scene>>,
|
|
/// All animations loaded from the glTF file.
|
|
#[cfg(feature = "bevy_animation")]
|
|
pub animations: Vec<Handle<AnimationClip>>,
|
|
/// Named animations loaded from the glTF file.
|
|
#[cfg(feature = "bevy_animation")]
|
|
pub named_animations: HashMap<String, Handle<AnimationClip>>,
|
|
/// The gltf root of the gltf asset, see <https://docs.rs/gltf/latest/gltf/struct.Gltf.html>. Only has a value when `GltfLoaderSettings::include_source` is true.
|
|
pub source: Option<gltf::Gltf>,
|
|
}
|
|
|
|
/// A glTF node with all of its child nodes, its [`GltfMesh`],
|
|
/// [`Transform`](bevy_transform::prelude::Transform) and an optional [`GltfExtras`].
|
|
///
|
|
/// See [the relevant glTF specification section](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-node).
|
|
#[derive(Asset, Debug, Clone, TypePath)]
|
|
pub struct GltfNode {
|
|
/// Direct children of the node.
|
|
pub children: Vec<GltfNode>,
|
|
/// Mesh of the node.
|
|
pub mesh: Option<Handle<GltfMesh>>,
|
|
/// Local transform.
|
|
pub transform: bevy_transform::prelude::Transform,
|
|
/// Additional data.
|
|
pub extras: Option<GltfExtras>,
|
|
}
|
|
|
|
/// A glTF mesh, which may consist of multiple [`GltfPrimitives`](GltfPrimitive)
|
|
/// and an optional [`GltfExtras`].
|
|
///
|
|
/// See [the relevant glTF specification section](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-mesh).
|
|
#[derive(Asset, Debug, Clone, TypePath)]
|
|
pub struct GltfMesh {
|
|
/// Primitives of the glTF mesh.
|
|
pub primitives: Vec<GltfPrimitive>,
|
|
/// Additional data.
|
|
pub extras: Option<GltfExtras>,
|
|
}
|
|
|
|
/// Part of a [`GltfMesh`] that consists of a [`Mesh`], an optional [`StandardMaterial`] and [`GltfExtras`].
|
|
///
|
|
/// See [the relevant glTF specification section](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-mesh-primitive).
|
|
#[derive(Asset, Debug, Clone, TypePath)]
|
|
pub struct GltfPrimitive {
|
|
/// Topology to be rendered.
|
|
pub mesh: Handle<Mesh>,
|
|
/// Material to apply to the `mesh`.
|
|
pub material: Option<Handle<StandardMaterial>>,
|
|
/// Additional data.
|
|
pub extras: Option<GltfExtras>,
|
|
/// Additional data of the `material`.
|
|
pub material_extras: Option<GltfExtras>,
|
|
}
|
|
|
|
/// Additional untyped data that can be present on most glTF types.
|
|
///
|
|
/// See [the relevant glTF specification section](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-extras).
|
|
#[derive(Clone, Debug, Reflect, Default, Component)]
|
|
#[reflect(Component)]
|
|
pub struct GltfExtras {
|
|
/// Content of the extra data.
|
|
pub value: String,
|
|
}
|