diff --git a/README.md b/README.md index 7a5850ba..2c0f010c 100644 --- a/README.md +++ b/README.md @@ -190,7 +190,6 @@ from our code against public documentation. **What's missing:** - hardware ray tracing in Rendering mode; - pen tilt (the windowing layer doesn't report it yet); -- cameras from FBX, COLLADA and USD files (glTF cameras are imported); - Alembic files in the older HDF5 container, and Alembic animation (the first sample is read); - displacement in shadows and the path tracer (the viewport displaces); - a native-speaker review of the translations; diff --git a/crates/formats/src/mesh_import/dae.rs b/crates/formats/src/mesh_import/dae.rs index 31af1865..d23ea61b 100644 --- a/crates/formats/src/mesh_import/dae.rs +++ b/crates/formats/src/mesh_import/dae.rs @@ -1,7 +1,7 @@ //! COLLADA (.dae): `triangles`, `polylist` and `polygons` with VERTEX / NORMAL / TEXCOORD inputs, //! materials bound through `instance_geometry` (and skinned `instance_controller`), the visual -//! scene's node transforms (matrix, translate, rotate, scale), and the asset's unit and up axis -//! (converted to meters, Y up, like glTF). +//! scene's node transforms (matrix, translate, rotate, scale, lookat), instanced cameras, and the +//! asset's unit and up axis (converted to meters, Y up, like glTF). use std::collections::HashMap; use std::rc::Rc; @@ -9,7 +9,7 @@ use std::rc::Rc; use quick_xml::events::{BytesStart, Event}; use texturecraft_geom::glam::{Mat3, Mat4, Vec2, Vec3, Vec4}; -use super::{Corner, PolygonSink, RawMesh, check_vertices}; +use super::{Corner, PolygonSink, RawCamera, RawMesh, check_vertices}; use crate::{FormatError, Result, malformed}; const KIND: &str = "COLLADA"; @@ -171,6 +171,7 @@ fn read_inner(bytes: &[u8]) -> Result { }; let global = up * Mat4::from_scale(Vec3::splat(meter)); let mut sink = PolygonSink::default(); + let mut cameras = Vec::new(); let scene = root.child("scene").and_then(|s| s.child("instance_visual_scene")).and_then(|i| d.get(i.attr("url"))); let scene = scene.or_else(|| root.child("library_visual_scenes").and_then(|l| l.child("visual_scene"))); let Some(scene) = scene else { @@ -191,6 +192,10 @@ fn read_inner(bytes: &[u8]) -> Result { let m = parent * node_transform(node)?; sink.part = node.attr("name").or_else(|| node.attr("id")).unwrap_or_default().to_string(); for inst in &node.children { + if inst.name == "instance_camera" { + cameras.extend(d.get(inst.attr("url")).and_then(|c| camera(c, m, &sink.part))); + continue; + } let (geom, bind_shape) = match inst.name.as_str() { "instance_geometry" => (d.get(inst.attr("url")), Mat4::IDENTITY), "instance_controller" => { @@ -211,7 +216,24 @@ fn read_inner(bytes: &[u8]) -> Result { let children: Vec<&El> = children.collect(); stack.extend(children.into_iter().rev().map(|c| (c, m, depth + 1))); } - Ok(sink.finish()) + let mut raw = sink.finish(); + raw.cameras = cameras; + Ok(raw) +} + +/// A `` instanced by node `name` at `m`: COLLADA cameras look down -Z with +Y up. +fn camera(cam: &El, m: Mat4, name: &str) -> Option { + let tc = cam.child("optics")?.child("technique_common")?; + let num = |e: &El, k: &str| e.child(k).and_then(|v| v.text.trim().parse::().ok()).filter(|v| v.is_finite() && *v > 0.0); + let name = if name.is_empty() { cam.attr("name").or_else(|| cam.attr("id")).unwrap_or_default() } else { name }; + if let Some(p) = tc.child("perspective") { + let x_to_y = |x: f32| Some(2.0 * ((x * 0.5).to_radians().tan() / num(p, "aspect_ratio")?).atan().to_degrees()); + let fov = num(p, "yfov").or_else(|| num(p, "xfov").and_then(x_to_y)).or_else(|| num(p, "xfov")).unwrap_or(40.0); + return Some(RawCamera::placed(name.into(), m, Vec3::NEG_Z, fov, None)); + } + let o = tc.child("orthographic")?; + let half = num(o, "ymag").or_else(|| Some(num(o, "xmag")? / num(o, "aspect_ratio")?)).or_else(|| num(o, "xmag")).unwrap_or(1.0); + Some(RawCamera::placed(name.into(), m, Vec3::NEG_Z, 40.0, Some(half))) } /// Row-major 4×4 floats to a matrix. @@ -225,7 +247,7 @@ fn node_transform(node: &El) -> Result { let mut m = Mat4::IDENTITY; for t in &node.children { let v = match t.name.as_str() { - "matrix" | "translate" | "rotate" | "scale" => t.floats()?, + "matrix" | "translate" | "rotate" | "scale" | "lookat" => t.floats()?, _ => continue, }; let step = match (t.name.as_str(), v.as_slice()) { @@ -233,6 +255,10 @@ fn node_transform(node: &El) -> Result { ("translate", [x, y, z, ..]) => Some(Mat4::from_translation(Vec3::new(*x, *y, *z))), ("scale", [x, y, z, ..]) => Some(Mat4::from_scale(Vec3::new(*x, *y, *z))), ("rotate", [x, y, z, deg, ..]) => Vec3::new(*x, *y, *z).try_normalize().map(|axis| Mat4::from_axis_angle(axis, deg.to_radians())), + // Eye, interest point and up: the node sits at the eye looking at the interest. + ("lookat", [ex, ey, ez, ix, iy, iz, ux, uy, uz, ..]) => { + Some(Mat4::look_at_rh(Vec3::new(*ex, *ey, *ez), Vec3::new(*ix, *iy, *iz), Vec3::new(*ux, *uy, *uz)).inverse()).filter(|m| m.is_finite()) + } _ => None, }; m *= step.unwrap_or(Mat4::IDENTITY); diff --git a/crates/formats/src/mesh_import/fbx.rs b/crates/formats/src/mesh_import/fbx.rs index a902b5d4..709e51eb 100644 --- a/crates/formats/src/mesh_import/fbx.rs +++ b/crates/formats/src/mesh_import/fbx.rs @@ -4,7 +4,8 @@ //! meters, Y up, like glTF). FBX 6 keeps the geometry inside its Model, names objects instead of //! numbering them and lists properties in `Properties60`; both read into the same scene. //! Materials bring their diffuse color and, when the file embeds it (a Video's `Content`), the -//! diffuse texture (for high-poly color map bakes). +//! diffuse texture (for high-poly color map bakes). Camera Models become the file's cameras (lens +//! from their camera NodeAttribute, or the Model's own properties in FBX 6). use std::borrow::Cow; use std::collections::{HashMap, HashSet}; @@ -13,7 +14,7 @@ use std::io::Read as _; use base64::Engine as _; use texturecraft_geom::glam::{Mat3, Mat4, Vec2, Vec3, Vec4}; -use super::{Corner, PolygonSink, RawBaseColor, RawMesh, check_vertices}; +use super::{Corner, PolygonSink, RawBaseColor, RawCamera, RawMesh, check_vertices, lens_fov}; use crate::{FormatError, Result, malformed}; const KIND: &str = "FBX"; @@ -83,6 +84,11 @@ impl Node { clean_name(if self.int_at(0).is_some() { self.str_at(1) } else { self.str_at(0) }) } + /// An object's class (`Mesh`, `Camera`, …): after its name. + fn class(&self) -> &str { + self.str_at(if self.int_at(0).is_some() { 2 } else { 1 }) + } + fn str_at(&self, i: usize) -> &str { match self.props.get(i) { Some(Prop::S(s)) => s, @@ -430,7 +436,7 @@ fn read_inner(bytes: &[u8]) -> Result { let root = |name: &str| nodes.iter().find(|n| n.name == name); let objects = root("Objects").ok_or_else(|| malformed(KIND, "the file has no Objects"))?; let (mut geometries, mut models, mut materials) = (HashMap::new(), HashMap::new(), HashMap::new()); - let (mut textures, mut videos) = (HashMap::new(), HashMap::new()); + let (mut textures, mut videos, mut attributes) = (HashMap::new(), HashMap::new(), HashMap::new()); // (geometry, model) instances; an FBX 6 Model holds its geometry itself. let mut instances: Vec<(String, Option)> = Vec::new(); for o in &objects.children { @@ -447,12 +453,14 @@ fn read_inner(bytes: &[u8]) -> Result { "Material" => materials.insert(id, o), "Texture" => textures.insert(id, o), "Video" => videos.insert(id, o), + "NodeAttribute" => attributes.insert(id, o), _ => None, }; } // Model parents, and each Model's materials in slot order. let mut parents: HashMap = HashMap::new(); let mut slots: HashMap> = HashMap::new(); + let mut attribute_of: HashMap = HashMap::new(); let connections: Vec<&Node> = root("Connections").into_iter().flat_map(|c| c.all("C").chain(c.all("Connect"))).collect(); for c in &connections { let (Some(child), Some(parent)) = (c.id_at(1), c.id_at(2)) else { continue }; @@ -465,6 +473,8 @@ fn read_inner(bytes: &[u8]) -> Result { instances.push((child, Some(parent))); } else if let Some(m) = materials.get(&child) { slots.entry(parent).or_default().push(material_name(&child, m)); + } else if let Some(a) = attributes.get(&child) { + attribute_of.insert(parent, *a); } } let placed: HashSet<&String> = instances.iter().map(|(g, _)| g).collect(); @@ -493,9 +503,30 @@ fn read_inner(bytes: &[u8]) -> Result { } let mut raw = sink.finish(); raw.base_colors = base_colors; + // In file order; FBX 6's built-in "Producer" views are not the scene's cameras. + for o in objects.children.iter().filter(|o| o.name == "Model" && o.class() == "Camera" && !o.object_name().starts_with("Producer ")) { + let Some(id) = o.id_at(0) else { continue }; + if let Ok(m) = world(&models, &parents, &id) { + raw.cameras.push(camera(o, attribute_of.get(&id).copied().unwrap_or(o), global * m)); + } + } Ok(raw) } +/// A camera Model at `m` with its lens in `lens`: FBX cameras look down +X with +Y up. The +/// vertical field of view comes from the focal length (mm) over the film height (inches). +/// shortcut: `OrthoZoom` is taken as the orthographic half view height in scene units; check it +/// against an exporter's orthographic camera when one is at hand. +fn camera(model: &Node, lens: &Node, m: Mat4) -> RawCamera { + let num = |k: &str| lens.number(k).map(|v| v as f32).filter(|v| v.is_finite() && *v > 0.0); + let fov = match num("FocalLength") { + Some(f) => lens_fov(f, num("FilmHeight").unwrap_or(0.612) * 25.4), + None => num("FieldOfViewY").or_else(|| num("FieldOfView")).unwrap_or(40.0), + }; + let ortho = (lens.number("CameraProjectionType") == Some(1.0)).then(|| num("OrthoZoom").unwrap_or(1.0)); + RawCamera::placed(model.object_name().to_string(), m, Vec3::X, fov, ortho) +} + /// A material's name in the mesh: its object name, or `Material_` when it has none. fn material_name(id: &str, m: &Node) -> String { let name = m.object_name(); diff --git a/crates/formats/src/mesh_import/gltf.rs b/crates/formats/src/mesh_import/gltf.rs index a404503b..bf1f88e0 100644 --- a/crates/formats/src/mesh_import/gltf.rs +++ b/crates/formats/src/mesh_import/gltf.rs @@ -63,7 +63,7 @@ fn read_inner(bytes: &[u8]) -> Result { gltf::camera::Projection::Orthographic(o) => (40.0, Some(o.ymag())), }; let name = cam.name().or_else(|| node.name()).map_or_else(|| format!("Camera {}", raw.cameras.len() + 1), str::to_string); - raw.cameras.push(super::RawCamera { name, eye: m.transform_point3(Vec3::ZERO), forward: m.transform_vector3(Vec3::NEG_Z), fov, ortho_half_height }); + raw.cameras.push(super::RawCamera::placed(name, m, Vec3::NEG_Z, fov, ortho_half_height)); } if let Some(mesh) = node.mesh() { let part = node.name().or_else(|| mesh.name()).unwrap_or_default(); diff --git a/crates/formats/src/mesh_import/mod.rs b/crates/formats/src/mesh_import/mod.rs index a1a977a3..a8e6ed1d 100644 --- a/crates/formats/src/mesh_import/mod.rs +++ b/crates/formats/src/mesh_import/mod.rs @@ -3,7 +3,8 @@ //! model, COLLADA node, USD Mesh prim, Alembic PolyMesh; unnamed parts take the file stem), materials contiguous, //! glTF UV convention (origin top-left, v down), //! normals (computed when missing), vertex colors (glTF, OBJ, PLY), UVs (auto-unwrapped when -//! missing) and MikkTSpace tangents. glTF cameras come along as the project's camera list. +//! missing) and MikkTSpace tangents. glTF, FBX, COLLADA and USD cameras come along as the +//! project's camera list. mod abc; mod dae; @@ -22,7 +23,7 @@ pub(crate) mod usd; use std::collections::HashMap; use std::collections::hash_map::Entry; -use texturecraft_geom::glam::{Vec2, Vec3, Vec4}; +use texturecraft_geom::glam::{Mat4, Vec2, Vec3, Vec4}; use texturecraft_geom::unwrap::{all_triangles_degenerate, auto_unwrap, weld_ids}; use texturecraft_geom::{BaseColor, MAX_TRIANGLES, Mesh, SubMesh, UvGBuffer}; @@ -49,7 +50,7 @@ pub const MESH_EXTENSIONS: [&str; 12] = ["gltf", "glb", "fbx", "dae", "usd", "us pub struct ImportedMesh { pub mesh: Mesh, pub report: MeshReport, - /// Cameras of the file (glTF), as orbit cameras around the mesh. + /// Cameras of the file (glTF, FBX, COLLADA, USD), as orbit cameras around the mesh. pub cameras: Vec, } @@ -66,6 +67,27 @@ pub(crate) struct RawCamera { pub ortho_half_height: Option, } +impl RawCamera { + /// A camera at the origin of `m` (local to world) looking along local `forward` with +Y + /// up; `ortho_half_height` is in local units. + pub fn placed(name: String, m: Mat4, forward: Vec3, fov: f32, ortho_half_height: Option) -> RawCamera { + let scale = m.transform_vector3(Vec3::Y).length(); + RawCamera { + name, + eye: m.transform_point3(Vec3::ZERO), + forward: m.transform_vector3(forward), + fov, + ortho_half_height: ortho_half_height.map(|h| h * scale), + } + } +} + +/// Vertical field of view in degrees of a lens of `focal` length over a film `aperture` high +/// (same units). +pub(crate) fn lens_fov(focal: f32, aperture: f32) -> f32 { + 2.0 * (aperture / (2.0 * focal)).atan().to_degrees() +} + /// What the importer found and fixed, for the import dialog. #[derive(Clone, Debug, Default, PartialEq)] pub struct MeshReport { @@ -375,7 +397,8 @@ fn geometry(raw: RawMesh) -> Result<(Mesh, Vec, Option>)> { /// Complete a raw mesh: submeshes, normals, UVs, tangents, validation and the report. pub(crate) fn finish(mut raw: RawMesh) -> Result { - let raw_cameras = std::mem::take(&mut raw.cameras); + let mut raw_cameras = std::mem::take(&mut raw.cameras); + raw_cameras.truncate(MAX_CAMERAS); let (mut mesh, mut warnings, uvs) = geometry(raw)?; let uvs_generated = match uvs { Some(uv) if uv.len() == mesh.positions.len() => { diff --git a/crates/formats/src/mesh_import/scene_tests.rs b/crates/formats/src/mesh_import/scene_tests.rs index b6828dd9..68b035e9 100644 --- a/crates/formats/src/mesh_import/scene_tests.rs +++ b/crates/formats/src/mesh_import/scene_tests.rs @@ -46,7 +46,8 @@ fn layer(name: &'static str, mapping: &'static str, reference: &'static str, dat n(name, vec![P::I(0)], children) } -/// A quad (material slot 0) and a triangle (slot 1) under a parented Model, two materials. +/// A quad (material slot 0) and a triangle (slot 1) under a parented Model, two materials, and +/// a camera 5 units out on +Z (Y up) turned to look down -Z, with its lens on a NodeAttribute. fn scene(z_up: bool) -> Vec { // The Z-up variant also turns the parent with PreRotation instead of Lcl Rotation. let rotation = if z_up { "PreRotation" } else { "Lcl Rotation" }; @@ -97,9 +98,19 @@ fn scene(z_up: bool) -> Vec { model(21, "Child\u{0}\u{1}Model", vec![prop("Lcl Translation", &[100.0, 0.0, 0.0])]), material(30, "Red\u{0}\u{1}Material"), material(31, "Blue\u{0}\u{1}Material"), + n( + "Model", + vec![P::L(40), P::S("Shot\u{0}\u{1}Model"), P::S("Camera")], + vec![props70(vec![prop("Lcl Translation", &[0.0, 0.0, 500.0]), prop("Lcl Rotation", &[0.0, 90.0, 0.0])])], + ), + n( + "NodeAttribute", + vec![P::L(41), P::S("\u{0}\u{1}NodeAttribute"), P::S("Camera")], + vec![props70(vec![prop("FocalLength", &[35.0]), prop("FilmHeight", &[0.612])])], + ), ], ), - n("Connections", vec![], vec![c(10, 21), c(21, 20), c(20, 0), c(30, 21), c(31, 21)]), + n("Connections", vec![], vec![c(10, 21), c(21, 20), c(20, 0), c(30, 21), c(31, 21), c(40, 0), c(41, 40)]), ] } @@ -267,13 +278,23 @@ fn check_fbx_scene(m: &Mesh, z_up: bool) { } } +/// The camera of [`scene`] (Y up): 35 mm over a 0.612" film, 5 m out, looking at the mesh. +fn check_fbx_camera(im: &ImportedMesh) { + let [shot] = im.cameras.as_slice() else { panic!("{:?}", im.cameras) }; + let near = |a: f32, b: f32| (a - b).abs() < 1e-3; + assert_eq!(shot.name, "Shot"); + assert!(!shot.ortho && near(shot.fov, 2.0 * (0.612f32 * 25.4 / 70.0).atan().to_degrees()), "{shot:?}"); + assert!(near(shot.yaw, 0.0) && near(shot.pitch, 0.0) && near(shot.distance, 4.0), "{shot:?}"); +} + #[test] fn fbx_binary_32_and_64_bit_compressed_or_not() { let nodes = scene(false); for (version, compress) in [(7400, false), (7400, true), (7500, false), (7500, true)] { let bytes = binary_fbx(&nodes, version, compress); - let m = import_mesh_bytes("scene.fbx", &bytes).unwrap_or_else(|e| panic!("{version} {compress}: {e}")).mesh; - check_fbx_scene(&m, false); + let im = import_mesh_bytes("scene.fbx", &bytes).unwrap_or_else(|e| panic!("{version} {compress}: {e}")); + check_fbx_scene(&im.mesh, false); + check_fbx_camera(&im); } } @@ -314,7 +335,9 @@ fn fbx_diffuse_colors_and_embedded_textures_reach_high_polys() { #[test] fn fbx_ascii_and_z_up() { let text = ascii_fbx(&scene(false)); - check_fbx_scene(&import_mesh_bytes("scene.FBX", text.as_bytes()).unwrap().mesh, false); + let im = import_mesh_bytes("scene.FBX", text.as_bytes()).unwrap(); + check_fbx_scene(&im.mesh, false); + check_fbx_camera(&im); check_fbx_scene(&import_mesh_bytes("z.fbx", ascii_fbx(&scene(true)).as_bytes()).unwrap().mesh, true); check_fbx_scene(&import_mesh_bytes("z.fbx", &binary_fbx(&scene(true), 7700, true)).unwrap().mesh, true); } @@ -331,6 +354,12 @@ Definitions: { Count: 4 } Objects: { + Model: "Model::Producer Perspective", "Camera" { + Version: 232 + Properties60: { + Property: "FocalLength", "Number", "A+",35 + } + } Model: "Model::Parent", "Null" { Version: 232 Properties60: { @@ -466,7 +495,9 @@ fn scene61() -> Vec { #[test] fn fbx_6_ascii_and_binary_read_like_7() { - check_fbx_scene(&import_mesh_bytes("six.fbx", FBX61.as_bytes()).unwrap().mesh, false); + let six = import_mesh_bytes("six.fbx", FBX61.as_bytes()).unwrap(); + check_fbx_scene(&six.mesh, false); + assert!(six.cameras.is_empty(), "the Producer views are not scene cameras"); check_fbx_scene(&import_mesh_bytes("six.fbx", &binary_fbx(&scene61(), 6100, false)).unwrap().mesh, false); hammer("x.fbx", FBX61.as_bytes()); hammer("x.fbx", &binary_fbx(&scene61(), 6100, true)); @@ -564,6 +595,9 @@ const DAE: &str = r##" + + 30 + @@ -576,6 +610,10 @@ const DAE: &str = r##" + + 0 -10 0 0 0 0 0 0 1 + + @@ -609,6 +647,11 @@ fn dae_polylist_z_up_units_and_node_transforms() { // Parts are named after their nodes (ids when unnamed). let parts: Vec<&str> = m.submeshes.iter().map(|s| s.name.as_str()).collect(); assert!(parts.contains(&"child") && parts.contains(&"other"), "{parts:?}"); + // The camera looks at the origin from -10 along Z-up Y: on +Z once converted, facing -Z. + let [cam] = r.cameras.as_slice() else { panic!("{:?}", r.cameras) }; + let near = |a: f32, b: f32| (a - b).abs() < 1e-3; + assert_eq!(cam.name, "Overview"); + assert!(!cam.ortho && near(cam.fov, 30.0) && near(cam.yaw, 0.0) && near(cam.pitch, 0.0), "{cam:?}"); } #[test] diff --git a/crates/formats/src/mesh_import/usd/mod.rs b/crates/formats/src/mesh_import/usd/mod.rs index b2e153d4..6af1d254 100644 --- a/crates/formats/src/mesh_import/usd/mod.rs +++ b/crates/formats/src/mesh_import/usd/mod.rs @@ -5,7 +5,8 @@ //! (`primvars:normals` or `normals`), UVs (`primvars:st`, `st0`, `UVMap` or the first //! `texCoord2f[]` primvar, indexed or not, any interpolation), `orientation`, and material names //! from `material:binding` (inherited down the hierarchy) and `materialBind` GeomSubsets. Stage -//! `metersPerUnit` (USD's fallback is 0.01) and `upAxis` become meters, Y up. A material's +//! `metersPerUnit` (USD's fallback is 0.01) and `upAxis` become meters, Y up. `Camera` prims +//! (focalLength, verticalAperture, projection) become the file's cameras. A material's //! UsdPreviewSurface gives its base color: the `diffuseColor` value, or the UsdUVTexture it is //! connected to when the texture file is inside the `.usdz` package (for color map bakes). A loose //! layer is packaged with its texture files first ([`package`]). @@ -25,7 +26,7 @@ use std::io::Read as _; use texturecraft_geom::glam::{DMat4, DQuat, DVec3, DVec4, Mat3, Mat4, Vec2, Vec3, Vec4}; use super::external::below; -use super::{Corner, MAX_MESH_FILE_BYTES, PolygonSink, RawBaseColor, RawMesh}; +use super::{Corner, MAX_MESH_FILE_BYTES, PolygonSink, RawBaseColor, RawCamera, RawMesh, lens_fov}; use crate::{FormatError, Result, malformed}; pub(crate) const KIND: &str = "USD"; @@ -248,6 +249,7 @@ struct Stage<'a> { layer: &'a Layer, by_path: HashMap<&'a str, usize>, sink: PolygonSink, + cameras: Vec, /// Up axis and units: stage space to meters, Y up. global: Mat4, visits: usize, @@ -263,6 +265,7 @@ fn compose(layer: &Layer, mut assets: Option<&mut (dyn Assets + '_)>) -> Result< } stage.visit(&[0], stage.global, None, 0)?; let mut raw = stage.sink.finish(); + raw.cameras = std::mem::take(&mut stage.cameras); let mut names: Vec = raw.groups.iter().map(|(m, _, _)| m.clone()).collect(); names.sort(); names.dedup(); @@ -284,7 +287,7 @@ impl<'a> Stage<'a> { _ => Mat4::IDENTITY, }; let by_path = layer.prims.iter().enumerate().map(|(i, p)| (p.path.as_str(), i)).collect(); - Stage { layer, by_path, sink: PolygonSink::default(), global: up * Mat4::from_scale(Vec3::splat(mpu)), visits: 0 } + Stage { layer, by_path, sink: PolygonSink::default(), cameras: Vec::new(), global: up * Mat4::from_scale(Vec3::splat(mpu)), visits: 0 } } fn spec(&self, i: usize) -> Option<&'a PrimSpec> { @@ -430,6 +433,7 @@ impl<'a> Stage<'a> { let binding = self.binding(c).or(binding); match self.type_name(c) { "Mesh" => self.mesh(c, world, binding.as_deref(), depth)?, + "Camera" => self.camera(c, world), "PointInstancer" => self.sink.warn("PointInstancer prims were skipped".into()), _ => {} } @@ -441,6 +445,16 @@ impl<'a> Stage<'a> { Ok(()) } + /// A camera prim (USD's lens fallbacks: 50 mm over 15.2908 mm; apertures are in tenths of + /// a scene unit, which orthographic views measure). + fn camera(&mut self, c: &[usize], world: Mat4) { + let name = self.spec(c.first().copied().unwrap_or_default()).map(|s| s.name.clone()).unwrap_or_default(); + let num = |k: &str, fallback: f32| self.nums(c, k).and_then(|v| v.first()).map(|v| *v as f32).filter(|v| v.is_finite() && *v > 0.0).unwrap_or(fallback); + let (focal, aperture) = (num("focalLength", 50.0), num("verticalAperture", 15.2908)); + let ortho = (self.token(c, "projection") == Some("orthographic")).then_some(aperture * 0.05); + self.cameras.push(RawCamera::placed(name, world, Vec3::NEG_Z, lens_fov(focal, aperture), ortho)); + } + /// The prim's local transform from `xformOpOrder`, and whether it resets the parent's. fn xform(&mut self, c: &[usize]) -> (Mat4, bool) { let order: Vec = match self.prop(c, "xformOpOrder").and_then(|p| p.value.as_ref()) { diff --git a/crates/formats/src/mesh_import/usd/tests.rs b/crates/formats/src/mesh_import/usd/tests.rs index 664b3576..6cfd0a07 100644 --- a/crates/formats/src/mesh_import/usd/tests.rs +++ b/crates/formats/src/mesh_import/usd/tests.rs @@ -118,6 +118,23 @@ class Mesh "Template" int[] faceVertexIndices = [0, 1, 2] point3f[] points = [(0, 0, 0), (9, 0, 0), (0, 9, 0)] } + +def Camera "Shot" +{ + float focalLength = 35 + float verticalAperture = 20.955 + double3 xformOp:translate = (0, -500, 100) + float3 xformOp:rotateXYZ = (90, 0, 0) + uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ"] +} + +def Camera "Top" +{ + token projection = "orthographic" + float verticalAperture = 40 + double3 xformOp:translate = (0, 0, 1000) + uniform token[] xformOpOrder = ["xformOp:translate"] +} "#; /// Internal references (with a hole and a binding inside the referenced class), a @@ -233,6 +250,14 @@ fn usda_hierarchy_units_axes_and_subsets() { uvs.sort_by(|a, b| a.x.total_cmp(&b.x).then(a.y.total_cmp(&b.y))); uvs.dedup(); assert_eq!(uvs, [Vec2::new(0.0, 0.0), Vec2::new(0.0, 1.0), Vec2::new(1.0, 0.0), Vec2::new(1.0, 1.0)]); + // Cameras: "Shot" stands 5 m back and 1 m up (Z up, cm), turned to look along +Y, which is + // -Z once converted; "Top" looks straight down orthographically. + let [shot, top] = imported.cameras.as_slice() else { panic!("{:?}", imported.cameras) }; + let near = |a: f32, b: f32| (a - b).abs() < 1e-3; + assert_eq!((shot.name.as_str(), top.name.as_str()), ("Shot", "Top")); + assert!(!shot.ortho && near(shot.fov, 2.0 * (20.955f32 / 70.0).atan().to_degrees()), "{shot:?}"); + assert!(near(shot.yaw, 0.0) && near(shot.pitch, 0.0) && near(shot.distance, 5.5), "{shot:?}"); + assert!(top.ortho && near(top.pitch, 89f32.to_radians()) && near(top.distance, 9.0), "{top:?}"); } #[test] diff --git a/docs/features.md b/docs/features.md index 84ebbdcb..67773c49 100644 --- a/docs/features.md +++ b/docs/features.md @@ -78,7 +78,7 @@ Everything TextureCraft does today, by area. What's still missing is in [`ROADMA anisotropy, subsurface scattering, transmission and parallax occlusion; image-based lighting from six procedural studio and outdoor environments or **your own HDR/EXR panoramas**; ACES or linear tone mapping, key-light shadows, bloom, vignette, sharpen, grain, **depth of field** and - **saved cameras** (also imported from glTF). + **saved cameras** (also imported from glTF, FBX, COLLADA and USD files). - **Rendering mode** (F10): a progressive, **path-traced** view of your textures with multi-bounce light, environment importance sampling, a denoiser and one-click PNG save, traced on the GPU (compute shaders, about 25× the CPU tracer, desktop and WebGPU) or on the CPU.