diff --git a/gltf-json/src/texture.rs b/gltf-json/src/texture.rs index acccd2f3..365fd089 100644 --- a/gltf-json/src/texture.rs +++ b/gltf-json/src/texture.rs @@ -1,3 +1,4 @@ +use crate::extensions::texture; use crate::validation::{Checked, Validate}; use crate::{extensions, image, Extras, Index}; use gltf_derive::Validate; @@ -179,10 +180,7 @@ where P: Fn() -> crate::Path, R: FnMut(&dyn Fn() -> crate::Path, crate::validation::Error), { - if cfg!(any( - feature = "allow_empty_texture", - feature = "EXT_texture_webp" - )) { + if cfg!(any(feature = "allow_empty_texture",)) { if !source_is_empty(source) { source.validate(root, path, report); } @@ -207,7 +205,7 @@ pub struct Texture { /// The index of the image used by this texture. #[serde(default = "source_default", skip_serializing_if = "source_is_empty")] - pub source: Index, + source: Index, /// Extension specific data. #[serde(default, skip_serializing_if = "Option::is_none")] @@ -220,6 +218,27 @@ pub struct Texture { pub extras: Extras, } +impl Texture { + /// The index of the image used by this texture. + pub fn source(&self) -> Index { + #[allow(unused_mut)] + let mut source = self.source; + #[cfg(feature = "EXT_texture_webp")] + { + if let Some(texture_webp) = &self.extensions { + if let Some(texture_webp) = &texture_webp.texture_webp { + // Only use the webp source if the source is not empty + // Otherwise, fallback to whatever was there originally + if !source_is_empty(&texture_webp.source) { + source = texture_webp.source; + } + } + } + } + source + } +} + impl Validate for Texture { fn validate(&self, root: &crate::Root, path: P, report: &mut R) where @@ -230,7 +249,8 @@ impl Validate for Texture { .validate(root, || path().field("sampler"), report); self.extensions .validate(root, || path().field("extensions"), report); - source_validate(&self.source, root, || path().field("source"), report); + + source_validate(&self.source(), root, || path().field("source"), report); } } diff --git a/src/texture.rs b/src/texture.rs index aa62a0b6..0de41a8b 100644 --- a/src/texture.rs +++ b/src/texture.rs @@ -1,5 +1,3 @@ -use std::error::Error; - use crate::{image, Document}; pub use json::texture::{MagFilter, MinFilter, WrappingMode}; @@ -159,29 +157,10 @@ impl<'a> Texture<'a> { .unwrap_or_else(|| Sampler::default(self.document)) } - fn source_index(&self) -> usize { - let default = self.json.source.value(); - - #[cfg(feature = "EXT_texture_webp")] - { - let opt_texture_webp = self - .json - .extensions - .as_ref() - .and_then(|ext| ext.texture_webp.as_ref()); - - if let Some(texture_webp) = opt_texture_webp { - return texture_webp.source.value(); - } - } - - return default; - } - /// Returns the image used by this texture. #[cfg(feature = "allow_empty_texture")] pub fn source(&self) -> Option> { - let index = self.source_index(); + let index = self.json.source().value(); if index == u32::MAX as usize { None } else { @@ -192,7 +171,10 @@ impl<'a> Texture<'a> { /// Returns the image used by this texture. #[cfg(not(feature = "allow_empty_texture"))] pub fn source(&self) -> image::Image<'a> { - self.document.images().nth(self.source_index()).unwrap() + self.document + .images() + .nth(self.json.source().value()) + .unwrap() } /// Returns extension data unknown to this crate version.