diff --git a/crates/mesh/src/gltf.rs b/crates/mesh/src/gltf.rs index 9d45a4453..2581aeafd 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 @@ -345,6 +346,7 @@ impl Doc<'_> { struct Skipped { triangles: usize, primitives: usize, + morphs: usize, } fn rows_of(v: &[f64]) -> impl Iterator + '_ { @@ -439,6 +441,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, @@ -648,7 +651,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. @@ -832,6 +840,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 d35b63d57..0e6733b65 100644 --- a/crates/mesh/src/tests/gltf.rs +++ b/crates/mesh/src/tests/gltf.rs @@ -829,3 +829,33 @@ 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); + // One scene, or an out-of-range choice, is not a warning about scenes. + let one = parse(&doc(json!(5), json!([{ "nodes": [0] }]))).unwrap(); + assert_eq!((one.roots.as_slice(), one.warnings.len()), ([0].as_slice(), 1)); + let s = parse(&one_mesh(&b, plain.clone())).unwrap(); + assert!(s.warnings.is_empty(), "{:?}", s.warnings); +}