Skip to content

Commit

Permalink
feat: support KHR_texture_basisu extension (only json)
Browse files Browse the repository at this point in the history
fix: missing comma and cfg

chore(basisu): sync origin/main
  • Loading branch information
aW4KeNiNG authored and alteous committed Jan 13, 2025
1 parent 88e719d commit 70312ff
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import = ["base64", "image", "urlencoding"]
KHR_lights_punctual = ["gltf-json/KHR_lights_punctual"]
KHR_materials_pbrSpecularGlossiness = ["gltf-json/KHR_materials_pbrSpecularGlossiness"]
KHR_materials_unlit = ["gltf-json/KHR_materials_unlit"]
KHR_texture_basisu = ["gltf-json/KHR_texture_basisu"]
KHR_texture_transform = ["gltf-json/KHR_texture_transform"]
KHR_materials_transmission = ["gltf-json/KHR_materials_transmission"]
KHR_materials_ior = ["gltf-json/KHR_materials_ior"]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ The following glTF extensions are supported by the crate:
- `KHR_lights_punctual`
- `KHR_materials_pbrSpecularGlossiness`
- `KHR_materials_unlit`
- `KHR_texture_basisu`
- `KHR_texture_transform`
- `KHR_materials_variants`
- `KHR_materials_volume`
- `KHR_materials_specular`
- `KHR_materials_transmission`
- `KHR_materials_ior`
- `KHR_materials_emissive_strength `
- `KHR_materials_emissive_strength`
- `EXT_texture_webp`

To use an extension, list its name in the `features` section.
Expand Down
1 change: 1 addition & 0 deletions gltf-json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ KHR_materials_transmission = []
KHR_materials_unlit = []
KHR_materials_variants = []
KHR_materials_volume = []
KHR_texture_basisu = []
KHR_texture_transform = []
KHR_materials_emissive_strength = []
EXT_texture_webp = []
5 changes: 3 additions & 2 deletions gltf-json/src/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub const ENABLED_EXTENSIONS: &[&str] = &[
"KHR_materials_pbrSpecularGlossiness",
#[cfg(feature = "KHR_materials_unlit")]
"KHR_materials_unlit",
#[cfg(feature = "KHR_texture_basisu")]
"KHR_texture_basisu",
#[cfg(feature = "KHR_texture_transform")]
"KHR_texture_transform",
#[cfg(feature = "KHR_materials_transmission")]
Expand All @@ -53,8 +55,6 @@ pub const ENABLED_EXTENSIONS: &[&str] = &[
#[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 = "EXT_texture_webp")]
"EXT_texture_webp",
#[cfg(feature = "allow_empty_texture")]
Expand All @@ -66,6 +66,7 @@ pub const SUPPORTED_EXTENSIONS: &[&str] = &[
"KHR_lights_punctual",
"KHR_materials_pbrSpecularGlossiness",
"KHR_materials_unlit",
"KHR_texture_basisu",
"KHR_texture_transform",
"KHR_materials_transmission",
"KHR_materials_ior",
Expand Down
23 changes: 19 additions & 4 deletions gltf-json/src/extensions/texture.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(any(feature = "KHR_texture_transform", feature = "EXT_texture_webp"))]
use crate::{extras::Extras, validation::Validate};
#[cfg(feature = "EXT_texture_webp")]
#[cfg(any(feature = "EXT_texture_webp", feature = "KHR_texture_basisu"))]
use crate::{image, Index};

use gltf_derive::Validate;
Expand All @@ -19,9 +19,13 @@ pub struct Sampler {
/// A texture and its sampler.
#[derive(Clone, Debug, Default, Deserialize, Serialize, Validate)]
pub struct Texture {
#[cfg(feature = "extensions")]
#[serde(default, flatten)]
pub others: Map<String, Value>,
#[cfg(feature = "KHR_texture_basisu")]
#[serde(
default,
rename = "KHR_texture_basisu",
skip_serializing_if = "Option::is_none"
)]
pub texture_basisu: Option<TextureBasisu>,

#[cfg(feature = "EXT_texture_webp")]
#[serde(
Expand All @@ -30,6 +34,10 @@ pub struct Texture {
skip_serializing_if = "Option::is_none"
)]
pub texture_webp: Option<TextureWebp>,

#[cfg(feature = "extensions")]
#[serde(default, flatten)]
pub others: Map<String, Value>,
}

#[cfg(feature = "EXT_texture_webp")]
Expand All @@ -39,6 +47,13 @@ pub struct TextureWebp {
pub source: Index<image::Image>,
}

#[cfg(feature = "KHR_texture_basisu")]
#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
pub struct TextureBasisu {
/// The index of the image used by this texture.
pub source: Index<image::Image>,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, Validate)]
/// Reference to a `Texture`.
pub struct Info {
Expand Down
2 changes: 2 additions & 0 deletions gltf-json/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub const VALID_MIME_TYPES: &[&str] = &[
"image/png",
#[cfg(feature = "EXT_texture_webp")]
"image/webp",
#[cfg(feature = "KHR_texture_basisu")]
"image/ktx2",
];

/// Image data used to create a texture.
Expand Down
12 changes: 12 additions & 0 deletions gltf-json/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ impl Texture {
pub fn primary_source(&self) -> Index<image::Image> {
#[allow(unused_mut)]
let mut source = self.source;
#[cfg(feature = "KHR_texture_basisu")]
{
if let Some(texture_basisu) = &self.extensions {
if let Some(texture_basisu) = &texture_basisu.texture_basisu {
// Only use the basisu source if the source is not empty
// Otherwise, fallback to whatever was there originally
if !source_is_empty(&texture_basisu.source) {
source = texture_basisu.source;
}
}
}
}
#[cfg(feature = "EXT_texture_webp")]
{
if let Some(texture_webp) = &self.extensions {
Expand Down
30 changes: 30 additions & 0 deletions src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,36 @@ impl<'a> Texture<'a> {
.unwrap()
}

/// Returns the fallback image used by this texture.
pub fn source_fallback(&self) -> Option<image::Image<'a>> {
let index = self.json.source.value();
if index == u32::MAX as usize {
None
} else {
Some(self.document.images().nth(index).unwrap())
}
}

/// Returns the basisu image used by this texture.
#[cfg(feature = "KHR_texture_basisu")]
pub fn source_basisu(&self) -> Option<image::Image<'a>> {
self.json
.extensions
.as_ref()
.and_then(|extensions| extensions.texture_basisu.as_ref())
.and_then(|texture| self.document.images().nth(texture.source.value()))
}

/// Returns the webp image used by this texture.
#[cfg(feature = "EXT_texture_webp")]
pub fn source_webp(&self) -> Option<image::Image<'a>> {
self.json
.extensions
.as_ref()
.and_then(|extensions| extensions.texture_webp.as_ref())
.and_then(|texture| self.document.images().nth(texture.source.value()))
}

/// Returns extension data unknown to this crate version.
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
Expand Down

0 comments on commit 70312ff

Please sign in to comment.