Skip to content

Commit

Permalink
add bevy primitive import extension
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Mar 13, 2024
1 parent ae82075 commit 4a85391
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
52 changes: 39 additions & 13 deletions bevy_gltf_kun/src/import/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,13 @@ use bevy::prelude::*;
use gltf_kun::{
extensions::{DefaultExtensions, Extension},
graph::{
gltf::{document::GltfDocument, node::Node},
gltf::{GltfDocument, Node, Primitive},
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) {
Expand All @@ -31,13 +21,47 @@ pub trait NodeExtensionImport<D>: Extension {
fn import_node(context: &mut ImportContext, entity: &mut EntityWorldMut, ext: Self);
}

pub trait PrimitiveExtensionImport<D>: Extension {
fn maybe_import_primitive(
context: &mut ImportContext,
entity: &mut EntityWorldMut,
primitive: Primitive,
) {
if let Some(ext) = primitive.get_extension::<Self>(context.graph) {
Self::import_primitive(context, entity, ext);
}
}

fn import_primitive(context: &mut ImportContext, entity: &mut EntityWorldMut, ext: Self);
}

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 BevyImportExtensions<D> {
fn import_root(context: &mut ImportContext);
fn import_node(context: &mut ImportContext, entity: &mut EntityWorldMut, node: Node);
fn import_primitive(
context: &mut ImportContext,
entity: &mut EntityWorldMut,
primitive: Primitive,
);
fn import_root(context: &mut ImportContext);
}

impl BevyImportExtensions<GltfDocument> for DefaultExtensions {
fn import_root(_context: &mut ImportContext) {}
fn import_primitive(
_context: &mut ImportContext,
_entity: &mut EntityWorldMut,
_primitive: Primitive,
) {
}

fn import_node(context: &mut ImportContext, entity: &mut EntityWorldMut, node: Node) {
#[cfg(feature = "omi_physics")]
Expand All @@ -47,4 +71,6 @@ impl BevyImportExtensions<GltfDocument> for DefaultExtensions {
)
}
}

fn import_root(_context: &mut ImportContext) {}
}
8 changes: 5 additions & 3 deletions bevy_gltf_kun/src/import/gltf/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use bevy::prelude::*;
use gltf_kun::graph::{
gltf::{self},
gltf::{self, GltfDocument},
GraphNodeWeight,
};

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

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

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

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

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

primitive_entities.extend(ents);

Expand Down
7 changes: 6 additions & 1 deletion bevy_gltf_kun/src/import/gltf/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ 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,
material::{default_material, import_material},
Expand Down Expand Up @@ -61,7 +64,7 @@ pub enum ImportPrimitiveError {
MorphBuildError(#[from] MorphBuildError),
}

pub fn import_primitive(
pub fn import_primitive<E: BevyImportExtensions<GltfDocument>>(
context: &mut ImportContext,
parent: &mut WorldChildBuilder,
is_scale_inverted: bool,
Expand Down Expand Up @@ -211,6 +214,8 @@ pub fn import_primitive(
bevy_mesh.set_morph_targets(handle);
}

E::import_primitive(context, &mut entity, *p);

let mesh_label = context
.load_context
.add_labeled_asset(primitive_label.clone(), bevy_mesh);
Expand Down

0 comments on commit 4a85391

Please sign in to comment.