Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix noncomformant behavior by not requiring the source field to be present in textures. #413

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion gltf-json/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ pub struct Texture {
pub sampler: Option<Index<Sampler>>,

/// The index of the image used by this texture.
pub source: Index<image::Image>,
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<Index<image::Image>>,

/// Extension specific data.
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down
12 changes: 6 additions & 6 deletions src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<image::Image<'a>> {
self.json
.source
.as_ref()
.map(|index| self.document.images().nth(index.value()).unwrap())
}

/// Returns extension data unknown to this crate version.
Expand Down
8 changes: 8 additions & 0 deletions tests/blank_texture.gltf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"asset": {
"version": "2.0"
},
"textures": [
{}
]
}
13 changes: 12 additions & 1 deletion tests/import_sanity_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ fn run() -> Result<(), Box<dyn StdError>> {
}
}

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
Expand All @@ -79,6 +80,16 @@ fn sparse_accessor_without_buffer_view_test() -> Result<(), Box<dyn StdError>> {
Ok(())
}

/// Test a file with a texture with neither sampler nor source
fn blank_texture_test() -> Result<(), Box<dyn StdError>> {
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());
Expand Down