mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
Expose FBX placement and polygon order
This commit is contained in:
1 parent
0a7bab2bed
commit
e469ec0558
2 files changed
+87
-8
No files matched your search
+64
-8
@@ -811,6 +811,11 @@ impl Document {
|
||||
|
||||
/// Build a scene with explicitly selected compatibility behavior.
|
||||
pub fn scene_with_options(&self, limits: &Limits, resolve: Resolver, options: &SceneOptions) -> Result<Scene> {
|
||||
self.scene_with_placements(limits, resolve, options).map(|(scene, _)| scene)
|
||||
}
|
||||
|
||||
/// Build a scene and list its mesh-node indices in FBX placement order.
|
||||
pub fn scene_with_placements(&self, limits: &Limits, resolve: Resolver, options: &SceneOptions) -> Result<(Scene, Vec<usize>)> {
|
||||
if self.version != 0 && self.version < 6000 {
|
||||
return Err(crate::Error::Unsupported(format!(
|
||||
"FBX {}.{} (only FBX 6.x and 7.x files are read)",
|
||||
@@ -927,6 +932,7 @@ impl Document {
|
||||
// One mesh per geometry and material-slot combination, shared by instances.
|
||||
let mut mesh_of: HashMap<(Key, Vec<usize>), usize> = HashMap::new();
|
||||
let mut placed = HashSet::new();
|
||||
let mut placement_nodes = HashMap::new();
|
||||
for (id, model) in &models {
|
||||
let Some(ni) = node_of.get(id).copied() else { continue };
|
||||
let geometries = b.geometries_of(*id);
|
||||
@@ -957,7 +963,7 @@ impl Document {
|
||||
};
|
||||
let transform = geometric(&Props::of(model));
|
||||
let occupied = b.scene.nodes.get(ni).is_some_and(|n| n.mesh.is_some());
|
||||
if transform.is_some() || occupied {
|
||||
let placement_node = if transform.is_some() || occupied {
|
||||
b.node_room()?;
|
||||
let child = b.scene.nodes.len();
|
||||
b.scene.nodes.push(Node {
|
||||
@@ -969,13 +975,19 @@ impl Document {
|
||||
if let Some(node) = b.scene.nodes.get_mut(ni) {
|
||||
node.children.push(child);
|
||||
}
|
||||
} else if let Some(node) = b.scene.nodes.get_mut(ni) {
|
||||
node.mesh = Some(mesh_index);
|
||||
}
|
||||
child
|
||||
} else {
|
||||
if let Some(node) = b.scene.nodes.get_mut(ni) {
|
||||
node.mesh = Some(mesh_index);
|
||||
}
|
||||
ni
|
||||
};
|
||||
placement_nodes.insert((gid, Some(*id)), placement_node);
|
||||
}
|
||||
}
|
||||
// Geometry with no Model connection still contributes a mesh at the origin.
|
||||
let placed_ids: HashSet<Key<'_>> = placed.iter().map(|(gid, _)| *gid).collect();
|
||||
let mut unplaced = Vec::new();
|
||||
for geom in objects.children.iter().filter(|o| o.name == "Geometry" && o.child("Vertices").is_some()) {
|
||||
let Some(gid) = key(geom.props.first()) else { continue };
|
||||
if placed_ids.contains(&gid) {
|
||||
@@ -989,15 +1001,47 @@ impl Document {
|
||||
b.node_room()?;
|
||||
let mesh_index = b.scene.meshes.len();
|
||||
b.scene.meshes.push(mesh);
|
||||
b.scene.roots.push(b.scene.nodes.len());
|
||||
let node_index = b.scene.nodes.len();
|
||||
b.scene.roots.push(node_index);
|
||||
b.scene.nodes.push(Node { mesh: Some(mesh_index), ..Node::default() });
|
||||
unplaced.push((key_text(gid), node_index));
|
||||
}
|
||||
let placements = ordered_placements(&models, &links, &placement_nodes, unplaced);
|
||||
let mut scene = b.scene;
|
||||
count_warning(&mut scene.warnings, b.skipped, "polygons with fewer than three corners or a vertex that is not there were left out");
|
||||
Ok(scene)
|
||||
Ok((scene, placements))
|
||||
}
|
||||
}
|
||||
|
||||
fn ordered_placements<'a>(
|
||||
models: &[(Key<'a>, &Record)],
|
||||
links: &[(Key<'a>, Key<'a>)],
|
||||
placement_nodes: &HashMap<(Key<'a>, Option<Key<'a>>), usize>,
|
||||
mut unplaced: Vec<(String, usize)>,
|
||||
) -> Vec<usize> {
|
||||
let mut placements = Vec::new();
|
||||
let mut seen = HashSet::new();
|
||||
let mut add = |key| {
|
||||
if let Some(index) = placement_nodes.get(&key).copied()
|
||||
&& seen.insert(index)
|
||||
{
|
||||
placements.push(index);
|
||||
}
|
||||
};
|
||||
// FBX 6 stores geometry in its model; connected FBX 7 geometry follows connection order.
|
||||
for (id, model) in models {
|
||||
if model.child("Vertices").is_some() {
|
||||
add((*id, Some(*id)));
|
||||
}
|
||||
}
|
||||
for (child, parent) in links {
|
||||
add((*child, Some(*parent)));
|
||||
}
|
||||
unplaced.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
placements.extend(unplaced.into_iter().map(|(_, index)| index));
|
||||
placements
|
||||
}
|
||||
|
||||
/// Diffuse material values as stored in FBX, before color-space conversion.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct DiffuseMaterial {
|
||||
@@ -1131,6 +1175,13 @@ enum Key<'a> {
|
||||
Name(&'a str),
|
||||
}
|
||||
|
||||
fn key_text(key: Key<'_>) -> String {
|
||||
match key {
|
||||
Key::Id(i) => i.to_string(),
|
||||
Key::Name(s) => s.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn key(p: Option<&Prop>) -> Option<Key<'_>> {
|
||||
match p? {
|
||||
Prop::Int(i) => Some(Key::Id(*i)),
|
||||
@@ -1528,8 +1579,13 @@ impl<'a> Build<'a, '_> {
|
||||
}
|
||||
None => materials.first().copied(),
|
||||
};
|
||||
let at = match part_of.get(&slot) {
|
||||
Some(at) => *at,
|
||||
let existing = if self.strict_geometry {
|
||||
parts.last().filter(|part| part.material == slot).map(|_| parts.len() - 1)
|
||||
} else {
|
||||
part_of.get(&slot).copied()
|
||||
};
|
||||
let at = match existing {
|
||||
Some(at) => at,
|
||||
None => {
|
||||
if parts.len() >= limits.max_parts {
|
||||
return Err(too_large(KIND, format!("more than {} material slots in a mesh", limits.max_parts)));
|
||||
|
||||
@@ -844,3 +844,26 @@ fn strict_binary_requires_a_closing_record() {
|
||||
assert!(read_document(truncated, &Limits::default()).is_ok());
|
||||
assert!(crate::fbx::read_document_strict(truncated, &Limits::default()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placement_and_polygon_order_follow_the_fbx() {
|
||||
let text = ASCII.replace("a: 0,1,2,-4,0,1,-5", "a: 0,1,2,-4,0,1,-5,0,1,-5").replace("a: 0,1\n", "a: 0,1,0\n");
|
||||
let doc = document(text.as_bytes());
|
||||
let options = crate::fbx::SceneOptions { strict_geometry: true, ..Default::default() };
|
||||
let (scene, placements) = doc.scene_with_placements(&Limits::default(), &mut no_external, &options).unwrap();
|
||||
assert_eq!(placements.len(), 1);
|
||||
let mesh = scene.nodes[placements[0]].mesh.unwrap();
|
||||
let parts = &scene.meshes[mesh].parts;
|
||||
assert_eq!(parts.len(), 3);
|
||||
assert_eq!(parts[0].material, parts[2].material);
|
||||
assert_ne!(parts[0].material, parts[1].material);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placement_order_follows_connections_not_model_declarations() {
|
||||
let text = with("Model: 201, \"Model::Later\", \"Mesh\" {}\n", "").replace("C: \"OO\",100,300", "C: \"OO\",100,201\n C: \"OO\",100,300");
|
||||
let doc = document(text.as_bytes());
|
||||
let (scene, placements) = doc.scene_with_placements(&Limits::default(), &mut no_external, &Default::default()).unwrap();
|
||||
let names: Vec<_> = placements.iter().map(|i| scene.nodes[*i].name.as_str()).collect();
|
||||
assert_eq!(names, ["Later", "Shape geometry"]);
|
||||
}
|
||||
Reference in new issue
Block a user