Skip to content

Commit

Permalink
add bevy root extensions import + clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Mar 12, 2024
1 parent 01ed0f0 commit fe3cd17
Show file tree
Hide file tree
Showing 30 changed files with 69 additions and 56 deletions.
2 changes: 1 addition & 1 deletion bevy_gltf_kun/src/extensions/omi_physics/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use gltf_kun::{
OmiPhysicsShape,
},
},
graph::{ByteNode, Property},
graph::{ByteNode, Extensions},
};

use crate::export::{extensions::BevyExtensionExport, gltf::ExportContext};
Expand Down
4 changes: 0 additions & 4 deletions bevy_gltf_kun/src/extensions/omi_physics/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use gltf_kun::{

use crate::import::{extensions::NodeExtensionImport, gltf::document::ImportContext};

/// Mark a collider to be added to the entity after asset loading.
/// This is needed because `Collider` doesn't implement `Reflect`.
#[derive(Component, Reflect)]
#[reflect(Component)]
pub enum ColliderMarker {
Expand Down Expand Up @@ -68,8 +66,6 @@ pub fn insert_rigid_bodies(mut commands: Commands, mut query: Query<(Entity, &Ri
}
}

/// Mark a rigid body to be added to the entity after asset loading.
/// While not necessary on its own, we want to spawn in the rigid body and collider at the same time.
#[derive(Component, Reflect)]
#[reflect(Component)]
pub struct RigidBodyMarker {
Expand Down
17 changes: 16 additions & 1 deletion bevy_gltf_kun/src/import/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,42 @@ use gltf_kun::{
extensions::{DefaultExtensions, Extension},
graph::{
gltf::{document::GltfDocument, node::Node},
Property,
Extensions,
},
};

use crate::import::gltf::document::ImportContext;

pub trait RootExtensionImport<D: Extensions>: Extension {
fn maybe_import_root(context: &mut ImportContext) {
if let Some(ext) = context.doc.get_extension::<Self>(context.graph) {
Self::import_root(context, ext);
}
}

fn import_root(context: &mut ImportContext, ext: Self);
}

pub trait NodeExtensionImport<D>: Extension {
fn maybe_import_node(context: &mut ImportContext, entity: &mut EntityWorldMut, node: Node) {
if let Some(ext) = node.get_extension::<Self>(context.graph) {
Self::import_node(context, entity, ext);
}
}

/// Called when a node with this extension is imported.
/// This is called while the tree is being traversed, so the node's children may not have been imported yet.
fn import_node(context: &mut ImportContext, entity: &mut EntityWorldMut, ext: Self);
}

pub trait BevyImportExtensions<D> {
fn import_root(context: &mut ImportContext);
fn import_node(context: &mut ImportContext, entity: &mut EntityWorldMut, node: Node);
}

impl BevyImportExtensions<GltfDocument> for DefaultExtensions {
fn import_root(_context: &mut ImportContext) {}

fn import_node(context: &mut ImportContext, entity: &mut EntityWorldMut, node: Node) {
#[cfg(feature = "omi_physics")]
{
Expand Down
3 changes: 3 additions & 0 deletions bevy_gltf_kun/src/import/gltf/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,8 @@ pub fn import_gltf_document<E: BevyImportExtensions<GltfDocument>>(
context.gltf.scenes.insert(i, handle);
}

// Load extensions.
E::import_root(context);

Ok(())
}
4 changes: 2 additions & 2 deletions bevy_gltf_kun/src/import/gltf/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use gltf_kun::graph::{
};
use thiserror::Error;

use crate::import::extensions::BevyImportExtensions;


use super::document::ImportContext;

#[derive(Debug, Error)]
pub enum MaterialImportError {}

pub fn import_material<E: BevyImportExtensions<GltfDocument>>(
pub fn import_material(
context: &mut ImportContext,
m: Material,
is_scale_inverted: bool,
Expand Down
8 changes: 4 additions & 4 deletions bevy_gltf_kun/src/import/gltf/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use bevy::prelude::*;
use gltf_kun::graph::{
gltf::{self, GltfDocument},
gltf::{self},
GraphNodeWeight,
};

use crate::import::extensions::BevyImportExtensions;


use super::{
document::ImportContext,
Expand All @@ -17,7 +17,7 @@ pub struct GltfMesh {
pub extras: Option<Box<serde_json::value::RawValue>>,
}

pub fn import_mesh<E: BevyImportExtensions<GltfDocument>>(
pub fn import_mesh(
context: &mut ImportContext,
entity: &mut EntityWorldMut,
mut m: gltf::mesh::Mesh,
Expand All @@ -33,7 +33,7 @@ pub fn import_mesh<E: BevyImportExtensions<GltfDocument>>(

entity.with_children(|parent| {
for (i, p) in m.primitives(context.graph).iter_mut().enumerate() {
match import_primitive::<E>(context, parent, is_scale_inverted, m, &mesh_label, i, p) {
match import_primitive(context, parent, is_scale_inverted, m, &mesh_label, i, p) {
Ok((ent, handle, weights)) => {
morph_weights = weights;
primitive_entities.push(ent);
Expand Down
4 changes: 2 additions & 2 deletions bevy_gltf_kun/src/import/gltf/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ pub fn import_node<E: BevyImportExtensions<GltfDocument>>(

let mesh = match n.mesh(context.graph) {
Some(m) => {
let (ents, mesh, morph_weights) =
import_mesh::<E>(context, &mut ent, m, is_scale_inverted);
let (ents, mesh, morph_weights) = import_mesh(context, &mut ent, m, is_scale_inverted);

primitive_entities.extend(ents);

Expand Down Expand Up @@ -109,6 +108,7 @@ pub fn import_node<E: BevyImportExtensions<GltfDocument>>(
.node_primitive_entities
.insert(handle.clone(), primitive_entities);

// Load extensions.
E::import_node(context, &mut ent, *n);

Ok(handle)
Expand Down
7 changes: 3 additions & 4 deletions bevy_gltf_kun/src/import/gltf/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ use gltf_kun::graph::{
Accessor, ComponentType, GetAccessorSliceError, Type,
},
primitive::{Mode, MorphTarget, Primitive, Semantic},
GltfDocument,
},
Graph, GraphNodeWeight,
};
use thiserror::Error;

use crate::import::extensions::BevyImportExtensions;


use super::{
document::ImportContext,
Expand Down Expand Up @@ -64,7 +63,7 @@ pub enum ImportPrimitiveError {
MorphBuildError(#[from] MorphBuildError),
}

pub fn import_primitive<E: BevyImportExtensions<GltfDocument>>(
pub fn import_primitive(
context: &mut ImportContext,
parent: &mut WorldChildBuilder,
is_scale_inverted: bool,
Expand Down Expand Up @@ -109,7 +108,7 @@ pub fn import_primitive<E: BevyImportExtensions<GltfDocument>>(
if let Some(material) = context.materials.get(&(m, is_scale_inverted)) {
material.clone()
} else {
let material = import_material::<E>(context, m, is_scale_inverted);
let material = import_material(context, m, is_scale_inverted);

context
.materials
Expand Down
2 changes: 1 addition & 1 deletion gltf_kun/src/extensions/omi_physics_body/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::error::Error;

use crate::{
extensions::{omi_physics_shape::OmiPhysicsShape, ExtensionExport},
graph::{gltf::document::GltfDocument, ByteNode, Property},
graph::{gltf::document::GltfDocument, ByteNode, Extensions},
io::format::gltf::GltfFormat,
};

Expand Down
2 changes: 1 addition & 1 deletion gltf_kun/src/extensions/omi_physics_body/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tracing::warn;

use crate::{
extensions::{omi_physics_shape::OmiPhysicsShape, ExtensionImport},
graph::{gltf::document::GltfDocument, ByteNode, Property},
graph::{gltf::document::GltfDocument, ByteNode, Extensions},
io::format::gltf::GltfFormat,
};

Expand Down
2 changes: 1 addition & 1 deletion gltf_kun/src/extensions/omi_physics_shape/export.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
extensions::ExtensionExport,
graph::{gltf::document::GltfDocument, ByteNode, Graph, Property},
graph::{gltf::document::GltfDocument, ByteNode, Extensions, Graph},
io::format::gltf::GltfFormat,
};

Expand Down
2 changes: 1 addition & 1 deletion gltf_kun/src/extensions/omi_physics_shape/import.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
extensions::ExtensionImport,
graph::{gltf::document::GltfDocument, Graph, Property},
graph::{gltf::document::GltfDocument, Extensions, Graph},
io::format::gltf::GltfFormat,
};

Expand Down
4 changes: 2 additions & 2 deletions gltf_kun/src/graph/gltf/accessor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use petgraph::graph::NodeIndex;
use thiserror::Error;

use crate::graph::{Edge, Graph, GraphNodeEdges, GraphNodeWeight, Property, Weight};
use crate::graph::{Edge, Extensions, Graph, GraphNodeEdges, GraphNodeWeight, Weight};

use self::iter::{AccessorElement, AccessorIter, AccessorIterCreateError};

Expand Down Expand Up @@ -114,7 +114,7 @@ impl From<Accessor> for NodeIndex {

impl GraphNodeWeight<AccessorWeight> for Accessor {}
impl GraphNodeEdges<AccessorEdge> for Accessor {}
impl Property for Accessor {}
impl Extensions for Accessor {}

impl Accessor {
pub fn buffer(&self, graph: &Graph) -> Option<Buffer> {
Expand Down
4 changes: 2 additions & 2 deletions gltf_kun/src/graph/gltf/animation/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use petgraph::graph::NodeIndex;

use crate::graph::{
gltf::{GltfEdge, Node},
Edge, Graph, GraphNodeEdges, GraphNodeWeight, Property, Weight,
Edge, Extensions, Graph, GraphNodeEdges, GraphNodeWeight, Weight,
};

use super::{AnimationSampler, GltfWeight};
Expand Down Expand Up @@ -89,7 +89,7 @@ impl From<AnimationChannel> for NodeIndex {

impl GraphNodeWeight<AnimationChannelWeight> for AnimationChannel {}
impl GraphNodeEdges<AnimationChannelEdge> for AnimationChannel {}
impl Property for AnimationChannel {}
impl Extensions for AnimationChannel {}

impl AnimationChannel {
pub fn sampler(&self, graph: &Graph) -> Option<AnimationSampler> {
Expand Down
4 changes: 2 additions & 2 deletions gltf_kun/src/graph/gltf/animation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use petgraph::graph::NodeIndex;

use crate::graph::{
gltf::GltfEdge, Edge, Graph, GraphNodeEdges, GraphNodeWeight, Property, Weight,
gltf::GltfEdge, Edge, Extensions, Graph, GraphNodeEdges, GraphNodeWeight, Weight,
};

use super::GltfWeight;
Expand Down Expand Up @@ -82,7 +82,7 @@ impl From<Animation> for NodeIndex {

impl GraphNodeWeight<AnimationWeight> for Animation {}
impl GraphNodeEdges<AnimationEdge> for Animation {}
impl Property for Animation {}
impl Extensions for Animation {}

impl Animation {
pub fn channels(&self, graph: &Graph) -> Vec<AnimationChannel> {
Expand Down
4 changes: 2 additions & 2 deletions gltf_kun/src/graph/gltf/animation/sampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use petgraph::graph::NodeIndex;

use crate::graph::{
gltf::{Accessor, GltfEdge, GltfWeight},
Edge, Graph, GraphNodeEdges, GraphNodeWeight, Property, Weight,
Edge, Extensions, Graph, GraphNodeEdges, GraphNodeWeight, Weight,
};

pub use gltf::animation::Interpolation;
Expand Down Expand Up @@ -78,7 +78,7 @@ impl From<AnimationSampler> for NodeIndex {

impl GraphNodeWeight<AnimationSamplerWeight> for AnimationSampler {}
impl GraphNodeEdges<AnimationSamplerEdge> for AnimationSampler {}
impl Property for AnimationSampler {}
impl Extensions for AnimationSampler {}

impl AnimationSampler {
pub fn input(&self, graph: &Graph) -> Option<Accessor> {
Expand Down
4 changes: 2 additions & 2 deletions gltf_kun/src/graph/gltf/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use petgraph::graph::NodeIndex;

use crate::graph::{GraphNodeWeight, Property, Weight};
use crate::graph::{Extensions, GraphNodeWeight, Weight};

use super::GltfWeight;

Expand Down Expand Up @@ -54,4 +54,4 @@ impl From<Buffer> for NodeIndex {
}

impl GraphNodeWeight<BufferWeight> for Buffer {}
impl Property for Buffer {}
impl Extensions for Buffer {}
4 changes: 2 additions & 2 deletions gltf_kun/src/graph/gltf/document.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use petgraph::graph::NodeIndex;

use crate::graph::{gltf::GltfEdge, Edge, Graph, GraphNodeEdges, Property, Weight};
use crate::graph::{gltf::GltfEdge, Edge, Extensions, Graph, GraphNodeEdges, Weight};

use super::{
Accessor, Animation, Buffer, GltfWeight, Image, Material, Mesh, Node, Scene, Skin, TextureInfo,
Expand Down Expand Up @@ -52,7 +52,7 @@ impl From<GltfDocument> for NodeIndex {
}

impl GraphNodeEdges<DocumentEdge> for GltfDocument {}
impl Property for GltfDocument {}
impl Extensions for GltfDocument {}

impl GltfDocument {
pub fn new(graph: &mut Graph) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions gltf_kun/src/graph/gltf/image.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use petgraph::graph::NodeIndex;

use crate::graph::{
gltf::GltfEdge, Edge, Graph, GraphNodeEdges, GraphNodeWeight, Property, Weight,
gltf::GltfEdge, Edge, Extensions, Graph, GraphNodeEdges, GraphNodeWeight, Weight,
};

use super::{buffer::Buffer, GltfWeight};
Expand Down Expand Up @@ -81,7 +81,7 @@ impl From<Image> for NodeIndex {

impl GraphNodeWeight<ImageWeight> for Image {}
impl GraphNodeEdges<ImageEdge> for Image {}
impl Property for Image {}
impl Extensions for Image {}

impl Image {
pub fn buffer(&self, graph: &Graph) -> Option<Buffer> {
Expand Down
4 changes: 2 additions & 2 deletions gltf_kun/src/graph/gltf/material.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use petgraph::graph::NodeIndex;

use crate::graph::{Edge, Graph, GraphNodeEdges, GraphNodeWeight, Property, Weight};
use crate::graph::{Edge, Extensions, Graph, GraphNodeEdges, GraphNodeWeight, Weight};

use super::{texture_info::TextureInfo, GltfEdge, GltfWeight};

Expand Down Expand Up @@ -109,7 +109,7 @@ impl From<Material> for NodeIndex {

impl GraphNodeWeight<MaterialWeight> for Material {}
impl GraphNodeEdges<MaterialEdge> for Material {}
impl Property for Material {}
impl Extensions for Material {}

impl Material {
pub fn base_color_texture_info(&self, graph: &Graph) -> Option<TextureInfo> {
Expand Down
4 changes: 2 additions & 2 deletions gltf_kun/src/graph/gltf/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use petgraph::graph::NodeIndex;

use crate::graph::{Edge, Graph, GraphNodeEdges, GraphNodeWeight, Property, Weight};
use crate::graph::{Edge, Extensions, Graph, GraphNodeEdges, GraphNodeWeight, Weight};

use super::{primitive::Primitive, GltfEdge, GltfWeight};

Expand Down Expand Up @@ -77,7 +77,7 @@ impl From<Mesh> for NodeIndex {

impl GraphNodeWeight<MeshWeight> for Mesh {}
impl GraphNodeEdges<MeshEdge> for Mesh {}
impl Property for Mesh {}
impl Extensions for Mesh {}

impl Mesh {
pub fn primitives(&self, graph: &Graph) -> Vec<Primitive> {
Expand Down
4 changes: 2 additions & 2 deletions gltf_kun/src/graph/gltf/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use glam::{Quat, Vec3};
use petgraph::{graph::NodeIndex, visit::EdgeRef};

use crate::graph::{Edge, Graph, GraphNodeEdges, GraphNodeWeight, Property, Weight};
use crate::graph::{Edge, Extensions, Graph, GraphNodeEdges, GraphNodeWeight, Weight};

use super::{mesh::Mesh, GltfEdge, GltfWeight, Skin};

Expand Down Expand Up @@ -96,7 +96,7 @@ impl From<Node> for NodeIndex {

impl GraphNodeWeight<NodeWeight> for Node {}
impl GraphNodeEdges<NodeEdge> for Node {}
impl Property for Node {}
impl Extensions for Node {}

impl Node {
pub fn children(&self, graph: &Graph) -> Vec<Node> {
Expand Down
Loading

0 comments on commit fe3cd17

Please sign in to comment.