From 585d991fdb66951da4e169900fc2167ef586a611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ralph=20K=C3=BCpper?= Date: Sat, 21 Nov 2026 08:18:03 +0100 Subject: [PATCH] glTF: accept zero accessors (no bufferView, no sparse) and report every rejected document glTF 2.0 says an accessor without a bufferView is all zeros; Blender writes them for shape keys that move nothing on a primitive. The gltf crate's validator rejects them, and every loader dropped the whole model with no message (gltf::Gltf::from_slice(data).ok()?). parse_gltf() lets exactly that case through (the crate gives no reader for such an accessor, which the loaders already treat as nothing) and prints why any other document is rejected. Co-Authored-By: Claude --- native/shared/src/models_gltf.rs | 10 ++-- native/shared/src/models_gltf_parse.rs | 67 ++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 native/shared/src/models_gltf_parse.rs diff --git a/native/shared/src/models_gltf.rs b/native/shared/src/models_gltf.rs index 0d4f476a..e7571356 100644 --- a/native/shared/src/models_gltf.rs +++ b/native/shared/src/models_gltf.rs @@ -26,6 +26,8 @@ use material_import::{ mod bake; #[path = "models_gltf_morph.rs"] mod morph; +#[path = "models_gltf_parse.rs"] +mod parse; pub use bake::require_source_geometry_hash; #[path = "models_gltf_texture_io.rs"] mod texture_io; @@ -140,7 +142,7 @@ fn read_accessor_f32( } pub fn load_gltf_animation(data: &[u8]) -> Option { - let gltf = gltf::Gltf::from_slice(data).ok()?; + let gltf = parse::parse_gltf(data)?; // Get buffer data let mut buffer_data: Vec> = Vec::new(); @@ -482,7 +484,7 @@ pub(super) fn load_gltf_with_textures( base_dir: Option<&std::path::Path>, source_label: Option<&str>, ) -> Option { - let gltf = gltf::Gltf::from_slice(data).ok()?; + let gltf = parse::parse_gltf(data)?; emit_unsupported_material_extension_diagnostics(&gltf, source_label.unwrap_or("")); // Get buffer data @@ -1064,7 +1066,7 @@ fn load_gltf_staged_impl( ) -> Option { use crate::staging::{StagedModel, StagedTexture}; - let gltf = gltf::Gltf::from_slice(data).ok()?; + let gltf = parse::parse_gltf(data)?; emit_unsupported_material_extension_diagnostics(&gltf, source_label); let mut buffer_data: Vec> = Vec::new(); @@ -1520,7 +1522,7 @@ fn stage_image_variants( } pub(super) fn load_gltf(data: &[u8]) -> Option { - let gltf = gltf::Gltf::from_slice(data).ok()?; + let gltf = parse::parse_gltf(data)?; emit_unsupported_material_extension_diagnostics(&gltf, ""); // Get buffer data (for .glb, embedded; for .gltf, inline base64) diff --git a/native/shared/src/models_gltf_parse.rs b/native/shared/src/models_gltf_parse.rs new file mode 100644 index 00000000..a000b3c1 --- /dev/null +++ b/native/shared/src/models_gltf_parse.rs @@ -0,0 +1,67 @@ +//! Parsing a glTF document for the loaders: the gltf crate's validation, with +//! the one case it gets wrong let through, and every rejection reported. +//! +//! glTF 2.0 (ยง5.1, accessor.bufferView): "When undefined, the accessor MUST be +//! initialized with zeros". Blender writes such accessors for shape keys that +//! move nothing on a primitive (`export_try_omit_sparse_sk`), and the gltf +//! crate's validator (1.4) calls them missing data, so a face rig with a +//! target that leaves the teeth alone failed to load at all, silently. When +//! every validation error is of that kind the document is taken without +//! validation; the crate yields no reader for such an accessor, which every +//! loader already treats as "nothing" (zero deltas). Anything else is still +//! rejected, now with a line saying why. + +pub(super) fn parse_gltf(data: &[u8]) -> Option { + match gltf::Gltf::from_slice(data) { + Ok(document) => Some(document), + Err(gltf::Error::Validation(errors)) if only_zero_accessors(&errors) => { + match gltf::Gltf::from_slice_without_validation(data) { + Ok(document) => Some(document), + Err(error) => { + eprintln!("bloom: glTF rejected: {error}"); + None + } + } + } + Err(error) => { + eprintln!("bloom: glTF rejected: {error}"); + None + } + } +} + +/// True when every error is an accessor without a bufferView (a zero accessor). +fn only_zero_accessors(errors: &[(gltf::json::Path, gltf::json::validation::Error)]) -> bool { + !errors.is_empty() + && errors.iter().all(|(path, error)| { + matches!(error, gltf::json::validation::Error::Missing) + && path.as_str().starts_with("accessors[") + && path.as_str().ends_with("].bufferView") + }) +} + +#[cfg(test)] +mod tests { + use super::parse_gltf; + + const ZERO_ACCESSOR: &str = r#"{"asset":{"version":"2.0"}, + "accessors":[{"componentType":5126,"count":3,"type":"VEC3"}]}"#; + + #[test] + fn a_zero_accessor_is_accepted() { + assert!(gltf::Gltf::from_slice(ZERO_ACCESSOR.as_bytes()).is_err()); + let document = parse_gltf(ZERO_ACCESSOR.as_bytes()).expect("zero accessor parses"); + let accessor = document.accessors().next().unwrap(); + assert!(accessor.view().is_none() && accessor.sparse().is_none()); + } + + #[test] + fn other_validation_errors_still_reject() { + // a zero accessor next to one with an invalid component type: the second must still reject + let bad = r#"{"asset":{"version":"2.0"}, + "accessors":[{"componentType":5126,"count":3,"type":"VEC3"}, + {"bufferView":0,"componentType":9999,"count":3,"type":"VEC3"}], + "bufferViews":[{"buffer":0,"byteLength":36}],"buffers":[{"byteLength":36}]}"#; + assert!(parse_gltf(bad.as_bytes()).is_none()); + } +}