diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index 49191c0c0..e65737723 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -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"; @@ -798,6 +797,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. diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index d5978883e..aafdd18a9 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -641,3 +641,38 @@ fn ascii_budget_applies_while_reading_values_and_records() { let ignored = format!(";{}\nObjects: {{}}", ",".repeat(10_000)); assert!(read_document(ignored.as_bytes(), &limits).is_ok()); } + +#[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(_)))); +}