Skip to content

Commit

Permalink
Add allow-empty-texture feature
Browse files Browse the repository at this point in the history
  • Loading branch information
alteous committed Apr 25, 2024
1 parent 81423e8 commit 6c1730f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ version = "0.25"

[features]
default = ["import", "utils", "names"]
allow-empty-texture = ["gltf-json/allow-empty-texture"]
extensions = ["gltf-json/extensions"]
extras = ["gltf-json/extras"]
names = ["gltf-json/names"]
Expand Down
1 change: 1 addition & 0 deletions gltf-json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ serde_json = { features = ["raw_value"], version = "1.0" }

[features]
default = []
allow-empty-texture = []
names = []
extensions = []
extras = []
Expand Down
7 changes: 7 additions & 0 deletions gltf-json/src/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ pub const ENABLED_EXTENSIONS: &[&str] = &[
"KHR_materials_ior",
#[cfg(feature = "KHR_materials_emissive_strength")]
"KHR_materials_emissive_strength",
// Allowlisted texture extensions. Processing is delegated to the user.
#[cfg(feature = "allow-empty-texture")]
"KHR_texture_basisu",
#[cfg(feature = "allow-empty-texture")]
"EXT_texture_webp",
#[cfg(feature = "allow-empty-texture")]
"MSFT_texture_dds",
];

/// Names of glTF 2.0 extensions supported by the library.
Expand Down
31 changes: 13 additions & 18 deletions gltf-json/src/texture.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::validation::{Checked, Error, Validate};
use crate::{extensions, image, Extras, Index, Path, Root};
use crate::validation::Checked;
use crate::{extensions, image, Extras, Index};
use gltf_derive::Validate;
use serde::{de, ser};
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -166,9 +166,14 @@ pub struct Sampler {
pub extras: Extras,
}

#[cfg(feature = "allow-empty-texture")]
pub type Source = Option<Index<image::Image>>;

#[cfg(not(feature = "allow-empty-texture"))]
pub type Source = Index<image::Image>;

/// A texture and its sampler.
#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
#[gltf(validate_hook = "texture_validate_hook")]
pub struct Texture {
/// Optional user-defined name for this object.
#[cfg(feature = "names")]
Expand All @@ -180,8 +185,11 @@ pub struct Texture {
pub sampler: Option<Index<Sampler>>,

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

/// Extension specific data.
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand All @@ -194,19 +202,6 @@ pub struct Texture {
pub extras: Extras,
}

fn texture_validate_hook<P, R>(texture: &Texture, root: &Root, path: P, report: &mut R)
where
P: Fn() -> Path,
R: FnMut(&dyn Fn() -> Path, Error),
{
let source_path = || path().field("source");
if let Some(index) = texture.source.as_ref() {
index.validate(root, source_path, report);
} else {
report(&source_path, Error::Missing);
}
}

#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
/// Reference to a `Texture`.
pub struct Info {
Expand Down
7 changes: 4 additions & 3 deletions src/accessor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
//! for accessor in gltf.accessors() {
//! match (accessor.data_type(), accessor.dimensions()) {
//! (DataType::F32, Dimensions::Vec3) => {
//! let iter = Iter::<[f32; 3]>::new(accessor, get_buffer_data);
//! for item in iter {
//! println!("{:?}", item);
//! if let Some(iter) = Iter::<[f32; 3]>::new(accessor, get_buffer_data) {
//! for item in iter {
//! println!("{:?}", item);
//! }
//! }
//! }
//! _ => {},
Expand Down
14 changes: 12 additions & 2 deletions src/texture.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{image, Document};

pub use json::texture::{MagFilter, MinFilter, WrappingMode};
pub use json::texture::{MagFilter, MinFilter, Source, WrappingMode};
#[cfg(feature = "extensions")]
use serde_json::{Map, Value};

Expand Down Expand Up @@ -158,10 +158,20 @@ impl<'a> Texture<'a> {
}

/// Returns the image used by this texture.
#[cfg(feature = "allow-empty-texture")]
pub fn source(&self) -> Option<image::Image<'a>> {
self.json
.source
.as_ref()
.map(|index| self.document.images().nth(index.value()).unwrap())
}

/// 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.json.source.unwrap().value())
.nth(self.json.source.value())
.unwrap()
}

Expand Down

0 comments on commit 6c1730f

Please sign in to comment.