Read FBX camera lenses and placements

This commit is contained in:
Dylan M. Taylor committed 2026-11-15 18:19:07 +00:00
1 parent 8bd1412eaa
commit ef91c57ad0
2 files changed
+76 -4

No files matched your search

+41 -4
View File
@@ -39,8 +39,7 @@
//! name is listed in [`Scene::external`]. The names come from the file: a resolver must not trust
//! them as paths.
//!
//! Not read: FBX 5 and older, animation stacks, skin deformers, blend shapes, cameras and
//! lights, NURBS and patches.
//! Not read: FBX 5 and older, animation stacks, skin deformers, blend shapes, lights, NURBS and patches.
//!
//! [`Limits::max_data_bytes`] bounds what one file may keep in memory: its parsed values (an
//! array element counts 8 bytes whatever its type in the file), the meshes built from them and
@@ -50,8 +49,8 @@ use std::cell::Cell;
use std::collections::HashMap;
use crate::{
AlphaMode, Corner, IDENTITY, Image, Limits, Mat4, Material, Mesh, NONE, Node, Part, Resolver, Result, Scene, TextureRef, base64_decode,
count_warning, malformed, mat_mul, srgb_to_linear, too_large,
AlphaMode, Camera, Corner, IDENTITY, Image, Limits, Mat4, Material, Mesh, NONE, Node, Part, Projection, Resolver, Result, Scene, TextureRef,
base64_decode, count_warning, malformed, mat_mul, srgb_to_linear, too_large,
};
const KIND: &str = "FBX";
@@ -782,6 +781,44 @@ impl Document {
}
}
// FBX cameras look along +X; a child turns the neutral camera's -Z onto +X.
// Keep this separate from the Model so its children retain their original transforms.
for (id, model) in &models {
let class_index = if matches!(model.props.first(), Some(Prop::Int(_))) { 2 } else { 1 };
if model.str_at(class_index) != Some("Camera") {
continue;
}
let name = short_name(model.first_str().unwrap_or(""));
if name.starts_with("Producer ") {
continue;
}
let Some(ni) = node_of.get(id).copied() else { continue };
let lens = b.children_of(*id, "NodeAttribute").last().and_then(|(key, _)| b.by_key.get(key)).copied().unwrap_or(model);
let props = Props::of(lens);
let positive = |key| props.f(key).filter(|v| *v > 0.0);
let projection = if props.f("CameraProjectionType") == Some(1.0) {
let mag = positive("OrthoZoom").unwrap_or(1.0);
Projection::Orthographic { xmag: mag, ymag: mag, znear: 0.01, zfar: 1000.0 }
} else {
let yfov = match positive("FocalLength") {
Some(focal) => 2.0 * (positive("FilmHeight").unwrap_or(0.612) * 25.4 / (2.0 * focal)).atan(),
None => positive("FieldOfViewY").or_else(|| positive("FieldOfView")).unwrap_or(40.0).to_radians(),
};
Projection::Perspective { yfov, aspect: None, znear: 0.01, zfar: None }
};
if b.scene.cameras.len() >= limits.max_extras {
return Err(too_large(KIND, "too many cameras"));
}
b.node_room()?;
let ci = b.scene.cameras.len();
b.scene.cameras.push(Camera { name: name.to_string(), projection });
let child = b.scene.nodes.len();
b.scene.nodes.push(Node { name: format!("{name} camera"), transform: rotate(1, -90.0), camera: Some(ci), ..Node::default() });
if let Some(node) = b.scene.nodes.get_mut(ni) {
node.children.push(child);
}
}
// One mesh per Model with Mesh geometry (material slots are per Model). Models instancing
// the same geometry with the same materials share one mesh rather than each building a
// copy.
+35
View File
@@ -627,3 +627,38 @@ fn fbx6_model_holds_its_mesh() {
assert_eq!(d.unit_scale(), Some(100.0));
}
}
#[test]
fn cameras_keep_lens_and_model_hierarchy() {
let text = r#"FBXHeaderExtension: { FBXVersion: 7400 }
Objects: {
Model: 1, "Model::Rig", "Null" { Properties70: { P: "Lcl Translation", "", "", "", 1,2,3 } }
Model: 2, "Model::View", "Camera" { }
NodeAttribute: 3, "NodeAttribute::Lens", "Camera" { Properties70: {
P: "FocalLength", "", "", "", 50
P: "FilmHeight", "", "", "", 1
} }
Model: 4, "Model::Ortho", "Camera" { Properties70: {
P: "CameraProjectionType", "", "", "", 1
P: "OrthoZoom", "", "", "", 3
} }
Model: 5, "Model::Producer Perspective", "Camera" { }
}
Connections: {
C: "OO",2,1
C: "OO",3,2
}
"#;
let scene = parse(text.as_bytes()).unwrap();
assert_eq!(scene.cameras.len(), 2);
let crate::Projection::Perspective { yfov, .. } = scene.cameras[0].projection else { panic!("perspective") };
assert!((yfov - 2.0 * (25.4_f64 / 100.0).atan()).abs() < 1e-12);
assert!(matches!(scene.cameras[1].projection, crate::Projection::Orthographic { ymag: 3.0, .. }));
let camera_node = scene.nodes.iter().find(|n| n.camera == Some(0)).unwrap();
let forward = crate::transform_point(&camera_node.transform, [0.0, 0.0, -1.0]);
assert!((forward[0] - 1.0).abs() < 1e-12);
let rig = scene.nodes.iter().find(|n| n.name == "Rig").unwrap();
assert!(rig.children.iter().any(|i| scene.nodes[*i].name == "View"));
let limits = Limits { max_extras: 1, ..Limits::default() };
assert!(matches!(read(text.as_bytes(), &limits, &mut no_external), Err(Error::TooLarge(_))));
}