-
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.
- Loading branch information
Showing
6 changed files
with
143 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,16 +1,12 @@ | ||
use std::{fs, io}; | ||
|
||
const CARGO_MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR"); | ||
const ASSETS_DIR: &str = "../assets"; | ||
|
||
const MODEL: &str = "TriangleWithoutIndices/TriangleWithoutIndices.gltf"; | ||
|
||
fn main() { | ||
let path = format!("{}/{}/{}", CARGO_MANIFEST_DIR, ASSETS_DIR, MODEL); | ||
let gltf = gltf_kun::import(&path).unwrap(); | ||
|
||
let file = fs::File::open(path).unwrap(); | ||
let reader = io::BufReader::new(file); | ||
let gltf = gltf_kun::Gltf::from_reader(reader).unwrap(); | ||
|
||
println!("{:#?}", gltf); | ||
gltf.nodes().iter().for_each(|node| { | ||
println!("{:#?}", node.data()); | ||
}); | ||
} |
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,36 @@ | ||
pub struct AssetData { | ||
pub version: String, | ||
pub generator: Option<String>, | ||
pub extensions_used: Vec<String>, | ||
pub extensions_required: Vec<String>, | ||
} | ||
|
||
pub struct MeshData { | ||
pub name: Option<String>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct NodeData { | ||
pub name: Option<String>, | ||
pub translation: [f32; 3], | ||
pub rotation: [f32; 4], | ||
pub scale: [f32; 3], | ||
} | ||
|
||
pub struct SceneData { | ||
pub name: Option<String>, | ||
} | ||
|
||
pub enum GraphNode { | ||
Asset(AssetData), | ||
Mesh(MeshData), | ||
Node(NodeData), | ||
Scene(SceneData), | ||
} | ||
|
||
pub enum GraphEdge { | ||
Parent, | ||
Child, | ||
} | ||
|
||
pub type GltfGraph = petgraph::graph::DiGraph<GraphNode, GraphEdge>; |
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 |
---|---|---|
@@ -1 +1,99 @@ | ||
pub use gltf::*; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
mod graph; | ||
|
||
use graph::*; | ||
use petgraph::graph::NodeIndex; | ||
use petgraph::visit::EdgeRef; | ||
|
||
pub struct Node { | ||
graph: Arc<GltfGraph>, | ||
index: NodeIndex, | ||
} | ||
|
||
impl Node { | ||
pub fn data(&self) -> &NodeData { | ||
match &self.graph[self.index] { | ||
GraphNode::Node(node) => node, | ||
_ => panic!("Node is not a NodeData"), | ||
} | ||
} | ||
|
||
pub fn children(&self) -> Vec<Node> { | ||
self.graph | ||
.edges(self.index) | ||
.filter_map(|edge| { | ||
let index = match edge.weight() { | ||
GraphEdge::Child => edge.target(), | ||
_ => return None, | ||
}; | ||
|
||
Some(Node { | ||
graph: self.graph.clone(), | ||
index, | ||
}) | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
pub struct Gltf { | ||
graph: Arc<GltfGraph>, | ||
} | ||
|
||
impl Gltf { | ||
/// Create a new Gltf from json | ||
pub fn from_json(json: &gltf::json::Root) -> Self { | ||
let mut graph = GltfGraph::new(); | ||
let mut nodes = HashMap::new(); | ||
|
||
json.nodes.iter().enumerate().for_each(|(i, node)| { | ||
let graph_node = graph.add_node(GraphNode::Node(NodeData { | ||
name: node.name.clone(), | ||
translation: node.translation.unwrap_or([0.0, 0.0, 0.0]), | ||
rotation: node.rotation.unwrap_or_default().0, | ||
scale: node.scale.unwrap_or([1.0, 1.0, 1.0]), | ||
})); | ||
|
||
nodes.insert(i, graph_node); | ||
}); | ||
|
||
json.nodes.iter().enumerate().for_each(|(i, node)| { | ||
let graph_node = nodes.get(&i).unwrap(); | ||
|
||
if let Some(children) = &node.children { | ||
children.iter().for_each(|child| { | ||
let child_graph_node = nodes.get(&child.value()).unwrap(); | ||
|
||
graph.add_edge(*graph_node, *child_graph_node, GraphEdge::Child); | ||
graph.add_edge(*child_graph_node, *graph_node, GraphEdge::Parent); | ||
}); | ||
} | ||
}); | ||
|
||
Gltf { | ||
graph: Arc::new(graph), | ||
} | ||
} | ||
|
||
/// Get all glTF nodes | ||
pub fn nodes(&self) -> Vec<Node> { | ||
self.graph | ||
.node_indices() | ||
.filter_map(|index| match self.graph[index] { | ||
GraphNode::Node(_) => Some(Node { | ||
graph: self.graph.clone(), | ||
index, | ||
}), | ||
_ => None, | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
/// Import a glTF from the file system | ||
pub fn import(path: &str) -> Result<Gltf, gltf::Error> { | ||
let (doc, _, _) = gltf::import(path)?; | ||
Ok(Gltf::from_json(&doc.into_json())) | ||
} |