Skip to content

Commit

Permalink
add webp fallback support at the gltf json level
Browse files Browse the repository at this point in the history
  • Loading branch information
marstaik committed Jun 18, 2024
1 parent 306c2b0 commit fca3dda
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
32 changes: 26 additions & 6 deletions gltf-json/src/texture.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::extensions::texture;
use crate::validation::{Checked, Validate};
use crate::{extensions, image, Extras, Index};
use gltf_derive::Validate;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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<image::Image>,
source: Index<image::Image>,

/// Extension specific data.
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand All @@ -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<image::Image> {
#[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<P, R>(&self, root: &crate::Root, path: P, report: &mut R)
where
Expand All @@ -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);
}
}

Expand Down
28 changes: 5 additions & 23 deletions src/texture.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::error::Error;

use crate::{image, Document};

pub use json::texture::{MagFilter, MinFilter, WrappingMode};
Expand Down Expand Up @@ -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<image::Image<'a>> {
let index = self.source_index();
let index = self.json.source().value();
if index == u32::MAX as usize {
None
} else {
Expand All @@ -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.
Expand Down

0 comments on commit fca3dda

Please sign in to comment.