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

Support KHR_texture_basisu extension #392

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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 @@ -28,6 +28,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
13 changes: 12 additions & 1 deletion 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 = "KHR_texture_basisu", feature = "EXT_texture_webp"))]
use crate::{image, Index};

use gltf_derive::Validate;
Expand All @@ -23,6 +23,10 @@ pub struct Texture {
#[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(
default,
Expand All @@ -39,6 +43,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 @@ -7,6 +7,8 @@ use serde_derive::{Deserialize, Serialize};
pub const VALID_MIME_TYPES: &[&str] = &[
"image/jpeg",
"image/png",
#[cfg(feature = "KHR_texture_basisu")]
"image/ktx2",
#[cfg(feature = "EXT_texture_webp")]
"image/webp",
];
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 @@ -223,6 +223,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
Loading