mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
craft-mesh: warn when glTF morph targets or other scenes are left out
A primitive's morph targets are not read, and only one scene becomes the roots. Both now add a warning: a count of the primitives drawn in their base shape, and which scene of how many was read. Closes #1938. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
892ef9116c
commit
8c1498d1f8
2 files changed
+42
-3
No files matched your search
+12
-3
@@ -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<const N: usize>(v: &[f64]) -> impl Iterator<Item = [f64; N]> + '_ {
|
||||
@@ -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<Scene> {
|
||||
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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in new issue
Block a user