diff --git a/gltf-json/src/texture.rs b/gltf-json/src/texture.rs index 837253c5..cf03e2e0 100644 --- a/gltf-json/src/texture.rs +++ b/gltf-json/src/texture.rs @@ -179,7 +179,8 @@ pub struct Texture { pub sampler: Option>, /// The index of the image used by this texture. - pub source: Index, + #[serde(skip_serializing_if = "Option::is_none")] + pub source: Option>, /// Extension specific data. #[serde(default, skip_serializing_if = "Option::is_none")] diff --git a/src/texture.rs b/src/texture.rs index 3aab4e07..b98a0e60 100644 --- a/src/texture.rs +++ b/src/texture.rs @@ -157,12 +157,12 @@ impl<'a> Texture<'a> { .unwrap_or_else(|| Sampler::default(self.document)) } - /// Returns the image used by this texture. - pub fn source(&self) -> image::Image<'a> { - self.document - .images() - .nth(self.json.source.value()) - .unwrap() + /// Returns the image used by this texture, if any. + pub fn source(&self) -> Option> { + self.json + .source + .as_ref() + .map(|index| self.document.images().nth(index.value()).unwrap()) } /// Returns extension data unknown to this crate version. diff --git a/tests/blank_texture.gltf b/tests/blank_texture.gltf new file mode 100644 index 00000000..adf8194a --- /dev/null +++ b/tests/blank_texture.gltf @@ -0,0 +1,8 @@ +{ + "asset": { + "version": "2.0" + }, + "textures": [ + {} + ] +} \ No newline at end of file diff --git a/tests/import_sanity_check.rs b/tests/import_sanity_check.rs index 7714fa75..ea372e26 100644 --- a/tests/import_sanity_check.rs +++ b/tests/import_sanity_check.rs @@ -60,7 +60,8 @@ fn run() -> Result<(), Box> { } } - sparse_accessor_without_buffer_view_test() + sparse_accessor_without_buffer_view_test()?; + blank_texture_test() } /// Test a file with a sparse accessor with no buffer view @@ -79,6 +80,16 @@ fn sparse_accessor_without_buffer_view_test() -> Result<(), Box> { Ok(()) } +/// Test a file with a texture with neither sampler nor source +fn blank_texture_test() -> Result<(), Box> { + let gltf_path = path::Path::new("tests/blank_texture.gltf"); + print!("{:?}: ", gltf_path); + let result = gltf::import(gltf_path)?; + sanity_check(&result.0, &result.1, &result.2); + println!("ok"); + Ok(()) +} + #[test] fn import_sanity_check() { assert!(run().is_ok());