Skip to content

Commit

Permalink
split physics extensions into more files
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Mar 11, 2024
1 parent e8b7287 commit 01ed0f0
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 276 deletions.
5 changes: 4 additions & 1 deletion bevy_gltf_kun/src/extensions/omi_physics/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use bevy::{ecs::system::RunSystemOnce, prelude::*};
use bevy_xpbd_3d::{parry::shape::ShapeType, prelude::*};
use gltf_kun::{
extensions::{
omi_physics_body::{BodyType, Motion, OmiPhysicsBody},
omi_physics_body::{
weight::{BodyType, Motion},
OmiPhysicsBody,
},
omi_physics_shape::{
physics_shape::{
BoxShape, CapsuleShape, Height, PhysicsShapeWeight, Radius, Size, SphereShape,
Expand Down
2 changes: 1 addition & 1 deletion bevy_gltf_kun/src/extensions/omi_physics/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::prelude::*;
use bevy_xpbd_3d::prelude::*;
use gltf_kun::{
extensions::{
omi_physics_body::{BodyType, OmiPhysicsBody},
omi_physics_body::{weight::BodyType, OmiPhysicsBody},
omi_physics_shape::physics_shape::{
BoxShape, CapsuleShape, CylinderShape, PhysicsShapeWeight, SphereShape,
},
Expand Down
77 changes: 77 additions & 0 deletions gltf_kun/src/extensions/omi_physics_body/export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::error::Error;

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

use super::{
json::{PhysicsBodyJson, ShapeRefJson},
OmiPhysicsBody, EXTENSION_NAME,
};

impl ExtensionExport<GltfDocument, GltfFormat> for OmiPhysicsBody {
fn export(
graph: &mut crate::graph::Graph,
doc: &GltfDocument,
format: &mut GltfFormat,
) -> Result<(), Box<dyn Error>> {
let mut added_extension = false;

doc.nodes(graph)
.iter()
.enumerate()
.filter_map(|(i, n)| n.get_extension::<Self>(graph).map(|e| (i, e)))
.for_each(|(i, ext)| {
let weight = ext.read(graph);

let node = format
.json
.nodes
.get_mut(i)
.expect("Node index out of bounds");

let extensions = node
.extensions
.get_or_insert(gltf::json::extensions::scene::Node::default());

let collider = ext
.collider(graph)
.iter()
.filter_map(|s| doc.get_extension::<OmiPhysicsShape>(graph).map(|e| (e, s)))
.find_map(|(e, s)| e.shapes(graph).position(|x| x == *s))
.map(|shape| ShapeRefJson {
shape: shape as isize,
});

let trigger = ext
.trigger(graph)
.iter()
.filter_map(|s| doc.get_extension::<OmiPhysicsShape>(graph).map(|e| (e, s)))
.find_map(|(e, s)| e.shapes(graph).position(|x| x == *s))
.map(|shape| ShapeRefJson {
shape: shape as isize,
});

let json = PhysicsBodyJson {
collider,
trigger,
motion: weight.motion,
};

extensions.others.insert(
EXTENSION_NAME.to_string(),
serde_json::to_value(json).expect("Failed to serialize extension"),
);

added_extension = true;
});

if added_extension {
format.json.extensions_used.push(EXTENSION_NAME.to_string());
}

Ok(())
}
}
80 changes: 80 additions & 0 deletions gltf_kun/src/extensions/omi_physics_body/import.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::error::Error;

use tracing::warn;

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

use super::{json::PhysicsBodyJson, OmiPhysicsBody, OmiPhysicsBodyWeight, EXTENSION_NAME};

impl ExtensionImport<GltfDocument, GltfFormat> for OmiPhysicsBody {
fn import(
graph: &mut crate::graph::Graph,
format: &mut GltfFormat,
doc: &GltfDocument,
) -> Result<(), Box<dyn Error>> {
format
.json
.nodes
.iter()
.enumerate()
.filter_map(|(i, n)| n.extensions.as_ref().map(|e| (i, e)))
.filter_map(|(i, e)| e.others.get(EXTENSION_NAME).map(|v| (i, v)))
.filter_map(|(i, v)| {
serde_json::from_value::<PhysicsBodyJson>(v.clone())
.map(|json| (i, json))
.ok()
})
.for_each(|(i, json)| {
let nodes = doc.nodes(graph);
let node = nodes.get(i).expect("Node index out of bounds");
let ext = node.create_extension::<Self>(graph);

if let Some(motion) = json.motion {
let weight = OmiPhysicsBodyWeight {
motion: Some(motion),
};
ext.write(graph, &weight);
}

if json.collider.is_none() && json.trigger.is_none() {
return;
}

let omi_physics_shapes = match doc.get_extension::<OmiPhysicsShape>(graph) {
Some(ext) => ext,
None => {
warn!("OMI_physics_shape extension not found");
return;
}
};

if let Some(shape_ref) = json.collider {
if let Ok(idx) = shape_ref.shape.try_into() {
let shape = omi_physics_shapes
.shapes(graph)
.nth(idx)
.expect("Collider index out of bounds");

ext.set_collider(graph, Some(shape));
}
}

if let Some(shape_ref) = json.trigger {
if let Ok(idx) = shape_ref.shape.try_into() {
let shape = omi_physics_shapes
.shapes(graph)
.nth(idx)
.expect("Trigger index out of bounds");

ext.set_trigger(graph, Some(shape));
}
}
});

Ok(())
}
}
187 changes: 0 additions & 187 deletions gltf_kun/src/extensions/omi_physics_body/io.rs

This file was deleted.

Loading

0 comments on commit 01ed0f0

Please sign in to comment.