diff --git a/crates/mesh/README.md b/crates/mesh/README.md index 61074a2cb..3b57208fa 100644 --- a/crates/mesh/README.md +++ b/crates/mesh/README.md @@ -110,9 +110,9 @@ FBX 6.x files (a `Model` holds its mesh, `Properties60`, objects connected by na geometry, transforms, the hierarchy and materials. Not read: FBX 5 and older, 6.x textures, animation, skin deformers, blend shapes, cameras, lights, NURBS. -Not read (glTF): required geometry extensions (Draco, meshopt) are -Not read (glTF): sparse accessors and required geometry extensions (Draco, meshopt) are -`Error::Unsupported`; points, lines, morph targets, tangents and other extensions are passed over. +Not read (glTF): required geometry extensions (Draco, meshopt) are `Error::Unsupported`. Points, +lines and morph targets are left out and counted in a warning; scenes other than the default are +named in one; other extensions are passed over. Not written: skins, animations, cameras, lights, and `.gltf` with separate files. ## Hostile input diff --git a/crates/mesh/src/gltf.rs b/crates/mesh/src/gltf.rs index e92b1fcba..13e53aca1 100644 --- a/crates/mesh/src/gltf.rs +++ b/crates/mesh/src/gltf.rs @@ -15,8 +15,9 @@ //! same accessors), and each primitive a [`Part`]. //! //! Not read: required extensions that change how geometry is stored (Draco, meshopt) are -//! [`Error::Unsupported`]; points, lines, morph targets and -//! other extensions are passed over (skipped primitives are counted in a warning). +//! [`Error::Unsupported`]; points, lines, morph targets, scenes other than the default and +//! other extensions are passed over (skipped primitives, primitives with morph targets and the +//! other scenes are named in warnings). //! //! Buffers are needed to read anything, so an external buffer is asked of the resolver (by its //! percent-decoded URI) and is [`Error::Missing`] when declined. External images are only named @@ -352,6 +353,7 @@ impl Doc<'_> { struct Skipped { triangles: usize, primitives: usize, + morphs: usize, } fn rows_of(v: &[f64]) -> impl Iterator + '_ { @@ -446,6 +448,7 @@ fn primitive(d: &Doc, p: &Value, mesh: &mut Mesh, shared: &mut Shared, materials return Ok(()); }; charge(&d.parts_left, 1, "primitives")?; + skipped.morphs += usize::from(!arr(p, "targets").is_empty()); let key = attrs.to_string(); let ([base, normal_base, uv_base, tangent_base], nv) = match shared.get(&key) { Some(known) => *known, @@ -655,7 +658,12 @@ fn nodes(j: &Value, limits: &Limits, scene: &mut Scene) -> Result<()> { children: indices(n, "children", list.len()), }); } - scene.roots = match arr(j, "scenes").get(idx(j, "scene").unwrap_or(0)).or_else(|| arr(j, "scenes").first()) { + let scenes = arr(j, "scenes"); + let chosen = idx(j, "scene").filter(|s| *s < scenes.len()).unwrap_or(0); + if scenes.len() > 1 { + scene.warnings.push(format!("scene {chosen} of {} was read; the others were left out", scenes.len())); + } + scene.roots = match scenes.get(chosen) { Some(s) => indices(s, "nodes", list.len()), None => { // No scene: every node that is nobody's child. @@ -839,6 +847,7 @@ pub fn read(bytes: &[u8], limits: &Limits, resolve: Resolver) -> Result { animations(&d, limits, &mut scene)?; count_warning(&mut scene.warnings, skipped.primitives, "primitive(s) are points, lines or have no positions and were left out"); count_warning(&mut scene.warnings, skipped.triangles, "triangle(s) could not be read and were left out"); + count_warning(&mut scene.warnings, skipped.morphs, "primitive(s) have morph targets, which were left out (drawn in the base shape)"); // A hierarchy with a cycle, or one too deep to walk, is refused here rather than by every caller. scene.instances(limits)?; Ok(scene) diff --git a/crates/mesh/src/tests/gltf.rs b/crates/mesh/src/tests/gltf.rs index d1386dce2..13db18a87 100644 --- a/crates/mesh/src/tests/gltf.rs +++ b/crates/mesh/src/tests/gltf.rs @@ -857,3 +857,42 @@ fn specular_glossiness_and_webp_textures() { let plain = &s.materials[1]; assert_eq!((plain.base_color, plain.metallic, plain.roughness, plain.base_color_texture), ([1.0; 4], 0.0, 0.0, None)); } + +#[test] +fn morph_targets_and_other_scenes_are_named_in_warnings() { + let mut b = B::default(); + let p = b.f32s(&QUAD[..9], "VEC3", 3); + let d = b.f32s(&[0.0; 9], "VEC3", 3); + let plain = json!({ "attributes": { "POSITION": p } }); + let morphed = json!({ "attributes": { "POSITION": p }, "targets": [{ "POSITION": d }] }); + let doc = |scene: Value, scenes: Value| { + b.glb(json!({ + "scene": scene, + "scenes": scenes, + "nodes": [{ "mesh": 0 }, { "mesh": 0 }], + "meshes": [{ "primitives": [plain, morphed, morphed] }], + })) + }; + let s = parse(&doc(json!(1), json!([{ "nodes": [0] }, { "nodes": [1] }, { "nodes": [] }]))).unwrap(); + assert_eq!(s.roots, [1]); + assert_eq!( + s.warnings, + ["scene 1 of 3 was read; the others were left out", "2 primitive(s) have morph targets, which were left out (drawn in the base shape)",] + ); + // The morphed primitives are still drawn, in their base shape. + assert_eq!(s.meshes[0].parts.len(), 3); + let morphs = "2 primitive(s) have morph targets, which were left out (drawn in the base shape)"; + // An out-of-range choice falls back to the first scene. + let s = parse(&doc(json!(5), json!([{ "nodes": [1] }, { "nodes": [0] }]))).unwrap(); + assert_eq!(s.roots, [1]); + assert_eq!(s.warnings, ["scene 0 of 2 was read; the others were left out", morphs]); + // One scene, or none, is not a warning about scenes. + let s = parse(&doc(json!(5), json!([{ "nodes": [0] }]))).unwrap(); + assert_eq!(s.roots, [0]); + assert_eq!(s.warnings, [morphs]); + let s = parse(&doc(json!(null), json!(null))).unwrap(); + assert_eq!(s.roots, [0, 1]); + assert_eq!(s.warnings, [morphs]); + let s = parse(&one_mesh(&b, plain.clone())).unwrap(); + assert!(s.warnings.is_empty(), "{:?}", s.warnings); +}