-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split physics extensions into more files
- Loading branch information
Showing
12 changed files
with
302 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.