2022-03-22 02:26:34 +00:00
|
|
|
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
|
|
|
|
use bevy_math::{Quat, Vec3};
|
2021-12-14 03:58:23 +00:00
|
|
|
use bevy_utils::HashMap;
|
2020-12-31 20:57:15 +00:00
|
|
|
|
2020-05-15 23:55:44 +00:00
|
|
|
mod loader;
|
|
|
|
pub use loader::*;
|
2020-04-19 17:08:47 +00:00
|
|
|
|
2020-07-17 01:47:51 +00:00
|
|
|
use bevy_app::prelude::*;
|
2020-12-31 20:57:15 +00:00
|
|
|
use bevy_asset::{AddAsset, Handle};
|
2021-12-14 03:58:23 +00:00
|
|
|
use bevy_pbr::StandardMaterial;
|
2022-03-22 02:26:34 +00:00
|
|
|
use bevy_reflect::{Reflect, TypeUuid};
|
2020-12-31 20:57:15 +00:00
|
|
|
use bevy_render::mesh::Mesh;
|
|
|
|
use bevy_scene::Scene;
|
2020-04-19 17:08:47 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
/// Adds support for glTF file loading to the app.
|
2020-05-15 23:55:44 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct GltfPlugin;
|
2020-04-20 02:29:33 +00:00
|
|
|
|
2020-08-08 03:22:17 +00:00
|
|
|
impl Plugin for GltfPlugin {
|
2021-07-27 20:21:06 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
2020-12-31 20:57:15 +00:00
|
|
|
app.init_asset_loader::<GltfLoader>()
|
|
|
|
.add_asset::<Gltf>()
|
|
|
|
.add_asset::<GltfNode>()
|
|
|
|
.add_asset::<GltfPrimitive>()
|
2022-03-22 02:26:34 +00:00
|
|
|
.add_asset::<GltfMesh>()
|
|
|
|
.add_asset::<GltfAnimation>()
|
|
|
|
.register_type::<GltfAnimatedNode>();
|
2020-04-19 17:08:47 +00:00
|
|
|
}
|
2020-05-16 07:27:30 +00:00
|
|
|
}
|
2020-12-31 20:57:15 +00:00
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
/// Representation of a loaded glTF file.
|
2020-12-31 20:57:15 +00:00
|
|
|
#[derive(Debug, TypeUuid)]
|
|
|
|
#[uuid = "5c7d5f8a-f7b0-4e45-a09e-406c0372fea2"]
|
|
|
|
pub struct Gltf {
|
|
|
|
pub scenes: Vec<Handle<Scene>>,
|
|
|
|
pub named_scenes: HashMap<String, Handle<Scene>>,
|
|
|
|
pub meshes: Vec<Handle<GltfMesh>>,
|
|
|
|
pub named_meshes: HashMap<String, Handle<GltfMesh>>,
|
|
|
|
pub materials: Vec<Handle<StandardMaterial>>,
|
|
|
|
pub named_materials: HashMap<String, Handle<StandardMaterial>>,
|
|
|
|
pub nodes: Vec<Handle<GltfNode>>,
|
|
|
|
pub named_nodes: HashMap<String, Handle<GltfNode>>,
|
|
|
|
pub default_scene: Option<Handle<Scene>>,
|
2022-03-22 02:26:34 +00:00
|
|
|
pub animations: Vec<Handle<GltfAnimation>>,
|
|
|
|
pub named_animations: HashMap<String, Handle<GltfAnimation>>,
|
2020-12-31 20:57:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
/// A glTF node with all of its child nodes, its [`GltfMesh`] and
|
|
|
|
/// [`Transform`](bevy_transform::prelude::Transform).
|
2020-12-31 20:57:15 +00:00
|
|
|
#[derive(Debug, Clone, TypeUuid)]
|
|
|
|
#[uuid = "dad74750-1fd6-460f-ac51-0a7937563865"]
|
|
|
|
pub struct GltfNode {
|
|
|
|
pub children: Vec<GltfNode>,
|
|
|
|
pub mesh: Option<Handle<GltfMesh>>,
|
|
|
|
pub transform: bevy_transform::prelude::Transform,
|
|
|
|
}
|
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
/// A glTF mesh, which may consists of multiple [`GtlfPrimitives`](GltfPrimitive).
|
2020-12-31 20:57:15 +00:00
|
|
|
#[derive(Debug, Clone, TypeUuid)]
|
|
|
|
#[uuid = "8ceaec9a-926a-4f29-8ee3-578a69f42315"]
|
|
|
|
pub struct GltfMesh {
|
|
|
|
pub primitives: Vec<GltfPrimitive>,
|
|
|
|
}
|
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
/// Part of a [`GltfMesh`] that consists of a [`Mesh`] and an optional [`StandardMaterial`].
|
2020-12-31 20:57:15 +00:00
|
|
|
#[derive(Debug, Clone, TypeUuid)]
|
|
|
|
#[uuid = "cbfca302-82fd-41cb-af77-cab6b3d50af1"]
|
|
|
|
pub struct GltfPrimitive {
|
|
|
|
pub mesh: Handle<Mesh>,
|
|
|
|
pub material: Option<Handle<StandardMaterial>>,
|
|
|
|
}
|
2022-03-22 02:26:34 +00:00
|
|
|
|
|
|
|
/// Interpolation method for an animation. Part of a [`GltfNodeAnimation`].
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum GltfAnimationInterpolation {
|
|
|
|
Linear,
|
|
|
|
Step,
|
|
|
|
CubicSpline,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// How a property of a glTF node should be animated. The property and its value can be found
|
|
|
|
/// through the [`GltfNodeAnimationKeyframes`] attribute.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct GltfNodeAnimation {
|
|
|
|
pub keyframe_timestamps: Vec<f32>,
|
|
|
|
pub keyframes: GltfNodeAnimationKeyframes,
|
|
|
|
pub interpolation: GltfAnimationInterpolation,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A glTF animation, listing how each node (by its index) that is part of it should be animated.
|
|
|
|
#[derive(Default, Clone, TypeUuid, Debug)]
|
|
|
|
#[uuid = "d81b7179-0448-4eb0-89fe-c067222725bf"]
|
|
|
|
pub struct GltfAnimation {
|
|
|
|
pub node_animations: HashMap<usize, Vec<GltfNodeAnimation>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Key frames of an animation.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum GltfNodeAnimationKeyframes {
|
|
|
|
Rotation(Vec<Quat>),
|
|
|
|
Translation(Vec<Vec3>),
|
|
|
|
Scale(Vec<Vec3>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for GltfNodeAnimation {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
keyframe_timestamps: Default::default(),
|
|
|
|
keyframes: GltfNodeAnimationKeyframes::Translation(Default::default()),
|
|
|
|
interpolation: GltfAnimationInterpolation::Linear,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A glTF node that is part of an animation, with its index.
|
|
|
|
#[derive(Component, Debug, Clone, Reflect, Default)]
|
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct GltfAnimatedNode {
|
|
|
|
pub index: usize,
|
|
|
|
}
|