Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions native/shared/src/models_gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -140,7 +142,7 @@ fn read_accessor_f32(
}

pub fn load_gltf_animation(data: &[u8]) -> Option<ModelAnimation> {
let gltf = gltf::Gltf::from_slice(data).ok()?;
let gltf = parse::parse_gltf(data)?;

// Get buffer data
let mut buffer_data: Vec<Vec<u8>> = Vec::new();
Expand Down Expand Up @@ -482,7 +484,7 @@ pub(super) fn load_gltf_with_textures(
base_dir: Option<&std::path::Path>,
source_label: Option<&str>,
) -> Option<ModelData> {
let gltf = gltf::Gltf::from_slice(data).ok()?;
let gltf = parse::parse_gltf(data)?;
emit_unsupported_material_extension_diagnostics(&gltf, source_label.unwrap_or("<memory glTF>"));

// Get buffer data
Expand Down Expand Up @@ -1064,7 +1066,7 @@ fn load_gltf_staged_impl(
) -> Option<crate::staging::StagedModel> {
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<u8>> = Vec::new();
Expand Down Expand Up @@ -1520,7 +1522,7 @@ fn stage_image_variants(
}

pub(super) fn load_gltf(data: &[u8]) -> Option<ModelData> {
let gltf = gltf::Gltf::from_slice(data).ok()?;
let gltf = parse::parse_gltf(data)?;
emit_unsupported_material_extension_diagnostics(&gltf, "<memory glTF>");

// Get buffer data (for .glb, embedded; for .gltf, inline base64)
Expand Down
67 changes: 67 additions & 0 deletions native/shared/src/models_gltf_parse.rs
Original file line number Diff line number Diff line change
@@ -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<gltf::Gltf> {
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());
}
}
Loading